1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.websocketx.extensions.compression;
17
18 import io.netty.handler.codec.compression.ZlibCodecFactory;
19 import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtension;
20 import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker;
21 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData;
22 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
23 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
24 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilterProvider;
25
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map.Entry;
29
30 import static io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.*;
31 import static io.netty.util.internal.ObjectUtil.*;
32
33
34
35
36
37 public final class PerMessageDeflateClientExtensionHandshaker implements WebSocketClientExtensionHandshaker {
38
39 private final int compressionLevel;
40 private final boolean allowClientWindowSize;
41 private final int requestedServerWindowSize;
42 private final boolean allowClientNoContext;
43 private final boolean requestedServerNoContext;
44 private final WebSocketExtensionFilterProvider extensionFilterProvider;
45
46
47
48
49 public PerMessageDeflateClientExtensionHandshaker() {
50 this(6, ZlibCodecFactory.isSupportingWindowSizeAndMemLevel(), MAX_WINDOW_SIZE, false, false);
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public PerMessageDeflateClientExtensionHandshaker(int compressionLevel,
71 boolean allowClientWindowSize, int requestedServerWindowSize,
72 boolean allowClientNoContext, boolean requestedServerNoContext) {
73 this(compressionLevel, allowClientWindowSize, requestedServerWindowSize,
74 allowClientNoContext, requestedServerNoContext, WebSocketExtensionFilterProvider.DEFAULT);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public PerMessageDeflateClientExtensionHandshaker(int compressionLevel,
97 boolean allowClientWindowSize, int requestedServerWindowSize,
98 boolean allowClientNoContext, boolean requestedServerNoContext,
99 WebSocketExtensionFilterProvider extensionFilterProvider) {
100
101 if (requestedServerWindowSize > MAX_WINDOW_SIZE || requestedServerWindowSize < MIN_WINDOW_SIZE) {
102 throw new IllegalArgumentException(
103 "requestedServerWindowSize: " + requestedServerWindowSize + " (expected: 8-15)");
104 }
105 if (compressionLevel < 0 || compressionLevel > 9) {
106 throw new IllegalArgumentException(
107 "compressionLevel: " + compressionLevel + " (expected: 0-9)");
108 }
109 this.compressionLevel = compressionLevel;
110 this.allowClientWindowSize = allowClientWindowSize;
111 this.requestedServerWindowSize = requestedServerWindowSize;
112 this.allowClientNoContext = allowClientNoContext;
113 this.requestedServerNoContext = requestedServerNoContext;
114 this.extensionFilterProvider = checkNotNull(extensionFilterProvider, "extensionFilterProvider");
115 }
116
117 @Override
118 public WebSocketExtensionData newRequestData() {
119 HashMap<String, String> parameters = new HashMap<String, String>(4);
120 if (requestedServerNoContext) {
121 parameters.put(SERVER_NO_CONTEXT, null);
122 }
123 if (allowClientNoContext) {
124 parameters.put(CLIENT_NO_CONTEXT, null);
125 }
126 if (requestedServerWindowSize != MAX_WINDOW_SIZE) {
127 parameters.put(SERVER_MAX_WINDOW, Integer.toString(requestedServerWindowSize));
128 }
129 if (allowClientWindowSize) {
130 parameters.put(CLIENT_MAX_WINDOW, null);
131 }
132 return new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters);
133 }
134
135 @Override
136 public WebSocketClientExtension handshakeExtension(WebSocketExtensionData extensionData) {
137 if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) {
138 return null;
139 }
140
141 boolean succeed = true;
142 int clientWindowSize = MAX_WINDOW_SIZE;
143 int serverWindowSize = MAX_WINDOW_SIZE;
144 boolean serverNoContext = false;
145 boolean clientNoContext = false;
146
147 Iterator<Entry<String, String>> parametersIterator =
148 extensionData.parameters().entrySet().iterator();
149 while (succeed && parametersIterator.hasNext()) {
150 Entry<String, String> parameter = parametersIterator.next();
151
152 if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
153
154 if (allowClientWindowSize) {
155 clientWindowSize = Integer.parseInt(parameter.getValue());
156 if (clientWindowSize > MAX_WINDOW_SIZE || clientWindowSize < MIN_WINDOW_SIZE) {
157 succeed = false;
158 }
159 } else {
160 succeed = false;
161 }
162 } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
163
164 serverWindowSize = Integer.parseInt(parameter.getValue());
165 if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) {
166 succeed = false;
167 }
168 } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
169
170 if (allowClientNoContext) {
171 clientNoContext = true;
172 } else {
173 succeed = false;
174 }
175 } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
176
177 serverNoContext = true;
178 } else {
179
180 succeed = false;
181 }
182 }
183
184 if ((requestedServerNoContext && !serverNoContext) ||
185 requestedServerWindowSize < serverWindowSize) {
186 succeed = false;
187 }
188
189 if (succeed) {
190 return new PermessageDeflateExtension(serverNoContext, serverWindowSize,
191 clientNoContext, clientWindowSize, extensionFilterProvider);
192 } else {
193 return null;
194 }
195 }
196
197 private final class PermessageDeflateExtension implements WebSocketClientExtension {
198
199 private final boolean serverNoContext;
200 private final int serverWindowSize;
201 private final boolean clientNoContext;
202 private final int clientWindowSize;
203 private final WebSocketExtensionFilterProvider extensionFilterProvider;
204
205 @Override
206 public int rsv() {
207 return RSV1;
208 }
209
210 PermessageDeflateExtension(boolean serverNoContext, int serverWindowSize,
211 boolean clientNoContext, int clientWindowSize,
212 WebSocketExtensionFilterProvider extensionFilterProvider) {
213 this.serverNoContext = serverNoContext;
214 this.serverWindowSize = serverWindowSize;
215 this.clientNoContext = clientNoContext;
216 this.clientWindowSize = clientWindowSize;
217 this.extensionFilterProvider = extensionFilterProvider;
218 }
219
220 @Override
221 public WebSocketExtensionEncoder newExtensionEncoder() {
222 return new PerMessageDeflateEncoder(compressionLevel, clientWindowSize, clientNoContext,
223 extensionFilterProvider.encoderFilter());
224 }
225
226 @Override
227 public WebSocketExtensionDecoder newExtensionDecoder() {
228 return new PerMessageDeflateDecoder(serverNoContext, extensionFilterProvider.decoderFilter());
229 }
230 }
231
232 }