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.HttpException;
32  import org.apache.http.HttpMessage;
33  import org.apache.http.HttpVersion;
34  import org.apache.http.ProtocolException;
35  import org.apache.http.annotation.Immutable;
36  import org.apache.http.entity.ContentLengthStrategy;
37  import org.apache.http.protocol.HTTP;
38  
39  /**
40   * The strict implementation of the content length strategy. This class
41   * will throw {@link ProtocolException} if it encounters an unsupported
42   * transfer encoding or a malformed <code>Content-Length</code> header
43   * value.
44   * <p>
45   * This class recognizes "chunked" and "identitiy" transfer-coding only.
46   *
47   * @since 4.0
48   */
49  @Immutable
50  public class StrictContentLengthStrategy implements ContentLengthStrategy {
51  
52      private final int implicitLen;
53      
54      /**
55       * Creates <tt>StrictContentLengthStrategy</tt> instance with the given length used per default
56       * when content length is not explicitly specified in the message.
57       * 
58       * @param implicitLen implicit content length.
59       * 
60       * @since 4.2
61       */
62      public StrictContentLengthStrategy(int implicitLen) {
63          super();
64          this.implicitLen = implicitLen;
65      }
66  
67      /**
68       * Creates <tt>StrictContentLengthStrategy</tt> instance. {@link ContentLengthStrategy#IDENTITY} 
69       * is used per default when content length is not explicitly specified in the message.
70       */
71      public StrictContentLengthStrategy() {
72          this(IDENTITY);
73      }
74  
75      public long determineLength(final HttpMessage message) throws HttpException {
76          if (message == null) {
77              throw new IllegalArgumentException("HTTP message may not be null");
78          }
79          // Although Transfer-Encoding is specified as a list, in practice
80          // it is either missing or has the single value "chunked". So we
81          // treat it as a single-valued header here.
82          Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
83          if (transferEncodingHeader != null) {
84              String s = transferEncodingHeader.getValue();
85              if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
86                  if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
87                      throw new ProtocolException(
88                              "Chunked transfer encoding not allowed for " +
89                              message.getProtocolVersion());
90                  }
91                  return CHUNKED;
92              } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
93                  return IDENTITY;
94              } else {
95                  throw new ProtocolException(
96                          "Unsupported transfer encoding: " + s);
97              }
98          }
99          Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
100         if (contentLengthHeader != null) {
101             String s = contentLengthHeader.getValue();
102             try {
103                 long len = Long.parseLong(s);
104                 if (len < 0) {
105                     throw new ProtocolException("Negative content length: " + s);
106                 }
107                 return len;
108             } catch (NumberFormatException e) {
109                 throw new ProtocolException("Invalid content length: " + s);
110             }
111         }
112         return this.implicitLen;
113     }
114 
115 }