001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java $
003: * $Revision: 573949 $
004: * $Date: 2007-09-09 07:46:25 +0200 (Sun, 09 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.entity;
033:
034: import org.apache.http.Header;
035: import org.apache.http.HttpException;
036: import org.apache.http.HttpMessage;
037: import org.apache.http.HttpVersion;
038: import org.apache.http.ProtocolException;
039: import org.apache.http.entity.ContentLengthStrategy;
040: import org.apache.http.protocol.HTTP;
041:
042: /**
043: * The strict implementation of the content length strategy.
044: * <p>
045: * This entity generator comforms to the entity transfer rules outlined in the
046: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec4.4">Section 4.4</a>,
047: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>,
048: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">Section 14.41</a>
049: * and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec14.13">Section 14.13</a>
050: * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
051: * </p>
052: * <h>4.4 Message Length</h>
053: * <p>
054: * The transfer-length of a message is the length of the message-body as it appears in the
055: * message; that is, after any transfer-codings have been applied. When a message-body is
056: * included with a message, the transfer-length of that body is determined by one of the
057: * following (in order of precedence):
058: * </p>
059: * <p>
060: * 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204,
061: * and 304 responses and any response to a HEAD request) is always terminated by the first
062: * empty line after the header fields, regardless of the entity-header fields present in the
063: * message.
064: * </p>
065: * <p>
066: * 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other
067: * than "identity", then the transfer-length is defined by use of the "chunked" transfer-
068: * coding (section 3.6), unless the message is terminated by closing the connection.
069: * </p>
070: * <p>
071: * 3.If a Content-Length header field (section 14.13) is present, its decimal value in
072: * OCTETs represents both the entity-length and the transfer-length. The Content-Length
073: * header field MUST NOT be sent if these two lengths are different (i.e., if a
074: * Transfer-Encoding
075: * </p>
076: * <pre>
077: * header field is present). If a message is received with both a
078: * Transfer-Encoding header field and a Content-Length header field,
079: * the latter MUST be ignored.
080: * </pre>
081: * <p>
082: * 4.If the message uses the media type "multipart/byteranges", and the ransfer-length is not
083: * otherwise specified, then this self- elimiting media type defines the transfer-length.
084: * This media type UST NOT be used unless the sender knows that the recipient can arse it; the
085: * presence in a request of a Range header with ultiple byte- range specifiers from a 1.1
086: * client implies that the lient can parse multipart/byteranges responses.
087: * </p>
088: * <pre>
089: * A range header might be forwarded by a 1.0 proxy that does not
090: * understand multipart/byteranges; in this case the server MUST
091: * delimit the message using methods defined in items 1,3 or 5 of
092: * this section.
093: * </pre>
094: * <p>
095: * 5.By the server closing the connection. (Closing the connection cannot be used to indicate
096: * the end of a request body, since that would leave no possibility for the server to send back
097: * a response.)
098: * </p>
099: * <p>
100: * For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body
101: * MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1
102: * compliant. If a request contains a message-body and a Content-Length is not given, the
103: * server SHOULD respond with 400 (bad request) if it cannot determine the length of the
104: * message, or with 411 (length required) if it wishes to insist on receiving a valid
105: * Content-Length.
106: * </p>
107: * <p>All HTTP/1.1 applications that receive entities MUST accept the "chunked" transfer-coding
108: * (section 3.6), thus allowing this mechanism to be used for messages when the message
109: * length cannot be determined in advance.
110: * </p>
111: * <h>3.6 Transfer Codings</h>
112: * <p>
113: * Transfer-coding values are used to indicate an encoding transformation that
114: * has been, can be, or may need to be applied to an entity-body in order to ensure
115: * "safe transport" through the network. This differs from a content coding in that
116: * the transfer-coding is a property of the message, not of the original entity.
117: * </p>
118: * <pre>
119: * transfer-coding = "chunked" | transfer-extension
120: * transfer-extension = token *( ";" parameter )
121: * </pre>
122: * <p>
123: * Parameters are in the form of attribute/value pairs.
124: * </p>
125: * <pre>
126: * parameter = attribute "=" value
127: * attribute = token
128: * value = token | quoted-string
129: * </pre>
130: * <p>
131: * All transfer-coding values are case-insensitive. HTTP/1.1 uses transfer-coding values in
132: * the TE header field (section 14.39) and in the Transfer-Encoding header field (section 14.41).
133: * </p>
134: * <p>
135: * Whenever a transfer-coding is applied to a message-body, the set of transfer-codings MUST
136: * include "chunked", unless the message is terminated by closing the connection. When the
137: * "chunked" transfer-coding is used, it MUST be the last transfer-coding applied to the
138: * message-body. The "chunked" transfer-coding MUST NOT be applied more than once to a
139: * message-body. These rules allow the recipient to determine the transfer-length of the
140: * message (section 4.4).
141: * </p>
142: * <h>14.41 Transfer-Encoding</h>
143: * <p>
144: * The Transfer-Encoding general-header field indicates what (if any) type of transformation has
145: * been applied to the message body in order to safely transfer it between the sender and the
146: * recipient. This differs from the content-coding in that the transfer-coding is a property of
147: * the message, not of the entity.
148: * </p>
149: * <pre>
150: * Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding
151: * </pre>
152: * <p>
153: * If multiple encodings have been applied to an entity, the transfer- codings MUST be listed in
154: * the order in which they were applied. Additional information about the encoding parameters
155: * MAY be provided by other entity-header fields not defined by this specification.
156: * </p>
157: * <h>14.13 Content-Length</h>
158: * <p>
159: * The Content-Length entity-header field indicates the size of the entity-body, in decimal
160: * number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of
161: * the entity-body that would have been sent had the request been a GET.
162: * </p>
163: * <pre>
164: * Content-Length = "Content-Length" ":" 1*DIGIT
165: * </pre>
166: * <p>
167: * Applications SHOULD use this field to indicate the transfer-length of the message-body,
168: * unless this is prohibited by the rules in section 4.4.
169: * </p>
170: *
171: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
172: *
173: * @version $Revision: 573949 $
174: *
175: * @since 4.0
176: */
177: public class StrictContentLengthStrategy implements
178: ContentLengthStrategy {
179:
180: public StrictContentLengthStrategy() {
181: super ();
182: }
183:
184: public long determineLength(final HttpMessage message)
185: throws HttpException {
186: if (message == null) {
187: throw new IllegalArgumentException(
188: "HTTP message may not be null");
189: }
190: // Although Transfer-Encoding is specified as a list, in practice
191: // it is either missing or has the single value "chunked". So we
192: // treat it as a single-valued header here.
193: Header transferEncodingHeader = message
194: .getFirstHeader(HTTP.TRANSFER_ENCODING);
195: Header contentLengthHeader = message
196: .getFirstHeader(HTTP.CONTENT_LEN);
197: if (transferEncodingHeader != null) {
198: String s = transferEncodingHeader.getValue();
199: if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
200: if (message.getProtocolVersion().lessEquals(
201: HttpVersion.HTTP_1_0)) {
202: throw new ProtocolException(
203: "Chunked transfer encoding not allowed for "
204: + message.getProtocolVersion());
205: }
206: return CHUNKED;
207: } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
208: return IDENTITY;
209: } else {
210: throw new ProtocolException(
211: "Unsupported transfer encoding: " + s);
212: }
213: } else if (contentLengthHeader != null) {
214: String s = contentLengthHeader.getValue();
215: try {
216: long len = Long.parseLong(s);
217: return len;
218: } catch (NumberFormatException e) {
219: throw new ProtocolException("Invalid content length: "
220: + s);
221: }
222: } else {
223: return IDENTITY;
224: }
225: }
226:
227: }
|