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 org.apache.http.impl.entity;
29
30 import org.apache.http.Header;
31 import org.apache.http.HeaderElement;
32 import org.apache.http.HttpException;
33 import org.apache.http.HttpMessage;
34 import org.apache.http.ParseException;
35 import org.apache.http.ProtocolException;
36 import org.apache.http.annotation.Immutable;
37 import org.apache.http.entity.ContentLengthStrategy;
38 import org.apache.http.protocol.HTTP;
39 import org.apache.http.util.Args;
40
41 /**
42 * The lax implementation of the content length strategy. This class will ignore
43 * unrecognized transfer encodings and malformed <code>Content-Length</code>
44 * header values.
45 * <p/>
46 * This class recognizes "chunked" and "identitiy" transfer-coding only.
47 *
48 * @since 4.0
49 */
50 @Immutable
51 public class LaxContentLengthStrategy implements ContentLengthStrategy {
52
53 public static final LaxContentLengthStrategy INSTANCE = new LaxContentLengthStrategy();
54
55 private final int implicitLen;
56
57 /**
58 * Creates <tt>LaxContentLengthStrategy</tt> instance with the given length used per default
59 * when content length is not explicitly specified in the message.
60 *
61 * @param implicitLen implicit content length.
62 *
63 * @since 4.2
64 */
65 public LaxContentLengthStrategy(final int implicitLen) {
66 super();
67 this.implicitLen = implicitLen;
68 }
69
70 /**
71 * Creates <tt>LaxContentLengthStrategy</tt> instance. {@link ContentLengthStrategy#IDENTITY}
72 * is used per default when content length is not explicitly specified in the message.
73 */
74 public LaxContentLengthStrategy() {
75 this(IDENTITY);
76 }
77
78 public long determineLength(final HttpMessage message) throws HttpException {
79 Args.notNull(message, "HTTP message");
80
81 final Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
82 // We use Transfer-Encoding if present and ignore Content-Length.
83 // RFC2616, 4.4 item number 3
84 if (transferEncodingHeader != null) {
85 HeaderElement[] encodings = null;
86 try {
87 encodings = transferEncodingHeader.getElements();
88 } catch (final ParseException px) {
89 throw new ProtocolException
90 ("Invalid Transfer-Encoding header value: " +
91 transferEncodingHeader, px);
92 }
93 // The chunked encoding must be the last one applied RFC2616, 14.41
94 final int len = encodings.length;
95 if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
96 return IDENTITY;
97 } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
98 encodings[len - 1].getName()))) {
99 return CHUNKED;
100 } else {
101 return IDENTITY;
102 }
103 }
104 final Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
105 if (contentLengthHeader != null) {
106 long contentlen = -1;
107 final Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
108 for (int i = headers.length - 1; i >= 0; i--) {
109 final Header header = headers[i];
110 try {
111 contentlen = Long.parseLong(header.getValue());
112 break;
113 } catch (final NumberFormatException e) {
114 }
115 // See if we can have better luck with another header, if present
116 }
117 if (contentlen >= 0) {
118 return contentlen;
119 } else {
120 return IDENTITY;
121 }
122 }
123 return this.implicitLen;
124 }
125
126 }