mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/PlainSocketFactory.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.conn.scheme;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.net.InetAddress;
michael@0 32 import java.net.InetSocketAddress;
michael@0 33 import java.net.Socket;
michael@0 34 import java.net.SocketTimeoutException;
michael@0 35 import java.net.UnknownHostException;
michael@0 36
michael@0 37 import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 38
michael@0 39 import ch.boye.httpclientandroidlib.conn.ConnectTimeoutException;
michael@0 40 import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
michael@0 41 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 42
michael@0 43 /**
michael@0 44 * The default class for creating plain (unencrypted) sockets.
michael@0 45 * <p>
michael@0 46 * The following parameters can be used to customize the behavior of this
michael@0 47 * class:
michael@0 48 * <ul>
michael@0 49 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
michael@0 50 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li>
michael@0 51 * </ul>
michael@0 52 *
michael@0 53 * @since 4.0
michael@0 54 */
michael@0 55 @SuppressWarnings("deprecation")
michael@0 56 @Immutable
michael@0 57 public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
michael@0 58
michael@0 59 private final HostNameResolver nameResolver;
michael@0 60
michael@0 61 /**
michael@0 62 * Gets the default factory.
michael@0 63 *
michael@0 64 * @return the default factory
michael@0 65 */
michael@0 66 public static PlainSocketFactory getSocketFactory() {
michael@0 67 return new PlainSocketFactory();
michael@0 68 }
michael@0 69
michael@0 70 @Deprecated
michael@0 71 public PlainSocketFactory(final HostNameResolver nameResolver) {
michael@0 72 super();
michael@0 73 this.nameResolver = nameResolver;
michael@0 74 }
michael@0 75
michael@0 76 public PlainSocketFactory() {
michael@0 77 super();
michael@0 78 this.nameResolver = null;
michael@0 79 }
michael@0 80
michael@0 81 /**
michael@0 82 * @param params Optional parameters. Parameters passed to this method will have no effect.
michael@0 83 * This method will create a unconnected instance of {@link Socket} class
michael@0 84 * using default constructor.
michael@0 85 *
michael@0 86 * @since 4.1
michael@0 87 */
michael@0 88 public Socket createSocket(final HttpParams params) {
michael@0 89 return new Socket();
michael@0 90 }
michael@0 91
michael@0 92 public Socket createSocket() {
michael@0 93 return new Socket();
michael@0 94 }
michael@0 95
michael@0 96 /**
michael@0 97 * @since 4.1
michael@0 98 */
michael@0 99 public Socket connectSocket(
michael@0 100 final Socket socket,
michael@0 101 final InetSocketAddress remoteAddress,
michael@0 102 final InetSocketAddress localAddress,
michael@0 103 final HttpParams params) throws IOException, ConnectTimeoutException {
michael@0 104 if (remoteAddress == null) {
michael@0 105 throw new IllegalArgumentException("Remote address may not be null");
michael@0 106 }
michael@0 107 if (params == null) {
michael@0 108 throw new IllegalArgumentException("HTTP parameters may not be null");
michael@0 109 }
michael@0 110 Socket sock = socket;
michael@0 111 if (sock == null) {
michael@0 112 sock = createSocket();
michael@0 113 }
michael@0 114 if (localAddress != null) {
michael@0 115 sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
michael@0 116 sock.bind(localAddress);
michael@0 117 }
michael@0 118 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
michael@0 119 int soTimeout = HttpConnectionParams.getSoTimeout(params);
michael@0 120
michael@0 121 try {
michael@0 122 sock.setSoTimeout(soTimeout);
michael@0 123 sock.connect(remoteAddress, connTimeout);
michael@0 124 } catch (SocketTimeoutException ex) {
michael@0 125 throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
michael@0 126 }
michael@0 127 return sock;
michael@0 128 }
michael@0 129
michael@0 130 /**
michael@0 131 * Checks whether a socket connection is secure.
michael@0 132 * This factory creates plain socket connections
michael@0 133 * which are not considered secure.
michael@0 134 *
michael@0 135 * @param sock the connected socket
michael@0 136 *
michael@0 137 * @return <code>false</code>
michael@0 138 *
michael@0 139 * @throws IllegalArgumentException if the argument is invalid
michael@0 140 */
michael@0 141 public final boolean isSecure(Socket sock)
michael@0 142 throws IllegalArgumentException {
michael@0 143
michael@0 144 if (sock == null) {
michael@0 145 throw new IllegalArgumentException("Socket may not be null.");
michael@0 146 }
michael@0 147 // This check is performed last since it calls a method implemented
michael@0 148 // by the argument object. getClass() is final in java.lang.Object.
michael@0 149 if (sock.isClosed()) {
michael@0 150 throw new IllegalArgumentException("Socket is closed.");
michael@0 151 }
michael@0 152 return false;
michael@0 153 }
michael@0 154
michael@0 155 /**
michael@0 156 * @deprecated Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}
michael@0 157 */
michael@0 158 @Deprecated
michael@0 159 public Socket connectSocket(
michael@0 160 final Socket socket,
michael@0 161 final String host, int port,
michael@0 162 final InetAddress localAddress, int localPort,
michael@0 163 final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
michael@0 164 InetSocketAddress local = null;
michael@0 165 if (localAddress != null || localPort > 0) {
michael@0 166 // we need to bind explicitly
michael@0 167 if (localPort < 0) {
michael@0 168 localPort = 0; // indicates "any"
michael@0 169 }
michael@0 170 local = new InetSocketAddress(localAddress, localPort);
michael@0 171 }
michael@0 172 InetAddress remoteAddress;
michael@0 173 if (this.nameResolver != null) {
michael@0 174 remoteAddress = this.nameResolver.resolve(host);
michael@0 175 } else {
michael@0 176 remoteAddress = InetAddress.getByName(host);
michael@0 177 }
michael@0 178 InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
michael@0 179 return connectSocket(socket, remote, local, params);
michael@0 180 }
michael@0 181
michael@0 182 }

mercurial