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.handler.codec.http.DefaultFullHttpRequest;
19  import io.netty.handler.codec.http.FullHttpRequest;
20  import io.netty.handler.codec.http.FullHttpResponse;
21  import io.netty.handler.codec.http.HttpHeaders;
22  import io.netty.handler.codec.http.HttpHeaders.Names;
23  import io.netty.handler.codec.http.HttpHeaders.Values;
24  import io.netty.handler.codec.http.HttpMethod;
25  import io.netty.handler.codec.http.HttpResponseStatus;
26  import io.netty.handler.codec.http.HttpVersion;
27  import io.netty.util.CharsetUtil;
28  import io.netty.util.internal.logging.InternalLogger;
29  import io.netty.util.internal.logging.InternalLoggerFactory;
30  
31  import java.net.URI;
32  
33  /**
34   * <p>
35   * Performs client side opening and closing handshakes for web socket specification version <a
36   * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
37   * 10</a>
38   * </p>
39   */
40  public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
41  
42      private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker08.class);
43  
44      public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
45  
46      private String expectedChallengeResponseString;
47  
48      private final boolean allowExtensions;
49  
50      /**
51       * Creates a new instance.
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 allowExtensions
61       *            Allow extensions to be used in the reserved bits of the web socket frame
62       * @param customHeaders
63       *            Map of custom headers to add to the client request
64       * @param maxFramePayloadLength
65       *            Maximum length of a frame's payload
66       */
67      public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
68              boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
69          super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
70          this.allowExtensions = allowExtensions;
71      }
72  
73      /**
74       * /**
75       * <p>
76       * Sends the opening request to the server:
77       * </p>
78       *
79       * <pre>
80       * GET /chat HTTP/1.1
81       * Host: server.example.com
82       * Upgrade: websocket
83       * Connection: Upgrade
84       * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
85       * Sec-WebSocket-Origin: http://example.com
86       * Sec-WebSocket-Protocol: chat, superchat
87       * Sec-WebSocket-Version: 8
88       * </pre>
89       *
90       */
91      @Override
92      protected FullHttpRequest newHandshakeRequest() {
93          // Get path
94          URI wsURL = uri();
95          String path = rawPath(wsURL);
96  
97          // Get 16 bit nonce and base 64 encode it
98          byte[] nonce = WebSocketUtil.randomBytes(16);
99          String key = WebSocketUtil.base64(nonce);
100 
101         String acceptSeed = key + MAGIC_GUID;
102         byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
103         expectedChallengeResponseString = WebSocketUtil.base64(sha1);
104 
105         if (logger.isDebugEnabled()) {
106             logger.debug(
107                     "WebSocket version 08 client handshake key: {}, expected response: {}",
108                     key, expectedChallengeResponseString);
109         }
110 
111         // Format request
112         FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
113         HttpHeaders headers = request.headers();
114 
115         headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)
116                .add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)
117                .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key)
118                .add(HttpHeaders.Names.HOST, websocketHostValue(wsURL))
119                .add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
120 
121         String expectedSubprotocol = expectedSubprotocol();
122         if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
123             headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
124         }
125 
126         headers.add(Names.SEC_WEBSOCKET_VERSION, "8");
127 
128         if (customHeaders != null) {
129             headers.add(customHeaders);
130         }
131         return request;
132     }
133 
134     /**
135      * <p>
136      * Process server response:
137      * </p>
138      *
139      * <pre>
140      * HTTP/1.1 101 Switching Protocols
141      * Upgrade: websocket
142      * Connection: Upgrade
143      * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
144      * Sec-WebSocket-Protocol: chat
145      * </pre>
146      *
147      * @param response
148      *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
149      * @throws WebSocketHandshakeException
150      */
151     @Override
152     protected void verify(FullHttpResponse response) {
153         final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
154         final HttpHeaders headers = response.headers();
155 
156         if (!response.getStatus().equals(status)) {
157             throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus());
158         }
159 
160         String upgrade = headers.get(Names.UPGRADE);
161         if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) {
162             throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade);
163         }
164 
165         if (!headers.containsValue(Names.CONNECTION, Values.UPGRADE, true)) {
166             throw new WebSocketHandshakeException("Invalid handshake response connection: "
167                     + headers.get(Names.CONNECTION));
168         }
169 
170         String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT);
171         if (accept == null || !accept.equals(expectedChallengeResponseString)) {
172             throw new WebSocketHandshakeException(String.format(
173                     "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString));
174         }
175     }
176 
177     @Override
178     protected WebSocketFrameDecoder newWebsocketDecoder() {
179         return new WebSocket08FrameDecoder(false, allowExtensions, maxFramePayloadLength());
180     }
181 
182     @Override
183     protected WebSocketFrameEncoder newWebSocketEncoder() {
184         return new WebSocket08FrameEncoder(true);
185     }
186 }