From e6ca403d29df79ca5e83d0a256d87d5360cf48f0 Mon Sep 17 00:00:00 2001 From: "Raymond, Jayson" Date: Wed, 6 Feb 2019 07:21:09 -0800 Subject: [PATCH] Some well known proxies respond with Content-Length=0, when returning 304. For robustness, always use the cached entity's content length, as modern browsers do. --- .../http/impl/cache/CachedHttpResponseGenerator.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java index 24336b374..e291f6e0a 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java @@ -145,10 +145,9 @@ class CachedHttpResponseGenerator { if (transferEncodingIsPresent(response)) { return; } - final Header contentLength = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); - if (contentLength == null) { - response.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(body.length)); - } + // Some well known proxies respond with Content-Length=0, when returning 304. For robustness, always + // use the cached entity's content length, as modern browsers do. + response.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(body.length)); } private boolean transferEncodingIsPresent(final HttpResponse response) {