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