47 "sun.net.www.protocol.http.HttpURLConnection";
48 private static final String netClientClazzStr = "sun.net.NetworkClient";
49 private static final String doTunnelingStr = "doTunneling";
50 private static final Field httpField;
51 private static final Field serverSocketField;
52 private static final Method doTunneling;
53
54 private final String server;
55 private InetSocketAddress external_address;
56 private HashMap<Integer, Object> optionsMap = new HashMap<>();
57
58 static {
59 try {
60 Class<?> httpClazz = Class.forName(httpURLClazzStr, true, null);
61 httpField = httpClazz.getDeclaredField("http");
62 doTunneling = httpClazz.getDeclaredMethod(doTunnelingStr);
63 Class<?> netClientClazz = Class.forName(netClientClazzStr, true, null);
64 serverSocketField = netClientClazz.getDeclaredField("serverSocket");
65
66 java.security.AccessController.doPrivileged(
67 new java.security.PrivilegedAction<Void>() {
68 public Void run() {
69 httpField.setAccessible(true);
70 serverSocketField.setAccessible(true);
71 return null;
72 }
73 });
74 } catch (ReflectiveOperationException x) {
75 throw new InternalError("Should not reach here", x);
76 }
77 }
78
79 HttpConnectSocketImpl(String server, int port) {
80 this.server = server;
81 this.port = port;
82 }
83
84 HttpConnectSocketImpl(Proxy proxy) {
85 SocketAddress a = proxy.address();
86 if ( !(a instanceof InetSocketAddress) )
87 throw new IllegalArgumentException("Unsupported address type");
129 } catch (IOException x) { /* gulp! */ }
130 }
131
132 @Override
133 public void setOption(int opt, Object val) throws SocketException {
134 super.setOption(opt, val);
135
136 if (external_address != null)
137 return; // we're connected, just return
138
139 // store options so that they can be re-applied to the impl after connect
140 optionsMap.put(opt, val);
141 }
142
143 private Socket privilegedDoTunnel(final String urlString,
144 final int timeout)
145 throws IOException
146 {
147 try {
148 return java.security.AccessController.doPrivileged(
149 new java.security.PrivilegedExceptionAction<Socket>() {
150 public Socket run() throws IOException {
151 return doTunnel(urlString, timeout);
152 }
153 });
154 } catch (java.security.PrivilegedActionException pae) {
155 throw (IOException) pae.getException();
156 }
157 }
158
159 private Socket doTunnel(String urlString, int connectTimeout)
160 throws IOException
161 {
162 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));
163 URL destURL = new URL(urlString);
164 HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
165 conn.setConnectTimeout(connectTimeout);
166 conn.setReadTimeout(this.timeout);
167 conn.connect();
168 doTunneling(conn);
169 try {
|
47 "sun.net.www.protocol.http.HttpURLConnection";
48 private static final String netClientClazzStr = "sun.net.NetworkClient";
49 private static final String doTunnelingStr = "doTunneling";
50 private static final Field httpField;
51 private static final Field serverSocketField;
52 private static final Method doTunneling;
53
54 private final String server;
55 private InetSocketAddress external_address;
56 private HashMap<Integer, Object> optionsMap = new HashMap<>();
57
58 static {
59 try {
60 Class<?> httpClazz = Class.forName(httpURLClazzStr, true, null);
61 httpField = httpClazz.getDeclaredField("http");
62 doTunneling = httpClazz.getDeclaredMethod(doTunnelingStr);
63 Class<?> netClientClazz = Class.forName(netClientClazzStr, true, null);
64 serverSocketField = netClientClazz.getDeclaredField("serverSocket");
65
66 java.security.AccessController.doPrivileged(
67 new java.security.PrivilegedAction<>() {
68 public Void run() {
69 httpField.setAccessible(true);
70 serverSocketField.setAccessible(true);
71 return null;
72 }
73 });
74 } catch (ReflectiveOperationException x) {
75 throw new InternalError("Should not reach here", x);
76 }
77 }
78
79 HttpConnectSocketImpl(String server, int port) {
80 this.server = server;
81 this.port = port;
82 }
83
84 HttpConnectSocketImpl(Proxy proxy) {
85 SocketAddress a = proxy.address();
86 if ( !(a instanceof InetSocketAddress) )
87 throw new IllegalArgumentException("Unsupported address type");
129 } catch (IOException x) { /* gulp! */ }
130 }
131
132 @Override
133 public void setOption(int opt, Object val) throws SocketException {
134 super.setOption(opt, val);
135
136 if (external_address != null)
137 return; // we're connected, just return
138
139 // store options so that they can be re-applied to the impl after connect
140 optionsMap.put(opt, val);
141 }
142
143 private Socket privilegedDoTunnel(final String urlString,
144 final int timeout)
145 throws IOException
146 {
147 try {
148 return java.security.AccessController.doPrivileged(
149 new java.security.PrivilegedExceptionAction<>() {
150 public Socket run() throws IOException {
151 return doTunnel(urlString, timeout);
152 }
153 });
154 } catch (java.security.PrivilegedActionException pae) {
155 throw (IOException) pae.getException();
156 }
157 }
158
159 private Socket doTunnel(String urlString, int connectTimeout)
160 throws IOException
161 {
162 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));
163 URL destURL = new URL(urlString);
164 HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
165 conn.setConnectTimeout(connectTimeout);
166 conn.setReadTimeout(this.timeout);
167 conn.connect();
168 doTunneling(conn);
169 try {
|