You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/07/14 15:31:42 UTC
svn commit: r1146700 - in /httpcomponents/httpcore/trunk/httpcore/src:
main/java/org/apache/http/protocol/ResponseContent.java
test/java/org/apache/http/protocol/TestStandardInterceptors.java
Author: olegk
Date: Thu Jul 14 13:31:41 2011
New Revision: 1146700
URL: http://svn.apache.org/viewvc?rev=1146700&view=rev
Log:
HTTPCORE-264: Allow overwriting of Content-Length & Transfer-Encoding headers
Contributed by William R. Speirs <bill.speirs at gmail.com>
Modified:
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/ResponseContent.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/ResponseContent.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/ResponseContent.java?rev=1146700&r1=1146699&r2=1146700&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/ResponseContent.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/ResponseContent.java Thu Jul 14 13:31:41 2011
@@ -52,19 +52,47 @@ import org.apache.http.annotation.Immuta
@Immutable
public class ResponseContent implements HttpResponseInterceptor {
+ private final boolean overwrite;
+
+ /**
+ * Default constructor. The <code>Content-Length</code> or <code>Transfer-Encoding</code>
+ * will cause the interceptor to throw {@link ProtocolException} if already present in the
+ * response message.
+ */
public ResponseContent() {
- super();
+ this(false);
+ }
+
+ /**
+ * Constructor that can be used to fine-tune behavior of this interceptor.
+ *
+ * @param overwrite If set to <code>true</code> the <code>Content-Length</code> and
+ * <code>Transfer-Encoding</code> headers will be created or updated if already present.
+ * If set to <code>false</code> the <code>Content-Length</code> and
+ * <code>Transfer-Encoding</code> headers will cause the interceptor to throw
+ * {@link ProtocolException} if already present in the response message.
+ */
+ public ResponseContent(boolean overwrite) {
+ super();
+ this.overwrite = overwrite;
}
+ /**
+ * Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
+ * @param response The HttpResponse to modify.
+ * @param context Unused.
+ * @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
+ * @throws IllegalArgumentException If the response is null.
+ */
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
- if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
+ if (response.containsHeader(HTTP.TRANSFER_ENCODING) && !this.overwrite) {
throw new ProtocolException("Transfer-encoding header already present");
}
- if (response.containsHeader(HTTP.CONTENT_LEN)) {
+ if (response.containsHeader(HTTP.CONTENT_LEN) && !this.overwrite) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
@@ -72,26 +100,26 @@ public class ResponseContent implements
if (entity != null) {
long len = entity.getContentLength();
if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
- response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
+ response.setHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else if (len >= 0) {
- response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
+ response.setHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !response.containsHeader(
HTTP.CONTENT_TYPE )) {
- response.addHeader(entity.getContentType());
+ response.setHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !response.containsHeader(
HTTP.CONTENT_ENCODING)) {
- response.addHeader(entity.getContentEncoding());
+ response.setHeader(entity.getContentEncoding());
}
} else {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT) {
- response.addHeader(HTTP.CONTENT_LEN, "0");
+ response.setHeader(HTTP.CONTENT_LEN, "0");
}
}
}
Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java?rev=1146700&r1=1146699&r2=1146700&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java Thu Jul 14 13:31:41 2011
@@ -838,6 +838,28 @@ public class TestStandardInterceptors {
}
@Test
+ public void testResponseContentOverwriteHeaders() throws Exception {
+ ResponseContent interceptor = new ResponseContent(true);
+ HttpContext context = new BasicHttpContext(null);
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+ response.addHeader(new BasicHeader(HTTP.CONTENT_LEN, "10"));
+ response.addHeader(new BasicHeader(HTTP.TRANSFER_ENCODING, "whatever"));
+ interceptor.process(response, context);
+ Assert.assertEquals("0", response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
+ Assert.assertEquals("whatever", response.getFirstHeader(HTTP.TRANSFER_ENCODING).getValue());
+ }
+
+ @Test
+ public void testResponseContentAddHeaders() throws Exception {
+ ResponseContent interceptor = new ResponseContent(true);
+ HttpContext context = new BasicHttpContext(null);
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+ interceptor.process(response, context);
+ Assert.assertEquals("0", response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
+ Assert.assertNull(response.getFirstHeader(HTTP.TRANSFER_ENCODING));
+ }
+
+ @Test
public void testResponseDateGenerated() throws Exception {
HttpContext context = new BasicHttpContext(null);
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");