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 WebSocketClientHandshaker07 extends WebSocketClientHandshaker {
49
50 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker07.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
74
75 public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
76 boolean allowExtensions, Map<String, String> customHeaders,
77 long maxFramePayloadLength) {
78 super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
79 this.allowExtensions = allowExtensions;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 @Override
103 public ChannelFuture handshake(Channel channel) {
104
105 URI wsURL = getWebSocketUrl();
106 String path = wsURL.getPath();
107 if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
108 path = wsURL.getPath() + '?' + wsURL.getQuery();
109 }
110
111 if (path == null || path.length() == 0) {
112 path = "/";
113 }
114
115
116 byte[] nonce = WebSocketUtil.randomBytes(16);
117 String key = WebSocketUtil.base64(ChannelBuffers.wrappedBuffer(nonce));
118
119 String acceptSeed = key + MAGIC_GUID;
120 ChannelBuffer sha1 = WebSocketUtil.sha1(ChannelBuffers.copiedBuffer(acceptSeed, CharsetUtil.US_ASCII));
121 expectedChallengeResponseString = WebSocketUtil.base64(sha1);
122
123 if (logger.isDebugEnabled()) {
124 logger.debug(String.format("WS Version 07 Client Handshake key: %s. Expected response: %s.", key,
125 expectedChallengeResponseString));
126 }
127
128
129 HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
130 request.headers().add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
131 request.headers().add(Names.CONNECTION, Values.UPGRADE);
132 request.headers().add(Names.SEC_WEBSOCKET_KEY, key);
133 request.headers().add(Names.HOST, wsURL.getHost());
134
135 int wsPort = wsURL.getPort();
136 String originValue = "http://" + wsURL.getHost();
137 if (wsPort != 80 && wsPort != 443) {
138
139
140 originValue = originValue + ':' + wsPort;
141 }
142 request.headers().add(Names.SEC_WEBSOCKET_ORIGIN, originValue);
143
144 String expectedSubprotocol = getExpectedSubprotocol();
145 if (expectedSubprotocol != null && expectedSubprotocol.length() > 0) {
146 request.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
147 }
148
149 request.headers().add(Names.SEC_WEBSOCKET_VERSION, "7");
150
151 if (customHeaders != null) {
152 for (Map.Entry<String, String> e : customHeaders.entrySet()) {
153 request.headers().add(e.getKey(), e.getValue());
154 }
155 }
156
157 final ChannelFuture handshakeFuture = new DefaultChannelFuture(channel, false);
158 ChannelFuture future = channel.write(request);
159 future.addListener(new ChannelFutureListener() {
160
161 public void operationComplete(ChannelFuture future) {
162 ChannelPipeline p = future.getChannel().getPipeline();
163 p.addAfter(
164 p.getContext(HttpRequestEncoder.class).getName(),
165 "ws-encoder", new WebSocket07FrameEncoder(true));
166
167 if (future.isSuccess()) {
168 handshakeFuture.setSuccess();
169 } else {
170 handshakeFuture.setFailure(future.getCause());
171 }
172 }
173 });
174
175 return handshakeFuture;
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 @Override
198 public void finishHandshake(Channel channel, HttpResponse response) {
199 final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
200
201 if (!response.getStatus().equals(status)) {
202 throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
203 }
204
205 String upgrade = response.headers().get(Names.UPGRADE);
206 if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) {
207 throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
208 + response.headers().get(Names.UPGRADE));
209 }
210
211 String connection = response.headers().get(Names.CONNECTION);
212 if (!Values.UPGRADE.equalsIgnoreCase(connection)) {
213 throw new WebSocketHandshakeException("Invalid handshake response connection: "
214 + response.headers().get(Names.CONNECTION));
215 }
216
217 String accept = response.headers().get(Names.SEC_WEBSOCKET_ACCEPT);
218 if (accept == null || !accept.equals(expectedChallengeResponseString)) {
219 throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept,
220 expectedChallengeResponseString));
221 }
222
223 String subprotocol = response.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
224 setActualSubprotocol(subprotocol);
225
226 setHandshakeComplete();
227 replaceDecoder(
228 channel,
229 new WebSocket07FrameDecoder(false, allowExtensions, getMaxFramePayloadLength()));
230 }
231 }