diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java index f18c73cbe..e5a57cc48 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java @@ -111,8 +111,9 @@ && entryDateHeaderNewerThenResponse(entry, response)) { // Remove cache headers that match response for (final HeaderIterator it = response.headerIterator(); it.hasNext(); ) { final Header responseHeader = it.nextHeader(); - // Since we do not expect a content in a 304 response, should retain the original Content-Encoding header - if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName())) { + // Since we do not expect a content in a 304 response, should retain the original Content-Encoding and Content-Length header + if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName()) + || HTTP.CONTENT_LEN.equals(responseHeader.getName())) { continue; } final Header[] matchingHeaders = headerGroup.getHeaders(responseHeader.getName()); @@ -133,8 +134,9 @@ && entryDateHeaderNewerThenResponse(entry, response)) { } for (final HeaderIterator it = response.headerIterator(); it.hasNext(); ) { final Header responseHeader = it.nextHeader(); - // Since we do not expect a content in a 304 response, should avoid updating Content-Encoding header - if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName())) { + // Since we do not expect a content in a 304 response, should avoid updating Content-Encoding and Content-Length header + if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName()) + || HTTP.CONTENT_LEN.equals(responseHeader.getName())) { continue; } headerGroup.addHeader(responseHeader); diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java index a0a873742..0f0ea51c0 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java @@ -260,6 +260,27 @@ public void testContentEncodingHeaderIsNotUpdatedByMerge() throws IOException { headersNotContain(updatedHeaders, "Content-Encoding", "gzip"); } + @Test + public void testContentLengthIsNotAddedWhenTransferEncodingIsPresent() throws IOException { + final Header[] headers = { + new BasicHeader("Date", DateUtils.formatDate(requestDate)), + new BasicHeader("ETag", "\"etag\""), + new BasicHeader("Transfer-Encoding", "chunked")}; + + entry = HttpTestUtils.makeCacheEntry(headers); + response.setHeaders(new Header[]{ + new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)), + new BasicHeader("Cache-Control", "public"), + new BasicHeader("Content-Length", "0")}); + + final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry, + new Date(), new Date(), response); + + final Header[] updatedHeaders = updatedEntry.getAllHeaders(); + headersContain(updatedHeaders, "Transfer-Encoding", "chunked"); + headersNotContain(updatedHeaders, "Content-Length", "0"); + } + private void headersContain(final Header[] headers, final String name, final String value) { for (final Header header : headers) { if (header.getName().equals(name)) {