Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/CookieHttpClientTest.java
38867 views
1
/*
2
* Copyright (c) 2012, 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
* @summary Cookiemanager does not store cookies if url is read
28
* before setting cookiemanager
29
*/
30
31
import java.net.CookieHandler;
32
import java.net.CookieManager;
33
import java.net.CookiePolicy;
34
import java.net.ServerSocket;
35
import java.net.Socket;
36
import java.net.URL;
37
import java.io.InputStream;
38
import java.io.IOException;
39
40
public class CookieHttpClientTest implements Runnable {
41
final ServerSocket ss;
42
static final int TIMEOUT = 10 * 1000;
43
44
static final String replyString = "HTTP/1.1 200 OK\r\n" +
45
"Set-Cookie: name=test\r\n" +
46
"Content-Length: 10\r\n\r\n" +
47
"1234567890";
48
49
// HTTP server, reply with Set-Cookie
50
@Override
51
public void run() {
52
Socket s = null;
53
try {
54
s = ss.accept();
55
s.setSoTimeout(TIMEOUT);
56
readOneRequest(s.getInputStream());
57
s.getOutputStream().write(replyString.getBytes());
58
59
readOneRequest(s.getInputStream());
60
s.getOutputStream().write(replyString.getBytes());
61
} catch (Exception e) {
62
e.printStackTrace();
63
} finally {
64
try { if (s != null) { s.close(); } ss.close(); }
65
catch (IOException unused) { /* gulp!burp! */ }
66
}
67
}
68
69
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
70
71
// Read until the end of a HTTP request
72
static void readOneRequest(InputStream is) throws IOException {
73
int requestEndCount = 0, r;
74
while ((r = is.read()) != -1) {
75
if (r == requestEnd[requestEndCount]) {
76
requestEndCount++;
77
if (requestEndCount == 4) {
78
break;
79
}
80
} else {
81
requestEndCount = 0;
82
}
83
}
84
}
85
86
CookieHttpClientTest() throws Exception {
87
/* start the server */
88
ss = new ServerSocket(0);
89
(new Thread(this)).start();
90
91
URL url = new URL("http://localhost:" + ss.getLocalPort() +"/");
92
93
// Run without a CookieHandler first
94
InputStream in = url.openConnection().getInputStream();
95
while (in.read() != -1); // read response body so connection can be reused
96
97
// Set a CookeHandler and retest using the HttpClient from the KAC
98
CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
99
CookieHandler.setDefault(manager);
100
101
in = url.openConnection().getInputStream();
102
while (in.read() != -1);
103
104
if (manager.getCookieStore().getCookies().isEmpty()) {
105
throw new RuntimeException("Failed: No cookies in the cookie Handler.");
106
}
107
}
108
109
public static void main(String args[]) throws Exception {
110
new CookieHttpClientTest();
111
}
112
}
113
114