### Eclipse Workspace Patch 1.0 #P httpcomponents-client Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java (revision 992759) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java (working copy) @@ -1354,7 +1354,7 @@ originResponse.addHeader("Allow", "GET,HEAD"); originResponse.addHeader("Cache-Control", "max-age=3600"); originResponse.addHeader("Content-Language", "en"); - originResponse.addHeader("Content-Encoding", "identity"); + originResponse.addHeader("Content-Encoding", "x-coding"); originResponse.addHeader("Content-MD5", "Q2hlY2sgSW50ZWdyaXR5IQ=="); originResponse.addHeader("Content-Length", "128"); originResponse.addHeader("Content-Type", "application/octet-stream"); @@ -1401,7 +1401,7 @@ originResponse.addHeader("Allow", "GET,HEAD"); originResponse.addHeader("Cache-Control", "max-age=3600"); originResponse.addHeader("Content-Language", "en"); - originResponse.addHeader("Content-Encoding", "identity"); + originResponse.addHeader("Content-Encoding", "x-coding"); originResponse.addHeader("Content-MD5", "Q2hlY2sgSW50ZWdyaXR5IQ=="); originResponse.addHeader("Content-Length", "128"); originResponse.addHeader("Content-Type", "application/octet-stream"); @@ -1425,7 +1425,7 @@ Assert.assertEquals("GET,HEAD", result.getFirstHeader("Allow").getValue()); Assert.assertEquals("max-age=3600", result.getFirstHeader("Cache-Control").getValue()); Assert.assertEquals("en", result.getFirstHeader("Content-Language").getValue()); - Assert.assertEquals("identity", result.getFirstHeader("Content-Encoding").getValue()); + Assert.assertEquals("x-coding", result.getFirstHeader("Content-Encoding").getValue()); Assert.assertEquals("Q2hlY2sgSW50ZWdyaXR5IQ==", result.getFirstHeader("Content-MD5") .getValue()); Assert.assertEquals(originResponse.getFirstHeader("Last-Modified").getValue(), result @@ -1873,7 +1873,7 @@ HttpResponse resp1 = make200Response(); resp1.setHeader("ETag", "W/\"v1\""); resp1.setHeader("Allow", "GET,HEAD"); - resp1.setHeader("Content-Encoding", "identity"); + resp1.setHeader("Content-Encoding", "x-coding"); resp1.setHeader("Content-Language", "en"); resp1.setHeader("Content-Length", "128"); resp1.setHeader("Content-MD5", "Q2hlY2sgSW50ZWdyaXR5IQ=="); Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java (revision 0) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java (revision 0) @@ -0,0 +1,42 @@ +package org.apache.http.impl.client.cache; + +import static org.junit.Assert.*; + +import org.apache.http.Header; +import org.apache.http.HeaderElement; +import org.apache.http.HttpResponse; +import org.junit.Test; + +/* + * This test class captures functionality required to achieve unconditional + * compliance with the HTTP/1.1 spec, i.e. all the SHOULD, SHOULD NOT, + * RECOMMENDED, and NOT RECOMMENDED behaviors. + */ +public class TestProtocolRecommendations extends AbstractProtocolTest { + + /* "identity: The default (identity) encoding; the use of no + * transformation whatsoever. This content-coding is used only in the + * Accept-Encoding header, and SHOULD NOT be used in the + * Content-Encoding header." + * + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5 + */ + @Test + public void testIdentityCodingIsNotUsedInContentEncodingHeader() + throws Exception { + originResponse.setHeader("Content-Encoding", "identity"); + backendExpectsAnyRequest().andReturn(originResponse); + replayMocks(); + HttpResponse result = impl.execute(host, request); + verifyMocks(); + boolean foundIdentity = false; + for(Header h : result.getHeaders("Content-Encoding")) { + for(HeaderElement elt : h.getElements()) { + if ("identity".equalsIgnoreCase(elt.getName())) { + foundIdentity = true; + } + } + } + assertFalse(foundIdentity); + } +} Index: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java =================================================================== --- httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java (revision 992759) +++ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java (working copy) @@ -26,8 +26,12 @@ */ package org.apache.http.impl.client.cache; +import java.util.ArrayList; import java.util.Date; +import java.util.List; +import org.apache.http.Header; +import org.apache.http.HeaderElement; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; @@ -39,6 +43,7 @@ import org.apache.http.client.cache.HeaderConstants; import org.apache.http.impl.client.RequestWrapper; import org.apache.http.impl.cookie.DateUtils; +import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; /** @@ -77,9 +82,40 @@ ensure200ForOPTIONSRequestWithNoBodyHasContentLengthZero(request, response); ensure206ContainsDateHeader(response); + + identityIsNotUsedInContentEncoding(response); } - private void authenticationRequiredDidNotHaveAProxyAuthenticationHeader(HttpRequest request, + private void identityIsNotUsedInContentEncoding(HttpResponse response) { + Header[] hdrs = response.getHeaders("Content-Encoding"); + if (hdrs == null || hdrs.length == 0) return; + List
newHeaders = new ArrayList
(); + boolean modified = false; + for(Header h : hdrs) { + StringBuilder buf = new StringBuilder(); + boolean first = true; + for(HeaderElement elt : h.getElements()) { + if ("identity".equalsIgnoreCase(elt.getName())) { + modified = true; + } else { + if (!first) buf.append(","); + buf.append(elt.toString()); + first = false; + } + } + String newHeaderValue = buf.toString(); + if (!"".equals(newHeaderValue)) { + newHeaders.add(new BasicHeader("Content-Encoding", newHeaderValue)); + } + } + if (!modified) return; + response.removeHeaders("Content-Encoding"); + for(Header h : newHeaders) { + response.addHeader(h); + } + } + + private void authenticationRequiredDidNotHaveAProxyAuthenticationHeader(HttpRequest request, HttpResponse response) throws ClientProtocolException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) return;