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