From c72bf533fd0dcb508bbd4cc91a0d10cfa9e177a2 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Tue, 23 Apr 2024 10:49:27 +0300 Subject: [PATCH] Add compression for HTTP/3 server support Related to #1531 --- .../java/reactor/netty/http/server/Http3Codec.java | 12 +++++++++++- .../reactor/netty/http/server/HttpServerConfig.java | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) 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 aab2615f0e..95a3740f69 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 @@ -59,6 +59,7 @@ final class Http3Codec extends ChannelInitializer { final ConnectionObserver listener; final BiFunction, ? super Connection, ? extends Mono> mapHandle; + final int minCompressionSize; final ChannelOperations.OnSetup opsFactory; final Duration readTimeout; final Duration requestTimeout; @@ -75,6 +76,7 @@ final class Http3Codec extends ChannelInitializer { HttpMessageLogFactory httpMessageLogFactory, ConnectionObserver listener, @Nullable BiFunction, ? super Connection, ? extends Mono> mapHandle, + int minCompressionSize, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -89,6 +91,7 @@ final class Http3Codec extends ChannelInitializer { this.httpMessageLogFactory = httpMessageLogFactory; this.listener = listener; this.mapHandle = mapHandle; + this.minCompressionSize = minCompressionSize; this.opsFactory = opsFactory; this.readTimeout = readTimeout; this.requestTimeout = requestTimeout; @@ -108,6 +111,12 @@ protected void initChannel(QuicStreamChannel channel) { new Http3StreamBridgeServerHandler(compressPredicate, cookieDecoder, cookieEncoder, formDecoderProvider, forwardedHeaderHandler, httpMessageLogFactory, listener, mapHandle, readTimeout, requestTimeout)); + boolean alwaysCompress = compressPredicate == null && minCompressionSize == 0; + + if (alwaysCompress) { + p.addLast(NettyPipeline.CompressionHandler, new SimpleCompressionHandler()); + } + ChannelOperations.addReactiveBridge(channel, opsFactory, listener); channel.pipeline().remove(this); @@ -128,12 +137,13 @@ static ChannelHandler newHttp3ServerConnectionHandler( HttpMessageLogFactory httpMessageLogFactory, ConnectionObserver listener, @Nullable BiFunction, ? super Connection, ? extends Mono> mapHandle, + int minCompressionSize, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, boolean validate) { return new Http3ServerConnectionHandler( new Http3Codec(accessLogEnabled, accessLog, compressPredicate, decoder, encoder, formDecoderProvider, forwardedHeaderHandler, - httpMessageLogFactory, listener, mapHandle, opsFactory, readTimeout, requestTimeout, validate)); + httpMessageLogFactory, listener, mapHandle, minCompressionSize, opsFactory, readTimeout, requestTimeout, validate)); } } \ No newline at end of file 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 8e2a5017a8..7511f8ae5e 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 @@ -600,6 +600,7 @@ static void configureHttp3Pipeline( HttpMessageLogFactory httpMessageLogFactory, ConnectionObserver listener, @Nullable BiFunction, ? super Connection, ? extends Mono> mapHandle, + int minCompressionSize, ChannelOperations.OnSetup opsFactory, @Nullable Duration readTimeout, @Nullable Duration requestTimeout, @@ -608,7 +609,7 @@ static void configureHttp3Pipeline( p.addLast(NettyPipeline.HttpCodec, newHttp3ServerConnectionHandler(accessLogEnabled, accessLog, compressPredicate, cookieDecoder, cookieEncoder, formDecoderProvider, forwardedHeaderHandler, httpMessageLogFactory, - listener, mapHandle, opsFactory, readTimeout, requestTimeout, validate)); + listener, mapHandle, minCompressionSize, opsFactory, readTimeout, requestTimeout, validate)); } static void configureH2Pipeline(ChannelPipeline p, @@ -1407,6 +1408,7 @@ else if ((protocols & h3) == h3) { httpMessageLogFactory, observer, mapHandle, + minCompressionSize, opsFactory, readTimeout, requestTimeout,