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.hc.core5.http.impl;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.http.ContentLengthStrategy;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HeaderElements;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.HttpHeaders;
37  import org.apache.hc.core5.http.HttpMessage;
38  import org.apache.hc.core5.http.NotImplementedException;
39  import org.apache.hc.core5.http.ProtocolException;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * The default implementation of the content length strategy. This class
44   * will throw {@link ProtocolException} if it encounters an unsupported
45   * transfer encoding, multiple {@code Content-Length} header
46   * values or a malformed {@code Content-Length} header value.
47   * <p>
48   * This class recognizes "chunked" transfer-coding only.
49   *
50   * @since 5.0
51   */
52  @Contract(threading = ThreadingBehavior.IMMUTABLE)
53  public class DefaultContentLengthStrategy implements ContentLengthStrategy {
54  
55      public static final DefaultContentLengthStrategy INSTANCE = new DefaultContentLengthStrategy();
56  
57      /**
58       * Creates {@code DefaultContentLengthStrategy} instance. {@link ContentLengthStrategy#UNDEFINED}
59       * is used per default when content length is not explicitly specified in the message.
60       */
61      public DefaultContentLengthStrategy() {
62      }
63  
64      @Override
65      public long determineLength(final HttpMessage message) throws HttpException {
66          Args.notNull(message, "HTTP message");
67          // Although Transfer-Encoding is specified as a list, in practice
68          // it is either missing or has the single value "chunked". So we
69          // treat it as a single-valued header here.
70          final Header transferEncodingHeader = message.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
71          if (transferEncodingHeader != null) {
72              final String s = transferEncodingHeader.getValue();
73              if (HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(s)) {
74                  return CHUNKED;
75              }
76              throw new NotImplementedException("Unsupported transfer encoding: " + s);
77          }
78          if (message.containsHeaders(HttpHeaders.CONTENT_LENGTH) > 1) {
79              throw new ProtocolException("Multiple Content-Length headers");
80          }
81          final Header contentLengthHeader = message.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
82          if (contentLengthHeader != null) {
83              final String s = contentLengthHeader.getValue();
84              try {
85                  final long len = Long.parseLong(s);
86                  if (len < 0) {
87                      throw new ProtocolException("Negative content length: " + s);
88                  }
89                  return len;
90              } catch (final NumberFormatException e) {
91                  throw new ProtocolException("Invalid content length: " + s);
92              }
93          }
94          return UNDEFINED;
95      }
96  
97  }