1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/DefaultConnectionReuseStrategy.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 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; 1.32 + 1.33 +import ch.boye.httpclientandroidlib.ConnectionReuseStrategy; 1.34 +import ch.boye.httpclientandroidlib.HttpConnection; 1.35 +import ch.boye.httpclientandroidlib.HeaderIterator; 1.36 +import ch.boye.httpclientandroidlib.HttpEntity; 1.37 +import ch.boye.httpclientandroidlib.HttpResponse; 1.38 +import ch.boye.httpclientandroidlib.HttpVersion; 1.39 +import ch.boye.httpclientandroidlib.ParseException; 1.40 +import ch.boye.httpclientandroidlib.ProtocolVersion; 1.41 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.42 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.43 +import ch.boye.httpclientandroidlib.protocol.ExecutionContext; 1.44 +import ch.boye.httpclientandroidlib.TokenIterator; 1.45 +import ch.boye.httpclientandroidlib.message.BasicTokenIterator; 1.46 + 1.47 +/** 1.48 + * Default implementation of a strategy deciding about connection re-use. 1.49 + * The default implementation first checks some basics, for example 1.50 + * whether the connection is still open or whether the end of the 1.51 + * request entity can be determined without closing the connection. 1.52 + * If these checks pass, the tokens in the <code>Connection</code> header will 1.53 + * be examined. In the absence of a <code>Connection</code> header, the 1.54 + * non-standard but commonly used <code>Proxy-Connection</code> header takes 1.55 + * it's role. A token <code>close</code> indicates that the connection cannot 1.56 + * be reused. If there is no such token, a token <code>keep-alive</code> 1.57 + * indicates that the connection should be re-used. If neither token is found, 1.58 + * or if there are no <code>Connection</code> headers, the default policy for 1.59 + * the HTTP version is applied. Since <code>HTTP/1.1</code>, connections are 1.60 + * re-used by default. Up until <code>HTTP/1.0</code>, connections are not 1.61 + * re-used by default. 1.62 + * 1.63 + * @since 4.0 1.64 + */ 1.65 +public class DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { 1.66 + 1.67 + public DefaultConnectionReuseStrategy() { 1.68 + super(); 1.69 + } 1.70 + 1.71 + // see interface ConnectionReuseStrategy 1.72 + public boolean keepAlive(final HttpResponse response, 1.73 + final HttpContext context) { 1.74 + if (response == null) { 1.75 + throw new IllegalArgumentException 1.76 + ("HTTP response may not be null."); 1.77 + } 1.78 + if (context == null) { 1.79 + throw new IllegalArgumentException 1.80 + ("HTTP context may not be null."); 1.81 + } 1.82 + 1.83 + HttpConnection conn = (HttpConnection) 1.84 + context.getAttribute(ExecutionContext.HTTP_CONNECTION); 1.85 + 1.86 + if (conn != null && !conn.isOpen()) 1.87 + return false; 1.88 + // do NOT check for stale connection, that is an expensive operation 1.89 + 1.90 + // Check for a self-terminating entity. If the end of the entity will 1.91 + // be indicated by closing the connection, there is no keep-alive. 1.92 + HttpEntity entity = response.getEntity(); 1.93 + ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); 1.94 + if (entity != null) { 1.95 + if (entity.getContentLength() < 0) { 1.96 + if (!entity.isChunked() || 1.97 + ver.lessEquals(HttpVersion.HTTP_1_0)) { 1.98 + // if the content length is not known and is not chunk 1.99 + // encoded, the connection cannot be reused 1.100 + return false; 1.101 + } 1.102 + } 1.103 + } 1.104 + 1.105 + // Check for the "Connection" header. If that is absent, check for 1.106 + // the "Proxy-Connection" header. The latter is an unspecified and 1.107 + // broken but unfortunately common extension of HTTP. 1.108 + HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE); 1.109 + if (!hit.hasNext()) 1.110 + hit = response.headerIterator("Proxy-Connection"); 1.111 + 1.112 + // Experimental usage of the "Connection" header in HTTP/1.0 is 1.113 + // documented in RFC 2068, section 19.7.1. A token "keep-alive" is 1.114 + // used to indicate that the connection should be persistent. 1.115 + // Note that the final specification of HTTP/1.1 in RFC 2616 does not 1.116 + // include this information. Neither is the "Connection" header 1.117 + // mentioned in RFC 1945, which informally describes HTTP/1.0. 1.118 + // 1.119 + // RFC 2616 specifies "close" as the only connection token with a 1.120 + // specific meaning: it disables persistent connections. 1.121 + // 1.122 + // The "Proxy-Connection" header is not formally specified anywhere, 1.123 + // but is commonly used to carry one token, "close" or "keep-alive". 1.124 + // The "Connection" header, on the other hand, is defined as a 1.125 + // sequence of tokens, where each token is a header name, and the 1.126 + // token "close" has the above-mentioned additional meaning. 1.127 + // 1.128 + // To get through this mess, we treat the "Proxy-Connection" header 1.129 + // in exactly the same way as the "Connection" header, but only if 1.130 + // the latter is missing. We scan the sequence of tokens for both 1.131 + // "close" and "keep-alive". As "close" is specified by RFC 2068, 1.132 + // it takes precedence and indicates a non-persistent connection. 1.133 + // If there is no "close" but a "keep-alive", we take the hint. 1.134 + 1.135 + if (hit.hasNext()) { 1.136 + try { 1.137 + TokenIterator ti = createTokenIterator(hit); 1.138 + boolean keepalive = false; 1.139 + while (ti.hasNext()) { 1.140 + final String token = ti.nextToken(); 1.141 + if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) { 1.142 + return false; 1.143 + } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) { 1.144 + // continue the loop, there may be a "close" afterwards 1.145 + keepalive = true; 1.146 + } 1.147 + } 1.148 + if (keepalive) 1.149 + return true; 1.150 + // neither "close" nor "keep-alive", use default policy 1.151 + 1.152 + } catch (ParseException px) { 1.153 + // invalid connection header means no persistent connection 1.154 + // we don't have logging in HttpCore, so the exception is lost 1.155 + return false; 1.156 + } 1.157 + } 1.158 + 1.159 + // default since HTTP/1.1 is persistent, before it was non-persistent 1.160 + return !ver.lessEquals(HttpVersion.HTTP_1_0); 1.161 + } 1.162 + 1.163 + 1.164 + /** 1.165 + * Creates a token iterator from a header iterator. 1.166 + * This method can be overridden to replace the implementation of 1.167 + * the token iterator. 1.168 + * 1.169 + * @param hit the header iterator 1.170 + * 1.171 + * @return the token iterator 1.172 + */ 1.173 + protected TokenIterator createTokenIterator(HeaderIterator hit) { 1.174 + return new BasicTokenIterator(hit); 1.175 + } 1.176 +}