author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58365 | 73950479184b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57760
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6439651 |
|
38557 | 27 |
* @modules jdk.httpserver |
2 | 28 |
* @run main/othervm UserAuth |
29 |
* @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore |
|
30 |
*/ |
|
31 |
||
32 |
import java.net.*; |
|
33 |
import com.sun.net.httpserver.*; |
|
34 |
import java.util.*; |
|
35 |
import java.io.*; |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
57760
diff
changeset
|
36 |
import static java.net.Proxy.NO_PROXY; |
2 | 37 |
|
38 |
public class UserCookie |
|
39 |
{ |
|
40 |
com.sun.net.httpserver.HttpServer httpServer; |
|
41 |
||
42 |
public static void main(String[] args) { |
|
43 |
new UserCookie(); |
|
44 |
} |
|
45 |
||
46 |
public UserCookie() { |
|
47 |
try { |
|
48 |
startHttpServer(); |
|
49 |
doClient(); |
|
50 |
} catch (IOException ioe) { |
|
51 |
ioe.printStackTrace(); |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
void doClient() { |
|
56 |
try { |
|
57 |
// set default CookieHandler to accept only accepts cookies from original server. |
|
58 |
CookieHandler.setDefault(new CookieManager()); |
|
59 |
||
60 |
InetSocketAddress address = httpServer.getAddress(); |
|
61 |
||
62 |
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
57760
diff
changeset
|
63 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY); |
2 | 64 |
uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter"); |
65 |
int resp = uc.getResponseCode(); |
|
66 |
||
67 |
System.out.println("Response Code is " + resp); |
|
68 |
if (resp != 200) |
|
69 |
throw new RuntimeException("Failed: Cookie header was not retained"); |
|
70 |
||
71 |
} catch (IOException e) { |
|
72 |
e.printStackTrace(); |
|
73 |
} finally { |
|
74 |
httpServer.stop(1); |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Http Server |
|
80 |
*/ |
|
81 |
void startHttpServer() throws IOException { |
|
57760
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
82 |
InetAddress address = InetAddress.getLocalHost(); |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
83 |
if (!InetAddress.getByName(address.getHostName()).equals(address)) { |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
84 |
// if this happens then we should possibly change the client |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
85 |
// side to use the address literal in its URL instead of |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
86 |
// the host name. |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
87 |
throw new IOException(address.getHostName() |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
88 |
+ " resolves to " |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
89 |
+ InetAddress.getByName(address.getHostName()) |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
90 |
+ " not to " |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
91 |
+ address + ": check host configuration."); |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
92 |
} |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
93 |
|
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
94 |
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0); |
2 | 95 |
|
96 |
// create HttpServer context |
|
97 |
HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); |
|
98 |
||
99 |
httpServer.start(); |
|
100 |
} |
|
101 |
||
102 |
class MyHandler implements HttpHandler { |
|
103 |
public void handle(HttpExchange t) throws IOException { |
|
104 |
Headers reqHeaders = t.getRequestHeaders(); |
|
105 |
||
106 |
List<String> cookie = reqHeaders.get("Cookie"); |
|
107 |
||
108 |
if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter")) |
|
109 |
t.sendResponseHeaders(400, -1); |
|
110 |
||
111 |
t.sendResponseHeaders(200, -1); |
|
112 |
t.close(); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
||
117 |
||
118 |
} |