From 7eee9c9b663723f9dd9d0abb80585e743ae5fc1a Mon Sep 17 00:00:00 2001 From: raccoonback Date: Thu, 9 Jan 2025 21:52:58 +0900 Subject: [PATCH] Set compressionOptions to null as the default state when compression is disabled - Updated compressionOptions to default to null when compression is not explicitly configured. - Ensures a clean default state, reflecting that no compression settings are applied unless enabled. --- .../server/Http2StreamBridgeServerHandler.java | 2 +- .../reactor/netty/http/server/Http3Codec.java | 4 ++-- .../http/server/Http3ServerOperations.java | 2 +- .../server/Http3StreamBridgeServerHandler.java | 2 +- .../netty/http/server/HttpServerConfig.java | 16 ++++++++-------- .../netty/http/server/HttpServerOperations.java | 4 ++-- .../netty/http/server/HttpTrafficHandler.java | 2 +- .../http/server/SimpleCompressionHandler.java | 17 +++++++++++++---- .../compression/HttpCompressionOptionsSpec.java | 4 ---- .../netty/http/server/HttpServerTests.java | 5 ++--- 10 files changed, 31 insertions(+), 27 deletions(-) diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http2StreamBridgeServerHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http2StreamBridgeServerHandler.java index 3bc110e625..9ea5eba9cb 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http2StreamBridgeServerHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http2StreamBridgeServerHandler.java @@ -86,7 +86,7 @@ final class Http2StreamBridgeServerHandler extends ChannelDuplexHandler { Http2StreamBridgeServerHandler( @Nullable BiPredicate compress, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ServerCookieDecoder decoder, ServerCookieEncoder encoder, HttpServerFormDecoderProvider formDecoderProvider, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3Codec.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3Codec.java index 1c72cceadd..450ed85194 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3Codec.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3Codec.java @@ -85,7 +85,7 @@ final class Http3Codec extends ChannelInitializer { @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -170,7 +170,7 @@ static ChannelHandler newHttp3ServerConnectionHandler( @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3ServerOperations.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3ServerOperations.java index 419501154c..695b265c18 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3ServerOperations.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3ServerOperations.java @@ -44,7 +44,7 @@ final class Http3ServerOperations extends HttpServerOperations { ConnectionObserver listener, HttpRequest nettyRequest, @Nullable BiPredicate compressionPredicate, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ConnectionInfo connectionInfo, ServerCookieDecoder decoder, ServerCookieEncoder encoder, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3StreamBridgeServerHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3StreamBridgeServerHandler.java index 7dc4dda650..0066a824a4 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3StreamBridgeServerHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/Http3StreamBridgeServerHandler.java @@ -76,7 +76,7 @@ final class Http3StreamBridgeServerHandler extends ChannelDuplexHandler { Http3StreamBridgeServerHandler( @Nullable BiPredicate compress, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ServerCookieDecoder decoder, ServerCookieEncoder encoder, HttpServerFormDecoderProvider formDecoderProvider, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerConfig.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerConfig.java index acac358b0f..7f098f6d1b 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerConfig.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerConfig.java @@ -346,7 +346,7 @@ public Function uriTagValue() { this.httpMessageLogFactory = ReactorNettyHttpMessageLogFactory.INSTANCE; this.maxKeepAliveRequests = -1; this.minCompressionSize = -1; - this.compressionOptions = HttpCompressionOptionsSpec.provideDefault(); + this.compressionOptions = null; this.protocols = new HttpProtocol[]{HttpProtocol.HTTP11}; this._protocols = h11; this.proxyProtocolSupportType = ProxyProtocolSupportType.OFF; @@ -496,7 +496,7 @@ static void addStreamHandlers(Channel ch, @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -610,7 +610,7 @@ static void configureHttp3Pipeline( @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -646,7 +646,7 @@ static void configureH2Pipeline(ChannelPipeline p, @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -717,7 +717,7 @@ static void configureHttp11OrH2CleartextPipeline(ChannelPipeline p, @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -806,7 +806,7 @@ static void configureHttp11Pipeline(ChannelPipeline p, @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @Nullable Function uriTagValue) { @@ -1037,7 +1037,7 @@ static final class H2Codec extends ChannelInitializer { @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -1113,7 +1113,7 @@ static final class Http11OrH2CleartextCodec extends ChannelInitializer @Nullable Function methodTagValue, @Nullable ChannelMetricsRecorder metricsRecorder, int minCompressionSize, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerOperations.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerOperations.java index 103b952111..ce30b1c683 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerOperations.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerOperations.java @@ -180,7 +180,7 @@ class HttpServerOperations extends HttpOperations compressionPredicate, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ConnectionInfo connectionInfo, ServerCookieDecoder decoder, ServerCookieEncoder encoder, @@ -1325,7 +1325,7 @@ static final class FailedHttpServerRequest extends HttpServerOperations { ZonedDateTime timestamp, ConnectionInfo connectionInfo, boolean validateHeaders) { - super(c, listener, nettyRequest, null, HttpCompressionOptionsSpec.provideDefault(), connectionInfo, + super(c, listener, nettyRequest, null, null, connectionInfo, ServerCookieDecoder.STRICT, ServerCookieEncoder.STRICT, DEFAULT_FORM_DECODER_SPEC, httpMessageLogFactory, isHttp2, null, null, null, secure, timestamp, validateHeaders); this.customResponse = nettyResponse; diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpTrafficHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpTrafficHandler.java index 03cd7889e5..e307655123 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpTrafficHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/HttpTrafficHandler.java @@ -114,7 +114,7 @@ final class HttpTrafficHandler extends ChannelDuplexHandler implements Runnable HttpTrafficHandler( @Nullable BiPredicate compress, - HttpCompressionOptionsSpec compressionOptions, + @Nullable HttpCompressionOptionsSpec compressionOptions, ServerCookieDecoder decoder, ServerCookieEncoder encoder, HttpServerFormDecoderProvider formDecoderProvider, diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/SimpleCompressionHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/SimpleCompressionHandler.java index 79b46b6b27..bc3f50b5bc 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/SimpleCompressionHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/SimpleCompressionHandler.java @@ -15,9 +15,6 @@ */ package reactor.netty.http.server; -import java.util.ArrayList; -import java.util.List; - import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -32,6 +29,10 @@ import io.netty.handler.codec.http.LastHttpContent; import io.netty.util.ReferenceCountUtil; import reactor.netty.http.server.compression.HttpCompressionOptionsSpec; +import reactor.util.annotation.Nullable; + +import java.util.ArrayList; +import java.util.List; /** * {@link HttpContentCompressor} to enable on-demand compression. @@ -43,11 +44,19 @@ final class SimpleCompressionHandler extends HttpContentCompressor { boolean decoded; HttpRequest request; + private SimpleCompressionHandler() { + super((CompressionOptions[]) null); + } + private SimpleCompressionHandler(CompressionOptions... options) { super(options); } - static SimpleCompressionHandler create(HttpCompressionOptionsSpec compressionOptions) { + static SimpleCompressionHandler create(@Nullable HttpCompressionOptionsSpec compressionOptions) { + if (compressionOptions == null) { + return new SimpleCompressionHandler(); + } + return new SimpleCompressionHandler( compressionOptions.adapt() ); diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/compression/HttpCompressionOptionsSpec.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/compression/HttpCompressionOptionsSpec.java index 56da24e6b9..dcf8f76d13 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/compression/HttpCompressionOptionsSpec.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/compression/HttpCompressionOptionsSpec.java @@ -74,10 +74,6 @@ else if (Zstd.isAvailable() && option instanceof ZstdOption) { } } - public static HttpCompressionOptionsSpec provideDefault() { - return new HttpCompressionOptionsSpec(); - } - public CompressionOptions[] adapt() { List options = new ArrayList<>( Arrays.asList( diff --git a/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java b/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java index 57a5ccff1a..d3ee56ad04 100644 --- a/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java +++ b/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java @@ -145,7 +145,6 @@ import reactor.netty.http.client.HttpClientRequest; import reactor.netty.http.client.PrematureCloseException; import reactor.netty.http.logging.ReactorNettyHttpMessageLogFactory; -import reactor.netty.http.server.compression.HttpCompressionOptionsSpec; import reactor.netty.resources.ConnectionProvider; import reactor.netty.resources.LoopResources; import reactor.netty.tcp.SslProvider; @@ -2215,7 +2214,7 @@ private void doTestStatus(HttpResponseStatus status) { ConnectionObserver.emptyListener(), new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"), null, - HttpCompressionOptionsSpec.provideDefault(), + null, new ConnectionInfo(localSocketAddress, DEFAULT_HOST_NAME, DEFAULT_HTTP_PORT, remoteSocketAddress, "http", true), ServerCookieDecoder.STRICT, ServerCookieEncoder.STRICT, @@ -3274,7 +3273,7 @@ private void doTestIsFormUrlencoded(String headerValue, boolean expectation) { ConnectionObserver.emptyListener(), request, null, - HttpCompressionOptionsSpec.provideDefault(), + null, new ConnectionInfo(localSocketAddress, DEFAULT_HOST_NAME, DEFAULT_HTTP_PORT, remoteSocketAddress, "http", true), ServerCookieDecoder.STRICT, ServerCookieEncoder.STRICT,