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 java.util.concurrent.atomic.AtomicReference;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.ContentLengthStrategy;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HeaderElements;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpHeaders;
39  import org.apache.hc.core5.http.HttpMessage;
40  import org.apache.hc.core5.http.NotImplementedException;
41  import org.apache.hc.core5.http.ProtocolException;
42  import org.apache.hc.core5.http.message.MessageSupport;
43  import org.apache.hc.core5.util.Args;
44  import org.apache.hc.core5.util.TextUtils;
45  
46  /**
47   * The default implementation of the content length strategy. This class
48   * will throw {@link ProtocolException} if it encounters an unsupported
49   * transfer encoding, multiple {@code Content-Length} header
50   * values or a malformed {@code Content-Length} header value.
51   * <p>
52   * This class recognizes "chunked" transfer-coding only.
53   *
54   * @since 5.0
55   */
56  @Contract(threading = ThreadingBehavior.IMMUTABLE)
57  public class DefaultContentLengthStrategy implements ContentLengthStrategy {
58  
59      public static final DefaultContentLengthStrategy INSTANCE = new DefaultContentLengthStrategy();
60  
61      /**
62       * Creates {@code DefaultContentLengthStrategy} instance. {@link ContentLengthStrategy#UNDEFINED}
63       * is used per default when content length is not explicitly specified in the message.
64       */
65      public DefaultContentLengthStrategy() {
66      }
67  
68      enum Coding { UNKNOWN, CHUNK }
69  
70      @Override
71      public long determineLength(final HttpMessage message) throws HttpException {
72          Args.notNull(message, "HTTP message");
73          final Header teh = message.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
74          if (teh != null) {
75              final AtomicReference<Coding> codingRef = new AtomicReference<>();
76              MessageSupport.parseTokens(message, HttpHeaders.TRANSFER_ENCODING, e -> {
77                  if (!TextUtils.isBlank(e)) {
78                      if (e.equalsIgnoreCase(HeaderElements.CHUNKED_ENCODING)) {
79                          if (!codingRef.compareAndSet(null, Coding.CHUNK)) {
80                              codingRef.set(Coding.UNKNOWN);
81                          }
82                      } else {
83                          codingRef.set(Coding.UNKNOWN);
84                      }
85                  }
86              });
87              if (codingRef.get() == Coding.CHUNK) {
88                  return CHUNKED;
89              }
90              throw new NotImplementedException("Unsupported transfer encoding: " + teh.getValue());
91          }
92          if (message.countHeaders(HttpHeaders.CONTENT_LENGTH) > 1) {
93              throw new ProtocolException("Multiple Content-Length headers");
94          }
95          final Header contentLengthHeader = message.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
96          if (contentLengthHeader != null) {
97              final String s = contentLengthHeader.getValue();
98              try {
99                  final long len = Long.parseLong(s);
100                 if (len < 0) {
101                     throw new ProtocolException("Negative content length: " + s);
102                 }
103                 return len;
104             } catch (final NumberFormatException e) {
105                 throw new ProtocolException("Invalid content length: " + s);
106             }
107         }
108         return UNDEFINED;
109     }
110 
111 }