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