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.params.HttpParams;
39 import org.apache.http.params.CoreProtocolPNames;
40 import org.apache.http.protocol.HTTP;
41
42 /**
43 * The lax implementation of the content length strategy. This class will ignore
44 * unrecognized transfer encodings and malformed <code>Content-Length</code>
45 * header values if the {@link CoreProtocolPNames#STRICT_TRANSFER_ENCODING}
46 * parameter of the given message is not set or set to <code>false</code>.
47 * <p>
48 * This class recognizes "chunked" and "identitiy" transfer-coding only.
49 * <p>
50 * The following parameters can be used to customize the behavior of this class:
51 * <ul>
52 * <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
53 * </ul>
54 *
55 * @since 4.0
56 */
57 @Immutable
58 public class LaxContentLengthStrategy implements ContentLengthStrategy {
59
60 private final int implicitLen;
61
62 /**
63 * Creates <tt>LaxContentLengthStrategy</tt> instance with the given length used per default
64 * when content length is not explicitly specified in the message.
65 *
66 * @param implicitLen implicit content length.
67 *
68 * @since 4.2
69 */
70 public LaxContentLengthStrategy(int implicitLen) {
71 super();
72 this.implicitLen = implicitLen;
73 }
74
75 /**
76 * Creates <tt>LaxContentLengthStrategy</tt> instance. {@link ContentLengthStrategy#IDENTITY}
77 * is used per default when content length is not explicitly specified in the message.
78 */
79 public LaxContentLengthStrategy() {
80 this(IDENTITY);
81 }
82
83 public long determineLength(final HttpMessage message) throws HttpException {
84 if (message == null) {
85 throw new IllegalArgumentException("HTTP message may not be null");
86 }
87
88 HttpParams params = message.getParams();
89 boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
90
91 Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
92 // We use Transfer-Encoding if present and ignore Content-Length.
93 // RFC2616, 4.4 item number 3
94 if (transferEncodingHeader != null) {
95 HeaderElement[] encodings = null;
96 try {
97 encodings = transferEncodingHeader.getElements();
98 } catch (ParseException px) {
99 throw new ProtocolException
100 ("Invalid Transfer-Encoding header value: " +
101 transferEncodingHeader, px);
102 }
103 if (strict) {
104 // Currently only chunk and identity are supported
105 for (int i = 0; i < encodings.length; i++) {
106 String encoding = encodings[i].getName();
107 if (encoding != null && encoding.length() > 0
108 && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
109 && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
110 throw new ProtocolException("Unsupported transfer encoding: " + encoding);
111 }
112 }
113 }
114 // The chunked encoding must be the last one applied RFC2616, 14.41
115 int len = encodings.length;
116 if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
117 return IDENTITY;
118 } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
119 encodings[len - 1].getName()))) {
120 return CHUNKED;
121 } else {
122 if (strict) {
123 throw new ProtocolException("Chunk-encoding must be the last one applied");
124 }
125 return IDENTITY;
126 }
127 }
128 Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
129 if (contentLengthHeader != null) {
130 long contentlen = -1;
131 Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
132 if (strict && headers.length > 1) {
133 throw new ProtocolException("Multiple content length headers");
134 }
135 for (int i = headers.length - 1; i >= 0; i--) {
136 Header header = headers[i];
137 try {
138 contentlen = Long.parseLong(header.getValue());
139 break;
140 } catch (NumberFormatException e) {
141 if (strict) {
142 throw new ProtocolException("Invalid content length: " + header.getValue());
143 }
144 }
145 // See if we can have better luck with another header, if present
146 }
147 if (contentlen >= 0) {
148 return contentlen;
149 } else {
150 return IDENTITY;
151 }
152 }
153 return this.implicitLen;
154 }
155
156 }