From e9864b06ffa48823c753bf6526ae9fba57bded62 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Wed, 23 Mar 2011 14:48:24 +0000 Subject: [PATCH] HTTPCLIENT-1073: if caching module strips off a body for an origin response that should not contain one, it should consume the body off the connection as well to ensure resources are released. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1084607 13f79535-47bb-0310-9956-ffa450edef68 --- .../cache/ResponseProtocolCompliance.java | 9 +- .../cache/TestResponseProtocolCompliance.java | 82 +++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java index 8c72fbdfd..08d9a28d4 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseProtocolCompliance.java @@ -26,12 +26,14 @@ */ package org.apache.http.impl.client.cache; +import java.io.IOException; 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.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; @@ -46,6 +48,7 @@ import org.apache.http.impl.cookie.DateParseException; import org.apache.http.impl.cookie.DateUtils; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; /** * @since 4.1 @@ -60,11 +63,13 @@ class ResponseProtocolCompliance { * * @param request The {@link HttpRequest} that generated an origin hit and response * @param response The {@link HttpResponse} from the origin server - * @throws ClientProtocolException when we are unable to 'convert' the response to a compliant one + * @throws IOException */ public void ensureProtocolCompliance(HttpRequest request, HttpResponse response) - throws ClientProtocolException { + throws IOException { if (backendResponseMustNotHaveBody(request, response)) { + HttpEntity body = response.getEntity(); + if (body != null) EntityUtils.consume(body); response.setEntity(null); } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java new file mode 100644 index 000000000..6326963bf --- /dev/null +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseProtocolCompliance.java @@ -0,0 +1,82 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.http.impl.client.cache; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.util.Date; + +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.HttpVersion; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.entity.InputStreamEntity; +import org.apache.http.impl.cookie.DateUtils; +import org.apache.http.message.BasicHttpResponse; +import org.junit.Before; +import org.junit.Test; + + +public class TestResponseProtocolCompliance { + + private ResponseProtocolCompliance impl; + + @Before + public void setUp() { + impl = new ResponseProtocolCompliance(); + } + + @Test + public void consumesBodyIfOriginSendsOneInResponseToHEAD() throws Exception { + HttpRequest req = new HttpHead("http://foo.example.com/"); + HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); + resp.setHeader("Date", DateUtils.formatDate(new Date())); + resp.setHeader("Server", "MyServer/1.0"); + + int nbytes = 128; + resp.setHeader("Content-Length","" + nbytes); + byte[] buf = HttpTestUtils.getRandomBytes(nbytes); + final Flag closed = new Flag(); + ByteArrayInputStream bais = new ByteArrayInputStream(buf) { + @Override + public void close() { + closed.set = true; + } + }; + resp.setEntity(new InputStreamEntity(bais, -1)); + + impl.ensureProtocolCompliance(req, resp); + assertNull(resp.getEntity()); + assertTrue(closed.set || bais.read() == -1); + } + + private static class Flag { + public boolean set; + } +}