From 573d28cfe6dc2caf2780a926e4fcfeed119e19a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fridrich=20=C5=A0trba?= <fridrich.strba@bluewin.ch>
Date: Thu, 30 Mar 2023 13:19:04 +0200
Subject: [PATCH 4/7] Disable Brotli and ZStd compression

---
 .../codec/http/HttpContentCompressor.java     | 73 +------------------
 .../codec/http/HttpContentDecompressor.java   |  7 --
 .../CompressorHttp2ConnectionEncoder.java     | 30 +-------
 .../DelegatingDecompressorFrameListener.java  |  7 --
 .../StandardCompressionOptions.java           | 45 +-----------
 5 files changed, 5 insertions(+), 157 deletions(-)

diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
index a413f9e8df..51e6c10934 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
@@ -22,9 +22,6 @@ import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.embedded.EmbeddedChannel;
 import io.netty.handler.codec.MessageToByteEncoder;
-import io.netty.handler.codec.compression.Brotli;
-import io.netty.handler.codec.compression.BrotliEncoder;
-import io.netty.handler.codec.compression.BrotliOptions;
 import io.netty.handler.codec.compression.CompressionOptions;
 import io.netty.handler.codec.compression.DeflateOptions;
 import io.netty.handler.codec.compression.GzipOptions;
@@ -32,9 +29,6 @@ import io.netty.handler.codec.compression.StandardCompressionOptions;
 import io.netty.handler.codec.compression.ZlibCodecFactory;
 import io.netty.handler.codec.compression.ZlibEncoder;
 import io.netty.handler.codec.compression.ZlibWrapper;
-import io.netty.handler.codec.compression.Zstd;
-import io.netty.handler.codec.compression.ZstdEncoder;
-import io.netty.handler.codec.compression.ZstdOptions;
 import io.netty.util.internal.ObjectUtil;
 
 /**
@@ -47,10 +41,8 @@ import io.netty.util.internal.ObjectUtil;
 public class HttpContentCompressor extends HttpContentEncoder {
 
     private final boolean supportsCompressionOptions;
-    private final BrotliOptions brotliOptions;
     private final GzipOptions gzipOptions;
     private final DeflateOptions deflateOptions;
-    private final ZstdOptions zstdOptions;
 
     private final int compressionLevel;
     private final int windowBits;
@@ -134,10 +126,8 @@ public class HttpContentCompressor extends HttpContentEncoder {
         this.windowBits = ObjectUtil.checkInRange(windowBits, 9, 15, "windowBits");
         this.memLevel = ObjectUtil.checkInRange(memLevel, 1, 9, "memLevel");
         this.contentSizeThreshold = ObjectUtil.checkPositiveOrZero(contentSizeThreshold, "contentSizeThreshold");
-        this.brotliOptions = null;
         this.gzipOptions = null;
         this.deflateOptions = null;
-        this.zstdOptions = null;
         this.factories = null;
         this.supportsCompressionOptions = false;
     }
@@ -166,15 +156,11 @@ public class HttpContentCompressor extends HttpContentEncoder {
      */
     public HttpContentCompressor(int contentSizeThreshold, CompressionOptions... compressionOptions) {
         this.contentSizeThreshold = ObjectUtil.checkPositiveOrZero(contentSizeThreshold, "contentSizeThreshold");
-        BrotliOptions brotliOptions = null;
         GzipOptions gzipOptions = null;
         DeflateOptions deflateOptions = null;
-        ZstdOptions zstdOptions = null;
         if (compressionOptions == null || compressionOptions.length == 0) {
-            brotliOptions = Brotli.isAvailable() ? StandardCompressionOptions.brotli() : null;
             gzipOptions = StandardCompressionOptions.gzip();
             deflateOptions = StandardCompressionOptions.deflate();
-            zstdOptions = Zstd.isAvailable() ? StandardCompressionOptions.zstd() : null;
         } else {
             ObjectUtil.deepCheckNotNull("compressionOptions", compressionOptions);
             for (CompressionOptions compressionOption : compressionOptions) {
@@ -184,14 +170,10 @@ public class HttpContentCompressor extends HttpContentEncoder {
                 // This results in the static analysis of native-image identifying the instanceof BrotliOptions check
                 // and thus BrotliOptions itself as unreachable, enabling native-image to link all classes
                 // at build time and not complain about the missing Brotli classes.
-                if (Brotli.isAvailable() && compressionOption instanceof BrotliOptions) {
-                    brotliOptions = (BrotliOptions) compressionOption;
-                } else if (compressionOption instanceof GzipOptions) {
+                if (compressionOption instanceof GzipOptions) {
                     gzipOptions = (GzipOptions) compressionOption;
                 } else if (compressionOption instanceof DeflateOptions) {
                     deflateOptions = (DeflateOptions) compressionOption;
-                } else if (compressionOption instanceof ZstdOptions) {
-                    zstdOptions = (ZstdOptions) compressionOption;
                 } else {
                     throw new IllegalArgumentException("Unsupported " + CompressionOptions.class.getSimpleName() +
                             ": " + compressionOption);
@@ -201,8 +183,6 @@ public class HttpContentCompressor extends HttpContentEncoder {
 
         this.gzipOptions = gzipOptions;
         this.deflateOptions = deflateOptions;
-        this.brotliOptions = brotliOptions;
-        this.zstdOptions = zstdOptions;
 
         this.factories = new HashMap<String, CompressionEncoderFactory>();
 
@@ -212,12 +192,6 @@ public class HttpContentCompressor extends HttpContentEncoder {
         if (this.deflateOptions != null) {
             this.factories.put("deflate", new DeflateEncoderFactory());
         }
-        if (Brotli.isAvailable() && this.brotliOptions != null) {
-            this.factories.put("br", new BrEncoderFactory());
-        }
-        if (this.zstdOptions != null) {
-            this.factories.put("zstd", new ZstdEncoderFactory());
-        }
 
         this.compressionLevel = -1;
         this.windowBits = -1;
@@ -290,8 +264,6 @@ public class HttpContentCompressor extends HttpContentEncoder {
     @SuppressWarnings("FloatingPointEquality")
     protected String determineEncoding(String acceptEncoding) {
         float starQ = -1.0f;
-        float brQ = -1.0f;
-        float zstdQ = -1.0f;
         float gzipQ = -1.0f;
         float deflateQ = -1.0f;
         for (String encoding : acceptEncoding.split(",")) {
@@ -307,34 +279,20 @@ public class HttpContentCompressor extends HttpContentEncoder {
             }
             if (encoding.contains("*")) {
                 starQ = q;
-            } else if (encoding.contains("br") && q > brQ) {
-                brQ = q;
-            } else if (encoding.contains("zstd") && q > zstdQ) {
-                zstdQ = q;
             } else if (encoding.contains("gzip") && q > gzipQ) {
                 gzipQ = q;
             } else if (encoding.contains("deflate") && q > deflateQ) {
                 deflateQ = q;
             }
         }
-        if (brQ > 0.0f || zstdQ > 0.0f || gzipQ > 0.0f || deflateQ > 0.0f) {
-            if (brQ != -1.0f && brQ >= zstdQ && this.brotliOptions != null) {
-                return "br";
-            } else if (zstdQ != -1.0f && zstdQ >= gzipQ && this.zstdOptions != null) {
-                return "zstd";
-            } else if (gzipQ != -1.0f && gzipQ >= deflateQ && this.gzipOptions != null) {
+        if (gzipQ > 0.0f || deflateQ > 0.0f) {
+            if (gzipQ != -1.0f && gzipQ >= deflateQ && this.gzipOptions != null) {
                 return "gzip";
             } else if (deflateQ != -1.0f && this.deflateOptions != null) {
                 return "deflate";
             }
         }
         if (starQ > 0.0f) {
-            if (brQ == -1.0f && this.brotliOptions != null) {
-                return "br";
-            }
-            if (zstdQ == -1.0f && this.zstdOptions != null) {
-                return "zstd";
-            }
             if (gzipQ == -1.0f && this.gzipOptions != null) {
                 return "gzip";
             }
@@ -415,29 +373,4 @@ public class HttpContentCompressor extends HttpContentEncoder {
                     deflateOptions.windowBits(), deflateOptions.memLevel());
         }
     }
-
-    /**
-     * Compression Encoder Factory that creates {@link BrotliEncoder}s
-     * used to compress http content for br content encoding
-     */
-    private final class BrEncoderFactory implements CompressionEncoderFactory {
-
-        @Override
-        public MessageToByteEncoder<ByteBuf> createEncoder() {
-            return new BrotliEncoder(brotliOptions.parameters());
-        }
-    }
-
-    /**
-     * Compression Encoder Factory for create {@link ZstdEncoder}
-     * used to compress http content for zstd content encoding
-     */
-    private final class ZstdEncoderFactory implements CompressionEncoderFactory {
-
-        @Override
-        public MessageToByteEncoder<ByteBuf> createEncoder() {
-            return new ZstdEncoder(zstdOptions.compressionLevel(),
-                    zstdOptions.blockSize(), zstdOptions.maxEncodeSize());
-        }
-    }
 }
diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java
index e8e1d90972..15f56aff91 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java
@@ -15,7 +15,6 @@
  */
 package io.netty.handler.codec.http;
 
-import static io.netty.handler.codec.http.HttpHeaderValues.BR;
 import static io.netty.handler.codec.http.HttpHeaderValues.DEFLATE;
 import static io.netty.handler.codec.http.HttpHeaderValues.GZIP;
 import static io.netty.handler.codec.http.HttpHeaderValues.X_DEFLATE;
@@ -23,8 +22,6 @@ import static io.netty.handler.codec.http.HttpHeaderValues.X_GZIP;
 import static io.netty.handler.codec.http.HttpHeaderValues.SNAPPY;
 
 import io.netty.channel.embedded.EmbeddedChannel;
-import io.netty.handler.codec.compression.Brotli;
-import io.netty.handler.codec.compression.BrotliDecoder;
 import io.netty.handler.codec.compression.ZlibCodecFactory;
 import io.netty.handler.codec.compression.ZlibWrapper;
 import io.netty.handler.codec.compression.SnappyFrameDecoder;
@@ -69,10 +66,6 @@ public class HttpContentDecompressor extends HttpContentDecoder {
             return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                     ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper));
         }
-        if (Brotli.isAvailable() && BR.contentEqualsIgnoreCase(contentEncoding)) {
-            return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
-              ctx.channel().config(), new BrotliDecoder());
-        }
 
         if (SNAPPY.contentEqualsIgnoreCase(contentEncoding)) {
             return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/CompressorHttp2ConnectionEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/CompressorHttp2ConnectionEncoder.java
index 52f1eeff20..150c1929b8 100644
--- a/codec-http2/src/main/java/io/netty/handler/codec/http2/CompressorHttp2ConnectionEncoder.java
+++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/CompressorHttp2ConnectionEncoder.java
@@ -21,30 +21,23 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelPromise;
 import io.netty.channel.embedded.EmbeddedChannel;
 import io.netty.handler.codec.ByteToMessageDecoder;
-import io.netty.handler.codec.compression.BrotliEncoder;
 import io.netty.handler.codec.compression.ZlibCodecFactory;
 import io.netty.handler.codec.compression.ZlibWrapper;
-import io.netty.handler.codec.compression.Brotli;
-import io.netty.handler.codec.compression.BrotliOptions;
 import io.netty.handler.codec.compression.CompressionOptions;
 import io.netty.handler.codec.compression.DeflateOptions;
 import io.netty.handler.codec.compression.GzipOptions;
 import io.netty.handler.codec.compression.StandardCompressionOptions;
-import io.netty.handler.codec.compression.ZstdEncoder;
-import io.netty.handler.codec.compression.ZstdOptions;
 import io.netty.util.concurrent.PromiseCombiner;
 import io.netty.util.internal.ObjectUtil;
 import io.netty.util.internal.UnstableApi;
 
 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_ENCODING;
 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
-import static io.netty.handler.codec.http.HttpHeaderValues.BR;
 import static io.netty.handler.codec.http.HttpHeaderValues.DEFLATE;
 import static io.netty.handler.codec.http.HttpHeaderValues.GZIP;
 import static io.netty.handler.codec.http.HttpHeaderValues.IDENTITY;
 import static io.netty.handler.codec.http.HttpHeaderValues.X_DEFLATE;
 import static io.netty.handler.codec.http.HttpHeaderValues.X_GZIP;
-import static io.netty.handler.codec.http.HttpHeaderValues.ZSTD;
 
 /**
  * A decorating HTTP2 encoder that will compress data frames according to the {@code content-encoding} header for each
@@ -64,10 +57,8 @@ public class CompressorHttp2ConnectionEncoder extends DecoratingHttp2ConnectionE
 
     private final boolean supportsCompressionOptions;
 
-    private BrotliOptions brotliOptions;
     private GzipOptions gzipCompressionOptions;
     private DeflateOptions deflateOptions;
-    private ZstdOptions zstdOptions;
 
     /**
      * Create a new {@link CompressorHttp2ConnectionEncoder} instance
@@ -78,12 +69,6 @@ public class CompressorHttp2ConnectionEncoder extends DecoratingHttp2ConnectionE
     }
 
     private static CompressionOptions[] defaultCompressionOptions() {
-        if (Brotli.isAvailable()) {
-            return new CompressionOptions[] {
-                    StandardCompressionOptions.brotli(),
-                    StandardCompressionOptions.gzip(),
-                    StandardCompressionOptions.deflate() };
-        }
         return new CompressionOptions[] { StandardCompressionOptions.gzip(), StandardCompressionOptions.deflate() };
     }
 
@@ -129,14 +114,10 @@ public class CompressorHttp2ConnectionEncoder extends DecoratingHttp2ConnectionE
             // This results in the static analysis of native-image identifying the instanceof BrotliOptions check
             // and thus BrotliOptions itself as unreachable, enabling native-image to link all classes at build time
             // and not complain about the missing Brotli classes.
-            if (Brotli.isAvailable() && compressionOptions instanceof BrotliOptions) {
-                brotliOptions = (BrotliOptions) compressionOptions;
-            } else if (compressionOptions instanceof GzipOptions) {
+            if (compressionOptions instanceof GzipOptions) {
                 gzipCompressionOptions = (GzipOptions) compressionOptions;
             } else if (compressionOptions instanceof DeflateOptions) {
                 deflateOptions = (DeflateOptions) compressionOptions;
-            } else if (compressionOptions instanceof ZstdOptions) {
-                zstdOptions = (ZstdOptions) compressionOptions;
             } else {
                 throw new IllegalArgumentException("Unsupported " + CompressionOptions.class.getSimpleName() +
                         ": " + compressionOptions);
@@ -274,15 +255,6 @@ public class CompressorHttp2ConnectionEncoder extends DecoratingHttp2ConnectionE
         if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) {
             return newCompressionChannel(ctx, ZlibWrapper.ZLIB);
         }
-        if (Brotli.isAvailable() && brotliOptions != null && BR.contentEqualsIgnoreCase(contentEncoding)) {
-            return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
-                    ctx.channel().config(), new BrotliEncoder(brotliOptions.parameters()));
-        }
-        if (zstdOptions != null && ZSTD.contentEqualsIgnoreCase(contentEncoding)) {
-            return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
-                    ctx.channel().config(), new ZstdEncoder(zstdOptions.compressionLevel(),
-                    zstdOptions.blockSize(), zstdOptions.maxEncodeSize()));
-        }
         // 'identity' or unsupported
         return null;
     }
diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DelegatingDecompressorFrameListener.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DelegatingDecompressorFrameListener.java
index 6380fabf12..bbd89747ab 100644
--- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DelegatingDecompressorFrameListener.java
+++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DelegatingDecompressorFrameListener.java
@@ -19,15 +19,12 @@ import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.embedded.EmbeddedChannel;
 import io.netty.handler.codec.ByteToMessageDecoder;
-import io.netty.handler.codec.compression.Brotli;
-import io.netty.handler.codec.compression.BrotliDecoder;
 import io.netty.handler.codec.compression.ZlibCodecFactory;
 import io.netty.handler.codec.compression.ZlibWrapper;
 import io.netty.util.internal.UnstableApi;
 
 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_ENCODING;
 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
-import static io.netty.handler.codec.http.HttpHeaderValues.BR;
 import static io.netty.handler.codec.http.HttpHeaderValues.DEFLATE;
 import static io.netty.handler.codec.http.HttpHeaderValues.GZIP;
 import static io.netty.handler.codec.http.HttpHeaderValues.IDENTITY;
@@ -178,10 +175,6 @@ public class DelegatingDecompressorFrameListener extends Http2FrameListenerDecor
             return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                     ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper));
         }
-        if (Brotli.isAvailable() && BR.contentEqualsIgnoreCase(contentEncoding)) {
-            return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
-              ctx.channel().config(), new BrotliDecoder());
-        }
         // 'identity' or unsupported
         return null;
     }
diff --git a/codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java b/codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java
index 39926123c7..698c4b510a 100644
--- a/codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java
+++ b/codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java
@@ -15,10 +15,8 @@
  */
 package io.netty.handler.codec.compression;
 
-import com.aayushatharva.brotli4j.encoder.Encoder;
-
 /**
- * Standard Compression Options for {@link BrotliOptions},
+ * Standard Compression Options for
  * {@link GzipOptions} and {@link DeflateOptions}
  */
 public final class StandardCompressionOptions {
@@ -28,47 +26,6 @@ public final class StandardCompressionOptions {
     }
 
     /**
-     * Default implementation of {@link BrotliOptions} with {@link Encoder.Parameters#setQuality(int)} set to 4
-     * and {@link Encoder.Parameters#setMode(Encoder.Mode)} set to {@link Encoder.Mode#TEXT}
-     */
-    public static BrotliOptions brotli() {
-        return BrotliOptions.DEFAULT;
-    }
-
-    /**
-     * Create a new {@link BrotliOptions}
-     *
-     * @param parameters {@link Encoder.Parameters} Instance
-     * @throws NullPointerException If {@link Encoder.Parameters} is {@code null}
-     */
-    public static BrotliOptions brotli(Encoder.Parameters parameters) {
-        return new BrotliOptions(parameters);
-    }
-
-    /**
-     * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
-     * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
-     * {@link ZstdConstants#MAX_BLOCK_SIZE}
-     */
-    public static ZstdOptions zstd() {
-        return ZstdOptions.DEFAULT;
-    }
-
-    /**
-     * Create a new {@link ZstdOptions}
-     *
-     * @param  blockSize
-     *           is used to calculate the compressionLevel
-     * @param  maxEncodeSize
-     *           specifies the size of the largest compressed object
-     * @param  compressionLevel
-     *           specifies the level of the compression
-     */
-    public static ZstdOptions zstd(int compressionLevel, int blockSize, int maxEncodeSize) {
-        return new ZstdOptions(compressionLevel, blockSize, maxEncodeSize);
-    }
-
-     /**
      * Default implementation of {@link GzipOptions} with
      * {@code compressionLevel()} set to 6, {@code windowBits()} set to 15 and {@code memLevel()} set to 8.
      */
-- 
2.41.0

