author | alanb |
Mon, 29 Apr 2019 09:03:06 +0100 | |
branch | niosocketimpl-branch |
changeset 57341 | 733e9746d615 |
parent 57277 | d2b2a4edbfe7 |
child 57347 | 16c087c9103e |
permissions | -rw-r--r-- |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
1 |
/* |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
2 |
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved. |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
4 |
* |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
10 |
* |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
15 |
* accompanied this code). |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
16 |
* |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
20 |
* |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
23 |
* questions. |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
24 |
*/ |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
25 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
26 |
package java.net; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
27 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
28 |
import java.io.IOException; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
29 |
import java.lang.reflect.Field; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
30 |
import java.lang.reflect.Method; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
31 |
import java.util.HashMap; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
32 |
import java.util.Map; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
33 |
import java.util.Set; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
34 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
35 |
/** |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
36 |
* Basic SocketImpl that relies on the internal HTTP protocol handler |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
37 |
* implementation to perform the HTTP tunneling and authentication. Once |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
38 |
* connected, all socket operations delegate to a platform SocketImpl. |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
39 |
* |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
40 |
* @since 1.8 |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
41 |
*/ |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
42 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
43 |
/*package*/ class HttpConnectSocketImpl extends DelegatingSocketImpl { |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
44 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
45 |
private static final String httpURLClazzStr = |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
46 |
"sun.net.www.protocol.http.HttpURLConnection"; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
47 |
private static final String netClientClazzStr = "sun.net.NetworkClient"; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
48 |
private static final String doTunnelingStr = "doTunneling"; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
49 |
private static final Field httpField; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
50 |
private static final Field serverSocketField; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
51 |
private static final Method doTunneling; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
52 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
53 |
private final String server; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
54 |
private InetSocketAddress external_address; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
55 |
private HashMap<Integer, Object> optionsMap = new HashMap<>(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
56 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
57 |
static { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
58 |
try { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
59 |
Class<?> httpClazz = Class.forName(httpURLClazzStr, true, null); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
60 |
httpField = httpClazz.getDeclaredField("http"); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
61 |
doTunneling = httpClazz.getDeclaredMethod(doTunnelingStr); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
62 |
Class<?> netClientClazz = Class.forName(netClientClazzStr, true, null); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
63 |
serverSocketField = netClientClazz.getDeclaredField("serverSocket"); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
64 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
65 |
java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
66 |
new java.security.PrivilegedAction<>() { |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
67 |
public Void run() { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
68 |
httpField.setAccessible(true); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
69 |
serverSocketField.setAccessible(true); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
70 |
return null; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
71 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
72 |
}); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
73 |
} catch (ReflectiveOperationException x) { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
74 |
throw new InternalError("Should not reach here", x); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
75 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
76 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
77 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
78 |
HttpConnectSocketImpl(Proxy proxy, SocketImpl delegate) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
79 |
super(delegate); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
80 |
SocketAddress a = proxy.address(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
81 |
if ( !(a instanceof InetSocketAddress) ) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
82 |
throw new IllegalArgumentException("Unsupported address type"); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
83 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
84 |
InetSocketAddress ad = (InetSocketAddress) a; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
85 |
server = ad.getHostString(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
86 |
port = ad.getPort(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
87 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
88 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
89 |
@Override |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
90 |
protected void connect(String host, int port) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
91 |
connect(new InetSocketAddress(host, port), 0); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
92 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
93 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
94 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
95 |
protected void connect(InetAddress address, int port) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
96 |
connect(new InetSocketAddress(address, port), 0); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
97 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
98 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
99 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
100 |
void setSocket(Socket socket) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
101 |
delegate.socket = socket; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
102 |
super.setSocket(socket); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
103 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
104 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
105 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
106 |
void setServerSocket(ServerSocket socket) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
107 |
throw new InternalError("should not get here"); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
108 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
109 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
110 |
@Override |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
111 |
protected void connect(SocketAddress endpoint, int timeout) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
112 |
throws IOException |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
113 |
{ |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
114 |
if (endpoint == null || !(endpoint instanceof InetSocketAddress)) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
115 |
throw new IllegalArgumentException("Unsupported address type"); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
116 |
final InetSocketAddress epoint = (InetSocketAddress)endpoint; |
54188
791052cc88db
8220663: Incorrect handling of IPv6 addresses in Socket(Proxy.HTTP)
chegar
parents:
54155
diff
changeset
|
117 |
String destHost = epoint.isUnresolved() ? epoint.getHostName() |
791052cc88db
8220663: Incorrect handling of IPv6 addresses in Socket(Proxy.HTTP)
chegar
parents:
54155
diff
changeset
|
118 |
: epoint.getAddress().getHostAddress(); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
119 |
final int destPort = epoint.getPort(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
120 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
121 |
SecurityManager security = System.getSecurityManager(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
122 |
if (security != null) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
123 |
security.checkConnect(destHost, destPort); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
124 |
|
54188
791052cc88db
8220663: Incorrect handling of IPv6 addresses in Socket(Proxy.HTTP)
chegar
parents:
54155
diff
changeset
|
125 |
if (destHost.contains(":")) |
791052cc88db
8220663: Incorrect handling of IPv6 addresses in Socket(Proxy.HTTP)
chegar
parents:
54155
diff
changeset
|
126 |
destHost = "[" + destHost + "]"; |
791052cc88db
8220663: Incorrect handling of IPv6 addresses in Socket(Proxy.HTTP)
chegar
parents:
54155
diff
changeset
|
127 |
|
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
128 |
// Connect to the HTTP proxy server |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
129 |
String urlString = "http://" + destHost + ":" + destPort; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
130 |
Socket httpSocket = privilegedDoTunnel(urlString, timeout); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
131 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
132 |
// Success! |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
133 |
external_address = epoint; |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
134 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
135 |
// close the original socket impl and release its descriptor |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
136 |
close(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
137 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
138 |
// update the Sockets impl to the impl from the http Socket |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
139 |
SocketImpl si = httpSocket.impl; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
140 |
getSocket().setImpl(si); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
141 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
142 |
// best effort is made to try and reset options previously set |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
143 |
Set<Map.Entry<Integer,Object>> options = optionsMap.entrySet(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
144 |
try { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
145 |
for(Map.Entry<Integer,Object> entry : options) { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
146 |
si.setOption(entry.getKey(), entry.getValue()); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
147 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
148 |
} catch (IOException x) { /* gulp! */ } |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
149 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
150 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
151 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
152 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
153 |
protected void listen(int backlog) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
154 |
throw new InternalError("should not get here"); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
155 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
156 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
157 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
158 |
protected void accept(SocketImpl s) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
159 |
throw new InternalError("should not get here"); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
160 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
161 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
162 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
163 |
void reset() { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
164 |
throw new InternalError("should not get here"); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
165 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
166 |
|
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
167 |
@Override |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
168 |
public void setOption(int opt, Object val) throws SocketException { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
169 |
delegate.setOption(opt, val); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
170 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
171 |
if (external_address != null) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
172 |
return; // we're connected, just return |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
173 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
174 |
// store options so that they can be re-applied to the impl after connect |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
175 |
optionsMap.put(opt, val); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
176 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
177 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
178 |
private Socket privilegedDoTunnel(final String urlString, |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
179 |
final int timeout) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
180 |
throws IOException |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
181 |
{ |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
182 |
try { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
183 |
return java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
184 |
new java.security.PrivilegedExceptionAction<>() { |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
185 |
public Socket run() throws IOException { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
186 |
return doTunnel(urlString, timeout); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
187 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
188 |
}); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
189 |
} catch (java.security.PrivilegedActionException pae) { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
190 |
throw (IOException) pae.getException(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
191 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
192 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
193 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
194 |
private Socket doTunnel(String urlString, int connectTimeout) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
195 |
throws IOException |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
196 |
{ |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
197 |
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port)); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
198 |
URL destURL = new URL(urlString); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
199 |
HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
200 |
conn.setConnectTimeout(connectTimeout); |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
201 |
int timeout = (int) getOption(SocketOptions.SO_TIMEOUT); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
202 |
if (timeout > 0) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
203 |
conn.setReadTimeout(timeout); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
204 |
} |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
205 |
conn.connect(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
206 |
doTunneling(conn); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
207 |
try { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
208 |
Object httpClient = httpField.get(conn); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
209 |
return (Socket) serverSocketField.get(httpClient); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
210 |
} catch (IllegalAccessException x) { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
211 |
throw new InternalError("Should not reach here", x); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
212 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
213 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
214 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
215 |
private void doTunneling(HttpURLConnection conn) throws IOException { |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
216 |
try { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
217 |
doTunneling.invoke(conn); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
218 |
} catch (ReflectiveOperationException x) { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
219 |
Throwable cause = x.getCause(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
220 |
if (cause instanceof IOException) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
221 |
throw (IOException) cause; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
222 |
} |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
223 |
throw new InternalError("Should not reach here", x); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
224 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
225 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
226 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
227 |
@Override |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
228 |
protected InetAddress getInetAddress() { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
229 |
if (external_address != null) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
230 |
return external_address.getAddress(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
231 |
else |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
232 |
return delegate.getInetAddress(); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
233 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
234 |
|
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
235 |
@Override |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
236 |
protected int getPort() { |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
237 |
if (external_address != null) |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
238 |
return external_address.getPort(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
239 |
else |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53473
diff
changeset
|
240 |
return delegate.getPort(); |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
241 |
} |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff
changeset
|
242 |
} |