Path: blob/master/test/jdk/sun/net/www/protocol/http/ZoneId.java
41159 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 802730826* @modules jdk.httpserver27* @summary verifies that HttpURLConnection does not send the zone id in the28* 'Host' field of the header:29* Host: [fe80::a00:27ff:aaaa:aaaa] instead of30* Host: [fe80::a00:27ff:aaaa:aaaa%eth0]"31*/3233import com.sun.net.httpserver.Headers;34import com.sun.net.httpserver.HttpExchange;35import com.sun.net.httpserver.HttpHandler;36import com.sun.net.httpserver.HttpServer;3738import java.io.IOException;39import java.net.*;40import java.util.Enumeration;41import java.util.List;42import java.util.Optional;43import java.util.concurrent.CompletableFuture;44import static java.lang.System.out;45import static java.net.Proxy.NO_PROXY;4647public class ZoneId {4849public static void main(String[] args) throws Exception {5051InetAddress address = getIPv6LookbackAddress();5253if (address == null) {54out.println("Cannot find the IPv6 loopback address. Skipping test.");55return;56}57String ip6_literal = address.getHostAddress();5859out.println("Found an appropriate IPv6 address: " + address);6061out.println("Starting http server...");62HttpServer server = HttpServer.create(new InetSocketAddress(address, 0), 0);63CompletableFuture<Headers> headers = new CompletableFuture<>();64server.createContext("/", createCapturingHandler(headers));65server.start();66out.println("Started at " + server.getAddress());6768try {69int port = server.getAddress().getPort();70String spec = "http://[" + address.getHostAddress() + "]:" + port;71out.println("Client is connecting to: " + spec);72URL url = new URL(spec);73HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);74uc.getResponseCode();75} finally {76out.println("Shutting down the server...");77server.stop(0);78}7980int idx = ip6_literal.lastIndexOf('%');81String ip6_address = ip6_literal.substring(0, idx);82List<String> hosts = headers.get().get("Host");8384out.println("Host: " + hosts);8586if (hosts.size() != 1 || hosts.get(0).contains("%") ||87!hosts.get(0).contains(ip6_address)) {88throw new RuntimeException("FAIL");89}90}9192static InetAddress getIPv6LookbackAddress() throws SocketException {93out.println("Searching for the IPv6 loopback address...");94Enumeration<NetworkInterface> is = NetworkInterface.getNetworkInterfaces();9596// The IPv6 loopback address contains a scope id, and is "connect-able".97while (is.hasMoreElements()) {98NetworkInterface i = is.nextElement();99if (!i.isLoopback())100continue;101Optional<InetAddress> addr = i.inetAddresses()102.filter(x -> x instanceof Inet6Address)103.filter(y -> y.toString().contains("%"))104.findFirst();105if (addr.isPresent())106return addr.get();107}108109return null;110}111112static HttpHandler createCapturingHandler(CompletableFuture<Headers> headers) {113return new HttpHandler() {114@Override115public void handle(HttpExchange exchange) throws IOException {116headers.complete(exchange.getRequestHeaders());117exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);118exchange.close();119}120};121}122}123124125