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