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