Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/CookieHandler/LocalHostCookie.java
41152 views
1
/*
2
* Copyright (c) 2013, 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
import com.sun.net.httpserver.*;
26
import java.io.IOException;
27
import java.io.OutputStream;
28
import java.net.*;
29
import java.util.List;
30
import java.util.Map;
31
import static java.net.Proxy.NO_PROXY;
32
33
/*
34
* @test
35
* @bug 7169142
36
* @key intermittent
37
* @modules jdk.httpserver
38
* @summary CookieHandler does not work with localhost. This requires
39
* binding to the wildcard address and might fail intermittently
40
* due to port reuse issues.
41
* @run main/othervm LocalHostCookie
42
*/
43
public class LocalHostCookie {
44
45
public static void main(String[] args) throws Exception {
46
new LocalHostCookie().runTest();
47
}
48
49
public void runTest() throws Exception {
50
Server s = null;
51
try {
52
s = new Server();
53
s.startServer();
54
URL url = new URL("http","localhost", s.getPort(), "/");
55
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(NO_PROXY);
56
urlConnection.setRequestMethod("GET");
57
urlConnection.setDoOutput(true);
58
urlConnection.connect();
59
urlConnection.getInputStream();
60
61
CookieHandler cookieHandler = CookieHandler.getDefault();
62
if (cookieHandler == null) {
63
cookieHandler = new java.net.CookieManager();
64
CookieHandler.setDefault(cookieHandler);
65
}
66
cookieHandler.put(urlConnection.getURL().toURI(),
67
urlConnection.getHeaderFields());
68
Map<String, List<String>> map =
69
cookieHandler.get(urlConnection.getURL().toURI(),
70
urlConnection.getHeaderFields());
71
if (map.containsKey("Cookie")) {
72
List<String> list = map.get("Cookie");
73
// name-value list will be empty if ".local" is not appended
74
if (list == null || list.size() == 0) {
75
throw new RuntimeException("Test failed!");
76
}
77
}
78
} finally {
79
if (s != null) {
80
s.stopServer();
81
}
82
}
83
}
84
85
class Server {
86
HttpServer server;
87
88
public void startServer() {
89
InetSocketAddress addr = new InetSocketAddress(0);
90
try {
91
server = HttpServer.create(addr, 0);
92
} catch (IOException ioe) {
93
throw new RuntimeException("Server could not be created");
94
}
95
96
server.createContext("/", new MyCookieHandler());
97
server.start();
98
}
99
100
public int getPort() {
101
return server.getAddress().getPort();
102
}
103
104
public void stopServer() {
105
if (server != null) {
106
server.stop(0);
107
}
108
}
109
}
110
111
class MyCookieHandler implements HttpHandler {
112
113
@Override
114
public void handle(HttpExchange exchange) throws IOException {
115
String requestMethod = exchange.getRequestMethod();
116
if (requestMethod.equalsIgnoreCase("GET")){
117
Headers responseHeaders = exchange.getResponseHeaders();
118
responseHeaders.set("Content-Type", "text/plain");
119
responseHeaders.set("Date", "June 13th 2012");
120
// No domain value set
121
responseHeaders.set("Set-Cookie2", "name=value");
122
exchange.sendResponseHeaders(200, 0);
123
OutputStream os = exchange.getResponseBody();
124
String str = "This is what the server sent!";
125
os.write(str.getBytes());
126
os.flush();
127
os.close();
128
}
129
}
130
}
131
}
132
133