J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4924226 |
| 26 | * @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server |
| 27 | * @library ../../../sun/net/www/httptest/ |
| 28 | * @build ClosedChannelList HttpServer HttpTransaction HttpCallback |
| 29 | * @compile LoopbackAddresses.java |
| 30 | * @run main/othervm LoopbackAddresses |
| 31 | */ |
| 32 | |
| 33 | import java.net.*; |
| 34 | import java.io.*; |
| 35 | |
| 36 | /** |
| 37 | * Our default proxy selector should bypass localhost and loopback |
| 38 | * addresses when selecting proxies. This is the existing behaviour. |
| 39 | */ |
| 40 | |
| 41 | public class LoopbackAddresses implements HttpCallback { |
| 42 | static HttpServer server; |
| 43 | |
| 44 | public void request (HttpTransaction req) { |
| 45 | req.setResponseEntityBody ("Hello ."); |
| 46 | try { |
| 47 | req.sendResponse (200, "Ok"); |
| 48 | req.orderlyClose(); |
| 49 | } catch (IOException e) { |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public static void main(String[] args) { |
| 54 | try { |
| 55 | server = new HttpServer (new LoopbackAddresses(), 1, 10, 0); |
| 56 | ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort()); |
| 57 | // start proxy server |
| 58 | new Thread(pserver).start(); |
| 59 | |
| 60 | System.setProperty("http.proxyHost", "localhost"); |
| 61 | System.setProperty("http.proxyPort", pserver.getPort()+""); |
| 62 | |
| 63 | URL url = new URL("http://localhost:"+server.getLocalPort()); |
| 64 | |
| 65 | try { |
| 66 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
| 67 | int respCode = urlc.getResponseCode(); |
| 68 | urlc.disconnect(); |
| 69 | } catch (IOException ioex) { |
| 70 | throw new RuntimeException("direct connection should succeed :"+ioex.getMessage()); |
| 71 | } |
| 72 | |
| 73 | try { |
| 74 | url = new URL("http://127.0.0.1:"+server.getLocalPort()); |
| 75 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
| 76 | int respCode = urlc.getResponseCode(); |
| 77 | urlc.disconnect(); |
| 78 | } catch (IOException ioex) { |
| 79 | throw new RuntimeException("direct connection should succeed :"+ioex.getMessage()); |
| 80 | } |
| 81 | } catch (Exception e) { |
| 82 | throw new RuntimeException(e); |
| 83 | } finally { |
| 84 | if (server != null) { |
| 85 | server.terminate(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | private static class ProxyServer extends Thread { |
| 92 | private static ServerSocket ss = null; |
| 93 | |
| 94 | // client requesting for a tunnel |
| 95 | private Socket clientSocket = null; |
| 96 | |
| 97 | /* |
| 98 | * Origin server's address and port that the client |
| 99 | * wants to establish the tunnel for communication. */ |
| 100 | private InetAddress serverInetAddr; |
| 101 | private int serverPort; |
| 102 | |
| 103 | public ProxyServer(InetAddress server, int port) throws IOException { |
| 104 | serverInetAddr = server; |
| 105 | serverPort = port; |
| 106 | ss = new ServerSocket(0); |
| 107 | } |
| 108 | |
| 109 | public void run() { |
| 110 | try { |
| 111 | clientSocket = ss.accept(); |
| 112 | throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket); |
| 113 | |
| 114 | } catch (IOException e) { |
| 115 | System.out.println("Proxy Failed: " + e); |
| 116 | e.printStackTrace(); |
| 117 | } finally { |
| 118 | try { |
| 119 | ss.close(); |
| 120 | } |
| 121 | catch (IOException excep) { |
| 122 | System.out.println("ProxyServer close error: " + excep); |
| 123 | excep.printStackTrace(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | *************************************************************** |
| 130 | * helper methods follow |
| 131 | *************************************************************** |
| 132 | */ |
| 133 | public int getPort() { |
| 134 | return ss.getLocalPort(); |
| 135 | } |
| 136 | } |
| 137 | } |