ZoneId.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2014, 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 * @test 26 * @bug 8027308 27 * @key intermittent 28 * @modules java.base/sun.net.www.protocol.http 29 * jdk.httpserver 30 * @summary verifies that HttpURLConnection does not send the zone id in the 31 * 'Host' field of the header: 32 * Host: [fe80::a00:27ff:aaaa:aaaa] instead of 33 * Host: [fe80::a00:27ff:aaaa:aaaa%eth0]" 34 */ 35 36import com.sun.net.httpserver.Headers; 37import com.sun.net.httpserver.HttpExchange; 38import com.sun.net.httpserver.HttpHandler; 39import com.sun.net.httpserver.HttpServer; 40 41import java.io.IOException; 42import java.net.*; 43import java.util.Enumeration; 44import java.util.List; 45import java.util.concurrent.CompletableFuture; 46import java.util.concurrent.ExecutionException; 47 48public class ZoneId { 49 50 public static void main(String[] args) throws Exception { 51 52 InetAddress address = getAppropriateIPv6Address(); 53 54 if (address == null) { 55 System.out.println( 56 "The test will be skipped as not a single " + 57 "appropriate IPv6 address was found on this machine"); 58 return; 59 } 60 String ip6_literal = address.getHostAddress(); 61 62 System.out.println("Found an appropriate IPv6 address: " + address); 63 64 System.out.println("Starting http server..."); 65 HttpServer server = 66 HttpServer.create(new InetSocketAddress(address, 0), 0); 67 CompletableFuture<Headers> headers = new CompletableFuture<>(); 68 server.createContext("/", createCapturingHandler(headers)); 69 server.start(); 70 System.out.println("Started at " + server.getAddress()); 71 try { 72 String spec = "http://[" + address.getHostAddress() + "]:" + server.getAddress().getPort(); 73 System.out.println("Client is connecting to: " + spec); 74 URLConnection urlConnection = new URL(spec).openConnection(); 75 ((sun.net.www.protocol.http.HttpURLConnection) urlConnection) 76 .getResponseCode(); 77 } finally { 78 System.out.println("Shutting down the server..."); 79 server.stop(0); 80 } 81 82 int idx = ip6_literal.lastIndexOf('%'); 83 String ip6_address = ip6_literal.substring(0, idx); 84 List<String> hosts = headers.get().get("Host"); 85 86 System.out.println("Host: " + hosts); 87 88 if (hosts.size() != 1 || hosts.get(0).contains("%") || 89 !hosts.get(0).contains(ip6_address)) { 90 throw new RuntimeException("FAIL"); 91 } 92 } 93 94 private static InetAddress getAppropriateIPv6Address() throws SocketException { 95 System.out.println("Searching through the network interfaces..."); 96 Enumeration<NetworkInterface> is = NetworkInterface.getNetworkInterfaces(); 97 while (is.hasMoreElements()) { 98 NetworkInterface i = is.nextElement(); 99 System.out.println("\tinterface: " + i); 100 101 // just a "good enough" marker that the interface 102 // does not support a loopback and therefore should not be used 103 if ( i.getHardwareAddress() == null) continue; 104 if (!i.isUp()) continue; 105 106 Enumeration<InetAddress> as = i.getInetAddresses(); 107 while (as.hasMoreElements()) { 108 InetAddress a = as.nextElement(); 109 System.out.println("\t\taddress: " + a.getHostAddress()); 110 if ( !(a instanceof Inet6Address && 111 a.toString().contains("%")) ) { 112 continue; 113 } 114 return a; 115 } 116 } 117 return null; 118 } 119 120 private static HttpHandler createCapturingHandler(CompletableFuture<Headers> headers) { 121 return new HttpHandler() { 122 @Override 123 public void handle(HttpExchange exchange) throws IOException { 124 headers.complete(exchange.getRequestHeaders()); 125 exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1); 126 exchange.close(); 127 } 128 }; 129 } 130} 131