### Eclipse Workspace Patch 1.0 #P httpcomponents-client Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java (revision 1080417) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java (working copy) @@ -36,12 +36,10 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ProtocolVersion; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.cookie.DateUtils; import org.apache.http.message.BasicHttpEntityEnclosingRequest; -import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpResponse; import org.apache.http.protocol.HttpContext; import org.easymock.Capture; @@ -78,7 +76,6 @@ private HttpEntity mockEntity; private HttpClient mockBackend; private HttpCache mockCache; - private HttpRequest request; private HttpResponse originResponse; private CachingHttpClient impl; @@ -89,8 +86,6 @@ body = makeBody(entityLength); - request = new BasicHttpRequest("GET", "/foo", HTTP_1_1); - originResponse = make200Response(); CacheConfig params = new CacheConfig(); @@ -265,128 +260,4 @@ Assert.assertNotNull(reqBody.getContentType()); } - /* - * "10.2.7 206 Partial Content ... The request MUST have included a Range - * header field (section 14.35) indicating the desired range, and MAY have - * included an If-Range header field (section 14.27) to make the request - * conditional." - * - * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7 - */ - @Test - public void testPartialContentIsNotReturnedToAClientThatDidNotAskForIt() throws Exception { - - // tester's note: I don't know what the cache will *do* in - // this situation, but it better not just pass the response - // on. - request.removeHeaders("Range"); - originResponse = new BasicHttpResponse(HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT, - "Partial Content"); - originResponse.setHeader("Content-Range", "bytes 0-499/1234"); - originResponse.setEntity(makeBody(500)); - - org.easymock.EasyMock.expect( - mockBackend.execute(org.easymock.EasyMock.isA(HttpHost.class), - org.easymock.EasyMock.isA(HttpRequest.class), - (HttpContext) org.easymock.EasyMock.isNull())).andReturn(originResponse); - - replayMocks(); - try { - HttpResponse result = impl.execute(host, request); - Assert.assertTrue(HttpStatus.SC_PARTIAL_CONTENT != result.getStatusLine() - .getStatusCode()); - } catch (ClientProtocolException acceptableBehavior) { - // this is probably ok - } - } - - /* - * "10.4.2 401 Unauthorized ... The response MUST include a WWW-Authenticate - * header field (section 14.47) containing a challenge applicable to the - * requested resource." - * - * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 - */ - @Test(expected = ClientProtocolException.class) - public void testCantReturnOrigin401ResponseWithoutWWWAuthenticateHeader() throws Exception { - - originResponse = new BasicHttpResponse(HTTP_1_1, 401, "Unauthorized"); - - org.easymock.EasyMock.expect( - mockBackend.execute(org.easymock.EasyMock.isA(HttpHost.class), - org.easymock.EasyMock.isA(HttpRequest.class), - (HttpContext) org.easymock.EasyMock.isNull())).andReturn(originResponse); - replayMocks(); - - // this is another case where we are caught in a sticky - // situation, where the origin was not 1.1-compliant. - try { - impl.execute(host, request); - } catch (ClientProtocolException possiblyAcceptableBehavior) { - verifyMocks(); - throw possiblyAcceptableBehavior; - } - } - - /* - * "10.4.6 405 Method Not Allowed ... The response MUST include an Allow - * header containing a list of valid methods for the requested resource. - * - * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 - */ - @Test(expected = ClientProtocolException.class) - public void testCantReturnAnOrigin405WithoutAllowHeader() throws Exception { - originResponse = new BasicHttpResponse(HTTP_1_1, 405, "Method Not Allowed"); - - org.easymock.EasyMock.expect( - mockBackend.execute(org.easymock.EasyMock.isA(HttpHost.class), - org.easymock.EasyMock.isA(HttpRequest.class), - (HttpContext) org.easymock.EasyMock.isNull())).andReturn(originResponse); - replayMocks(); - - // this is another case where we are caught in a sticky - // situation, where the origin was not 1.1-compliant. - try { - impl.execute(host, request); - } catch (ClientProtocolException possiblyAcceptableBehavior) { - verifyMocks(); - throw possiblyAcceptableBehavior; - } - } - - /* - * "10.4.8 407 Proxy Authentication Required ... The proxy MUST return a - * Proxy-Authenticate header field (section 14.33) containing a challenge - * applicable to the proxy for the requested resource." - * - * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8 - */ - @Test - public void testCantReturnA407WithoutAProxyAuthenticateHeader() throws Exception { - originResponse = new BasicHttpResponse(HTTP_1_1, 407, "Proxy Authentication Required"); - - org.easymock.EasyMock.expect( - mockBackend.execute(org.easymock.EasyMock.isA(HttpHost.class), - org.easymock.EasyMock.isA(HttpRequest.class), - (HttpContext) org.easymock.EasyMock.isNull())).andReturn(originResponse); - replayMocks(); - - boolean gotException = false; - // this is another case where we are caught in a sticky - // situation, where the origin was not 1.1-compliant. - try { - HttpResponse result = impl.execute(host, request); - Assert.fail("should have gotten ClientProtocolException"); - - if (result.getStatusLine().getStatusCode() == 407) { - Assert.assertNotNull(result.getFirstHeader("Proxy-Authentication")); - } - } catch (ClientProtocolException possiblyAcceptableBehavior) { - gotException = true; - } - - verifyMocks(); - Assert.assertTrue(gotException); - } - } \ No newline at end of file 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 1080417) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java (working copy) @@ -681,41 +681,6 @@ } /* - * "A proxy MUST NOT forward a 100 (Continue) response if the request - * message was received from an HTTP/1.0 (or earlier) client and did not - * include an Expect request-header field with the '100-continue' - * expectation. This requirement overrides the general rule for forwarding - * of 1xx responses (see section 10.1)." - * - * http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 - */ - @Test - public void test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForThem() - throws Exception { - - BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest("POST", "/", - new ProtocolVersion("HTTP", 1, 0)); - post.setEntity(body); - post.setHeader("Content-Length", "128"); - - originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue"); - EasyMock.expect( - mockBackend.execute(EasyMock.eq(host), EasyMock.isA(HttpRequest.class), - (HttpContext) EasyMock.isNull())).andReturn(originResponse); - replayMocks(); - - try { - // if a 100 response gets up to us from the HttpClient - // backend, we can't really handle it at that point - impl.execute(host, post); - Assert.fail("should have thrown an exception"); - } catch (ClientProtocolException expected) { - } - - verifyMocks(); - } - - /* * "9.2 OPTIONS. ...Responses to this method are not cacheable. * * http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2 Index: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java =================================================================== --- httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java (revision 1080575) +++ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java (working copy) @@ -843,7 +843,12 @@ HttpResponse backendResponse) throws IOException { log.debug("Handling Backend response"); - responseCompliance.ensureProtocolCompliance(request, backendResponse); + try { + responseCompliance.ensureProtocolCompliance(request, backendResponse); + } catch (ClientProtocolException cpe) { + responseCache.flushCacheEntriesFor(target, request); + return backendResponse; + } boolean cacheable = responseCachingPolicy.isResponseCacheable(request, backendResponse); responseCache.flushInvalidatedCacheEntriesFor(target, request, backendResponse);