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