Skip to content

Commit de9b5d4

Browse files
fix gzip handling header bug, memory opt (#1215) (#1216)
Co-authored-by: Laura Trotta <153528055+l-trotta@users.noreply.github.com>
1 parent f1d05af commit de9b5d4

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void data(final ByteBuffer src, final boolean endOfStream) throws Cont
7575

7676
@Override
7777
protected final void completed() throws IOException {
78-
result = new ByteArrayEntity(buffer.toByteArray(), contentType, contentEncoding);
78+
result = new ByteArrayEntity(buffer.array(), 0, buffer.length(), contentType, contentEncoding);
7979
if (resultCallback != null) {
8080
resultCallback.completed(result);
8181
}

rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod
343343
httpResponse.setEntity(new GzipDecompressingEntity(entity));
344344
httpResponse.removeHeaders(CONTENT_ENCODING);
345345
httpResponse.removeHeaders(CONTENT_LENGTH);
346+
} else if ("gzip".equals(httpResponse.getFirstHeader(CONTENT_ENCODING) != null
347+
? httpResponse.getFirstHeader(CONTENT_ENCODING).getValue() : null)) {
348+
// HC5 5.6+ auto-decompressed the entity, but left the headers as they were,
349+
// meaning content encoding will still be gzip, fixing that.
350+
httpResponse.removeHeaders(CONTENT_ENCODING);
351+
httpResponse.removeHeaders(CONTENT_LENGTH);
352+
if (entity.getContentLength() >= 0) {
353+
httpResponse.setHeader(CONTENT_LENGTH, Long.toString(entity.getContentLength()));
354+
}
346355
}
347356
}
348357

rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void startHttpServer() throws Exception {
5959
}
6060

6161
@AfterAll
62-
public static void stopHttpServers() throws IOException {
62+
public static void stopHttpServers() {
6363
httpServer.stop(0);
6464
httpServer = null;
6565
}
@@ -231,6 +231,31 @@ public void testCompressingClientAsync() throws Exception {
231231
restClient.close();
232232
}
233233

234+
/**
235+
* Verifies that when HC5's built-in auto-decompression is active (the default since 5.6),
236+
* response headers are reconciled with the already-decompressed entity, meaning the content encoding is
237+
* not gzip.
238+
*/
239+
@Test
240+
public void testAutoDecompressionAsync() throws Exception {
241+
InetSocketAddress address = httpServer.getAddress();
242+
Rest5Client restClient = Rest5Client.builder(new HttpHost("http", address.getHostString(),
243+
address.getPort()))
244+
.setCompressionEnabled(true)
245+
.build();
246+
247+
Request request = new Request("POST", "/");
248+
request.setEntity(new StringEntity("auto-decompress client", ContentType.TEXT_PLAIN));
249+
250+
FutureResponse futureResponse = new FutureResponse();
251+
restClient.performRequestAsync(request, futureResponse);
252+
Response response = futureResponse.get();
253+
254+
checkResponse("gzip#gzip#auto-decompress client", response);
255+
256+
restClient.close();
257+
}
258+
234259
public static class FutureResponse extends CompletableFuture<Response> implements ResponseListener {
235260
@Override
236261
public void onSuccess(Response response) {

0 commit comments

Comments
 (0)