Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<Response> implements ResponseListener {
@Override
public void onSuccess(Response response) {
Expand Down
Loading