|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.impl; |
|
29 |
|
30 import ch.boye.httpclientandroidlib.ConnectionReuseStrategy; |
|
31 import ch.boye.httpclientandroidlib.HttpConnection; |
|
32 import ch.boye.httpclientandroidlib.HeaderIterator; |
|
33 import ch.boye.httpclientandroidlib.HttpEntity; |
|
34 import ch.boye.httpclientandroidlib.HttpResponse; |
|
35 import ch.boye.httpclientandroidlib.HttpVersion; |
|
36 import ch.boye.httpclientandroidlib.ParseException; |
|
37 import ch.boye.httpclientandroidlib.ProtocolVersion; |
|
38 import ch.boye.httpclientandroidlib.protocol.HTTP; |
|
39 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
40 import ch.boye.httpclientandroidlib.protocol.ExecutionContext; |
|
41 import ch.boye.httpclientandroidlib.TokenIterator; |
|
42 import ch.boye.httpclientandroidlib.message.BasicTokenIterator; |
|
43 |
|
44 /** |
|
45 * Default implementation of a strategy deciding about connection re-use. |
|
46 * The default implementation first checks some basics, for example |
|
47 * whether the connection is still open or whether the end of the |
|
48 * request entity can be determined without closing the connection. |
|
49 * If these checks pass, the tokens in the <code>Connection</code> header will |
|
50 * be examined. In the absence of a <code>Connection</code> header, the |
|
51 * non-standard but commonly used <code>Proxy-Connection</code> header takes |
|
52 * it's role. A token <code>close</code> indicates that the connection cannot |
|
53 * be reused. If there is no such token, a token <code>keep-alive</code> |
|
54 * indicates that the connection should be re-used. If neither token is found, |
|
55 * or if there are no <code>Connection</code> headers, the default policy for |
|
56 * the HTTP version is applied. Since <code>HTTP/1.1</code>, connections are |
|
57 * re-used by default. Up until <code>HTTP/1.0</code>, connections are not |
|
58 * re-used by default. |
|
59 * |
|
60 * @since 4.0 |
|
61 */ |
|
62 public class DefaultConnectionReuseStrategy implements ConnectionReuseStrategy { |
|
63 |
|
64 public DefaultConnectionReuseStrategy() { |
|
65 super(); |
|
66 } |
|
67 |
|
68 // see interface ConnectionReuseStrategy |
|
69 public boolean keepAlive(final HttpResponse response, |
|
70 final HttpContext context) { |
|
71 if (response == null) { |
|
72 throw new IllegalArgumentException |
|
73 ("HTTP response may not be null."); |
|
74 } |
|
75 if (context == null) { |
|
76 throw new IllegalArgumentException |
|
77 ("HTTP context may not be null."); |
|
78 } |
|
79 |
|
80 HttpConnection conn = (HttpConnection) |
|
81 context.getAttribute(ExecutionContext.HTTP_CONNECTION); |
|
82 |
|
83 if (conn != null && !conn.isOpen()) |
|
84 return false; |
|
85 // do NOT check for stale connection, that is an expensive operation |
|
86 |
|
87 // Check for a self-terminating entity. If the end of the entity will |
|
88 // be indicated by closing the connection, there is no keep-alive. |
|
89 HttpEntity entity = response.getEntity(); |
|
90 ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); |
|
91 if (entity != null) { |
|
92 if (entity.getContentLength() < 0) { |
|
93 if (!entity.isChunked() || |
|
94 ver.lessEquals(HttpVersion.HTTP_1_0)) { |
|
95 // if the content length is not known and is not chunk |
|
96 // encoded, the connection cannot be reused |
|
97 return false; |
|
98 } |
|
99 } |
|
100 } |
|
101 |
|
102 // Check for the "Connection" header. If that is absent, check for |
|
103 // the "Proxy-Connection" header. The latter is an unspecified and |
|
104 // broken but unfortunately common extension of HTTP. |
|
105 HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE); |
|
106 if (!hit.hasNext()) |
|
107 hit = response.headerIterator("Proxy-Connection"); |
|
108 |
|
109 // Experimental usage of the "Connection" header in HTTP/1.0 is |
|
110 // documented in RFC 2068, section 19.7.1. A token "keep-alive" is |
|
111 // used to indicate that the connection should be persistent. |
|
112 // Note that the final specification of HTTP/1.1 in RFC 2616 does not |
|
113 // include this information. Neither is the "Connection" header |
|
114 // mentioned in RFC 1945, which informally describes HTTP/1.0. |
|
115 // |
|
116 // RFC 2616 specifies "close" as the only connection token with a |
|
117 // specific meaning: it disables persistent connections. |
|
118 // |
|
119 // The "Proxy-Connection" header is not formally specified anywhere, |
|
120 // but is commonly used to carry one token, "close" or "keep-alive". |
|
121 // The "Connection" header, on the other hand, is defined as a |
|
122 // sequence of tokens, where each token is a header name, and the |
|
123 // token "close" has the above-mentioned additional meaning. |
|
124 // |
|
125 // To get through this mess, we treat the "Proxy-Connection" header |
|
126 // in exactly the same way as the "Connection" header, but only if |
|
127 // the latter is missing. We scan the sequence of tokens for both |
|
128 // "close" and "keep-alive". As "close" is specified by RFC 2068, |
|
129 // it takes precedence and indicates a non-persistent connection. |
|
130 // If there is no "close" but a "keep-alive", we take the hint. |
|
131 |
|
132 if (hit.hasNext()) { |
|
133 try { |
|
134 TokenIterator ti = createTokenIterator(hit); |
|
135 boolean keepalive = false; |
|
136 while (ti.hasNext()) { |
|
137 final String token = ti.nextToken(); |
|
138 if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) { |
|
139 return false; |
|
140 } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) { |
|
141 // continue the loop, there may be a "close" afterwards |
|
142 keepalive = true; |
|
143 } |
|
144 } |
|
145 if (keepalive) |
|
146 return true; |
|
147 // neither "close" nor "keep-alive", use default policy |
|
148 |
|
149 } catch (ParseException px) { |
|
150 // invalid connection header means no persistent connection |
|
151 // we don't have logging in HttpCore, so the exception is lost |
|
152 return false; |
|
153 } |
|
154 } |
|
155 |
|
156 // default since HTTP/1.1 is persistent, before it was non-persistent |
|
157 return !ver.lessEquals(HttpVersion.HTTP_1_0); |
|
158 } |
|
159 |
|
160 |
|
161 /** |
|
162 * Creates a token iterator from a header iterator. |
|
163 * This method can be overridden to replace the implementation of |
|
164 * the token iterator. |
|
165 * |
|
166 * @param hit the header iterator |
|
167 * |
|
168 * @return the token iterator |
|
169 */ |
|
170 protected TokenIterator createTokenIterator(HeaderIterator hit) { |
|
171 return new BasicTokenIterator(hit); |
|
172 } |
|
173 } |