1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java $ 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 602537 $ 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2007-12-08 11:42:06 -0800 (Sat, 08 Dec 2007) $ 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements. See the NOTICE file 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership. The ASF licenses this file 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License. You may obtain a copy of the License at 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied. See the License for the 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License. 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation. For more 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>. 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl; 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.ConnectionReuseStrategy; 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpConnection; 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HeaderIterator; 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpEntity; 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpResponse; 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpVersion; 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.ParseException; 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.ProtocolVersion; 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HTTP; 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HttpContext; 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.ExecutionContext; 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.TokenIterator; 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.message.BasicTokenIterator; 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Default implementation of a strategy deciding about connection re-use. 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The default implementation first checks some basics, for example 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * whether the connection is still open or whether the end of the 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * request entity can be determined without closing the connection. 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * If these checks pass, the tokens in the "Connection" header will 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * be examined. In the absence of a "Connection" header, the 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * non-standard but commonly used "Proxy-Connection" header takes 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * it's role. A token "close" indicates that the connection cannot 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * be reused. If there is no such token, a token "keep-alive" indicates 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * that the connection should be re-used. If neither token is found, 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or if there are no "Connection" headers, the default policy for 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the HTTP version is applied. Since HTTP/1.1, connections are re-used 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * by default. Up until HTTP/1.0, connections are not re-used by default. 62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:rolandw at apache.org">Roland Weber</a> 65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 602537 $ 67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0 69d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * 70d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead. 71d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 72d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * for further details. 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 74d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class DefaultConnectionReuseStrategy 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project implements ConnectionReuseStrategy { 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public DefaultConnectionReuseStrategy() { 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super(); 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // see interface ConnectionReuseStrategy 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public boolean keepAlive(final HttpResponse response, 84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final HttpContext context) { 85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (response == null) { 86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException 87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project ("HTTP response may not be null."); 88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (context == null) { 90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException 91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project ("HTTP context may not be null."); 92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project HttpConnection conn = (HttpConnection) 95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project context.getAttribute(ExecutionContext.HTTP_CONNECTION); 96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (conn != null && !conn.isOpen()) 98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return false; 99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // do NOT check for stale connection, that is an expensive operation 100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // Check for a self-terminating entity. If the end of the entity will 102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // be indicated by closing the connection, there is no keep-alive. 103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project HttpEntity entity = response.getEntity(); 104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); 105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (entity != null) { 106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (entity.getContentLength() < 0) { 107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (!entity.isChunked() || 108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project ver.lessEquals(HttpVersion.HTTP_1_0)) { 109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // if the content length is not known and is not chunk 110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // encoded, the connection cannot be reused 111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return false; 112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // Check for the "Connection" header. If that is absent, check for 117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // the "Proxy-Connection" header. The latter is an unspecified and 118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // broken but unfortunately common extension of HTTP. 119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE); 120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (!hit.hasNext()) 121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project hit = response.headerIterator("Proxy-Connection"); 122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // Experimental usage of the "Connection" header in HTTP/1.0 is 124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // documented in RFC 2068, section 19.7.1. A token "keep-alive" is 125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // used to indicate that the connection should be persistent. 126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // Note that the final specification of HTTP/1.1 in RFC 2616 does not 127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // include this information. Neither is the "Connection" header 128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // mentioned in RFC 1945, which informally describes HTTP/1.0. 129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // 130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // RFC 2616 specifies "close" as the only connection token with a 131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // specific meaning: it disables persistent connections. 132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // 133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // The "Proxy-Connection" header is not formally specified anywhere, 134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // but is commonly used to carry one token, "close" or "keep-alive". 135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // The "Connection" header, on the other hand, is defined as a 136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // sequence of tokens, where each token is a header name, and the 137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // token "close" has the above-mentioned additional meaning. 138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // 139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // To get through this mess, we treat the "Proxy-Connection" header 140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // in exactly the same way as the "Connection" header, but only if 141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // the latter is missing. We scan the sequence of tokens for both 142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // "close" and "keep-alive". As "close" is specified by RFC 2068, 143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // it takes precedence and indicates a non-persistent connection. 144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // If there is no "close" but a "keep-alive", we take the hint. 145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (hit.hasNext()) { 147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project try { 148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project TokenIterator ti = createTokenIterator(hit); 149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project boolean keepalive = false; 150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project while (ti.hasNext()) { 151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final String token = ti.nextToken(); 152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) { 153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return false; 154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) { 155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // continue the loop, there may be a "close" afterwards 156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project keepalive = true; 157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (keepalive) 160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return true; 161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // neither "close" nor "keep-alive", use default policy 162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } catch (ParseException px) { 164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // invalid connection header means no persistent connection 165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // we don't have logging in HttpCore, so the exception is lost 166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return false; 167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // default since HTTP/1.1 is persistent, before it was non-persistent 171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return !ver.lessEquals(HttpVersion.HTTP_1_0); 172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Creates a token iterator from a header iterator. 177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This method can be overridden to replace the implementation of 178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the token iterator. 179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param hit the header iterator 181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the token iterator 183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project protected TokenIterator createTokenIterator(HeaderIterator hit) { 185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return new BasicTokenIterator(hit); 186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} 188