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 import org.jboss.netty.logging.InternalLogger;
35 import org.jboss.netty.logging.InternalLoggerFactory;
36 import org.jboss.netty.util.CharsetUtil;
37
38 import java.net.URI;
39 import java.util.Map;
40
41
42
43
44
45
46
47
48 public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
49
50 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker13.class);
51
52 public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
53
54 private String expectedChallengeResponseString;
55
56 private final boolean allowExtensions;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public WebSocketClientHandshaker13(URI webSocketURL, WebSocketVersion version, String subprotocol,
74 boolean allowExtensions, Map<String, String> customHeaders) {
75 this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, Long.MAX_VALUE);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public WebSocketClientHandshaker13(URI webSocketURL, WebSocketVersion version, String subprotocol,
96 boolean allowExtensions, Map<String, String> customHeaders, long maxFramePayloadLength) {
97 super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
98 this.allowExtensions = allowExtensions;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @Override
122 public ChannelFuture handshake(Channel channel) throws Exception {
123
124 URI wsURL = getWebSocketUrl();
125 String path = wsURL.getPath();
126 if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
127 path = wsURL.getPath() + '?' + wsURL.getQuery();
128 }
129
130 if (path == null || path.length() == 0) {
131 path = "/";
132 }
133
134
135 ChannelBuffer nonce = ChannelBuffers.wrappedBuffer(WebSocketUtil.randomBytes(16));
136 String key = WebSocketUtil.base64(nonce);
137
138 String acceptSeed = key + MAGIC_GUID;
139 ChannelBuffer sha1 = WebSocketUtil.sha1(ChannelBuffers.copiedBuffer(acceptSeed, CharsetUtil.US_ASCII));
140 expectedChallengeResponseString = WebSocketUtil.base64(sha1);
141
142 if (logger.isDebugEnabled()) {
143 logger.debug(String.format("WS Version 13 Client Handshake key: %s. Expected response: %s.", key,
144 expectedChallengeResponseString));
145 }
146
147
148 int wsPort = wsURL.getPort();
149 HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
150 request.headers().add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
151 request.headers().add(Names.CONNECTION, Values.UPGRADE);
152 request.headers().add(Names.SEC_WEBSOCKET_KEY, key);
153 request.headers().add(Names.HOST, wsURL.getHost() + ':' + wsPort);
154
155 String originValue = "http://" + wsURL.getHost();
156 if (wsPort != 80 && wsPort != 443) {
157
158
159 originValue = originValue + ':' + wsPort;
160 }
161 request.headers().add(Names.ORIGIN, originValue);
162
163 String expectedSubprotocol = getExpectedSubprotocol();
164 if (expectedSubprotocol != null && expectedSubprotocol.length() != 0) {
165 request.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
166 }
167
168 request.headers().add(Names.SEC_WEBSOCKET_VERSION, "13");
169
170 if (customHeaders != null) {
171 for (Map.Entry<String, String> e: customHeaders.entrySet()) {
172 request.headers().add(e.getKey(), e.getValue());
173 }
174 }
175
176 ChannelFuture future = channel.write(request);
177 final ChannelFuture handshakeFuture = new DefaultChannelFuture(channel, false);
178
179 future.addListener(new ChannelFutureListener() {
180 public void operationComplete(ChannelFuture future) {
181 ChannelPipeline p = future.getChannel().getPipeline();
182 p.replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true));
183
184 if (future.isSuccess()) {
185 handshakeFuture.setSuccess();
186 } else {
187 handshakeFuture.setFailure(future.getCause());
188 }
189 }
190 });
191
192 return handshakeFuture;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 @Override
215 public void finishHandshake(Channel channel, HttpResponse response) {
216 final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
217
218 if (!response.getStatus().equals(status)) {
219 throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
220 }
221
222 String upgrade = response.headers().get(Names.UPGRADE);
223
224
225 if (upgrade == null || !upgrade.toLowerCase().equals(Values.WEBSOCKET.toLowerCase())) {
226 throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
227 + response.headers().get(Names.UPGRADE));
228 }
229
230
231
232 String connection = response.headers().get(Names.CONNECTION);
233 if (connection == null || !connection.toLowerCase().equals(Values.UPGRADE.toLowerCase())) {
234 throw new WebSocketHandshakeException("Invalid handshake response connection: "
235 + response.headers().get(Names.CONNECTION));
236 }
237
238 String accept = response.headers().get(Names.SEC_WEBSOCKET_ACCEPT);
239 if (accept == null || !accept.equals(expectedChallengeResponseString)) {
240 throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept,
241 expectedChallengeResponseString));
242 }
243
244 String subprotocol = response.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
245 setActualSubprotocol(subprotocol);
246
247 setHandshakeComplete();
248 replaceDecoder(
249 channel,
250 new WebSocket13FrameDecoder(false, allowExtensions, getMaxFramePayloadLength()));
251 }
252 }