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