mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestAddCookies.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.client.protocol;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.net.URI;
michael@0 32 import java.net.URISyntaxException;
michael@0 33 import java.util.ArrayList;
michael@0 34 import java.util.Date;
michael@0 35 import java.util.List;
michael@0 36
michael@0 37 import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 38
michael@0 39 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
michael@0 40 /* LogFactory removed by HttpClient for Android script. */
michael@0 41 import ch.boye.httpclientandroidlib.Header;
michael@0 42 import ch.boye.httpclientandroidlib.HttpException;
michael@0 43 import ch.boye.httpclientandroidlib.HttpHost;
michael@0 44 import ch.boye.httpclientandroidlib.HttpRequest;
michael@0 45 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
michael@0 46 import ch.boye.httpclientandroidlib.ProtocolException;
michael@0 47 import ch.boye.httpclientandroidlib.client.CookieStore;
michael@0 48 import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest;
michael@0 49 import ch.boye.httpclientandroidlib.client.params.HttpClientParams;
michael@0 50 import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection;
michael@0 51 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
michael@0 52 import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0 53 import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0 54 import ch.boye.httpclientandroidlib.cookie.CookieSpec;
michael@0 55 import ch.boye.httpclientandroidlib.cookie.CookieSpecRegistry;
michael@0 56 import ch.boye.httpclientandroidlib.cookie.SetCookie2;
michael@0 57 import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0 58 import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
michael@0 59
michael@0 60 /**
michael@0 61 * Request interceptor that matches cookies available in the current
michael@0 62 * {@link CookieStore} to the request being executed and generates
michael@0 63 * corresponding <code>Cookie</code> request headers.
michael@0 64 * <p>
michael@0 65 * The following parameters can be used to customize the behavior of this
michael@0 66 * class:
michael@0 67 * <ul>
michael@0 68 * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
michael@0 69 * <li>{@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
michael@0 70 * <li>{@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}</li>
michael@0 71 * </ul>
michael@0 72 *
michael@0 73 * @since 4.0
michael@0 74 */
michael@0 75 @Immutable
michael@0 76 public class RequestAddCookies implements HttpRequestInterceptor {
michael@0 77
michael@0 78 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0 79
michael@0 80 public RequestAddCookies() {
michael@0 81 super();
michael@0 82 }
michael@0 83
michael@0 84 public void process(final HttpRequest request, final HttpContext context)
michael@0 85 throws HttpException, IOException {
michael@0 86 if (request == null) {
michael@0 87 throw new IllegalArgumentException("HTTP request may not be null");
michael@0 88 }
michael@0 89 if (context == null) {
michael@0 90 throw new IllegalArgumentException("HTTP context may not be null");
michael@0 91 }
michael@0 92
michael@0 93 String method = request.getRequestLine().getMethod();
michael@0 94 if (method.equalsIgnoreCase("CONNECT")) {
michael@0 95 return;
michael@0 96 }
michael@0 97
michael@0 98 // Obtain cookie store
michael@0 99 CookieStore cookieStore = (CookieStore) context.getAttribute(
michael@0 100 ClientContext.COOKIE_STORE);
michael@0 101 if (cookieStore == null) {
michael@0 102 this.log.debug("Cookie store not specified in HTTP context");
michael@0 103 return;
michael@0 104 }
michael@0 105
michael@0 106 // Obtain the registry of cookie specs
michael@0 107 CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute(
michael@0 108 ClientContext.COOKIESPEC_REGISTRY);
michael@0 109 if (registry == null) {
michael@0 110 this.log.debug("CookieSpec registry not specified in HTTP context");
michael@0 111 return;
michael@0 112 }
michael@0 113
michael@0 114 // Obtain the target host (required)
michael@0 115 HttpHost targetHost = (HttpHost) context.getAttribute(
michael@0 116 ExecutionContext.HTTP_TARGET_HOST);
michael@0 117 if (targetHost == null) {
michael@0 118 this.log.debug("Target host not set in the context");
michael@0 119 return;
michael@0 120 }
michael@0 121
michael@0 122 // Obtain the client connection (required)
michael@0 123 HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
michael@0 124 ExecutionContext.HTTP_CONNECTION);
michael@0 125 if (conn == null) {
michael@0 126 this.log.debug("HTTP connection not set in the context");
michael@0 127 return;
michael@0 128 }
michael@0 129
michael@0 130 String policy = HttpClientParams.getCookiePolicy(request.getParams());
michael@0 131 if (this.log.isDebugEnabled()) {
michael@0 132 this.log.debug("CookieSpec selected: " + policy);
michael@0 133 }
michael@0 134
michael@0 135 URI requestURI;
michael@0 136 if (request instanceof HttpUriRequest) {
michael@0 137 requestURI = ((HttpUriRequest) request).getURI();
michael@0 138 } else {
michael@0 139 try {
michael@0 140 requestURI = new URI(request.getRequestLine().getUri());
michael@0 141 } catch (URISyntaxException ex) {
michael@0 142 throw new ProtocolException("Invalid request URI: " +
michael@0 143 request.getRequestLine().getUri(), ex);
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 String hostName = targetHost.getHostName();
michael@0 148 int port = targetHost.getPort();
michael@0 149 if (port < 0) {
michael@0 150 HttpRoute route = conn.getRoute();
michael@0 151 if (route.getHopCount() == 1) {
michael@0 152 port = conn.getRemotePort();
michael@0 153 } else {
michael@0 154 // Target port will be selected by the proxy.
michael@0 155 // Use conventional ports for known schemes
michael@0 156 String scheme = targetHost.getSchemeName();
michael@0 157 if (scheme.equalsIgnoreCase("http")) {
michael@0 158 port = 80;
michael@0 159 } else if (scheme.equalsIgnoreCase("https")) {
michael@0 160 port = 443;
michael@0 161 } else {
michael@0 162 port = 0;
michael@0 163 }
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 CookieOrigin cookieOrigin = new CookieOrigin(
michael@0 168 hostName,
michael@0 169 port,
michael@0 170 requestURI.getPath(),
michael@0 171 conn.isSecure());
michael@0 172
michael@0 173 // Get an instance of the selected cookie policy
michael@0 174 CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
michael@0 175 // Get all cookies available in the HTTP state
michael@0 176 List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies());
michael@0 177 // Find cookies matching the given origin
michael@0 178 List<Cookie> matchedCookies = new ArrayList<Cookie>();
michael@0 179 Date now = new Date();
michael@0 180 for (Cookie cookie : cookies) {
michael@0 181 if (!cookie.isExpired(now)) {
michael@0 182 if (cookieSpec.match(cookie, cookieOrigin)) {
michael@0 183 if (this.log.isDebugEnabled()) {
michael@0 184 this.log.debug("Cookie " + cookie + " match " + cookieOrigin);
michael@0 185 }
michael@0 186 matchedCookies.add(cookie);
michael@0 187 }
michael@0 188 } else {
michael@0 189 if (this.log.isDebugEnabled()) {
michael@0 190 this.log.debug("Cookie " + cookie + " expired");
michael@0 191 }
michael@0 192 }
michael@0 193 }
michael@0 194 // Generate Cookie request headers
michael@0 195 if (!matchedCookies.isEmpty()) {
michael@0 196 List<Header> headers = cookieSpec.formatCookies(matchedCookies);
michael@0 197 for (Header header : headers) {
michael@0 198 request.addHeader(header);
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 int ver = cookieSpec.getVersion();
michael@0 203 if (ver > 0) {
michael@0 204 boolean needVersionHeader = false;
michael@0 205 for (Cookie cookie : matchedCookies) {
michael@0 206 if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
michael@0 207 needVersionHeader = true;
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 if (needVersionHeader) {
michael@0 212 Header header = cookieSpec.getVersionHeader();
michael@0 213 if (header != null) {
michael@0 214 // Advertise cookie version support
michael@0 215 request.addHeader(header);
michael@0 216 }
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 // Stick the CookieSpec and CookieOrigin instances to the HTTP context
michael@0 221 // so they could be obtained by the response interceptor
michael@0 222 context.setAttribute(ClientContext.COOKIE_SPEC, cookieSpec);
michael@0 223 context.setAttribute(ClientContext.COOKIE_ORIGIN, cookieOrigin);
michael@0 224 }
michael@0 225
michael@0 226 }

mercurial