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         headers.add(HttpHeaderNames.UPGRADE, WEBSOCKET)
135                .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
136                .add(HttpHeaderNames.HOST, websocketHostValue(wsURL))
137                .add(HttpHeaderNames.ORIGIN, websocketOriginValue(wsURL))
138                .add(HttpHeaderNames.SEC_WEBSOCKET_KEY1, key1)
139                .add(HttpHeaderNames.SEC_WEBSOCKET_KEY2, key2);
140 
141         String expectedSubprotocol = expectedSubprotocol();
142         if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
143             headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
144         }
145 
146         if (customHeaders != null) {
147             headers.add(customHeaders);
148         }
149 
150         // Set Content-Length to workaround some known defect.
151         // See also: http://www.ietf.org/mail-archive/web/hybi/current/msg02149.html
152         headers.set(HttpHeaderNames.CONTENT_LENGTH, key3.length);
153         request.content().writeBytes(key3);
154         return request;
155     }
156 
157     /**
158      * <p>
159      * Process server response:
160      * </p>
161      *
162      * <pre>
163      * HTTP/1.1 101 WebSocket Protocol Handshake
164      * Upgrade: WebSocket
165      * Connection: Upgrade
166      * Sec-WebSocket-Origin: http://example.com
167      * Sec-WebSocket-Location: ws://example.com/demo
168      * Sec-WebSocket-Protocol: sample
169      *
170      * 8jKS'y:G*Co,Wxa-
171      * </pre>
172      *
173      * @param response
174      *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
175      * @throws WebSocketHandshakeException
176      */
177     @Override
178     protected void verify(FullHttpResponse response) {
179         if (!response.status().equals(HttpResponseStatus.SWITCHING_PROTOCOLS)) {
180             throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status());
181         }
182 
183         HttpHeaders headers = response.headers();
184 
185         CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
186         if (!WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
187             throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
188                     + upgrade);
189         }
190 
191         if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
192             throw new WebSocketHandshakeException("Invalid handshake response connection: "
193                     + headers.get(HttpHeaderNames.CONNECTION));
194         }
195 
196         ByteBuf challenge = response.content();
197         if (!challenge.equals(expectedChallengeResponseBytes)) {
198             throw new WebSocketHandshakeException("Invalid challenge");
199         }
200     }
201 
202     private static String insertRandomCharacters(String key) {
203         int count = WebSocketUtil.randomNumber(1, 12);
204 
205         char[] randomChars = new char[count];
206         int randCount = 0;
207         while (randCount < count) {
208             int rand = (int) (Math.random() * 0x7e + 0x21);
209             if (0x21 < rand && rand < 0x2f || 0x3a < rand && rand < 0x7e) {
210                 randomChars[randCount] = (char) rand;
211                 randCount += 1;
212             }
213         }
214 
215         for (int i = 0; i < count; i++) {
216             int split = WebSocketUtil.randomNumber(0, key.length());
217             String part1 = key.substring(0, split);
218             String part2 = key.substring(split);
219             key = part1 + randomChars[i] + part2;
220         }
221 
222         return key;
223     }
224 
225     private static String insertSpaces(String key, int spaces) {
226         for (int i = 0; i < spaces; i++) {
227             int split = WebSocketUtil.randomNumber(1, key.length() - 1);
228             String part1 = key.substring(0, split);
229             String part2 = key.substring(split);
230             key = part1 + ' ' + part2;
231         }
232 
233         return key;
234     }
235 
236     @Override
237     protected WebSocketFrameDecoder newWebsocketDecoder() {
238         return new WebSocket00FrameDecoder(maxFramePayloadLength());
239     }
240 
241     @Override
242     protected WebSocketFrameEncoder newWebSocketEncoder() {
243         return new WebSocket00FrameEncoder();
244     }
245 }