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