1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/DefaultHttpClient.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.client; 1.32 + 1.33 +import ch.boye.httpclientandroidlib.HttpVersion; 1.34 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe; 1.35 +import ch.boye.httpclientandroidlib.client.HttpClient; 1.36 +import ch.boye.httpclientandroidlib.client.protocol.RequestAddCookies; 1.37 +import ch.boye.httpclientandroidlib.client.protocol.RequestAuthCache; 1.38 +import ch.boye.httpclientandroidlib.client.protocol.RequestClientConnControl; 1.39 +import ch.boye.httpclientandroidlib.client.protocol.RequestDefaultHeaders; 1.40 +import ch.boye.httpclientandroidlib.client.protocol.RequestProxyAuthentication; 1.41 +import ch.boye.httpclientandroidlib.client.protocol.RequestTargetAuthentication; 1.42 +import ch.boye.httpclientandroidlib.client.protocol.ResponseAuthCache; 1.43 +import ch.boye.httpclientandroidlib.client.protocol.ResponseProcessCookies; 1.44 +import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; 1.45 +import ch.boye.httpclientandroidlib.params.CoreConnectionPNames; 1.46 +import ch.boye.httpclientandroidlib.params.CoreProtocolPNames; 1.47 +import ch.boye.httpclientandroidlib.params.HttpConnectionParams; 1.48 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.49 +import ch.boye.httpclientandroidlib.params.HttpProtocolParams; 1.50 +import ch.boye.httpclientandroidlib.params.SyncBasicHttpParams; 1.51 +import ch.boye.httpclientandroidlib.protocol.BasicHttpProcessor; 1.52 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.53 +import ch.boye.httpclientandroidlib.protocol.RequestContent; 1.54 +import ch.boye.httpclientandroidlib.protocol.RequestExpectContinue; 1.55 +import ch.boye.httpclientandroidlib.protocol.RequestTargetHost; 1.56 +import ch.boye.httpclientandroidlib.protocol.RequestUserAgent; 1.57 +import ch.boye.httpclientandroidlib.util.VersionInfo; 1.58 + 1.59 +/** 1.60 + * Default implementation of {@link HttpClient} pre-configured for most common use scenarios. 1.61 + * <p> 1.62 + * This class creates the following chain of protocol interceptors per default: 1.63 + * <ul> 1.64 + * <li>{@link RequestDefaultHeaders}</li> 1.65 + * <li>{@link RequestContent}</li> 1.66 + * <li>{@link RequestTargetHost}</li> 1.67 + * <li>{@link RequestClientConnControl}</li> 1.68 + * <li>{@link RequestUserAgent}</li> 1.69 + * <li>{@link RequestExpectContinue}</li> 1.70 + * <li>{@link RequestAddCookies}</li> 1.71 + * <li>{@link ResponseProcessCookies}</li> 1.72 + * <li>{@link RequestTargetAuthentication}</li> 1.73 + * <li>{@link RequestProxyAuthentication}</li> 1.74 + * </ul> 1.75 + * <p> 1.76 + * This class sets up the following parameters if not explicitly set: 1.77 + * <ul> 1.78 + * <li>Version: HttpVersion.HTTP_1_1</li> 1.79 + * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET</li> 1.80 + * <li>NoTcpDelay: true</li> 1.81 + * <li>SocketBufferSize: 8192</li> 1.82 + * <li>UserAgent: Apache-HttpClient/release (java 1.5)</li> 1.83 + * </ul> 1.84 + * <p> 1.85 + * The following parameters can be used to customize the behavior of this 1.86 + * class: 1.87 + * <ul> 1.88 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#PROTOCOL_VERSION}</li> 1.89 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> 1.90 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> 1.91 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li> 1.92 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li> 1.93 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USER_AGENT}</li> 1.94 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#TCP_NODELAY}</li> 1.95 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_TIMEOUT}</li> 1.96 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_LINGER}</li> 1.97 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li> 1.98 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> 1.99 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li> 1.100 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> 1.101 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li> 1.102 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li> 1.103 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> 1.104 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> 1.105 + * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li> 1.106 + * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li> 1.107 + * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li> 1.108 + * <li>{@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li> 1.109 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}</li> 1.110 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li> 1.111 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_REDIRECTS}</li> 1.112 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#MAX_REDIRECTS}</li> 1.113 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li> 1.114 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#VIRTUAL_HOST}</li> 1.115 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HOST}</li> 1.116 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HEADERS}</li> 1.117 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#CONNECTION_MANAGER_FACTORY_CLASS_NAME}</li> 1.118 + * </ul> 1.119 + * 1.120 + * @since 4.0 1.121 + */ 1.122 +@ThreadSafe 1.123 +public class DefaultHttpClient extends AbstractHttpClient { 1.124 + 1.125 + /** 1.126 + * Creates a new HTTP client from parameters and a connection manager. 1.127 + * 1.128 + * @param params the parameters 1.129 + * @param conman the connection manager 1.130 + */ 1.131 + public DefaultHttpClient( 1.132 + final ClientConnectionManager conman, 1.133 + final HttpParams params) { 1.134 + super(conman, params); 1.135 + } 1.136 + 1.137 + 1.138 + /** 1.139 + * @since 4.1 1.140 + */ 1.141 + public DefaultHttpClient( 1.142 + final ClientConnectionManager conman) { 1.143 + super(conman, null); 1.144 + } 1.145 + 1.146 + 1.147 + public DefaultHttpClient(final HttpParams params) { 1.148 + super(null, params); 1.149 + } 1.150 + 1.151 + 1.152 + public DefaultHttpClient() { 1.153 + super(null, null); 1.154 + } 1.155 + 1.156 + 1.157 + /** 1.158 + * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)} 1.159 + * 1.160 + * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it. 1.161 + */ 1.162 + @Override 1.163 + protected HttpParams createHttpParams() { 1.164 + HttpParams params = new SyncBasicHttpParams(); 1.165 + setDefaultHttpParams(params); 1.166 + return params; 1.167 + } 1.168 + 1.169 + /** 1.170 + * Saves the default set of HttpParams in the provided parameter. 1.171 + * These are: 1.172 + * <ul> 1.173 + * <li>{@link CoreProtocolPNames#PROTOCOL_VERSION}: 1.1</li> 1.174 + * <li>{@link CoreProtocolPNames#HTTP_CONTENT_CHARSET}: ISO-8859-1</li> 1.175 + * <li>{@link CoreConnectionPNames#TCP_NODELAY}: true</li> 1.176 + * <li>{@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 8192</li> 1.177 + * <li>{@link CoreProtocolPNames#USER_AGENT}: Apache-HttpClient/<release> (java 1.5)</li> 1.178 + * </ul> 1.179 + */ 1.180 + public static void setDefaultHttpParams(HttpParams params) { 1.181 + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 1.182 + HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 1.183 + HttpConnectionParams.setTcpNoDelay(params, true); 1.184 + HttpConnectionParams.setSocketBufferSize(params, 8192); 1.185 + 1.186 + // determine the release version from packaged version info 1.187 + final VersionInfo vi = VersionInfo.loadVersionInfo 1.188 + ("ch.boye.httpclientandroidlib.client", DefaultHttpClient.class.getClassLoader()); 1.189 + final String release = (vi != null) ? 1.190 + vi.getRelease() : VersionInfo.UNAVAILABLE; 1.191 + HttpProtocolParams.setUserAgent(params, 1.192 + "Apache-HttpClient/" + release + " (java 1.5)"); 1.193 + } 1.194 + 1.195 + 1.196 + @Override 1.197 + protected BasicHttpProcessor createHttpProcessor() { 1.198 + BasicHttpProcessor httpproc = new BasicHttpProcessor(); 1.199 + httpproc.addInterceptor(new RequestDefaultHeaders()); 1.200 + // Required protocol interceptors 1.201 + httpproc.addInterceptor(new RequestContent()); 1.202 + httpproc.addInterceptor(new RequestTargetHost()); 1.203 + // Recommended protocol interceptors 1.204 + httpproc.addInterceptor(new RequestClientConnControl()); 1.205 + httpproc.addInterceptor(new RequestUserAgent()); 1.206 + httpproc.addInterceptor(new RequestExpectContinue()); 1.207 + // HTTP state management interceptors 1.208 + httpproc.addInterceptor(new RequestAddCookies()); 1.209 + httpproc.addInterceptor(new ResponseProcessCookies()); 1.210 + // HTTP authentication interceptors 1.211 + httpproc.addInterceptor(new RequestAuthCache()); 1.212 + httpproc.addInterceptor(new ResponseAuthCache()); 1.213 + httpproc.addInterceptor(new RequestTargetAuthentication()); 1.214 + httpproc.addInterceptor(new RequestProxyAuthentication()); 1.215 + return httpproc; 1.216 + } 1.217 + 1.218 +}