1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http2;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.handler.codec.compression.ZlibEncoder;
20 import org.jboss.netty.handler.codec.compression.ZlibWrapper;
21 import org.jboss.netty.handler.codec.embedder.EncoderEmbedder;
22
23
24
25
26
27
28
29
30
31
32
33
34 public class HttpContentCompressor extends HttpContentEncoder {
35
36 private final int compressionLevel;
37 private final int windowBits;
38 private final int memLevel;
39
40
41
42
43
44 public HttpContentCompressor() {
45 this(6);
46 }
47
48
49
50
51
52
53
54
55
56
57 public HttpContentCompressor(int compressionLevel) {
58 this(compressionLevel, 15, 8);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel) {
81 if (compressionLevel < 0 || compressionLevel > 9) {
82 throw new IllegalArgumentException(
83 "compressionLevel: " + compressionLevel + " (expected: 0-9)");
84 }
85 if (windowBits < 9 || windowBits > 15) {
86 throw new IllegalArgumentException(
87 "windowBits: " + windowBits + " (expected: 9-15)");
88 }
89 if (memLevel < 1 || memLevel > 9) {
90 throw new IllegalArgumentException(
91 "memLevel: " + memLevel + " (expected: 1-9)");
92 }
93 this.compressionLevel = compressionLevel;
94 this.windowBits = windowBits;
95 this.memLevel = memLevel;
96 }
97
98 @Override
99 protected EncoderEmbedder<ChannelBuffer> newContentEncoder(String acceptEncoding) throws Exception {
100 ZlibWrapper wrapper = determineWrapper(acceptEncoding);
101 if (wrapper == null) {
102 return null;
103 }
104
105 return new EncoderEmbedder<ChannelBuffer>(
106 new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel));
107 }
108
109 @Override
110 protected String getTargetContentEncoding(String acceptEncoding) throws Exception {
111 ZlibWrapper wrapper = determineWrapper(acceptEncoding);
112 if (wrapper == null) {
113 return null;
114 }
115
116 switch (wrapper) {
117 case GZIP:
118 return "gzip";
119 case ZLIB:
120 return "deflate";
121 default:
122 throw new Error();
123 }
124 }
125
126 private ZlibWrapper determineWrapper(String acceptEncoding) {
127 float starQ = -1.0f;
128 float gzipQ = -1.0f;
129 float deflateQ = -1.0f;
130 for (String encoding : acceptEncoding.split(",")) {
131 float q = 1.0f;
132 int equalsPos = encoding.indexOf('=');
133 if (equalsPos != -1) {
134 try {
135 q = Float.valueOf(encoding.substring(equalsPos + 1));
136 } catch (NumberFormatException e) {
137
138 q = 0.0f;
139 }
140 }
141 if (encoding.indexOf("*") >= 0) {
142 starQ = q;
143 } else if (encoding.indexOf("gzip") >= 0 && q > gzipQ) {
144 gzipQ = q;
145 } else if (encoding.indexOf("deflate") >= 0 && q > deflateQ) {
146 deflateQ = q;
147 }
148 }
149 if (gzipQ > 0.0f || deflateQ > 0.0f) {
150 if (gzipQ >= deflateQ) {
151 return ZlibWrapper.GZIP;
152 } else {
153 return ZlibWrapper.ZLIB;
154 }
155 }
156 if (starQ > 0.0f) {
157 if (gzipQ == -1.0f) {
158 return ZlibWrapper.GZIP;
159 }
160 if (deflateQ == -1.0f) {
161 return ZlibWrapper.ZLIB;
162 }
163 }
164 return null;
165 }
166 }