Path: blob/master/test/jdk/java/net/ProxySelector/LoopbackAddresses.java
41149 views
/*1* Copyright (c) 2003, 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/* @test24* @bug 492422625* @key intermittent26* @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server.27* This test might fail intermittently as it needs a server that28* binds to the wildcard address.29* @modules java.base/sun.net.www30* @library ../../../sun/net/www/httptest/ /test/lib31* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback32* @compile LoopbackAddresses.java33* @run main/othervm LoopbackAddresses34*/3536import java.net.*;37import java.io.*;38import jdk.test.lib.net.URIBuilder;3940/**41* Our default proxy selector should bypass localhost and loopback42* addresses when selecting proxies. This is the existing behaviour.43*/4445public class LoopbackAddresses implements HttpCallback {46static TestHttpServer server;4748public void request (HttpTransaction req) {49req.setResponseEntityBody ("Hello .");50try {51req.sendResponse (200, "Ok");52req.orderlyClose();53} catch (IOException e) {54}55}5657public static void main(String[] args) {58try {59InetAddress loopback = InetAddress.getLoopbackAddress();6061// This server needs to bind to the wildcard address as we want it62// to answer both for the loopback and "localhost".63// Though "localhost" usually point to the loopback there is no64// hard guarantee.65server = new TestHttpServer (new LoopbackAddresses(), 1, 10, 0);66ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());67// start proxy server68new Thread(pserver).start();6970System.setProperty("http.proxyHost", loopback.getHostAddress());71System.setProperty("http.proxyPort", pserver.getPort()+"");7273URL url = new URL("http://localhost:"+server.getLocalPort());7475try {76HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();77int respCode = urlc.getResponseCode();78urlc.disconnect();79} catch (IOException ioex) {80throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());81}8283try {84url = URIBuilder.newBuilder()85.scheme("http")86.host(loopback.getHostAddress())87.port(server.getLocalPort())88.toURL();89HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();90int respCode = urlc.getResponseCode();91urlc.disconnect();92} catch (IOException ioex) {93throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());94}95} catch (Exception e) {96throw new RuntimeException(e);97} finally {98if (server != null) {99server.terminate();100}101}102103}104105private static class ProxyServer extends Thread {106private static ServerSocket ss = null;107108// client requesting for a tunnel109private Socket clientSocket = null;110111/*112* Origin server's address and port that the client113* wants to establish the tunnel for communication. */114private InetAddress serverInetAddr;115private int serverPort;116117public ProxyServer(InetAddress server, int port) throws IOException {118serverInetAddr = server;119serverPort = port;120ss = new ServerSocket();121ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));122}123124public void run() {125try {126clientSocket = ss.accept();127throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket);128129} catch (IOException e) {130System.out.println("Proxy Failed: " + e);131e.printStackTrace();132} finally {133try {134ss.close();135}136catch (IOException excep) {137System.out.println("ProxyServer close error: " + excep);138excep.printStackTrace();139}140}141}142143/**144***************************************************************145* helper methods follow146***************************************************************147*/148public int getPort() {149return ss.getLocalPort();150}151}152}153154155