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.protocol;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.HeaderElements;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.HttpRequestInterceptor;
40  import org.apache.hc.core5.http.HttpVersion;
41  import org.apache.hc.core5.http.ProtocolException;
42  import org.apache.hc.core5.http.ProtocolVersion;
43  import org.apache.hc.core5.http.message.MessageSupport;
44  import org.apache.hc.core5.util.Args;
45  
46  /**
47   * RequestContent is the most important interceptor for outgoing requests.
48   * It is responsible for delimiting content length by adding
49   * {@code Content-Length} or {@code Transfer-Content} headers based
50   * on the properties of the enclosed entity and the protocol version.
51   * This interceptor is required for correct functioning of client side protocol
52   * processors.
53   *
54   * @since 4.0
55   */
56  @Contract(threading = ThreadingBehavior.IMMUTABLE)
57  public class RequestContent implements HttpRequestInterceptor {
58  
59      private final boolean overwrite;
60  
61      /**
62       * Default constructor. The {@code Content-Length} or {@code Transfer-Encoding}
63       * will cause the interceptor to throw {@link ProtocolException} if already present in the
64       * response message.
65       */
66      public RequestContent() {
67          this(false);
68      }
69  
70      /**
71       * Constructor that can be used to fine-tune behavior of this interceptor.
72       *
73       * @param overwrite If set to {@code true} the {@code Content-Length} and
74       * {@code Transfer-Encoding} headers will be created or updated if already present.
75       * If set to {@code false} the {@code Content-Length} and
76       * {@code Transfer-Encoding} headers will cause the interceptor to throw
77       * {@link ProtocolException} if already present in the response message.
78       *
79       * @since 4.2
80       */
81       public RequestContent(final boolean overwrite) {
82           super();
83           this.overwrite = overwrite;
84      }
85  
86      @Override
87      public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context)
88              throws HttpException, IOException {
89          Args.notNull(request, "HTTP request");
90          if (this.overwrite) {
91              request.removeHeaders(HttpHeaders.TRANSFER_ENCODING);
92              request.removeHeaders(HttpHeaders.CONTENT_LENGTH);
93          } else {
94              if (request.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
95                  throw new ProtocolException("Transfer-encoding header already present");
96              }
97              if (request.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
98                  throw new ProtocolException("Content-Length header already present");
99              }
100         }
101         if (entity != null) {
102             final ProtocolVersion ver = context.getProtocolVersion();
103             // Must specify a transfer encoding or a content length
104             if (entity.isChunked() || entity.getContentLength() < 0) {
105                 if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
106                     throw new ProtocolException(
107                             "Chunked transfer encoding not allowed for " + ver);
108                 }
109                 request.addHeader(HttpHeaders.TRANSFER_ENCODING, HeaderElements.CHUNKED_ENCODING);
110                 MessageSupport.addTrailerHeader(request, entity);
111             } else {
112                 request.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(entity.getContentLength()));
113             }
114             MessageSupport.addContentTypeHeader(request, entity);
115             MessageSupport.addContentEncodingHeader(request, entity);
116         }
117     }
118 
119 }