mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/DefaultClientConnectionOperator.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.conn;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.net.ConnectException;
michael@0 32 import java.net.InetSocketAddress;
michael@0 33 import java.net.Socket;
michael@0 34 import java.net.InetAddress;
michael@0 35 import java.net.UnknownHostException;
michael@0 36
michael@0 37 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
michael@0 38 /* LogFactory removed by HttpClient for Android script. */
michael@0 39 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0 40
michael@0 41 import ch.boye.httpclientandroidlib.HttpHost;
michael@0 42 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 43 import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
michael@0 44 import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0 45
michael@0 46 import ch.boye.httpclientandroidlib.conn.ConnectTimeoutException;
michael@0 47 import ch.boye.httpclientandroidlib.conn.HttpHostConnectException;
michael@0 48 import ch.boye.httpclientandroidlib.conn.OperatedClientConnection;
michael@0 49 import ch.boye.httpclientandroidlib.conn.ClientConnectionOperator;
michael@0 50 import ch.boye.httpclientandroidlib.conn.scheme.LayeredSchemeSocketFactory;
michael@0 51 import ch.boye.httpclientandroidlib.conn.scheme.Scheme;
michael@0 52 import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry;
michael@0 53 import ch.boye.httpclientandroidlib.conn.scheme.SchemeSocketFactory;
michael@0 54
michael@0 55 /**
michael@0 56 * Default implementation of a {@link ClientConnectionOperator}. It uses a {@link SchemeRegistry}
michael@0 57 * to look up {@link SchemeSocketFactory} objects.
michael@0 58 * <p>
michael@0 59 * This connection operator is multihome network aware and will attempt to retry failed connects
michael@0 60 * against all known IP addresses sequentially until the connect is successful or all known
michael@0 61 * addresses fail to respond. Please note the same
michael@0 62 * {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT} value will be used
michael@0 63 * for each connection attempt, so in the worst case the total elapsed time before timeout
michael@0 64 * can be <code>CONNECTION_TIMEOUT * n</code> where <code>n</code> is the number of IP addresses
michael@0 65 * of the given host. One can disable multihome support by overriding
michael@0 66 * the {@link #resolveHostname(String)} method and returning only one IP address for the given
michael@0 67 * host name.
michael@0 68 * <p>
michael@0 69 * The following parameters can be used to customize the behavior of this
michael@0 70 * class:
michael@0 71 * <ul>
michael@0 72 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
michael@0 73 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_TIMEOUT}</li>
michael@0 74 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_LINGER}</li>
michael@0 75 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li>
michael@0 76 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#TCP_NODELAY}</li>
michael@0 77 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
michael@0 78 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
michael@0 79 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
michael@0 80 * </ul>
michael@0 81 *
michael@0 82 * @since 4.0
michael@0 83 */
michael@0 84 @ThreadSafe
michael@0 85 public class DefaultClientConnectionOperator implements ClientConnectionOperator {
michael@0 86
michael@0 87 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0 88
michael@0 89 /** The scheme registry for looking up socket factories. */
michael@0 90 protected final SchemeRegistry schemeRegistry; // @ThreadSafe
michael@0 91
michael@0 92 /**
michael@0 93 * Creates a new client connection operator for the given scheme registry.
michael@0 94 *
michael@0 95 * @param schemes the scheme registry
michael@0 96 */
michael@0 97 public DefaultClientConnectionOperator(final SchemeRegistry schemes) {
michael@0 98 if (schemes == null) {
michael@0 99 throw new IllegalArgumentException("Scheme registry amy not be null");
michael@0 100 }
michael@0 101 this.schemeRegistry = schemes;
michael@0 102 }
michael@0 103
michael@0 104 public OperatedClientConnection createConnection() {
michael@0 105 return new DefaultClientConnection();
michael@0 106 }
michael@0 107
michael@0 108 public void openConnection(
michael@0 109 final OperatedClientConnection conn,
michael@0 110 final HttpHost target,
michael@0 111 final InetAddress local,
michael@0 112 final HttpContext context,
michael@0 113 final HttpParams params) throws IOException {
michael@0 114 if (conn == null) {
michael@0 115 throw new IllegalArgumentException("Connection may not be null");
michael@0 116 }
michael@0 117 if (target == null) {
michael@0 118 throw new IllegalArgumentException("Target host may not be null");
michael@0 119 }
michael@0 120 if (params == null) {
michael@0 121 throw new IllegalArgumentException("Parameters may not be null");
michael@0 122 }
michael@0 123 if (conn.isOpen()) {
michael@0 124 throw new IllegalStateException("Connection must not be open");
michael@0 125 }
michael@0 126
michael@0 127 Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
michael@0 128 SchemeSocketFactory sf = schm.getSchemeSocketFactory();
michael@0 129
michael@0 130 InetAddress[] addresses = resolveHostname(target.getHostName());
michael@0 131 int port = schm.resolvePort(target.getPort());
michael@0 132 for (int i = 0; i < addresses.length; i++) {
michael@0 133 InetAddress address = addresses[i];
michael@0 134 boolean last = i == addresses.length - 1;
michael@0 135
michael@0 136 Socket sock = sf.createSocket(params);
michael@0 137 conn.opening(sock, target);
michael@0 138
michael@0 139 InetSocketAddress remoteAddress = new HttpInetSocketAddress(target, address, port);
michael@0 140 InetSocketAddress localAddress = null;
michael@0 141 if (local != null) {
michael@0 142 localAddress = new InetSocketAddress(local, 0);
michael@0 143 }
michael@0 144 if (this.log.isDebugEnabled()) {
michael@0 145 this.log.debug("Connecting to " + remoteAddress);
michael@0 146 }
michael@0 147 try {
michael@0 148 Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params);
michael@0 149 if (sock != connsock) {
michael@0 150 sock = connsock;
michael@0 151 conn.opening(sock, target);
michael@0 152 }
michael@0 153 prepareSocket(sock, context, params);
michael@0 154 conn.openCompleted(sf.isSecure(sock), params);
michael@0 155 return;
michael@0 156 } catch (ConnectException ex) {
michael@0 157 if (last) {
michael@0 158 throw new HttpHostConnectException(target, ex);
michael@0 159 }
michael@0 160 } catch (ConnectTimeoutException ex) {
michael@0 161 if (last) {
michael@0 162 throw ex;
michael@0 163 }
michael@0 164 }
michael@0 165 if (this.log.isDebugEnabled()) {
michael@0 166 this.log.debug("Connect to " + remoteAddress + " timed out. " +
michael@0 167 "Connection will be retried using another IP address");
michael@0 168 }
michael@0 169 }
michael@0 170 }
michael@0 171
michael@0 172 public void updateSecureConnection(
michael@0 173 final OperatedClientConnection conn,
michael@0 174 final HttpHost target,
michael@0 175 final HttpContext context,
michael@0 176 final HttpParams params) throws IOException {
michael@0 177 if (conn == null) {
michael@0 178 throw new IllegalArgumentException("Connection may not be null");
michael@0 179 }
michael@0 180 if (target == null) {
michael@0 181 throw new IllegalArgumentException("Target host may not be null");
michael@0 182 }
michael@0 183 if (params == null) {
michael@0 184 throw new IllegalArgumentException("Parameters may not be null");
michael@0 185 }
michael@0 186 if (!conn.isOpen()) {
michael@0 187 throw new IllegalStateException("Connection must be open");
michael@0 188 }
michael@0 189
michael@0 190 final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
michael@0 191 if (!(schm.getSchemeSocketFactory() instanceof LayeredSchemeSocketFactory)) {
michael@0 192 throw new IllegalArgumentException
michael@0 193 ("Target scheme (" + schm.getName() +
michael@0 194 ") must have layered socket factory.");
michael@0 195 }
michael@0 196
michael@0 197 LayeredSchemeSocketFactory lsf = (LayeredSchemeSocketFactory) schm.getSchemeSocketFactory();
michael@0 198 Socket sock;
michael@0 199 try {
michael@0 200 sock = lsf.createLayeredSocket(
michael@0 201 conn.getSocket(), target.getHostName(), target.getPort(), true);
michael@0 202 } catch (ConnectException ex) {
michael@0 203 throw new HttpHostConnectException(target, ex);
michael@0 204 }
michael@0 205 prepareSocket(sock, context, params);
michael@0 206 conn.update(sock, target, lsf.isSecure(sock), params);
michael@0 207 }
michael@0 208
michael@0 209 /**
michael@0 210 * Performs standard initializations on a newly created socket.
michael@0 211 *
michael@0 212 * @param sock the socket to prepare
michael@0 213 * @param context the context for the connection
michael@0 214 * @param params the parameters from which to prepare the socket
michael@0 215 *
michael@0 216 * @throws IOException in case of an IO problem
michael@0 217 */
michael@0 218 protected void prepareSocket(
michael@0 219 final Socket sock,
michael@0 220 final HttpContext context,
michael@0 221 final HttpParams params) throws IOException {
michael@0 222 sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
michael@0 223 sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
michael@0 224
michael@0 225 int linger = HttpConnectionParams.getLinger(params);
michael@0 226 if (linger >= 0) {
michael@0 227 sock.setSoLinger(linger > 0, linger);
michael@0 228 }
michael@0 229 }
michael@0 230
michael@0 231 /**
michael@0 232 * Resolves the given host name to an array of corresponding IP addresses, based on the
michael@0 233 * configured name service on the system.
michael@0 234 *
michael@0 235 * @param host host name to resolve
michael@0 236 * @return array of IP addresses
michael@0 237 * @exception UnknownHostException if no IP address for the host could be determined.
michael@0 238 *
michael@0 239 * @since 4.1
michael@0 240 */
michael@0 241 protected InetAddress[] resolveHostname(final String host) throws UnknownHostException {
michael@0 242 return InetAddress.getAllByName(host);
michael@0 243 }
michael@0 244
michael@0 245 }
michael@0 246

mercurial