18174
|
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 |
s.stopServer();
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
class Server {
|
|
80 |
HttpServer server;
|
|
81 |
|
|
82 |
public void startServer() {
|
|
83 |
InetSocketAddress addr = new InetSocketAddress(0);
|
|
84 |
try {
|
|
85 |
server = HttpServer.create(addr, 0);
|
|
86 |
} catch (IOException ioe) {
|
|
87 |
throw new RuntimeException("Server could not be created");
|
|
88 |
}
|
|
89 |
|
|
90 |
server.createContext("/", new MyCookieHandler());
|
|
91 |
server.start();
|
|
92 |
}
|
|
93 |
|
|
94 |
public int getPort() {
|
|
95 |
return server.getAddress().getPort();
|
|
96 |
}
|
|
97 |
|
|
98 |
public void stopServer() {
|
|
99 |
server.stop(0);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
class MyCookieHandler implements HttpHandler {
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public void handle(HttpExchange exchange) throws IOException {
|
|
107 |
String requestMethod = exchange.getRequestMethod();
|
|
108 |
if (requestMethod.equalsIgnoreCase("GET")){
|
|
109 |
Headers responseHeaders = exchange.getResponseHeaders();
|
|
110 |
responseHeaders.set("Content-Type", "text/plain");
|
|
111 |
responseHeaders.set("Date", "June 13th 2012");
|
|
112 |
// No domain value set
|
|
113 |
responseHeaders.set("Set-Cookie2", "name=value");
|
|
114 |
exchange.sendResponseHeaders(200, 0);
|
|
115 |
OutputStream os = exchange.getResponseBody();
|
|
116 |
String str = "This is what the server sent!";
|
|
117 |
os.write(str.getBytes());
|
|
118 |
os.flush();
|
|
119 |
os.close();
|
|
120 |
}
|
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|