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 io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.embedded.EmbeddedChannel;
20 import io.netty.handler.codec.compression.ZlibCodecFactory;
21 import io.netty.handler.codec.compression.ZlibWrapper;
22
23
24
25
26
27
28
29
30 public class HttpContentCompressor extends HttpContentEncoder {
31
32 private final int compressionLevel;
33 private final int windowBits;
34 private final int memLevel;
35 private ChannelHandlerContext ctx;
36
37
38
39
40
41 public HttpContentCompressor() {
42 this(6);
43 }
44
45
46
47
48
49
50
51
52
53
54 public HttpContentCompressor(int compressionLevel) {
55 this(compressionLevel, 15, 8);
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel) {
78 if (compressionLevel < 0 || compressionLevel > 9) {
79 throw new IllegalArgumentException(
80 "compressionLevel: " + compressionLevel +
81 " (expected: 0-9)");
82 }
83 if (windowBits < 9 || windowBits > 15) {
84 throw new IllegalArgumentException(
85 "windowBits: " + windowBits + " (expected: 9-15)");
86 }
87 if (memLevel < 1 || memLevel > 9) {
88 throw new IllegalArgumentException(
89 "memLevel: " + memLevel + " (expected: 1-9)");
90 }
91 this.compressionLevel = compressionLevel;
92 this.windowBits = windowBits;
93 this.memLevel = memLevel;
94 }
95
96 @Override
97 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
98 this.ctx = ctx;
99 }
100
101 @Override
102 protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception {
103 String contentEncoding = headers.headers().get(HttpHeaders.Names.CONTENT_ENCODING);
104 if (contentEncoding != null) {
105
106
107 return null;
108 }
109
110 ZlibWrapper wrapper = determineWrapper(acceptEncoding);
111 if (wrapper == null) {
112 return null;
113 }
114
115 String targetContentEncoding;
116 switch (wrapper) {
117 case GZIP:
118 targetContentEncoding = "gzip";
119 break;
120 case ZLIB:
121 targetContentEncoding = "deflate";
122 break;
123 default:
124 throw new Error();
125 }
126
127 return new Result(
128 targetContentEncoding,
129 new EmbeddedChannel(ctx.channel().metadata().hasDisconnect(),
130 ctx.channel().config(), ZlibCodecFactory.newZlibEncoder(
131 wrapper, compressionLevel, windowBits, memLevel)));
132 }
133
134 @SuppressWarnings("FloatingPointEquality")
135 protected ZlibWrapper determineWrapper(String acceptEncoding) {
136 float starQ = -1.0f;
137 float gzipQ = -1.0f;
138 float deflateQ = -1.0f;
139 for (String encoding : acceptEncoding.split(",")) {
140 float q = 1.0f;
141 int equalsPos = encoding.indexOf('=');
142 if (equalsPos != -1) {
143 try {
144 q = Float.parseFloat(encoding.substring(equalsPos + 1));
145 } catch (NumberFormatException e) {
146
147 q = 0.0f;
148 }
149 }
150 if (encoding.contains("*")) {
151 starQ = q;
152 } else if (encoding.contains("gzip") && q > gzipQ) {
153 gzipQ = q;
154 } else if (encoding.contains("deflate") && q > deflateQ) {
155 deflateQ = q;
156 }
157 }
158 if (gzipQ > 0.0f || deflateQ > 0.0f) {
159 if (gzipQ >= deflateQ) {
160 return ZlibWrapper.GZIP;
161 } else {
162 return ZlibWrapper.ZLIB;
163 }
164 }
165 if (starQ > 0.0f) {
166 if (gzipQ == -1.0f) {
167 return ZlibWrapper.GZIP;
168 }
169 if (deflateQ == -1.0f) {
170 return ZlibWrapper.ZLIB;
171 }
172 }
173 return null;
174 }
175 }