Skip to content

Commit

Permalink
Supports compression level setting
Browse files Browse the repository at this point in the history
  • Loading branch information
raccoonback committed Dec 29, 2024
1 parent a5edafd commit 94a3d1d
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
final class Http2StreamBridgeServerHandler extends ChannelDuplexHandler {

final BiPredicate<HttpServerRequest, HttpServerResponse> compress;
final int compressionLevel;
final ServerCookieDecoder cookieDecoder;
final ServerCookieEncoder cookieEncoder;
final HttpServerFormDecoderProvider formDecoderProvider;
Expand All @@ -84,6 +85,7 @@ final class Http2StreamBridgeServerHandler extends ChannelDuplexHandler {

Http2StreamBridgeServerHandler(
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compress,
int compressionLevel,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
HttpServerFormDecoderProvider formDecoderProvider,
Expand All @@ -94,6 +96,7 @@ final class Http2StreamBridgeServerHandler extends ChannelDuplexHandler {
@Nullable Duration readTimeout,
@Nullable Duration requestTimeout) {
this.compress = compress;
this.compressionLevel = compressionLevel;
this.cookieDecoder = decoder;
this.cookieEncoder = encoder;
this.formDecoderProvider = formDecoderProvider;
Expand Down Expand Up @@ -140,6 +143,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
listener,
request,
compress,
compressionLevel,
connectionInfo,
cookieDecoder,
cookieEncoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
final Function<String, String> methodTagValue;
final ChannelMetricsRecorder metricsRecorder;
final int minCompressionSize;
final int compressionLevel;
final ChannelOperations.OnSetup opsFactory;
final Duration readTimeout;
final Duration requestTimeout;
Expand All @@ -83,6 +84,7 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
@Nullable Function<String, String> methodTagValue,
@Nullable ChannelMetricsRecorder metricsRecorder,
int minCompressionSize,
int compressionLevel,
ChannelOperations.OnSetup opsFactory,
@Nullable Duration readTimeout,
@Nullable Duration requestTimeout,
Expand All @@ -101,6 +103,7 @@ final class Http3Codec extends ChannelInitializer<QuicStreamChannel> {
this.methodTagValue = methodTagValue;
this.metricsRecorder = metricsRecorder;
this.minCompressionSize = minCompressionSize;
this.compressionLevel = compressionLevel;
this.opsFactory = opsFactory;
this.readTimeout = readTimeout;
this.requestTimeout = requestTimeout;
Expand All @@ -118,13 +121,13 @@ protected void initChannel(QuicStreamChannel channel) {

p.addLast(NettyPipeline.H3ToHttp11Codec, new Http3FrameToHttpObjectCodec(true, validate))
.addLast(NettyPipeline.HttpTrafficHandler,
new Http3StreamBridgeServerHandler(compressPredicate, cookieDecoder, cookieEncoder, formDecoderProvider,
new Http3StreamBridgeServerHandler(compressPredicate, compressionLevel, cookieDecoder, cookieEncoder, formDecoderProvider,
forwardedHeaderHandler, httpMessageLogFactory, listener, mapHandle, readTimeout, requestTimeout));

boolean alwaysCompress = compressPredicate == null && minCompressionSize == 0;

if (alwaysCompress) {
p.addLast(NettyPipeline.CompressionHandler, new SimpleCompressionHandler());
p.addLast(NettyPipeline.CompressionHandler, SimpleCompressionHandler.create(compressionLevel));
}

ChannelOperations.addReactiveBridge(channel, opsFactory, listener);
Expand Down Expand Up @@ -166,14 +169,15 @@ static ChannelHandler newHttp3ServerConnectionHandler(
@Nullable Function<String, String> methodTagValue,
@Nullable ChannelMetricsRecorder metricsRecorder,
int minCompressionSize,
int compressionLevel,
ChannelOperations.OnSetup opsFactory,
@Nullable Duration readTimeout,
@Nullable Duration requestTimeout,
@Nullable Function<String, String> uriTagValue,
boolean validate) {
return new Http3ServerConnectionHandler(
new Http3Codec(accessLogEnabled, accessLog, compressPredicate, decoder, encoder, formDecoderProvider, forwardedHeaderHandler,
httpMessageLogFactory, listener, mapHandle, methodTagValue, metricsRecorder, minCompressionSize,
httpMessageLogFactory, listener, mapHandle, methodTagValue, metricsRecorder, minCompressionSize, compressionLevel,
opsFactory, readTimeout, requestTimeout, uriTagValue, validate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ final class Http3ServerOperations extends HttpServerOperations {
ConnectionObserver listener,
HttpRequest nettyRequest,
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate,
int compressionLevel,
ConnectionInfo connectionInfo,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
Expand All @@ -54,7 +55,7 @@ final class Http3ServerOperations extends HttpServerOperations {
@Nullable Duration requestTimeout,
boolean secured,
ZonedDateTime timestamp) {
super(c, listener, nettyRequest, compressionPredicate, connectionInfo, decoder, encoder, formDecoderProvider,
super(c, listener, nettyRequest, compressionPredicate, compressionLevel, connectionInfo, decoder, encoder, formDecoderProvider,
httpMessageLogFactory, isHttp2, mapHandle, readTimeout, requestTimeout, secured, timestamp, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

final class Http3StreamBridgeServerHandler extends ChannelDuplexHandler {
final BiPredicate<HttpServerRequest, HttpServerResponse> compress;
final int compressionLevel;
final ServerCookieDecoder cookieDecoder;
final ServerCookieEncoder cookieEncoder;
final HttpServerFormDecoderProvider formDecoderProvider;
Expand All @@ -74,6 +75,7 @@ final class Http3StreamBridgeServerHandler extends ChannelDuplexHandler {

Http3StreamBridgeServerHandler(
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compress,
int compressionLevel,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
HttpServerFormDecoderProvider formDecoderProvider,
Expand All @@ -84,6 +86,7 @@ final class Http3StreamBridgeServerHandler extends ChannelDuplexHandler {
@Nullable Duration readTimeout,
@Nullable Duration requestTimeout) {
this.compress = compress;
this.compressionLevel = compressionLevel;
this.cookieDecoder = decoder;
this.cookieEncoder = encoder;
this.formDecoderProvider = formDecoderProvider;
Expand Down Expand Up @@ -131,6 +134,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
listener,
request,
compress,
compressionLevel,
connectionInfo,
cookieDecoder,
cookieEncoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.internal.ObjectUtil;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import reactor.netty.Connection;
Expand Down Expand Up @@ -301,15 +302,35 @@ public final HttpServer compress(BiPredicate<HttpServerRequest, HttpServerRespon
/**
* Specifies whether GZip response compression is enabled if the client request
* presents accept encoding.
* default compression level is 6.
*
* @param compressionEnabled if true GZip response compression
* is enabled if the client request presents accept encoding, otherwise disabled.
* @return a new {@link HttpServer}
*/
public final HttpServer compress(boolean compressionEnabled) {
return compress(compressionEnabled, 6);
}

/**
* Enable GZip response compression if the client request presents accept encoding
* headers AND the response reaches a minimum threshold.
* And then, specify the compression level of the set compression rate and speed.
*
* @param compressionEnabled if true GZip response compression
* is enabled if the client request presents accept encoding, otherwise disabled.
* @param compressionLevel must be between 0 and 9.
* value in bytes
*
* @return a new {@link HttpServer}
*/
public final HttpServer compress(boolean compressionEnabled, int compressionLevel) {
ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");

HttpServer dup = duplicate();
if (compressionEnabled) {
dup.configuration().minCompressionSize = 0;
dup.configuration().compressionLevel = compressionLevel;
}
else {
dup.configuration().minCompressionSize = -1;
Expand Down
Loading

0 comments on commit 94a3d1d

Please sign in to comment.