Skip to content
Open
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 @@ -113,7 +113,7 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body
TimeoutHandler timeoutHandler = null;
try {
HttpRequest request = buildRequest(headers, body);
responseFuture = this.httpClient.sendAsync(request, this.compression ? new DecompressingBodyHandler() : HttpResponse.BodyHandlers.ofInputStream());
responseFuture = this.httpClient.sendAsync(request, this.compression ? new DecompressingBodyHandler(this.method) : HttpResponse.BodyHandlers.ofInputStream());
if (this.timeout != null) {
timeoutHandler = new TimeoutHandler(responseFuture, this.timeout);
HttpResponse<InputStream> response = responseFuture.get();
Expand Down Expand Up @@ -325,13 +325,37 @@ public void handleCancellationException(CancellationException ex) throws HttpTim
*/
private static final class DecompressingBodyHandler implements BodyHandler<InputStream> {

private final HttpMethod method;

private DecompressingBodyHandler(HttpMethod method) {
this.method = method;
}

@Override
public BodySubscriber<InputStream> apply(ResponseInfo responseInfo) {
String contentEncoding = responseInfo.headers().firstValue(HttpHeaders.CONTENT_ENCODING).orElse("");

String contentEncoding = responseInfo.headers()
.firstValue(HttpHeaders.CONTENT_ENCODING)
.orElse("");

// Skip gzip/deflate if HEAD request (HEAD has no body)
if (this.method == HttpMethod.HEAD) {
return BodySubscribers.replacing(InputStream.nullInputStream());
}

// Skip if Content-Length = 0 (empty body)
String contentLength = responseInfo.headers()
.firstValue(HttpHeaders.CONTENT_LENGTH)
.orElse(null);

if ("0".equals(contentLength)) {
return BodySubscribers.replacing(InputStream.nullInputStream());
}

if (contentEncoding.equalsIgnoreCase("gzip")) {
return BodySubscribers.mapping(
BodySubscribers.ofInputStream(),
(InputStream is) -> {
is -> {
try {
return new GZIPInputStream(is);
}
Expand All @@ -340,15 +364,16 @@ public BodySubscriber<InputStream> apply(ResponseInfo responseInfo) {
}
});
}
else if (contentEncoding.equalsIgnoreCase("deflate")) {

if (contentEncoding.equalsIgnoreCase("deflate")) {
return BodySubscribers.mapping(
BodySubscribers.ofInputStream(),
InflaterInputStream::new);
}
else {
return BodySubscribers.ofInputStream();
}

return BodySubscribers.ofInputStream();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.InetSocketAddress;

import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.web.client.RestClient;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;



/**
* Unit tests for {@link JdkClientHttpRequest}.
Expand Down Expand Up @@ -74,5 +82,42 @@ void futureCancelled() {
private JdkClientHttpRequest createRequest(Duration timeout) {
return new JdkClientHttpRequest(client, URI.create("https://abc.com"), HttpMethod.GET, executor, timeout, false);
}
@Test
void headRequestWithGzipContentEncodingShouldNotFail() throws Exception {
com.sun.net.httpserver.HttpServer server =
com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);

server.createContext("/test", exchange -> {
// Simulate HEAD-like response: gzip header + no body
exchange.getResponseHeaders().add("Content-Encoding", "gzip");
exchange.getResponseHeaders().add("Content-Length", "0");
exchange.sendResponseHeaders(200, 0); // no body
exchange.close();
});

server.start();
int port = server.getAddress().getPort();

try {
RestClient client = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory())
.build();

// The original bug: this line used to blow up with gzip + empty body.
assertDoesNotThrow(() ->
client.head()
.uri("http://localhost:" + port + "/test")
.retrieve()
.toBodilessEntity()
);
}
finally {
server.stop(0);
}
}





}