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 WebSocketClientHandshaker07 extends WebSocketClientHandshaker {
42
43 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker07.class);
44 public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
45
46 private String expectedChallengeResponseString;
47
48 private final boolean allowExtensions;
49 private final boolean performMasking;
50 private final boolean allowMaskMismatch;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
70 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
71 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, true, false);
72 }
73
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 public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
99 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
100 boolean performMasking, boolean allowMaskMismatch) {
101 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
102 allowMaskMismatch, DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS);
103 }
104
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 public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
132 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
133 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis) {
134 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
135 allowMaskMismatch, forceCloseTimeoutMillis, false);
136 }
137
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 WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
168 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
169 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis,
170 boolean absoluteUpgradeUrl) {
171 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, performMasking,
172 allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl, true);
173 }
174
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 WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
208 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
209 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis,
210 boolean absoluteUpgradeUrl, boolean generateOriginHeader) {
211 super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength, forceCloseTimeoutMillis,
212 absoluteUpgradeUrl, generateOriginHeader);
213 this.allowExtensions = allowExtensions;
214 this.performMasking = performMasking;
215 this.allowMaskMismatch = allowMaskMismatch;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 @Override
237 protected FullHttpRequest newHandshakeRequest() {
238 URI wsURL = uri();
239
240
241 byte[] nonce = WebSocketUtil.randomBytes(16);
242 String key = WebSocketUtil.base64(nonce);
243
244 String acceptSeed = key + MAGIC_GUID;
245 byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
246 expectedChallengeResponseString = WebSocketUtil.base64(sha1);
247
248 if (logger.isDebugEnabled()) {
249 logger.debug(
250 "WebSocket version 07 client handshake key: {}, expected response: {}",
251 key, expectedChallengeResponseString);
252 }
253
254
255 FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, upgradeUrl(wsURL),
256 Unpooled.EMPTY_BUFFER);
257 HttpHeaders headers = request.headers();
258
259 if (customHeaders != null) {
260 headers.add(customHeaders);
261 if (!headers.contains(HttpHeaderNames.HOST)) {
262
263
264
265 headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
266 }
267 } else {
268 headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
269 }
270
271 headers.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET)
272 .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
273 .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
274
275 if (generateOriginHeader && !headers.contains(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN)) {
276 headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
277 }
278
279 String expectedSubprotocol = expectedSubprotocol();
280 if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
281 headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
282 }
283
284 headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
285 return request;
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 @Override
306 protected void verify(FullHttpResponse response) {
307 HttpResponseStatus status = response.status();
308 if (!HttpResponseStatus.SWITCHING_PROTOCOLS.equals(status)) {
309 throw new WebSocketClientHandshakeException("Invalid handshake response getStatus: " + status, response);
310 }
311
312 HttpHeaders headers = response.headers();
313 CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
314 if (!HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
315 throw new WebSocketClientHandshakeException("Invalid handshake response upgrade: " + upgrade, response);
316 }
317
318 if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
319 throw new WebSocketClientHandshakeException("Invalid handshake response connection: "
320 + headers.get(HttpHeaderNames.CONNECTION), response);
321 }
322
323 CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT);
324 if (accept == null || !accept.equals(expectedChallengeResponseString)) {
325 throw new WebSocketClientHandshakeException(String.format(
326 "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString), response);
327 }
328 }
329
330 @Override
331 protected WebSocketFrameDecoder newWebsocketDecoder() {
332 return new WebSocket07FrameDecoder(false, allowExtensions, maxFramePayloadLength(), allowMaskMismatch);
333 }
334
335 @Override
336 protected WebSocketFrameEncoder newWebSocketEncoder() {
337 return new WebSocket07FrameEncoder(performMasking);
338 }
339
340 @Override
341 public WebSocketClientHandshaker07 setForceCloseTimeoutMillis(long forceCloseTimeoutMillis) {
342 super.setForceCloseTimeoutMillis(forceCloseTimeoutMillis);
343 return this;
344 }
345
346 }