View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http;
17  
18  import io.netty5.handler.codec.compression.Brotli;
19  import io.netty5.handler.codec.compression.BrotliCompressor;
20  import io.netty5.handler.codec.compression.BrotliOptions;
21  import io.netty5.handler.codec.compression.CompressionOptions;
22  import io.netty5.handler.codec.compression.Compressor;
23  import io.netty5.handler.codec.compression.DeflateOptions;
24  import io.netty5.handler.codec.compression.GzipOptions;
25  import io.netty5.handler.codec.compression.StandardCompressionOptions;
26  import io.netty5.handler.codec.compression.ZlibCompressor;
27  import io.netty5.handler.codec.compression.ZlibWrapper;
28  import io.netty5.handler.codec.compression.Zstd;
29  import io.netty5.handler.codec.compression.ZstdCompressor;
30  import io.netty5.handler.codec.compression.ZstdOptions;
31  import io.netty5.util.internal.ObjectUtil;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  import java.util.function.Supplier;
36  
37  /**
38   * Compresses an {@link HttpMessage} and an {@link HttpContent} in {@code gzip} or
39   * {@code deflate} encoding while respecting the {@code "Accept-Encoding"} header.
40   * If there is no matching encoding, no compression is done.  For more
41   * information on how this handler modifies the message, please refer to
42   * {@link HttpContentEncoder}.
43   */
44  public class HttpContentCompressor extends HttpContentEncoder {
45  
46      private final boolean supportsCompressionOptions;
47      private final BrotliOptions brotliOptions;
48      private final GzipOptions gzipOptions;
49      private final DeflateOptions deflateOptions;
50      private final ZstdOptions zstdOptions;
51  
52      private final int compressionLevel;
53      private final int windowBits;
54      private final int memLevel;
55      private final int contentSizeThreshold;
56      private final Map<String, Supplier<? extends Compressor>> factories;
57  
58      /**
59       * Creates a new handler with the default compression level (<tt>6</tt>),
60       * default window size (<tt>15</tt>) and default memory level (<tt>8</tt>).
61       */
62      public HttpContentCompressor() {
63          this(6);
64      }
65  
66      /**
67       * Creates a new handler with the specified compression level, default
68       * window size (<tt>15</tt>) and default memory level (<tt>8</tt>).
69       *
70       * @param compressionLevel
71       *        {@code 1} yields the fastest compression and {@code 9} yields the
72       *        best compression.  {@code 0} means no compression.  The default
73       *        compression level is {@code 6}.
74       */
75      @Deprecated
76      public HttpContentCompressor(int compressionLevel) {
77          this(compressionLevel, 15, 8, 0);
78      }
79  
80      /**
81       * Creates a new handler with the specified compression level, window size,
82       * and memory level..
83       *
84       * @param compressionLevel
85       *        {@code 1} yields the fastest compression and {@code 9} yields the
86       *        best compression.  {@code 0} means no compression.  The default
87       *        compression level is {@code 6}.
88       * @param windowBits
89       *        The base two logarithm of the size of the history buffer.  The
90       *        value should be in the range {@code 9} to {@code 15} inclusive.
91       *        Larger values result in better compression at the expense of
92       *        memory usage.  The default value is {@code 15}.
93       * @param memLevel
94       *        How much memory should be allocated for the internal compression
95       *        state.  {@code 1} uses minimum memory and {@code 9} uses maximum
96       *        memory.  Larger values result in better and faster compression
97       *        at the expense of memory usage.  The default value is {@code 8}
98       */
99      @Deprecated
100     public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel) {
101         this(compressionLevel, windowBits, memLevel, 0);
102     }
103 
104     /**
105      * Creates a new handler with the specified compression level, window size,
106      * and memory level..
107      *
108      * @param compressionLevel
109      *        {@code 1} yields the fastest compression and {@code 9} yields the
110      *        best compression.  {@code 0} means no compression.  The default
111      *        compression level is {@code 6}.
112      * @param windowBits
113      *        The base two logarithm of the size of the history buffer.  The
114      *        value should be in the range {@code 9} to {@code 15} inclusive.
115      *        Larger values result in better compression at the expense of
116      *        memory usage.  The default value is {@code 15}.
117      * @param memLevel
118      *        How much memory should be allocated for the internal compression
119      *        state.  {@code 1} uses minimum memory and {@code 9} uses maximum
120      *        memory.  Larger values result in better and faster compression
121      *        at the expense of memory usage.  The default value is {@code 8}
122      * @param contentSizeThreshold
123      *        The response body is compressed when the size of the response
124      *        body exceeds the threshold. The value should be a non negative
125      *        number. {@code 0} will enable compression for all responses.
126      */
127     @Deprecated
128     public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel, int contentSizeThreshold) {
129         this.compressionLevel = ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");
130         this.windowBits = ObjectUtil.checkInRange(windowBits, 9, 15, "windowBits");
131         this.memLevel = ObjectUtil.checkInRange(memLevel, 1, 9, "memLevel");
132         this.contentSizeThreshold = ObjectUtil.checkPositiveOrZero(contentSizeThreshold, "contentSizeThreshold");
133         brotliOptions = null;
134         gzipOptions = null;
135         deflateOptions = null;
136         zstdOptions = null;
137         factories = null;
138         supportsCompressionOptions = false;
139     }
140 
141     /**
142      * Create a new {@link HttpContentCompressor} Instance with specified
143      * {@link CompressionOptions}s and contentSizeThreshold set to {@code 0}
144      *
145      * @param compressionOptions {@link CompressionOptions} or {@code null} if the default
146      *        should be used.
147      */
148     public HttpContentCompressor(CompressionOptions... compressionOptions) {
149         this(0, compressionOptions);
150     }
151 
152     /**
153      * Create a new {@link HttpContentCompressor} instance with specified
154      * {@link CompressionOptions}s
155      *
156      * @param contentSizeThreshold
157      *        The response body is compressed when the size of the response
158      *        body exceeds the threshold. The value should be a non negative
159      *        number. {@code 0} will enable compression for all responses.
160      * @param compressionOptions {@link CompressionOptions} or {@code null}
161      *        if the default should be used.
162      */
163     public HttpContentCompressor(int contentSizeThreshold, CompressionOptions... compressionOptions) {
164         this.contentSizeThreshold = ObjectUtil.checkPositiveOrZero(contentSizeThreshold, "contentSizeThreshold");
165         BrotliOptions brotliOptions = null;
166         GzipOptions gzipOptions = null;
167         DeflateOptions deflateOptions = null;
168         ZstdOptions zstdOptions = null;
169         if (compressionOptions == null || compressionOptions.length == 0) {
170             brotliOptions = Brotli.isAvailable() ? StandardCompressionOptions.brotli() : null;
171             gzipOptions = StandardCompressionOptions.gzip();
172             deflateOptions = StandardCompressionOptions.deflate();
173             zstdOptions = Zstd.isAvailable() ? StandardCompressionOptions.zstd() : null;
174         } else {
175             ObjectUtil.deepCheckNotNull("compressionOptions", compressionOptions);
176             for (CompressionOptions compressionOption : compressionOptions) {
177                 // BrotliOptions' class initialization depends on Brotli classes being on the classpath.
178                 // The Brotli.isAvailable check ensures that BrotliOptions will only get instantiated if Brotli is
179                 // on the classpath.
180                 // This results in the static analysis of native-image identifying the instanceof BrotliOptions check
181                 // and thus BrotliOptions itself as unreachable, enabling native-image to link all classes
182                 // at build time and not complain about the missing Brotli classes.
183                 if (Brotli.isAvailable() && compressionOption instanceof BrotliOptions) {
184                     brotliOptions = (BrotliOptions) compressionOption;
185                 } else if (compressionOption instanceof GzipOptions) {
186                     gzipOptions = (GzipOptions) compressionOption;
187                 } else if (compressionOption instanceof DeflateOptions) {
188                     deflateOptions = (DeflateOptions) compressionOption;
189                 } else if (compressionOption instanceof ZstdOptions) {
190                     zstdOptions = (ZstdOptions) compressionOption;
191                 } else {
192                     throw new IllegalArgumentException("Unsupported " + CompressionOptions.class.getSimpleName() +
193                             ": " + compressionOption);
194                 }
195             }
196         }
197 
198         this.gzipOptions = gzipOptions;
199         this.deflateOptions = deflateOptions;
200         this.brotliOptions = brotliOptions;
201         this.zstdOptions = zstdOptions;
202 
203         factories = new HashMap<>();
204 
205         if (this.gzipOptions != null) {
206             factories.put("gzip", ZlibCompressor.newFactory(
207                     ZlibWrapper.GZIP, gzipOptions.compressionLevel()));
208         }
209         if (this.deflateOptions != null) {
210             factories.put("deflate", ZlibCompressor.newFactory(
211                     ZlibWrapper.ZLIB, deflateOptions.compressionLevel()));
212         }
213 
214         if (Brotli.isAvailable() && this.brotliOptions != null) {
215             factories.put("br", BrotliCompressor.newFactory(brotliOptions.parameters()));
216         }
217         if (this.zstdOptions != null) {
218             factories.put("zstd", ZstdCompressor.newFactory(zstdOptions.compressionLevel(),
219                     zstdOptions.blockSize(), zstdOptions.maxEncodeSize()));
220         }
221 
222         compressionLevel = -1;
223         windowBits = -1;
224         memLevel = -1;
225         supportsCompressionOptions = true;
226     }
227 
228     @Override
229     protected Result beginEncode(HttpResponse httpResponse, String acceptEncoding) {
230         if (contentSizeThreshold > 0) {
231             if (httpResponse instanceof HttpContent &&
232                     ((HttpContent<?>) httpResponse).payload().readableBytes() < contentSizeThreshold) {
233                 return null;
234             }
235         }
236 
237         String contentEncoding = httpResponse.headers().get(HttpHeaderNames.CONTENT_ENCODING);
238         if (contentEncoding != null) {
239             // Content-Encoding was set, either as something specific or as the IDENTITY encoding
240             // Therefore, we should NOT encode here
241             return null;
242         }
243 
244         if (supportsCompressionOptions) {
245             String targetContentEncoding = determineEncoding(acceptEncoding);
246             if (targetContentEncoding == null) {
247                 return null;
248             }
249 
250             Supplier<? extends Compressor> compressorFactory = factories.get(targetContentEncoding);
251 
252             if (compressorFactory == null) {
253                 throw new Error();
254             }
255 
256             return new Result(targetContentEncoding, compressorFactory.get());
257         } else {
258             ZlibWrapper wrapper = determineWrapper(acceptEncoding);
259             if (wrapper == null) {
260                 return null;
261             }
262 
263             String targetContentEncoding;
264             switch (wrapper) {
265                 case GZIP:
266                     targetContentEncoding = "gzip";
267                     break;
268                 case ZLIB:
269                     targetContentEncoding = "deflate";
270                     break;
271                 default:
272                     throw new Error();
273             }
274 
275             return new Result(
276                     targetContentEncoding, ZlibCompressor.newFactory(wrapper, compressionLevel).get());
277         }
278     }
279 
280     @SuppressWarnings("FloatingPointEquality")
281     protected String determineEncoding(String acceptEncoding) {
282         float starQ = -1.0f;
283         float brQ = -1.0f;
284         float zstdQ = -1.0f;
285         float gzipQ = -1.0f;
286         float deflateQ = -1.0f;
287         for (String encoding : acceptEncoding.split(",")) {
288             float q = 1.0f;
289             int equalsPos = encoding.indexOf('=');
290             if (equalsPos != -1) {
291                 try {
292                     q = Float.parseFloat(encoding.substring(equalsPos + 1));
293                 } catch (NumberFormatException e) {
294                     // Ignore encoding
295                     q = 0.0f;
296                 }
297             }
298             if (encoding.contains("*")) {
299                 starQ = q;
300             } else if (encoding.contains("br") && q > brQ) {
301                 brQ = q;
302             } else if (encoding.contains("zstd") && q > zstdQ) {
303                 zstdQ = q;
304             } else if (encoding.contains("gzip") && q > gzipQ) {
305                 gzipQ = q;
306             } else if (encoding.contains("deflate") && q > deflateQ) {
307                 deflateQ = q;
308             }
309         }
310         if (brQ > 0.0f || zstdQ > 0.0f || gzipQ > 0.0f || deflateQ > 0.0f) {
311             if (brQ != -1.0f && brQ >= zstdQ && brotliOptions != null) {
312                 return "br";
313             } else if (zstdQ != -1.0f && zstdQ >= gzipQ && zstdOptions != null) {
314                 return "zstd";
315             } else if (gzipQ != -1.0f && gzipQ >= deflateQ && gzipOptions != null) {
316                 return "gzip";
317             } else if (deflateQ != -1.0f && deflateOptions != null) {
318                 return "deflate";
319             }
320         }
321         if (starQ > 0.0f) {
322             if (brQ == -1.0f && brotliOptions != null) {
323                 return "br";
324             }
325             if (zstdQ == -1.0f && zstdOptions != null) {
326                 return "zstd";
327             }
328             if (gzipQ == -1.0f && gzipOptions != null) {
329                 return "gzip";
330             }
331             if (deflateQ == -1.0f && deflateOptions != null) {
332                 return "deflate";
333             }
334         }
335         return null;
336     }
337 
338     @Deprecated
339     @SuppressWarnings("FloatingPointEquality")
340     protected ZlibWrapper determineWrapper(String acceptEncoding) {
341         float starQ = -1.0f;
342         float gzipQ = -1.0f;
343         float deflateQ = -1.0f;
344         for (String encoding : acceptEncoding.split(",")) {
345             float q = 1.0f;
346             int equalsPos = encoding.indexOf('=');
347             if (equalsPos != -1) {
348                 try {
349                     q = Float.parseFloat(encoding.substring(equalsPos + 1));
350                 } catch (NumberFormatException e) {
351                     // Ignore encoding
352                     q = 0.0f;
353                 }
354             }
355             if (encoding.contains("*")) {
356                 starQ = q;
357             } else if (encoding.contains("gzip") && q > gzipQ) {
358                 gzipQ = q;
359             } else if (encoding.contains("deflate") && q > deflateQ) {
360                 deflateQ = q;
361             }
362         }
363         if (gzipQ > 0.0f || deflateQ > 0.0f) {
364             if (gzipQ >= deflateQ) {
365                 return ZlibWrapper.GZIP;
366             } else {
367                 return ZlibWrapper.ZLIB;
368             }
369         }
370         if (starQ > 0.0f) {
371             if (gzipQ == -1.0f) {
372                 return ZlibWrapper.GZIP;
373             }
374             if (deflateQ == -1.0f) {
375                 return ZlibWrapper.ZLIB;
376             }
377         }
378         return null;
379     }
380 }