1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestAddCookies.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 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.client.protocol; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.net.URI; 1.35 +import java.net.URISyntaxException; 1.36 +import java.util.ArrayList; 1.37 +import java.util.Date; 1.38 +import java.util.List; 1.39 + 1.40 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.41 + 1.42 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; 1.43 +/* LogFactory removed by HttpClient for Android script. */ 1.44 +import ch.boye.httpclientandroidlib.Header; 1.45 +import ch.boye.httpclientandroidlib.HttpException; 1.46 +import ch.boye.httpclientandroidlib.HttpHost; 1.47 +import ch.boye.httpclientandroidlib.HttpRequest; 1.48 +import ch.boye.httpclientandroidlib.HttpRequestInterceptor; 1.49 +import ch.boye.httpclientandroidlib.ProtocolException; 1.50 +import ch.boye.httpclientandroidlib.client.CookieStore; 1.51 +import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; 1.52 +import ch.boye.httpclientandroidlib.client.params.HttpClientParams; 1.53 +import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection; 1.54 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; 1.55 +import ch.boye.httpclientandroidlib.cookie.Cookie; 1.56 +import ch.boye.httpclientandroidlib.cookie.CookieOrigin; 1.57 +import ch.boye.httpclientandroidlib.cookie.CookieSpec; 1.58 +import ch.boye.httpclientandroidlib.cookie.CookieSpecRegistry; 1.59 +import ch.boye.httpclientandroidlib.cookie.SetCookie2; 1.60 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.61 +import ch.boye.httpclientandroidlib.protocol.ExecutionContext; 1.62 + 1.63 +/** 1.64 + * Request interceptor that matches cookies available in the current 1.65 + * {@link CookieStore} to the request being executed and generates 1.66 + * corresponding <code>Cookie</code> request headers. 1.67 + * <p> 1.68 + * The following parameters can be used to customize the behavior of this 1.69 + * class: 1.70 + * <ul> 1.71 + * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li> 1.72 + * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li> 1.73 + * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}</li> 1.74 + * </ul> 1.75 + * 1.76 + * @since 4.0 1.77 + */ 1.78 +@Immutable 1.79 +public class RequestAddCookies implements HttpRequestInterceptor { 1.80 + 1.81 + public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); 1.82 + 1.83 + public RequestAddCookies() { 1.84 + super(); 1.85 + } 1.86 + 1.87 + public void process(final HttpRequest request, final HttpContext context) 1.88 + throws HttpException, IOException { 1.89 + if (request == null) { 1.90 + throw new IllegalArgumentException("HTTP request may not be null"); 1.91 + } 1.92 + if (context == null) { 1.93 + throw new IllegalArgumentException("HTTP context may not be null"); 1.94 + } 1.95 + 1.96 + String method = request.getRequestLine().getMethod(); 1.97 + if (method.equalsIgnoreCase("CONNECT")) { 1.98 + return; 1.99 + } 1.100 + 1.101 + // Obtain cookie store 1.102 + CookieStore cookieStore = (CookieStore) context.getAttribute( 1.103 + ClientContext.COOKIE_STORE); 1.104 + if (cookieStore == null) { 1.105 + this.log.debug("Cookie store not specified in HTTP context"); 1.106 + return; 1.107 + } 1.108 + 1.109 + // Obtain the registry of cookie specs 1.110 + CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute( 1.111 + ClientContext.COOKIESPEC_REGISTRY); 1.112 + if (registry == null) { 1.113 + this.log.debug("CookieSpec registry not specified in HTTP context"); 1.114 + return; 1.115 + } 1.116 + 1.117 + // Obtain the target host (required) 1.118 + HttpHost targetHost = (HttpHost) context.getAttribute( 1.119 + ExecutionContext.HTTP_TARGET_HOST); 1.120 + if (targetHost == null) { 1.121 + this.log.debug("Target host not set in the context"); 1.122 + return; 1.123 + } 1.124 + 1.125 + // Obtain the client connection (required) 1.126 + HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute( 1.127 + ExecutionContext.HTTP_CONNECTION); 1.128 + if (conn == null) { 1.129 + this.log.debug("HTTP connection not set in the context"); 1.130 + return; 1.131 + } 1.132 + 1.133 + String policy = HttpClientParams.getCookiePolicy(request.getParams()); 1.134 + if (this.log.isDebugEnabled()) { 1.135 + this.log.debug("CookieSpec selected: " + policy); 1.136 + } 1.137 + 1.138 + URI requestURI; 1.139 + if (request instanceof HttpUriRequest) { 1.140 + requestURI = ((HttpUriRequest) request).getURI(); 1.141 + } else { 1.142 + try { 1.143 + requestURI = new URI(request.getRequestLine().getUri()); 1.144 + } catch (URISyntaxException ex) { 1.145 + throw new ProtocolException("Invalid request URI: " + 1.146 + request.getRequestLine().getUri(), ex); 1.147 + } 1.148 + } 1.149 + 1.150 + String hostName = targetHost.getHostName(); 1.151 + int port = targetHost.getPort(); 1.152 + if (port < 0) { 1.153 + HttpRoute route = conn.getRoute(); 1.154 + if (route.getHopCount() == 1) { 1.155 + port = conn.getRemotePort(); 1.156 + } else { 1.157 + // Target port will be selected by the proxy. 1.158 + // Use conventional ports for known schemes 1.159 + String scheme = targetHost.getSchemeName(); 1.160 + if (scheme.equalsIgnoreCase("http")) { 1.161 + port = 80; 1.162 + } else if (scheme.equalsIgnoreCase("https")) { 1.163 + port = 443; 1.164 + } else { 1.165 + port = 0; 1.166 + } 1.167 + } 1.168 + } 1.169 + 1.170 + CookieOrigin cookieOrigin = new CookieOrigin( 1.171 + hostName, 1.172 + port, 1.173 + requestURI.getPath(), 1.174 + conn.isSecure()); 1.175 + 1.176 + // Get an instance of the selected cookie policy 1.177 + CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams()); 1.178 + // Get all cookies available in the HTTP state 1.179 + List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies()); 1.180 + // Find cookies matching the given origin 1.181 + List<Cookie> matchedCookies = new ArrayList<Cookie>(); 1.182 + Date now = new Date(); 1.183 + for (Cookie cookie : cookies) { 1.184 + if (!cookie.isExpired(now)) { 1.185 + if (cookieSpec.match(cookie, cookieOrigin)) { 1.186 + if (this.log.isDebugEnabled()) { 1.187 + this.log.debug("Cookie " + cookie + " match " + cookieOrigin); 1.188 + } 1.189 + matchedCookies.add(cookie); 1.190 + } 1.191 + } else { 1.192 + if (this.log.isDebugEnabled()) { 1.193 + this.log.debug("Cookie " + cookie + " expired"); 1.194 + } 1.195 + } 1.196 + } 1.197 + // Generate Cookie request headers 1.198 + if (!matchedCookies.isEmpty()) { 1.199 + List<Header> headers = cookieSpec.formatCookies(matchedCookies); 1.200 + for (Header header : headers) { 1.201 + request.addHeader(header); 1.202 + } 1.203 + } 1.204 + 1.205 + int ver = cookieSpec.getVersion(); 1.206 + if (ver > 0) { 1.207 + boolean needVersionHeader = false; 1.208 + for (Cookie cookie : matchedCookies) { 1.209 + if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) { 1.210 + needVersionHeader = true; 1.211 + } 1.212 + } 1.213 + 1.214 + if (needVersionHeader) { 1.215 + Header header = cookieSpec.getVersionHeader(); 1.216 + if (header != null) { 1.217 + // Advertise cookie version support 1.218 + request.addHeader(header); 1.219 + } 1.220 + } 1.221 + } 1.222 + 1.223 + // Stick the CookieSpec and CookieOrigin instances to the HTTP context 1.224 + // so they could be obtained by the response interceptor 1.225 + context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec); 1.226 + context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin); 1.227 + } 1.228 + 1.229 +}