• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java $
3  * $Revision: 576073 $
4  * $Date: 2007-09-16 03:53:13 -0700 (Sun, 16 Sep 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl.entity;
33 
34 import org.apache.http.Header;
35 import org.apache.http.HeaderElement;
36 import org.apache.http.HttpException;
37 import org.apache.http.HttpMessage;
38 import org.apache.http.ParseException;
39 import org.apache.http.ProtocolException;
40 import org.apache.http.entity.ContentLengthStrategy;
41 import org.apache.http.params.HttpParams;
42 import org.apache.http.params.CoreProtocolPNames;
43 import org.apache.http.protocol.HTTP;
44 
45 /**
46  * The lax implementation of the content length strategy.
47  * <p>
48  * This strategy conforms to the entity transfer rules outlined in
49  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec4.4">Section 4.4</a>,
50  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>,
51  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">Section 14.41</a>
52  * and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec14.13">Section 14.13</a>
53  * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>, but is lenient
54  * about unsupported transfer codecs and malformed content-length headers.
55  * </p>
56  * <h>4.4 Message Length</h>
57  * <p>
58  * The transfer-length of a message is the length of the message-body as it appears in the
59  * message; that is, after any transfer-codings have been applied. When a message-body is
60  * included with a message, the transfer-length of that body is determined by one of the
61  * following (in order of precedence):
62  * </p>
63  * <p>
64  * 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204,
65  * and 304 responses and any response to a HEAD request) is always terminated by the first
66  * empty line after the header fields, regardless of the entity-header fields present in the
67  * message.
68  * </p>
69  * <p>
70  * 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other
71  * than "identity", then the transfer-length is defined by use of the "chunked" transfer-
72  * coding (section 3.6), unless the message is terminated by closing the connection.
73  * </p>
74  * <p>
75  * 3.If a Content-Length header field (section 14.13) is present, its decimal value in
76  * OCTETs represents both the entity-length and the transfer-length. The Content-Length
77  * header field MUST NOT be sent if these two lengths are different (i.e., if a
78  * Transfer-Encoding
79  * </p>
80  * <pre>
81  *    header field is present). If a message is received with both a
82  *    Transfer-Encoding header field and a Content-Length header field,
83  *    the latter MUST be ignored.
84  * </pre>
85  * <p>
86  * 4.If the message uses the media type "multipart/byteranges", and the ransfer-length is not
87  * otherwise specified, then this self- elimiting media type defines the transfer-length.
88  * This media type UST NOT be used unless the sender knows that the recipient can arse it; the
89  * presence in a request of a Range header with ultiple byte- range specifiers from a 1.1
90  * client implies that the lient can parse multipart/byteranges responses.
91  * </p>
92  * <pre>
93  *     A range header might be forwarded by a 1.0 proxy that does not
94  *     understand multipart/byteranges; in this case the server MUST
95  *     delimit the message using methods defined in items 1,3 or 5 of
96  *     this section.
97  * </pre>
98  * <p>
99  * 5.By the server closing the connection. (Closing the connection cannot be used to indicate
100  * the end of a request body, since that would leave no possibility for the server to send back
101  * a response.)
102  * </p>
103  * <p>
104  * For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body
105  * MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1
106  * compliant. If a request contains a message-body and a Content-Length is not given, the
107  * server SHOULD respond with 400 (bad request) if it cannot determine the length of the
108  * message, or with 411 (length required) if it wishes to insist on receiving a valid
109  * Content-Length.
110  * </p>
111  * <p>All HTTP/1.1 applications that receive entities MUST accept the "chunked" transfer-coding
112  * (section 3.6), thus allowing this mechanism to be used for messages when the message
113  * length cannot be determined in advance.
114  * </p>
115  * <h>3.6 Transfer Codings</h>
116  * <p>
117  * Transfer-coding values are used to indicate an encoding transformation that
118  * has been, can be, or may need to be applied to an entity-body in order to ensure
119  * "safe transport" through the network. This differs from a content coding in that
120  * the transfer-coding is a property of the message, not of the original entity.
121  * </p>
122  * <pre>
123  * transfer-coding         = "chunked" | transfer-extension
124  * transfer-extension      = token *( ";" parameter )
125  * </pre>
126  * <p>
127  * Parameters are in the form of attribute/value pairs.
128  * </p>
129  * <pre>
130  * parameter               = attribute "=" value
131  * attribute               = token
132  * value                   = token | quoted-string
133  * </pre>
134  * <p>
135  * All transfer-coding values are case-insensitive. HTTP/1.1 uses transfer-coding values in
136  * the TE header field (section 14.39) and in the Transfer-Encoding header field (section 14.41).
137  * </p>
138  * <p>
139  * Whenever a transfer-coding is applied to a message-body, the set of transfer-codings MUST
140  * include "chunked", unless the message is terminated by closing the connection. When the
141  * "chunked" transfer-coding is used, it MUST be the last transfer-coding applied to the
142  * message-body. The "chunked" transfer-coding MUST NOT be applied more than once to a
143  * message-body. These rules allow the recipient to determine the transfer-length of the
144  * message (section 4.4).
145  * </p>
146  * <h>14.41 Transfer-Encoding</h>
147  * <p>
148  * The Transfer-Encoding general-header field indicates what (if any) type of transformation has
149  * been applied to the message body in order to safely transfer it between the sender and the
150  * recipient. This differs from the content-coding in that the transfer-coding is a property of
151  * the message, not of the entity.
152  * </p>
153  * <pre>
154  *   Transfer-Encoding       = "Transfer-Encoding" ":" 1#transfer-coding
155  * </pre>
156  * <p>
157  * If multiple encodings have been applied to an entity, the transfer- codings MUST be listed in
158  * the order in which they were applied. Additional information about the encoding parameters
159  * MAY be provided by other entity-header fields not defined by this specification.
160  * </p>
161  * <h>14.13 Content-Length</h>
162  * <p>
163  * The Content-Length entity-header field indicates the size of the entity-body, in decimal
164  * number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of
165  * the entity-body that would have been sent had the request been a GET.
166  * </p>
167  * <pre>
168  *   Content-Length    = "Content-Length" ":" 1*DIGIT
169  * </pre>
170  * <p>
171  * Applications SHOULD use this field to indicate the transfer-length of the message-body,
172  * unless this is prohibited by the rules in section 4.4.
173  * </p>
174  *
175  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
176  *
177  * @version $Revision: 576073 $
178  *
179  * @since 4.0
180  */
181 public class LaxContentLengthStrategy implements ContentLengthStrategy {
182 
LaxContentLengthStrategy()183     public LaxContentLengthStrategy() {
184         super();
185     }
186 
determineLength(final HttpMessage message)187     public long determineLength(final HttpMessage message) throws HttpException {
188         if (message == null) {
189             throw new IllegalArgumentException("HTTP message may not be null");
190         }
191 
192         HttpParams params = message.getParams();
193         boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
194 
195         Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
196         Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
197         // We use Transfer-Encoding if present and ignore Content-Length.
198         // RFC2616, 4.4 item number 3
199         if (transferEncodingHeader != null) {
200             HeaderElement[] encodings = null;
201             try {
202                 encodings = transferEncodingHeader.getElements();
203             } catch (ParseException px) {
204                 throw new ProtocolException
205                     ("Invalid Transfer-Encoding header value: " +
206                      transferEncodingHeader, px);
207             }
208             if (strict) {
209                 // Currently only chunk and identity are supported
210                 for (int i = 0; i < encodings.length; i++) {
211                     String encoding = encodings[i].getName();
212                     if (encoding != null && encoding.length() > 0
213                         && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
214                         && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
215                         throw new ProtocolException("Unsupported transfer encoding: " + encoding);
216                     }
217                 }
218             }
219             // The chunked encoding must be the last one applied RFC2616, 14.41
220             int len = encodings.length;
221             if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
222                 return IDENTITY;
223             } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
224                     encodings[len - 1].getName()))) {
225                 return CHUNKED;
226             } else {
227                 if (strict) {
228                     throw new ProtocolException("Chunk-encoding must be the last one applied");
229                 }
230                 return IDENTITY;
231             }
232         } else if (contentLengthHeader != null) {
233             long contentlen = -1;
234             Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
235             if (strict && headers.length > 1) {
236                 throw new ProtocolException("Multiple content length headers");
237             }
238             for (int i = headers.length - 1; i >= 0; i--) {
239                 Header header = headers[i];
240                 try {
241                     contentlen = Long.parseLong(header.getValue());
242                     break;
243                 } catch (NumberFormatException e) {
244                     if (strict) {
245                         throw new ProtocolException("Invalid content length: " + header.getValue());
246                     }
247                 }
248                 // See if we can have better luck with another header, if present
249             }
250             if (contentlen >= 0) {
251                 return contentlen;
252             } else {
253                 return IDENTITY;
254             }
255         } else {
256             return IDENTITY;
257         }
258     }
259 
260 }
261