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