Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ProxySelector/LoopbackAddresses.java
38811 views
/*1* Copyright (c) 2003, 2012, 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* @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server26* @library ../../../sun/net/www/httptest/27* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback28* @compile LoopbackAddresses.java29* @run main/othervm LoopbackAddresses30*/3132import java.net.*;33import java.io.*;3435/**36* Our default proxy selector should bypass localhost and loopback37* addresses when selecting proxies. This is the existing behaviour.38*/3940public class LoopbackAddresses implements HttpCallback {41static TestHttpServer server;4243public void request (HttpTransaction req) {44req.setResponseEntityBody ("Hello .");45try {46req.sendResponse (200, "Ok");47req.orderlyClose();48} catch (IOException e) {49}50}5152public static void main(String[] args) {53try {54server = new TestHttpServer (new LoopbackAddresses(), 1, 10, 0);55ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());56// start proxy server57new Thread(pserver).start();5859System.setProperty("http.proxyHost", "localhost");60System.setProperty("http.proxyPort", pserver.getPort()+"");6162URL url = new URL("http://localhost:"+server.getLocalPort());6364try {65HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();66int respCode = urlc.getResponseCode();67urlc.disconnect();68} catch (IOException ioex) {69throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());70}7172try {73url = new URL("http://127.0.0.1:"+server.getLocalPort());74HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();75int respCode = urlc.getResponseCode();76urlc.disconnect();77} catch (IOException ioex) {78throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());79}80} catch (Exception e) {81throw new RuntimeException(e);82} finally {83if (server != null) {84server.terminate();85}86}8788}8990private static class ProxyServer extends Thread {91private static ServerSocket ss = null;9293// client requesting for a tunnel94private Socket clientSocket = null;9596/*97* Origin server's address and port that the client98* wants to establish the tunnel for communication. */99private InetAddress serverInetAddr;100private int serverPort;101102public ProxyServer(InetAddress server, int port) throws IOException {103serverInetAddr = server;104serverPort = port;105ss = new ServerSocket(0);106}107108public void run() {109try {110clientSocket = ss.accept();111throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket);112113} catch (IOException e) {114System.out.println("Proxy Failed: " + e);115e.printStackTrace();116} finally {117try {118ss.close();119}120catch (IOException excep) {121System.out.println("ProxyServer close error: " + excep);122excep.printStackTrace();123}124}125}126127/**128***************************************************************129* helper methods follow130***************************************************************131*/132public int getPort() {133return ss.getLocalPort();134}135}136}137138139