1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.websocketx;
17
18 import io.netty.buffer.Unpooled;
19 import io.netty.handler.codec.http.DefaultFullHttpRequest;
20 import io.netty.handler.codec.http.FullHttpRequest;
21 import io.netty.handler.codec.http.FullHttpResponse;
22 import io.netty.handler.codec.http.HttpHeaderNames;
23 import io.netty.handler.codec.http.HttpHeaderValues;
24 import io.netty.handler.codec.http.HttpHeaders;
25 import io.netty.handler.codec.http.HttpMethod;
26 import io.netty.handler.codec.http.HttpResponseStatus;
27 import io.netty.handler.codec.http.HttpVersion;
28 import io.netty.util.CharsetUtil;
29 import io.netty.util.internal.logging.InternalLogger;
30 import io.netty.util.internal.logging.InternalLoggerFactory;
31
32 import java.net.URI;
33
34
35
36
37
38
39
40
41 public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
42
43 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker08.class);
44
45 public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
46
47 private String expectedChallengeResponseString;
48
49 private final boolean allowExtensions;
50 private final boolean performMasking;
51 private final boolean allowMaskMismatch;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
71 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
72 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, true,
73 false, DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS);
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
101 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
102 boolean performMasking, boolean allowMaskMismatch) {
103 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
104 allowMaskMismatch, DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
134 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
135 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis) {
136 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
137 allowMaskMismatch, forceCloseTimeoutMillis, false, true);
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
170 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
171 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis,
172 boolean absoluteUpgradeUrl) {
173 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
174 allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl, true);
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
210 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
211 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis,
212 boolean absoluteUpgradeUrl, boolean generateOriginHeader) {
213 super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength, forceCloseTimeoutMillis,
214 absoluteUpgradeUrl, generateOriginHeader);
215 this.allowExtensions = allowExtensions;
216 this.performMasking = performMasking;
217 this.allowMaskMismatch = allowMaskMismatch;
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 @Override
239 protected FullHttpRequest newHandshakeRequest() {
240 URI wsURL = uri();
241
242
243 byte[] nonce = WebSocketUtil.randomBytes(16);
244 String key = WebSocketUtil.base64(nonce);
245
246 String acceptSeed = key + MAGIC_GUID;
247 byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
248 expectedChallengeResponseString = WebSocketUtil.base64(sha1);
249
250 if (logger.isDebugEnabled()) {
251 logger.debug(
252 "WebSocket version 08 client handshake key: {}, expected response: {}",
253 key, expectedChallengeResponseString);
254 }
255
256
257 FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, upgradeUrl(wsURL),
258 Unpooled.EMPTY_BUFFER);
259 HttpHeaders headers = request.headers();
260
261 if (customHeaders != null) {
262 headers.add(customHeaders);
263 if (!headers.contains(HttpHeaderNames.HOST)) {
264
265
266
267 headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
268 }
269 } else {
270 headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
271 }
272
273 headers.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET)
274 .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
275 .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
276
277 if (generateOriginHeader && !headers.contains(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN)) {
278 headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
279 }
280
281 String expectedSubprotocol = expectedSubprotocol();
282 if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
283 headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
284 }
285
286 headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
287 return request;
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 @Override
308 protected void verify(FullHttpResponse response) {
309 HttpResponseStatus status = response.status();
310 if (!HttpResponseStatus.SWITCHING_PROTOCOLS.equals(status)) {
311 throw new WebSocketClientHandshakeException("Invalid handshake response getStatus: " + status, response);
312 }
313
314 HttpHeaders headers = response.headers();
315 CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
316 if (!HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
317 throw new WebSocketClientHandshakeException("Invalid handshake response upgrade: " + upgrade, response);
318 }
319
320 if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
321 throw new WebSocketClientHandshakeException("Invalid handshake response connection: "
322 + headers.get(HttpHeaderNames.CONNECTION), response);
323 }
324
325 CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT);
326 if (accept == null || !accept.equals(expectedChallengeResponseString)) {
327 throw new WebSocketClientHandshakeException(String.format(
328 "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString), response);
329 }
330 }
331
332 @Override
333 protected WebSocketFrameDecoder newWebsocketDecoder() {
334 return new WebSocket08FrameDecoder(false, allowExtensions, maxFramePayloadLength(), allowMaskMismatch);
335 }
336
337 @Override
338 protected WebSocketFrameEncoder newWebSocketEncoder() {
339 return new WebSocket08FrameEncoder(performMasking);
340 }
341
342 @Override
343 public WebSocketClientHandshaker08 setForceCloseTimeoutMillis(long forceCloseTimeoutMillis) {
344 super.setForceCloseTimeoutMillis(forceCloseTimeoutMillis);
345 return this;
346 }
347
348 }