View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.handler.codec.http.DefaultFullHttpRequest;
21  import io.netty.handler.codec.http.FullHttpRequest;
22  import io.netty.handler.codec.http.FullHttpResponse;
23  import io.netty.handler.codec.http.HttpHeaderNames;
24  import io.netty.handler.codec.http.HttpHeaderValues;
25  import io.netty.handler.codec.http.HttpHeaders;
26  import io.netty.handler.codec.http.HttpMethod;
27  import io.netty.handler.codec.http.HttpResponseStatus;
28  import io.netty.handler.codec.http.HttpVersion;
29  import io.netty.util.AsciiString;
30  
31  import java.net.URI;
32  import java.nio.ByteBuffer;
33  
34  /**
35   * <p>
36   * Performs client side opening and closing handshakes for web socket specification version <a
37   * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" >draft-ietf-hybi-thewebsocketprotocol-
38   * 00</a>
39   * </p>
40   * <p>
41   * A very large portion of this code was taken from the Netty 3.2 HTTP example.
42   * </p>
43   */
44  public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
45  
46      private static final AsciiString WEBSOCKET = AsciiString.cached("WebSocket");
47  
48      private ByteBuf expectedChallengeResponseBytes;
49  
50      /**
51       * Constructor specifying the destination web socket location and version to initiate
52       *
53       * @param webSocketURL
54       *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
55       *            sent to this URL.
56       * @param version
57       *            Version of web socket specification to use to connect to the server
58       * @param subprotocol
59       *            Sub protocol request sent to the server.
60       * @param customHeaders
61       *            Map of custom headers to add to the client request
62       * @param maxFramePayloadLength
63       *            Maximum length of a frame's payload
64       */
65      public WebSocketClientHandshaker00(URI webSocketURL, WebSocketVersion version, String subprotocol,
66              HttpHeaders customHeaders, int maxFramePayloadLength) {
67          super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
68      }
69  
70      /**
71       * <p>
72       * Sends the opening request to the server:
73       * </p>
74       *
75       * <pre>
76       * GET /demo HTTP/1.1
77       * Upgrade: WebSocket
78       * Connection: Upgrade
79       * Host: example.com
80       * Origin: http://example.com
81       * Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
82       * Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
83       *
84       * ^n:ds[4U
85       * </pre>
86       *
87       */
88      @Override
89      protected FullHttpRequest newHandshakeRequest() {
90          // Make keys
91          int spaces1 = WebSocketUtil.randomNumber(1, 12);
92          int spaces2 = WebSocketUtil.randomNumber(1, 12);
93  
94          int max1 = Integer.MAX_VALUE / spaces1;
95          int max2 = Integer.MAX_VALUE / spaces2;
96  
97          int number1 = WebSocketUtil.randomNumber(0, max1);
98          int number2 = WebSocketUtil.randomNumber(0, max2);
99  
100         int product1 = number1 * spaces1;
101         int product2 = number2 * spaces2;
102 
103         String key1 = Integer.toString(product1);
104         String key2 = Integer.toString(product2);
105 
106         key1 = insertRandomCharacters(key1);
107         key2 = insertRandomCharacters(key2);
108 
109         key1 = insertSpaces(key1, spaces1);
110         key2 = insertSpaces(key2, spaces2);
111 
112         byte[] key3 = WebSocketUtil.randomBytes(8);
113 
114         ByteBuffer buffer = ByteBuffer.allocate(4);
115         buffer.putInt(number1);
116         byte[] number1Array = buffer.array();
117         buffer = ByteBuffer.allocate(4);
118         buffer.putInt(number2);
119         byte[] number2Array = buffer.array();
120 
121         byte[] challenge = new byte[16];
122         System.arraycopy(number1Array, 0, challenge, 0, 4);
123         System.arraycopy(number2Array, 0, challenge, 4, 4);
124         System.arraycopy(key3, 0, challenge, 8, 8);
125         expectedChallengeResponseBytes = Unpooled.wrappedBuffer(WebSocketUtil.md5(challenge));
126 
127         // Get path
128         URI wsURL = uri();
129         String path = rawPath(wsURL);
130 
131         // Format request
132         FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
133         HttpHeaders headers = request.headers();
134 
135         if (customHeaders != null) {
136             headers.add(customHeaders);
137         }
138 
139         headers.set(HttpHeaderNames.UPGRADE, WEBSOCKET)
140                .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
141                .set(HttpHeaderNames.HOST, websocketHostValue(wsURL))
142                .set(HttpHeaderNames.ORIGIN, websocketOriginValue(wsURL))
143                .set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, key1)
144                .set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, key2);
145 
146         String expectedSubprotocol = expectedSubprotocol();
147         if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
148             headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
149         }
150 
151         // Set Content-Length to workaround some known defect.
152         // See also: http://www.ietf.org/mail-archive/web/hybi/current/msg02149.html
153         headers.set(HttpHeaderNames.CONTENT_LENGTH, key3.length);
154         request.content().writeBytes(key3);
155         return request;
156     }
157 
158     /**
159      * <p>
160      * Process server response:
161      * </p>
162      *
163      * <pre>
164      * HTTP/1.1 101 WebSocket Protocol Handshake
165      * Upgrade: WebSocket
166      * Connection: Upgrade
167      * Sec-WebSocket-Origin: http://example.com
168      * Sec-WebSocket-Location: ws://example.com/demo
169      * Sec-WebSocket-Protocol: sample
170      *
171      * 8jKS'y:G*Co,Wxa-
172      * </pre>
173      *
174      * @param response
175      *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
176      * @throws WebSocketHandshakeException
177      */
178     @Override
179     protected void verify(FullHttpResponse response) {
180         if (!response.status().equals(HttpResponseStatus.SWITCHING_PROTOCOLS)) {
181             throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status());
182         }
183 
184         HttpHeaders headers = response.headers();
185 
186         CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
187         if (!WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
188             throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
189                     + upgrade);
190         }
191 
192         if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
193             throw new WebSocketHandshakeException("Invalid handshake response connection: "
194                     + headers.get(HttpHeaderNames.CONNECTION));
195         }
196 
197         ByteBuf challenge = response.content();
198         if (!challenge.equals(expectedChallengeResponseBytes)) {
199             throw new WebSocketHandshakeException("Invalid challenge");
200         }
201     }
202 
203     private static String insertRandomCharacters(String key) {
204         int count = WebSocketUtil.randomNumber(1, 12);
205 
206         char[] randomChars = new char[count];
207         int randCount = 0;
208         while (randCount < count) {
209             int rand = (int) (Math.random() * 0x7e + 0x21);
210             if (0x21 < rand && rand < 0x2f || 0x3a < rand && rand < 0x7e) {
211                 randomChars[randCount] = (char) rand;
212                 randCount += 1;
213             }
214         }
215 
216         for (int i = 0; i < count; i++) {
217             int split = WebSocketUtil.randomNumber(0, key.length());
218             String part1 = key.substring(0, split);
219             String part2 = key.substring(split);
220             key = part1 + randomChars[i] + part2;
221         }
222 
223         return key;
224     }
225 
226     private static String insertSpaces(String key, int spaces) {
227         for (int i = 0; i < spaces; i++) {
228             int split = WebSocketUtil.randomNumber(1, key.length() - 1);
229             String part1 = key.substring(0, split);
230             String part2 = key.substring(split);
231             key = part1 + ' ' + part2;
232         }
233 
234         return key;
235     }
236 
237     @Override
238     protected WebSocketFrameDecoder newWebsocketDecoder() {
239         return new WebSocket00FrameDecoder(maxFramePayloadLength());
240     }
241 
242     @Override
243     protected WebSocketFrameEncoder newWebSocketEncoder() {
244         return new WebSocket00FrameEncoder();
245     }
246 }