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