1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import static io.netty.handler.codec.http.HttpHeaderValues.BR;
19 import static io.netty.handler.codec.http.HttpHeaderValues.DEFLATE;
20 import static io.netty.handler.codec.http.HttpHeaderValues.GZIP;
21 import static io.netty.handler.codec.http.HttpHeaderValues.X_DEFLATE;
22 import static io.netty.handler.codec.http.HttpHeaderValues.X_GZIP;
23
24 import io.netty.channel.embedded.EmbeddedChannel;
25 import io.netty.handler.codec.compression.Brotli;
26 import io.netty.handler.codec.compression.BrotliDecoder;
27 import io.netty.handler.codec.compression.ZlibCodecFactory;
28 import io.netty.handler.codec.compression.ZlibWrapper;
29
30
31
32
33
34
35 public class HttpContentDecompressor extends HttpContentDecoder {
36
37 private final boolean strict;
38
39
40
41
42 public HttpContentDecompressor() {
43 this(false);
44 }
45
46
47
48
49
50
51
52 public HttpContentDecompressor(boolean strict) {
53 this.strict = strict;
54 }
55
56 @Override
57 protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception {
58 if (GZIP.contentEqualsIgnoreCase(contentEncoding) ||
59 X_GZIP.contentEqualsIgnoreCase(contentEncoding)) {
60 return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
61 ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
62 }
63 if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) ||
64 X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) {
65 final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE;
66
67 return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
68 ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper));
69 }
70 if (Brotli.isAvailable() && BR.contentEqualsIgnoreCase(contentEncoding)) {
71 return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
72 ctx.channel().config(), new BrotliDecoder());
73 }
74
75
76 return null;
77 }
78 }