Polishing contribution

Closes gh-35225
This commit is contained in:
Brian Clozel
2025-08-25 19:12:24 +02:00
parent 4101714830
commit 18eb2a6073
5 changed files with 60 additions and 54 deletions
@@ -67,7 +67,7 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
private static final Set<String> DISALLOWED_HEADERS = disallowedHeaders();
private static final List<String> ALLOWED_ENCODINGS = List.of("gzip", "deflate");
private static final List<String> SUPPORTED_ENCODINGS = List.of("gzip", "deflate");
private final HttpClient httpClient;
@@ -80,18 +80,18 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
private final @Nullable Duration timeout;
private final boolean compressionEnabled;
private final boolean compression;
public JdkClientHttpRequest(HttpClient httpClient, URI uri, HttpMethod method, Executor executor,
@Nullable Duration readTimeout, boolean compressionEnabled) {
JdkClientHttpRequest(HttpClient httpClient, URI uri, HttpMethod method, Executor executor,
@Nullable Duration readTimeout, boolean compression) {
this.httpClient = httpClient;
this.uri = uri;
this.method = method;
this.executor = executor;
this.timeout = readTimeout;
this.compressionEnabled = compressionEnabled;
this.compression = compression;
}
@@ -164,13 +164,10 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
private HttpRequest buildRequest(HttpHeaders headers, @Nullable Body body) {
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(this.uri);
// When compression is enabled and valid encoding is absent, we add gzip as standard encoding
if (this.compressionEnabled) {
if (headers.containsHeader(HttpHeaders.ACCEPT_ENCODING) &&
!ALLOWED_ENCODINGS.contains(headers.getFirst(HttpHeaders.ACCEPT_ENCODING))) {
headers.remove(HttpHeaders.ACCEPT_ENCODING);
if (this.compression) {
if (!headers.containsHeader(HttpHeaders.ACCEPT_ENCODING)) {
headers.addAll(HttpHeaders.ACCEPT_ENCODING, SUPPORTED_ENCODINGS);
}
headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
}
headers.forEach((headerName, headerValues) -> {
@@ -310,16 +307,15 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
}
/**
* Custom BodyHandler that checks the Content-Encoding header and applies the appropriate decompression algorithm.
* BodyHandler that checks the Content-Encoding header and applies the appropriate decompression algorithm.
* Supports Gzip and Deflate encoded responses.
*/
public static final class DecompressingBodyHandler implements BodyHandler<InputStream> {
private static final class DecompressingBodyHandler implements BodyHandler<InputStream> {
@Override
public BodySubscriber<InputStream> apply(ResponseInfo responseInfo) {
String contentEncoding = responseInfo.headers().firstValue(HttpHeaders.CONTENT_ENCODING).orElse("");
if (contentEncoding.equalsIgnoreCase("gzip")) {
// If the content is gzipped, wrap the InputStream with a GZIPInputStream
return BodySubscribers.mapping(
BodySubscribers.ofInputStream(),
(InputStream is) -> {
@@ -327,18 +323,16 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
return new GZIPInputStream(is);
}
catch (IOException ex) {
throw new UncheckedIOException(ex); // Propagate IOExceptions
throw new UncheckedIOException(ex);
}
});
}
else if (contentEncoding.equalsIgnoreCase("deflate")) {
// If the content is encoded using deflate, wrap the InputStream with a InflaterInputStream
return BodySubscribers.mapping(
BodySubscribers.ofInputStream(),
InflaterInputStream::new);
}
else {
// Otherwise, return a standard InputStream BodySubscriber
return BodySubscribers.ofInputStream();
}
}
@@ -43,7 +43,7 @@ public class JdkClientHttpRequestFactory implements ClientHttpRequestFactory {
private @Nullable Duration readTimeout;
private boolean compressionEnabled;
private boolean compression = true;
/**
@@ -99,17 +99,17 @@ public class JdkClientHttpRequestFactory implements ClientHttpRequestFactory {
}
/**
* Sets custom {@link BodyHandler} that can handle gzip encoded {@link HttpClient}'s response.
* @param compressionEnabled to enable compression by default for all {@link HttpClient}'s requests.
* Set whether support for uncompressing "gzip" and "deflate" HTTP responses is enabled.
* @since 7.0
*/
public void setCompressionEnabled(boolean compressionEnabled) {
this.compressionEnabled = compressionEnabled;
public void enableCompression(boolean enable) {
this.compression = enable;
}
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return new JdkClientHttpRequest(this.httpClient, uri, httpMethod, this.executor, this.readTimeout, this.compressionEnabled);
return new JdkClientHttpRequest(this.httpClient, uri, httpMethod, this.executor, this.readTimeout, this.compression);
}
}