author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
|
30820 | 27 |
* @modules java.base/sun.net.www |
2 | 28 |
* @library ../../../sun/net/www/httptest/ |
13788 | 29 |
* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback |
2 | 30 |
* @compile LoopbackAddresses.java |
31 |
* @run main/othervm LoopbackAddresses |
|
32 |
*/ |
|
33 |
||
34 |
import java.net.*; |
|
35 |
import java.io.*; |
|
36 |
||
37 |
/** |
|
38 |
* Our default proxy selector should bypass localhost and loopback |
|
39 |
* addresses when selecting proxies. This is the existing behaviour. |
|
40 |
*/ |
|
41 |
||
42 |
public class LoopbackAddresses implements HttpCallback { |
|
13788 | 43 |
static TestHttpServer server; |
2 | 44 |
|
45 |
public void request (HttpTransaction req) { |
|
46 |
req.setResponseEntityBody ("Hello ."); |
|
47 |
try { |
|
48 |
req.sendResponse (200, "Ok"); |
|
49 |
req.orderlyClose(); |
|
50 |
} catch (IOException e) { |
|
51 |
} |
|
52 |
} |
|
53 |
||
54 |
public static void main(String[] args) { |
|
55 |
try { |
|
13788 | 56 |
server = new TestHttpServer (new LoopbackAddresses(), 1, 10, 0); |
2 | 57 |
ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort()); |
58 |
// start proxy server |
|
59 |
new Thread(pserver).start(); |
|
60 |
||
61 |
System.setProperty("http.proxyHost", "localhost"); |
|
62 |
System.setProperty("http.proxyPort", pserver.getPort()+""); |
|
63 |
||
64 |
URL url = new URL("http://localhost:"+server.getLocalPort()); |
|
65 |
||
66 |
try { |
|
67 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
68 |
int respCode = urlc.getResponseCode(); |
|
69 |
urlc.disconnect(); |
|
70 |
} catch (IOException ioex) { |
|
71 |
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage()); |
|
72 |
} |
|
73 |
||
74 |
try { |
|
75 |
url = new URL("http://127.0.0.1:"+server.getLocalPort()); |
|
76 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
77 |
int respCode = urlc.getResponseCode(); |
|
78 |
urlc.disconnect(); |
|
79 |
} catch (IOException ioex) { |
|
80 |
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage()); |
|
81 |
} |
|
82 |
} catch (Exception e) { |
|
83 |
throw new RuntimeException(e); |
|
84 |
} finally { |
|
85 |
if (server != null) { |
|
86 |
server.terminate(); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
} |
|
91 |
||
92 |
private static class ProxyServer extends Thread { |
|
93 |
private static ServerSocket ss = null; |
|
94 |
||
95 |
// client requesting for a tunnel |
|
96 |
private Socket clientSocket = null; |
|
97 |
||
98 |
/* |
|
99 |
* Origin server's address and port that the client |
|
100 |
* wants to establish the tunnel for communication. */ |
|
101 |
private InetAddress serverInetAddr; |
|
102 |
private int serverPort; |
|
103 |
||
104 |
public ProxyServer(InetAddress server, int port) throws IOException { |
|
105 |
serverInetAddr = server; |
|
106 |
serverPort = port; |
|
107 |
ss = new ServerSocket(0); |
|
108 |
} |
|
109 |
||
110 |
public void run() { |
|
111 |
try { |
|
112 |
clientSocket = ss.accept(); |
|
113 |
throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket); |
|
114 |
||
115 |
} catch (IOException e) { |
|
116 |
System.out.println("Proxy Failed: " + e); |
|
117 |
e.printStackTrace(); |
|
118 |
} finally { |
|
119 |
try { |
|
120 |
ss.close(); |
|
121 |
} |
|
122 |
catch (IOException excep) { |
|
123 |
System.out.println("ProxyServer close error: " + excep); |
|
124 |
excep.printStackTrace(); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
*************************************************************** |
|
131 |
* helper methods follow |
|
132 |
*************************************************************** |
|
133 |
*/ |
|
134 |
public int getPort() { |
|
135 |
return ss.getLocalPort(); |
|
136 |
} |
|
137 |
} |
|
138 |
} |