Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java
41154 views
1
/*
2
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 7129083
27
* @library /test/lib
28
* @summary Cookiemanager does not store cookies if url is read
29
* before setting cookiemanager
30
*/
31
32
import java.net.CookieHandler;
33
import java.net.CookieManager;
34
import java.net.CookiePolicy;
35
import java.net.InetAddress;
36
import java.net.InetSocketAddress;
37
import java.net.ServerSocket;
38
import java.net.Socket;
39
import java.net.URL;
40
import java.io.InputStream;
41
import java.io.IOException;
42
43
import jdk.test.lib.net.URIBuilder;
44
45
public class CookieHttpClientTest implements Runnable {
46
final ServerSocket ss;
47
static final int TIMEOUT = 10 * 1000;
48
49
static final String replyString = "HTTP/1.1 200 OK\r\n" +
50
"Set-Cookie: name=test\r\n" +
51
"Content-Length: 10\r\n\r\n" +
52
"1234567890";
53
54
// HTTP server, reply with Set-Cookie
55
@Override
56
public void run() {
57
Socket s = null;
58
try {
59
s = ss.accept();
60
s.setSoTimeout(TIMEOUT);
61
readOneRequest(s.getInputStream());
62
s.getOutputStream().write(replyString.getBytes());
63
64
readOneRequest(s.getInputStream());
65
s.getOutputStream().write(replyString.getBytes());
66
} catch (Exception e) {
67
e.printStackTrace();
68
} finally {
69
try { if (s != null) { s.close(); } ss.close(); }
70
catch (IOException unused) { /* gulp!burp! */ }
71
}
72
}
73
74
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
75
76
// Read until the end of a HTTP request
77
static void readOneRequest(InputStream is) throws IOException {
78
int requestEndCount = 0, r;
79
while ((r = is.read()) != -1) {
80
if (r == requestEnd[requestEndCount]) {
81
requestEndCount++;
82
if (requestEndCount == 4) {
83
break;
84
}
85
} else {
86
requestEndCount = 0;
87
}
88
}
89
}
90
91
CookieHttpClientTest() throws Exception {
92
/* start the server */
93
ss = new ServerSocket();
94
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
95
(new Thread(this)).start();
96
97
URL url = URIBuilder.newBuilder()
98
.scheme("http")
99
.loopback()
100
.port(ss.getLocalPort())
101
.path("/").toURL();
102
103
// Run without a CookieHandler first
104
InputStream in = url.openConnection().getInputStream();
105
while (in.read() != -1); // read response body so connection can be reused
106
107
// Set a CookeHandler and retest using the HttpClient from the KAC
108
CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
109
CookieHandler.setDefault(manager);
110
111
in = url.openConnection().getInputStream();
112
while (in.read() != -1);
113
114
if (manager.getCookieStore().getCookies().isEmpty()) {
115
throw new RuntimeException("Failed: No cookies in the cookie Handler.");
116
}
117
}
118
119
public static void main(String args[]) throws Exception {
120
new CookieHttpClientTest();
121
}
122
}
123
124