author | jboes |
Fri, 01 Nov 2019 12:57:01 +0000 | |
changeset 58896 | bd9daab73a8e |
permissions | -rw-r--r-- |
58896
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
1 |
/* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
4 |
* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
8 |
* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
13 |
* accompanied this code). |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
14 |
* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
18 |
* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
21 |
* questions. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
22 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
23 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
24 |
/* @test |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
25 |
* @bug 8231632 |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
26 |
* @summary HttpURLConnection::usingProxy could specify that it lazily evaluates the fact |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
27 |
* @modules java.base/sun.net.www |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
28 |
* @library /test/lib |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
29 |
* @run main/othervm HttpURLConnUsingProxy |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
30 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
31 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
32 |
import java.io.*; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
33 |
import java.net.*; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
34 |
import java.nio.charset.StandardCharsets; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
35 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpExchange; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
37 |
import com.sun.net.httpserver.HttpHandler; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
38 |
import com.sun.net.httpserver.HttpServer; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
39 |
import jdk.test.lib.net.URIBuilder; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
40 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
41 |
public class HttpURLConnUsingProxy { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
42 |
static HttpServer server; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
43 |
static Proxy proxy; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
44 |
static InetSocketAddress isa; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
45 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
46 |
static class Handler implements HttpHandler { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
47 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
48 |
@Override |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
49 |
public void handle(HttpExchange exchange) throws IOException { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
50 |
byte[] response = "Hello World!".getBytes(StandardCharsets.UTF_8); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
51 |
try (InputStream req = exchange.getRequestBody()) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
52 |
req.readAllBytes(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
53 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
54 |
exchange.sendResponseHeaders(200, response.length); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
55 |
try (OutputStream resp = exchange.getResponseBody()) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
56 |
resp.write(response); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
57 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
58 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
59 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
60 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
61 |
public static void main(String[] args) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
62 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
63 |
InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
64 |
InetSocketAddress addr = new InetSocketAddress(loopbackAddress, 0); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
65 |
server = HttpServer.create(addr, 0); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
66 |
server.createContext("/HttpURLConnUsingProxy/http1/", new Handler()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
67 |
server.start(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
68 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
69 |
ProxyServer pserver = new ProxyServer(loopbackAddress, |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
70 |
server.getAddress().getPort()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
71 |
// Start proxy server |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
72 |
new Thread(pserver).start(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
73 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
74 |
URL url = URIBuilder.newBuilder() |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
75 |
.scheme("http") |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
76 |
.loopback() |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
77 |
.port(server.getAddress().getPort()) |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
78 |
.path("/HttpURLConnUsingProxy/http1/x.html") |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
79 |
.toURLUnchecked(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
80 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
81 |
// NO_PROXY |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
82 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
83 |
HttpURLConnection urlc = |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
84 |
(HttpURLConnection) url.openConnection(Proxy.NO_PROXY); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
85 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
86 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
87 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
88 |
urlc.disconnect(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
89 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
90 |
throw new RuntimeException("Direct connection should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
91 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
92 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
93 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
94 |
// Non-existing proxy |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
95 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
96 |
isa = InetSocketAddress.createUnresolved("inexistent", 8080); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
97 |
proxy = new Proxy(Proxy.Type.HTTP, isa); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
98 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(proxy); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
99 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
100 |
InputStream is = urlc.getInputStream(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
101 |
is.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
102 |
throw new RuntimeException("Non-existing proxy should cause IOException"); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
103 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
104 |
// expected |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
105 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
106 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
107 |
// Normal proxy settings |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
108 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
109 |
isa = InetSocketAddress.createUnresolved(loopbackAddress.getHostAddress(), |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
110 |
pserver.getPort()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
111 |
proxy = new Proxy(Proxy.Type.HTTP, isa); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
112 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(proxy); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
113 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
114 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
115 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
116 |
urlc.disconnect(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
117 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
118 |
throw new RuntimeException("Connection through local proxy should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
119 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
120 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
121 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
122 |
// Reuse proxy with new HttpURLConnection |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
123 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
124 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(proxy); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
125 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
126 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
127 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
128 |
read(urlc.getInputStream()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
129 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
130 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
131 |
throw new RuntimeException("Connection through local proxy should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
132 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
133 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
134 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
135 |
// Reuse proxy with existing HttpURLConnection |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
136 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
137 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(proxy); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
138 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
139 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
140 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
141 |
read(urlc.getInputStream()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
142 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
143 |
urlc.disconnect(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
144 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
145 |
throw new RuntimeException("Connection through local proxy should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
146 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
147 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
148 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
149 |
// ProxySelector with normal proxy settings |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
150 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
151 |
ProxySelector.setDefault(ProxySelector.of(isa)); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
152 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
153 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
154 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
155 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
156 |
read(urlc.getInputStream()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
157 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
158 |
urlc.disconnect(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
159 |
assertEqual(urlc.usingProxy(), true); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
160 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
161 |
throw new RuntimeException("Connection through local proxy should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
162 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
163 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
164 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
165 |
// ProxySelector with proxying disabled |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
166 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
167 |
ProxySelector.setDefault(ProxySelector.of(null)); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
168 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
169 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
170 |
urlc.getResponseCode(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
171 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
172 |
read(urlc.getInputStream()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
173 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
174 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
175 |
throw new RuntimeException("Direct connection should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
176 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
177 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
178 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
179 |
// ProxySelector overwritten |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
180 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
181 |
ProxySelector.setDefault(ProxySelector.of(isa)); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
182 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
183 |
assertEqual(urlc.usingProxy(), false); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
184 |
urlc.disconnect(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
185 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
186 |
throw new RuntimeException("Direct connection should succeed: " |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
187 |
+ ioe.getMessage()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
188 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
189 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
190 |
} catch (Exception e) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
191 |
throw new RuntimeException(e); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
192 |
} finally { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
193 |
if (server != null) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
194 |
server.stop(0); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
195 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
196 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
197 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
198 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
199 |
static class ProxyServer extends Thread { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
200 |
private static ServerSocket ss = null; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
201 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
202 |
// Client requesting a tunnel |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
203 |
private Socket clientSocket = null; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
204 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
205 |
/* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
206 |
* Origin server's address and port that the client |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
207 |
* wants to establish the tunnel for communication. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
208 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
209 |
private InetAddress serverInetAddr; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
210 |
private int serverPort; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
211 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
212 |
public ProxyServer(InetAddress server, int port) throws IOException { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
213 |
serverInetAddr = server; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
214 |
serverPort = port; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
215 |
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
216 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
217 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
218 |
public void run() { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
219 |
while (true) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
220 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
221 |
clientSocket = ss.accept(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
222 |
processRequests(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
223 |
} catch (Exception e) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
224 |
System.out.println("Proxy failed: " + e); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
225 |
e.printStackTrace(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
226 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
227 |
ss.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
228 |
} catch (IOException ioe) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
229 |
System.out.println("ProxyServer close error: " + ioe); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
230 |
ioe.printStackTrace(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
231 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
232 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
233 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
234 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
235 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
236 |
private void processRequests() throws Exception { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
237 |
// Connection set to tunneling mode |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
238 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
239 |
Socket serverSocket = new Socket(serverInetAddr, serverPort); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
240 |
ProxyTunnel clientToServer = new ProxyTunnel( |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
241 |
clientSocket, serverSocket); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
242 |
ProxyTunnel serverToClient = new ProxyTunnel( |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
243 |
serverSocket, clientSocket); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
244 |
clientToServer.start(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
245 |
serverToClient.start(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
246 |
System.out.println("Proxy: Started tunneling..."); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
247 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
248 |
clientToServer.join(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
249 |
serverToClient.join(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
250 |
System.out.println("Proxy: Finished tunneling..."); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
251 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
252 |
clientToServer.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
253 |
serverToClient.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
254 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
255 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
256 |
/** |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
257 |
* ************************************************************** |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
258 |
* Helper methods follow |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
259 |
* ************************************************************** |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
260 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
261 |
public int getPort() { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
262 |
return ss.getLocalPort(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
263 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
264 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
265 |
/* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
266 |
* This inner class provides unidirectional data flow through the sockets |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
267 |
* by continuously copying bytes from input socket to output socket |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
268 |
* while both sockets are open and EOF has not been received. |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
269 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
270 |
static class ProxyTunnel extends Thread { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
271 |
Socket sockIn; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
272 |
Socket sockOut; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
273 |
InputStream input; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
274 |
OutputStream output; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
275 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
276 |
public ProxyTunnel(Socket sockIn, Socket sockOut) throws Exception { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
277 |
this.sockIn = sockIn; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
278 |
this.sockOut = sockOut; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
279 |
input = sockIn.getInputStream(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
280 |
output = sockOut.getOutputStream(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
281 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
282 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
283 |
public void run() { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
284 |
int BUFFER_SIZE = 400; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
285 |
byte[] buf = new byte[BUFFER_SIZE]; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
286 |
int bytesRead = 0; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
287 |
int count = 0; // Keep track of amount of data transferred |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
288 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
289 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
290 |
while ((bytesRead = input.read(buf)) >= 0) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
291 |
output.write(buf, 0, bytesRead); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
292 |
output.flush(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
293 |
count += bytesRead; |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
294 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
295 |
} catch (IOException e) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
296 |
/* |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
297 |
* Peer end has closed connection |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
298 |
* so we close tunnel |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
299 |
*/ |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
300 |
close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
301 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
302 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
303 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
304 |
public void close() { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
305 |
try { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
306 |
if (!sockIn.isClosed()) |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
307 |
sockIn.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
308 |
if (!sockOut.isClosed()) |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
309 |
sockOut.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
310 |
} catch (IOException ignored) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
311 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
312 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
313 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
314 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
315 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
316 |
private static void assertEqual(boolean usingProxy, boolean expected) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
317 |
if (usingProxy != expected) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
318 |
throw new RuntimeException("Expected: " + expected |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
319 |
+ " but usingProxy returned: " + usingProxy); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
320 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
321 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
322 |
|
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
323 |
private static String read(InputStream inputStream) throws IOException { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
324 |
StringBuilder sb = new StringBuilder(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
325 |
BufferedReader bufferedReader = new BufferedReader( |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
326 |
new InputStreamReader(inputStream, StandardCharsets.UTF_8)); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
327 |
int i = bufferedReader.read(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
328 |
while (i != -1) { |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
329 |
sb.append((char) i); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
330 |
i = bufferedReader.read(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
331 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
332 |
bufferedReader.close(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
333 |
return sb.toString(); |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
334 |
} |
bd9daab73a8e
8231632: HttpURLConnection::usingProxy could specify that it may lazily evaluate the fact
jboes
parents:
diff
changeset
|
335 |
} |