in httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java [137:179]
public ClassicHttpResponse execute(
final ClassicHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws IOException, HttpException {
Args.notNull(request, "HTTP request");
Args.notNull(scope, "Scope");
final HttpClientContext clientContext = scope.clientContext;
final RequestConfig requestConfig = clientContext.getRequestConfig();
/* Signal support for Accept-Encoding transfer encodings. */
if (!request.containsHeader(HttpHeaders.ACCEPT_ENCODING) && requestConfig.isContentCompressionEnabled()) {
request.addHeader(acceptEncoding);
}
final ClassicHttpResponse response = chain.proceed(request, scope);
final HttpEntity entity = response.getEntity();
// entity can be null in case of 304 Not Modified, 204 No Content or similar
// check for zero length entity.
if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) {
final String contentEncoding = entity.getContentEncoding();
if (contentEncoding != null) {
final ParserCursor cursor = new ParserCursor(0, contentEncoding.length());
final HeaderElement[] codecs = BasicHeaderValueParser.INSTANCE.parseElements(contentEncoding, cursor);
for (final HeaderElement codec : codecs) {
final String codecname = codec.getName().toLowerCase(Locale.ROOT);
final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname);
if (decoderFactory != null) {
response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
response.removeHeaders(HttpHeaders.CONTENT_ENCODING);
response.removeHeaders(HttpHeaders.CONTENT_MD5);
} else {
if (!"identity".equals(codecname) && !ignoreUnknown) {
throw new HttpException("Unsupported Content-Encoding: " + codec.getName());
}
}
}
}
}
return response;
}