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