diff --git a/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java b/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java index a2a01c883f..0b8e9cc7b8 100644 --- a/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java +++ b/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java @@ -75,7 +75,7 @@ protected void data(final ByteBuffer src, final boolean endOfStream) throws Cont @Override protected final void completed() throws IOException { - result = new ByteArrayEntity(buffer.toByteArray(), contentType, contentEncoding); + result = new ByteArrayEntity(buffer.array(), 0, buffer.length(), contentType, contentEncoding); if (resultCallback != null) { resultCallback.completed(result); } diff --git a/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java b/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java index f14834532b..151ed2f9cb 100644 --- a/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java +++ b/rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java @@ -343,6 +343,15 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod httpResponse.setEntity(new GzipDecompressingEntity(entity)); httpResponse.removeHeaders(CONTENT_ENCODING); httpResponse.removeHeaders(CONTENT_LENGTH); + } else if ("gzip".equals(httpResponse.getFirstHeader(CONTENT_ENCODING) != null + ? httpResponse.getFirstHeader(CONTENT_ENCODING).getValue() : null)) { + // HC5 5.6+ auto-decompressed the entity, but left the headers as they were, + // meaning content encoding will still be gzip, fixing that. + httpResponse.removeHeaders(CONTENT_ENCODING); + httpResponse.removeHeaders(CONTENT_LENGTH); + if (entity.getContentLength() >= 0) { + httpResponse.setHeader(CONTENT_LENGTH, Long.toString(entity.getContentLength())); + } } } diff --git a/rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java b/rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java index 9eb56f5c78..df581427f6 100644 --- a/rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java +++ b/rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java @@ -59,7 +59,7 @@ public static void startHttpServer() throws Exception { } @AfterAll - public static void stopHttpServers() throws IOException { + public static void stopHttpServers() { httpServer.stop(0); httpServer = null; } @@ -231,6 +231,31 @@ public void testCompressingClientAsync() throws Exception { restClient.close(); } + /** + * Verifies that when HC5's built-in auto-decompression is active (the default since 5.6), + * response headers are reconciled with the already-decompressed entity, meaning the content encoding is + * not gzip. + */ + @Test + public void testAutoDecompressionAsync() throws Exception { + InetSocketAddress address = httpServer.getAddress(); + Rest5Client restClient = Rest5Client.builder(new HttpHost("http", address.getHostString(), + address.getPort())) + .setCompressionEnabled(true) + .build(); + + Request request = new Request("POST", "/"); + request.setEntity(new StringEntity("auto-decompress client", ContentType.TEXT_PLAIN)); + + FutureResponse futureResponse = new FutureResponse(); + restClient.performRequestAsync(request, futureResponse); + Response response = futureResponse.get(); + + checkResponse("gzip#gzip#auto-decompress client", response); + + restClient.close(); + } + public static class FutureResponse extends CompletableFuture implements ResponseListener { @Override public void onSuccess(Response response) {