author | darcy |
Fri, 20 May 2016 16:34:14 -0700 | |
changeset 38456 | c15f03cb9b0e |
parent 30820 | 0d4717a011d3 |
child 38557 | 5c485e1ea6fa |
permissions | -rw-r--r-- |
24869
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
1 |
/* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
4 |
* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
8 |
* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
13 |
* accompanied this code). |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
14 |
* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
18 |
* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
21 |
* questions. |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
22 |
*/ |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
23 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
24 |
/* |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
25 |
* @test |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
26 |
* @bug 8027308 |
38456
c15f03cb9b0e
8157487: Mark ZoneId.java as intermittently failing
darcy
parents:
30820
diff
changeset
|
27 |
* @key intermittent |
30820 | 28 |
* @modules java.base/sun.net.www.protocol.http |
24869
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
29 |
* @summary verifies that HttpURLConnection does not send the zone id in the |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
30 |
* 'Host' field of the header: |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
31 |
* Host: [fe80::a00:27ff:aaaa:aaaa] instead of |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
32 |
* Host: [fe80::a00:27ff:aaaa:aaaa%eth0]" |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
33 |
*/ |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
34 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
35 |
import com.sun.net.httpserver.Headers; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpExchange; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
37 |
import com.sun.net.httpserver.HttpHandler; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
38 |
import com.sun.net.httpserver.HttpServer; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
39 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
40 |
import java.io.IOException; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
41 |
import java.net.*; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
42 |
import java.util.Enumeration; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
43 |
import java.util.List; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
44 |
import java.util.concurrent.CompletableFuture; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
45 |
import java.util.concurrent.ExecutionException; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
46 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
47 |
public class ZoneId { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
48 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
49 |
public static void main(String[] args) throws Exception { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
50 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
51 |
InetAddress address = getAppropriateIPv6Address(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
52 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
53 |
if (address == null) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
54 |
System.out.println( |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
55 |
"The test will be skipped as not a single " + |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
56 |
"appropriate IPv6 address was found on this machine"); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
57 |
return; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
58 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
59 |
String ip6_literal = address.getHostAddress(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
60 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
61 |
System.out.println("Found an appropriate IPv6 address: " + address); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
62 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
63 |
System.out.println("Starting http server..."); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
64 |
HttpServer server = |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
65 |
HttpServer.create(new InetSocketAddress(address, 0), 0); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
66 |
CompletableFuture<Headers> headers = new CompletableFuture<>(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
67 |
server.createContext("/", createCapturingHandler(headers)); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
68 |
server.start(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
69 |
System.out.println("Started at " + server.getAddress()); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
70 |
try { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
71 |
String spec = "http://[" + address.getHostAddress() + "]:" + server.getAddress().getPort(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
72 |
System.out.println("Client is connecting to: " + spec); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
73 |
URLConnection urlConnection = new URL(spec).openConnection(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
74 |
((sun.net.www.protocol.http.HttpURLConnection) urlConnection) |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
75 |
.getResponseCode(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
76 |
} finally { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
77 |
System.out.println("Shutting down the server..."); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
78 |
server.stop(0); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
79 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
80 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
81 |
int idx = ip6_literal.lastIndexOf('%'); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
82 |
String ip6_address = ip6_literal.substring(0, idx); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
83 |
List<String> hosts = headers.get().get("Host"); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
84 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
85 |
System.out.println("Host: " + hosts); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
86 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
87 |
if (hosts.size() != 1 || hosts.get(0).contains("%") || |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
88 |
!hosts.get(0).contains(ip6_address)) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
89 |
throw new RuntimeException("FAIL"); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
90 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
91 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
92 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
93 |
private static InetAddress getAppropriateIPv6Address() throws SocketException { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
94 |
System.out.println("Searching through the network interfaces..."); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
95 |
Enumeration<NetworkInterface> is = NetworkInterface.getNetworkInterfaces(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
96 |
while (is.hasMoreElements()) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
97 |
NetworkInterface i = is.nextElement(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
98 |
System.out.println("\tinterface: " + i); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
99 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
100 |
// just a "good enough" marker that the interface |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
101 |
// does not support a loopback and therefore should not be used |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
102 |
if ( i.getHardwareAddress() == null) continue; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
103 |
if (!i.isUp()) continue; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
104 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
105 |
Enumeration<InetAddress> as = i.getInetAddresses(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
106 |
while (as.hasMoreElements()) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
107 |
InetAddress a = as.nextElement(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
108 |
System.out.println("\t\taddress: " + a.getHostAddress()); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
109 |
if ( !(a instanceof Inet6Address && |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
110 |
a.toString().contains("%")) ) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
111 |
continue; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
112 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
113 |
return a; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
114 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
115 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
116 |
return null; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
117 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
118 |
|
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
119 |
private static HttpHandler createCapturingHandler(CompletableFuture<Headers> headers) { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
120 |
return new HttpHandler() { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
121 |
@Override |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
122 |
public void handle(HttpExchange exchange) throws IOException { |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
123 |
headers.complete(exchange.getRequestHeaders()); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
124 |
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
125 |
exchange.close(); |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
126 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
127 |
}; |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
128 |
} |
6cdd5aa7e259
8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents:
diff
changeset
|
129 |
} |