mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/DefaultHttpClient.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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.client;
michael@0 29
michael@0 30 import ch.boye.httpclientandroidlib.HttpVersion;
michael@0 31 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0 32 import ch.boye.httpclientandroidlib.client.HttpClient;
michael@0 33 import ch.boye.httpclientandroidlib.client.protocol.RequestAddCookies;
michael@0 34 import ch.boye.httpclientandroidlib.client.protocol.RequestAuthCache;
michael@0 35 import ch.boye.httpclientandroidlib.client.protocol.RequestClientConnControl;
michael@0 36 import ch.boye.httpclientandroidlib.client.protocol.RequestDefaultHeaders;
michael@0 37 import ch.boye.httpclientandroidlib.client.protocol.RequestProxyAuthentication;
michael@0 38 import ch.boye.httpclientandroidlib.client.protocol.RequestTargetAuthentication;
michael@0 39 import ch.boye.httpclientandroidlib.client.protocol.ResponseAuthCache;
michael@0 40 import ch.boye.httpclientandroidlib.client.protocol.ResponseProcessCookies;
michael@0 41 import ch.boye.httpclientandroidlib.conn.ClientConnectionManager;
michael@0 42 import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
michael@0 43 import ch.boye.httpclientandroidlib.params.CoreProtocolPNames;
michael@0 44 import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
michael@0 45 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 46 import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
michael@0 47 import ch.boye.httpclientandroidlib.params.SyncBasicHttpParams;
michael@0 48 import ch.boye.httpclientandroidlib.protocol.BasicHttpProcessor;
michael@0 49 import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0 50 import ch.boye.httpclientandroidlib.protocol.RequestContent;
michael@0 51 import ch.boye.httpclientandroidlib.protocol.RequestExpectContinue;
michael@0 52 import ch.boye.httpclientandroidlib.protocol.RequestTargetHost;
michael@0 53 import ch.boye.httpclientandroidlib.protocol.RequestUserAgent;
michael@0 54 import ch.boye.httpclientandroidlib.util.VersionInfo;
michael@0 55
michael@0 56 /**
michael@0 57 * Default implementation of {@link HttpClient} pre-configured for most common use scenarios.
michael@0 58 * <p>
michael@0 59 * This class creates the following chain of protocol interceptors per default:
michael@0 60 * <ul>
michael@0 61 * <li>{@link RequestDefaultHeaders}</li>
michael@0 62 * <li>{@link RequestContent}</li>
michael@0 63 * <li>{@link RequestTargetHost}</li>
michael@0 64 * <li>{@link RequestClientConnControl}</li>
michael@0 65 * <li>{@link RequestUserAgent}</li>
michael@0 66 * <li>{@link RequestExpectContinue}</li>
michael@0 67 * <li>{@link RequestAddCookies}</li>
michael@0 68 * <li>{@link ResponseProcessCookies}</li>
michael@0 69 * <li>{@link RequestTargetAuthentication}</li>
michael@0 70 * <li>{@link RequestProxyAuthentication}</li>
michael@0 71 * </ul>
michael@0 72 * <p>
michael@0 73 * This class sets up the following parameters if not explicitly set:
michael@0 74 * <ul>
michael@0 75 * <li>Version: HttpVersion.HTTP_1_1</li>
michael@0 76 * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET</li>
michael@0 77 * <li>NoTcpDelay: true</li>
michael@0 78 * <li>SocketBufferSize: 8192</li>
michael@0 79 * <li>UserAgent: Apache-HttpClient/release (java 1.5)</li>
michael@0 80 * </ul>
michael@0 81 * <p>
michael@0 82 * The following parameters can be used to customize the behavior of this
michael@0 83 * class:
michael@0 84 * <ul>
michael@0 85 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
michael@0 86 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
michael@0 87 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
michael@0 88 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li>
michael@0 89 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li>
michael@0 90 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USER_AGENT}</li>
michael@0 91 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#TCP_NODELAY}</li>
michael@0 92 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_TIMEOUT}</li>
michael@0 93 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_LINGER}</li>
michael@0 94 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li>
michael@0 95 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
michael@0 96 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
michael@0 97 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
michael@0 98 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
michael@0 99 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
michael@0 100 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
michael@0 101 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
michael@0 102 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
michael@0 103 * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
michael@0 104 * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
michael@0 105 * <li>{@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
michael@0 106 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}</li>
michael@0 107 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
michael@0 108 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
michael@0 109 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#MAX_REDIRECTS}</li>
michael@0 110 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
michael@0 111 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#VIRTUAL_HOST}</li>
michael@0 112 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HOST}</li>
michael@0 113 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HEADERS}</li>
michael@0 114 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#CONNECTION_MANAGER_FACTORY_CLASS_NAME}</li>
michael@0 115 * </ul>
michael@0 116 *
michael@0 117 * @since 4.0
michael@0 118 */
michael@0 119 @ThreadSafe
michael@0 120 public class DefaultHttpClient extends AbstractHttpClient {
michael@0 121
michael@0 122 /**
michael@0 123 * Creates a new HTTP client from parameters and a connection manager.
michael@0 124 *
michael@0 125 * @param params the parameters
michael@0 126 * @param conman the connection manager
michael@0 127 */
michael@0 128 public DefaultHttpClient(
michael@0 129 final ClientConnectionManager conman,
michael@0 130 final HttpParams params) {
michael@0 131 super(conman, params);
michael@0 132 }
michael@0 133
michael@0 134
michael@0 135 /**
michael@0 136 * @since 4.1
michael@0 137 */
michael@0 138 public DefaultHttpClient(
michael@0 139 final ClientConnectionManager conman) {
michael@0 140 super(conman, null);
michael@0 141 }
michael@0 142
michael@0 143
michael@0 144 public DefaultHttpClient(final HttpParams params) {
michael@0 145 super(null, params);
michael@0 146 }
michael@0 147
michael@0 148
michael@0 149 public DefaultHttpClient() {
michael@0 150 super(null, null);
michael@0 151 }
michael@0 152
michael@0 153
michael@0 154 /**
michael@0 155 * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)}
michael@0 156 *
michael@0 157 * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it.
michael@0 158 */
michael@0 159 @Override
michael@0 160 protected HttpParams createHttpParams() {
michael@0 161 HttpParams params = new SyncBasicHttpParams();
michael@0 162 setDefaultHttpParams(params);
michael@0 163 return params;
michael@0 164 }
michael@0 165
michael@0 166 /**
michael@0 167 * Saves the default set of HttpParams in the provided parameter.
michael@0 168 * These are:
michael@0 169 * <ul>
michael@0 170 * <li>{@link CoreProtocolPNames#PROTOCOL_VERSION}: 1.1</li>
michael@0 171 * <li>{@link CoreProtocolPNames#HTTP_CONTENT_CHARSET}: ISO-8859-1</li>
michael@0 172 * <li>{@link CoreConnectionPNames#TCP_NODELAY}: true</li>
michael@0 173 * <li>{@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 8192</li>
michael@0 174 * <li>{@link CoreProtocolPNames#USER_AGENT}: Apache-HttpClient/<release> (java 1.5)</li>
michael@0 175 * </ul>
michael@0 176 */
michael@0 177 public static void setDefaultHttpParams(HttpParams params) {
michael@0 178 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
michael@0 179 HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
michael@0 180 HttpConnectionParams.setTcpNoDelay(params, true);
michael@0 181 HttpConnectionParams.setSocketBufferSize(params, 8192);
michael@0 182
michael@0 183 // determine the release version from packaged version info
michael@0 184 final VersionInfo vi = VersionInfo.loadVersionInfo
michael@0 185 ("ch.boye.httpclientandroidlib.client", DefaultHttpClient.class.getClassLoader());
michael@0 186 final String release = (vi != null) ?
michael@0 187 vi.getRelease() : VersionInfo.UNAVAILABLE;
michael@0 188 HttpProtocolParams.setUserAgent(params,
michael@0 189 "Apache-HttpClient/" + release + " (java 1.5)");
michael@0 190 }
michael@0 191
michael@0 192
michael@0 193 @Override
michael@0 194 protected BasicHttpProcessor createHttpProcessor() {
michael@0 195 BasicHttpProcessor httpproc = new BasicHttpProcessor();
michael@0 196 httpproc.addInterceptor(new RequestDefaultHeaders());
michael@0 197 // Required protocol interceptors
michael@0 198 httpproc.addInterceptor(new RequestContent());
michael@0 199 httpproc.addInterceptor(new RequestTargetHost());
michael@0 200 // Recommended protocol interceptors
michael@0 201 httpproc.addInterceptor(new RequestClientConnControl());
michael@0 202 httpproc.addInterceptor(new RequestUserAgent());
michael@0 203 httpproc.addInterceptor(new RequestExpectContinue());
michael@0 204 // HTTP state management interceptors
michael@0 205 httpproc.addInterceptor(new RequestAddCookies());
michael@0 206 httpproc.addInterceptor(new ResponseProcessCookies());
michael@0 207 // HTTP authentication interceptors
michael@0 208 httpproc.addInterceptor(new RequestAuthCache());
michael@0 209 httpproc.addInterceptor(new ResponseAuthCache());
michael@0 210 httpproc.addInterceptor(new RequestTargetAuthentication());
michael@0 211 httpproc.addInterceptor(new RequestProxyAuthentication());
michael@0 212 return httpproc;
michael@0 213 }
michael@0 214
michael@0 215 }

mercurial