Skip to content

Commit

Permalink
Merge #3567 into 2.0.0-M4
Browse files Browse the repository at this point in the history
Signed-off-by: Violeta Georgieva <[email protected]>
  • Loading branch information
violetagg committed Jan 28, 2025
2 parents b94b2e2 + f22a2bd commit 4f2c12a
Show file tree
Hide file tree
Showing 16 changed files with 796 additions and 23 deletions.
22 changes: 22 additions & 0 deletions docs/modules/ROOT/pages/http-server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ You can configure the `HTTP` server to send a compressed response, depending on
* `compress(int)`: The compression is performed once the response size exceeds the given value (in bytes).
* `compress(BiPredicate<HttpServerRequest, HttpServerResponse>)`: The compression is performed if
the predicate returns `true`.
* `compressOptions(HttpCompressionOption... compressionOptions)`: Specifies the compression options for GZip, Deflate, and ZSTD.
+
[NOTE]
====
GZip Compression Options
* compression level : only the range 0 to 9 is allowed. (default: 6)
* window bits : only the range 0 to 9 is allowed. (default: 15)
* memory level : only the range 1 to 9 is allowed. (default: 8)
Deflate Compression Options
* compression level : only the range 0 to 9 is allowed. (default: 6)
* window bits : only the range 0 to 9 is allowed. (default: 15)
* memory level : only the range 1 to 9 is allowed. (default: 8)
ZSTD Compression Options
* compression level : only the range -131072 to 9 is allowed. (default: 3)
* block size : only the positive number is allowed. (default: 65536, that is 64KB)
* max encode size : only the positive number is allowed. (default: 33554432, that is 32MB)
====

The following example uses the `compress` method (set to `true`) to enable compression:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2024 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2018-2025 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@
import reactor.netty5.ReactorNetty;
import reactor.netty5.http.logging.HttpMessageArgProviderFactory;
import reactor.netty5.http.logging.HttpMessageLogFactory;
import reactor.netty5.http.server.compression.HttpCompressionOptionsSpec;
import reactor.util.annotation.Nullable;

import static io.netty5.handler.codec.http.HttpResponseStatus.CONTINUE;
Expand All @@ -60,6 +61,7 @@
final class Http2StreamBridgeServerHandler extends ChannelHandlerAdapter {

final BiPredicate<HttpServerRequest, HttpServerResponse> compress;
final HttpCompressionOptionsSpec compressionOptions;
final HttpServerFormDecoderProvider formDecoderProvider;
final BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler;
final HttpMessageLogFactory httpMessageLogFactory;
Expand All @@ -80,6 +82,7 @@ final class Http2StreamBridgeServerHandler extends ChannelHandlerAdapter {

Http2StreamBridgeServerHandler(
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compress,
@Nullable HttpCompressionOptionsSpec compressionOptions,
HttpServerFormDecoderProvider formDecoderProvider,
@Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler,
HttpMessageLogFactory httpMessageLogFactory,
Expand All @@ -88,6 +91,7 @@ final class Http2StreamBridgeServerHandler extends ChannelHandlerAdapter {
@Nullable Duration readTimeout,
@Nullable Duration requestTimeout) {
this.compress = compress;
this.compressionOptions = compressionOptions;
this.formDecoderProvider = formDecoderProvider;
this.forwardedHeaderHandler = forwardedHeaderHandler;
this.httpMessageLogFactory = httpMessageLogFactory;
Expand Down Expand Up @@ -129,6 +133,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
ops = new HttpServerOperations(Connection.from(ctx.channel()),
listener,
request,
compressionOptions,
compress,
connectionInfo,
formDecoderProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2024 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2011-2025 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,8 @@
import reactor.netty5.http.HttpProtocol;
import reactor.netty5.http.logging.HttpMessageLogFactory;
import reactor.netty5.http.logging.ReactorNettyHttpMessageLogFactory;
import reactor.netty5.http.server.compression.HttpCompressionOption;
import reactor.netty5.http.server.compression.HttpCompressionOptionsSpec;
import reactor.netty5.http.server.logging.AccessLog;
import reactor.netty5.http.server.logging.AccessLogArgProvider;
import reactor.netty5.http.server.logging.AccessLogFactory;
Expand Down Expand Up @@ -188,6 +190,7 @@ 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.
Expand Down Expand Up @@ -223,6 +226,41 @@ public final HttpServer compress(int minResponseSize) {
return dup;
}

/**
* Specifies GZip, Deflate, ZSTD compression option
* with {@link reactor.netty5.http.server.compression.GzipOption}, {@link reactor.netty5.http.server.compression.DeflateOption},
* {@link reactor.netty5.http.server.compression.ZstdOption}.
*
* @param compressionOptions configures {@link HttpCompressionOption} after enable compress
*
* <pre>
* {@code
* HttpServer.create()
* .compress(true)
* .compressOptions(
* GzipOption.builder()
* .compressionLevel(6)
* .windowBits(15)
* .memoryLevel(8)
* .build(),
* ZstdOption.builder()
* .compressionLevel(3)
* .build()
* )
* .bindNow();
* }
* </pre>
* @return a new {@link HttpServer}
* @since 1.2.3
*/
public final HttpServer compressOptions(HttpCompressionOption... compressionOptions) {
Objects.requireNonNull(compressionOptions, "compressionOptions");

HttpServer dup = duplicate();
dup.configuration().compressionOptions = new HttpCompressionOptionsSpec(compressionOptions);
return dup;
}

/**
* Specifies a custom request handler for deriving information about the connection.
*
Expand Down
Loading

0 comments on commit 4f2c12a

Please sign in to comment.