Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/HttpConnectSocketImpl.java
38829 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.lang.reflect.Field;29import java.lang.reflect.Method;30import java.util.HashMap;31import java.util.Map;32import java.util.Set;3334/**35* Basic SocketImpl that relies on the internal HTTP protocol handler36* implementation to perform the HTTP tunneling and authentication. The37* sockets impl is swapped out and replaced with the socket from the HTTP38* handler after the tunnel is successfully setup.39*40* @since 1.841*/4243/*package*/ class HttpConnectSocketImpl extends PlainSocketImpl {4445private static final String httpURLClazzStr =46"sun.net.www.protocol.http.HttpURLConnection";47private static final String netClientClazzStr = "sun.net.NetworkClient";48private static final String doTunnelingStr = "doTunneling";49private static final Field httpField;50private static final Field serverSocketField;51private static final Method doTunneling;5253private final String server;54private InetSocketAddress external_address;55private HashMap<Integer, Object> optionsMap = new HashMap<>();5657static {58try {59Class<?> httpClazz = Class.forName(httpURLClazzStr, true, null);60httpField = httpClazz.getDeclaredField("http");61doTunneling = httpClazz.getDeclaredMethod(doTunnelingStr);62Class<?> netClientClazz = Class.forName(netClientClazzStr, true, null);63serverSocketField = netClientClazz.getDeclaredField("serverSocket");6465java.security.AccessController.doPrivileged(66new java.security.PrivilegedAction<Void>() {67public Void run() {68httpField.setAccessible(true);69serverSocketField.setAccessible(true);70return null;71}72});73} catch (ReflectiveOperationException x) {74throw new InternalError("Should not reach here", x);75}76}7778HttpConnectSocketImpl(String server, int port) {79this.server = server;80this.port = port;81}8283HttpConnectSocketImpl(Proxy proxy) {84SocketAddress a = proxy.address();85if ( !(a instanceof InetSocketAddress) )86throw new IllegalArgumentException("Unsupported address type");8788InetSocketAddress ad = (InetSocketAddress) a;89server = ad.getHostString();90port = ad.getPort();91}9293@Override94protected void connect(SocketAddress endpoint, int timeout)95throws IOException96{97if (endpoint == null || !(endpoint instanceof InetSocketAddress))98throw new IllegalArgumentException("Unsupported address type");99final InetSocketAddress epoint = (InetSocketAddress)endpoint;100final String destHost = epoint.isUnresolved() ? epoint.getHostName()101: epoint.getAddress().getHostAddress();102final int destPort = epoint.getPort();103104SecurityManager security = System.getSecurityManager();105if (security != null)106security.checkConnect(destHost, destPort);107108// Connect to the HTTP proxy server109String urlString = "http://" + destHost + ":" + destPort;110Socket httpSocket = privilegedDoTunnel(urlString, timeout);111112// Success!113external_address = epoint;114115// close the original socket impl and release its descriptor116close();117118// update the Sockets impl to the impl from the http Socket119AbstractPlainSocketImpl psi = (AbstractPlainSocketImpl) httpSocket.impl;120this.getSocket().impl = psi;121122// best effort is made to try and reset options previously set123Set<Map.Entry<Integer,Object>> options = optionsMap.entrySet();124try {125for(Map.Entry<Integer,Object> entry : options) {126psi.setOption(entry.getKey(), entry.getValue());127}128} catch (IOException x) { /* gulp! */ }129}130131@Override132public void setOption(int opt, Object val) throws SocketException {133super.setOption(opt, val);134135if (external_address != null)136return; // we're connected, just return137138// store options so that they can be re-applied to the impl after connect139optionsMap.put(opt, val);140}141142private Socket privilegedDoTunnel(final String urlString,143final int timeout)144throws IOException145{146try {147return java.security.AccessController.doPrivileged(148new java.security.PrivilegedExceptionAction<Socket>() {149public Socket run() throws IOException {150return doTunnel(urlString, timeout);151}152});153} catch (java.security.PrivilegedActionException pae) {154throw (IOException) pae.getException();155}156}157158private Socket doTunnel(String urlString, int connectTimeout)159throws IOException160{161Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));162URL destURL = new URL(urlString);163HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);164conn.setConnectTimeout(connectTimeout);165conn.setReadTimeout(this.timeout);166conn.connect();167doTunneling(conn);168try {169Object httpClient = httpField.get(conn);170return (Socket) serverSocketField.get(httpClient);171} catch (IllegalAccessException x) {172throw new InternalError("Should not reach here", x);173}174}175176private void doTunneling(HttpURLConnection conn) {177try {178doTunneling.invoke(conn);179} catch (ReflectiveOperationException x) {180throw new InternalError("Should not reach here", x);181}182}183184@Override185protected InetAddress getInetAddress() {186if (external_address != null)187return external_address.getAddress();188else189return super.getInetAddress();190}191192@Override193protected int getPort() {194if (external_address != null)195return external_address.getPort();196else197return super.getPort();198}199200@Override201protected int getLocalPort() {202if (socket != null)203return super.getLocalPort();204if (external_address != null)205return external_address.getPort();206else207return super.getLocalPort();208}209}210211212