From b151df7e8ccd5dad31193eb8ad7a7f34e14efb07 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 1 May 2021 17:26:07 +0200 Subject: [PATCH] HTTPCLIENT-2157: response object generated by the classic caching backend is missing the original content encoding --- .../apache/hc/client5/http/impl/cache/CachingExec.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java index fa702e0ed..11983264a 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java @@ -51,6 +51,7 @@ import org.apache.hc.client5.http.utils.DateUtils; import org.apache.hc.core5.function.Factory; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpException; @@ -219,10 +220,13 @@ class CachingExec extends CachingExecBase implements ExecChainHandler { response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT); final SimpleBody body = cacheResponse.getBody(); if (body != null) { + final ContentType contentType = body.getContentType(); + final Header h = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING); + final String contentEncoding = h != null ? h.getValue() : null; if (body.isText()) { - response.setEntity(new StringEntity(body.getBodyText(), body.getContentType())); + response.setEntity(new StringEntity(body.getBodyText(), contentType, contentEncoding, false)); } else { - response.setEntity(new ByteArrayEntity(body.getBodyBytes(), body.getContentType())); + response.setEntity(new ByteArrayEntity(body.getBodyBytes(), contentType, contentEncoding, false)); } } scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);