1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.websocketx;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.channel.Channel;
21 import org.jboss.netty.channel.ChannelFuture;
22 import org.jboss.netty.channel.ChannelFutureListener;
23 import org.jboss.netty.channel.ChannelPipeline;
24 import org.jboss.netty.channel.DefaultChannelFuture;
25 import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
26 import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
27 import org.jboss.netty.handler.codec.http.HttpHeaders.Values;
28 import org.jboss.netty.handler.codec.http.HttpMethod;
29 import org.jboss.netty.handler.codec.http.HttpRequest;
30 import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
31 import org.jboss.netty.handler.codec.http.HttpResponse;
32 import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
33 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
34 import org.jboss.netty.handler.codec.http.HttpVersion;
35
36 import java.net.URI;
37 import java.nio.ByteBuffer;
38 import java.util.Map;
39
40
41
42
43
44
45
46
47
48
49
50 public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
51
52 private ChannelBuffer expectedChallengeResponseBytes;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public WebSocketClientHandshaker00(URI webSocketURL, WebSocketVersion version, String subprotocol,
68 Map<String, String> customHeaders) {
69 this(webSocketURL, version, subprotocol, customHeaders, Long.MAX_VALUE);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public WebSocketClientHandshaker00(URI webSocketURL, WebSocketVersion version, String subprotocol,
88 Map<String, String> customHeaders, long maxFramePayloadLength) {
89 super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 @Override
113 public ChannelFuture handshake(Channel channel) {
114
115 int spaces1 = WebSocketUtil.randomNumber(1, 12);
116 int spaces2 = WebSocketUtil.randomNumber(1, 12);
117
118 int max1 = Integer.MAX_VALUE / spaces1;
119 int max2 = Integer.MAX_VALUE / spaces2;
120
121 int number1 = WebSocketUtil.randomNumber(0, max1);
122 int number2 = WebSocketUtil.randomNumber(0, max2);
123
124 int product1 = number1 * spaces1;
125 int product2 = number2 * spaces2;
126
127 String key1 = Integer.toString(product1);
128 String key2 = Integer.toString(product2);
129
130 key1 = insertRandomCharacters(key1);
131 key2 = insertRandomCharacters(key2);
132
133 key1 = insertSpaces(key1, spaces1);
134 key2 = insertSpaces(key2, spaces2);
135
136 byte[] key3 = WebSocketUtil.randomBytes(8);
137
138 ByteBuffer buffer = ByteBuffer.allocate(4);
139 buffer.putInt(number1);
140 byte[] number1Array = buffer.array();
141 buffer = ByteBuffer.allocate(4);
142 buffer.putInt(number2);
143 byte[] number2Array = buffer.array();
144
145 byte[] challenge = new byte[16];
146 System.arraycopy(number1Array, 0, challenge, 0, 4);
147 System.arraycopy(number2Array, 0, challenge, 4, 4);
148 System.arraycopy(key3, 0, challenge, 8, 8);
149 expectedChallengeResponseBytes = WebSocketUtil.md5(ChannelBuffers.wrappedBuffer(challenge));
150
151
152 URI wsURL = getWebSocketUrl();
153 String path = wsURL.getPath();
154 if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
155 path = wsURL.getPath() + '?' + wsURL.getQuery();
156 }
157
158 if (path == null || path.length() == 0) {
159 path = "/";
160 }
161
162
163 HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
164 request.addHeader(Names.UPGRADE, Values.WEBSOCKET);
165 request.addHeader(Names.CONNECTION, Values.UPGRADE);
166 request.addHeader(Names.HOST, wsURL.getHost());
167
168 int wsPort = wsURL.getPort();
169 String originValue = "http://" + wsURL.getHost();
170 if (wsPort != 80 && wsPort != 443) {
171
172
173 originValue = originValue + ':' + wsPort;
174 }
175 request.addHeader(Names.ORIGIN, originValue);
176
177 request.addHeader(Names.SEC_WEBSOCKET_KEY1, key1);
178 request.addHeader(Names.SEC_WEBSOCKET_KEY2, key2);
179 String expectedSubprotocol = getExpectedSubprotocol();
180 if (expectedSubprotocol != null && expectedSubprotocol.length() != 0) {
181 request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
182 }
183
184 if (customHeaders != null) {
185 for (Map.Entry<String, String> e: customHeaders.entrySet()) {
186 request.addHeader(e.getKey(), e.getValue());
187 }
188 }
189
190
191
192 request.setHeader(Names.CONTENT_LENGTH, key3.length);
193 request.setContent(ChannelBuffers.copiedBuffer(key3));
194
195 final ChannelFuture handshakeFuture = new DefaultChannelFuture(channel, false);
196 ChannelFuture future = channel.write(request);
197
198 future.addListener(new ChannelFutureListener() {
199 public void operationComplete(ChannelFuture future) {
200 ChannelPipeline p = future.getChannel().getPipeline();
201 p.replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder());
202
203 if (future.isSuccess()) {
204 handshakeFuture.setSuccess();
205 } else {
206 handshakeFuture.setFailure(future.getCause());
207 }
208 }
209 });
210
211 return handshakeFuture;
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 @Override
237 public void finishHandshake(Channel channel, HttpResponse response) {
238 final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
239
240 if (!response.getStatus().equals(status)) {
241 throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
242 }
243
244 String upgrade = response.getHeader(Names.UPGRADE);
245 if (!Values.WEBSOCKET.equals(upgrade)) {
246 throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
247 + upgrade);
248 }
249
250 String connection = response.getHeader(Names.CONNECTION);
251 if (!Values.UPGRADE.equals(connection)) {
252 throw new WebSocketHandshakeException("Invalid handshake response connection: "
253 + connection);
254 }
255
256 ChannelBuffer challenge = response.getContent();
257 if (!challenge.equals(expectedChallengeResponseBytes)) {
258 throw new WebSocketHandshakeException("Invalid challenge");
259 }
260
261 String subprotocol = response.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
262 setActualSubprotocol(subprotocol);
263
264 setHandshakeComplete();
265
266 channel.getPipeline().get(HttpResponseDecoder.class).replace("ws-decoder",
267 new WebSocket00FrameDecoder(getMaxFramePayloadLength()));
268 }
269
270 private static String insertRandomCharacters(String key) {
271 int count = WebSocketUtil.randomNumber(1, 12);
272
273 char[] randomChars = new char[count];
274 int randCount = 0;
275 while (randCount < count) {
276 int rand = (int) (Math.random() * 0x7e + 0x21);
277 if (0x21 < rand && rand < 0x2f || 0x3a < rand && rand < 0x7e) {
278 randomChars[randCount] = (char) rand;
279 randCount += 1;
280 }
281 }
282
283 for (int i = 0; i < count; i++) {
284 int split = WebSocketUtil.randomNumber(0, key.length());
285 String part1 = key.substring(0, split);
286 String part2 = key.substring(split);
287 key = part1 + randomChars[i] + part2;
288 }
289
290 return key;
291 }
292
293 private static String insertSpaces(String key, int spaces) {
294 for (int i = 0; i < spaces; i++) {
295 int split = WebSocketUtil.randomNumber(1, key.length() - 1);
296 String part1 = key.substring(0, split);
297 String part2 = key.substring(split);
298 key = part1 + ' ' + part2;
299 }
300
301 return key;
302 }
303
304 }