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.HttpHeaderNames;
22  import io.netty.handler.codec.http.HttpHeaderValues;
23  import io.netty.handler.codec.http.HttpHeaders;
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-07" >draft-ietf-hybi-thewebsocketprotocol-
37   * 10</a>
38   * </p>
39   */
40  public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker {
41  
42      private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker07.class);
43      public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
44  
45      private String expectedChallengeResponseString;
46  
47      private final boolean allowExtensions;
48      private final boolean performMasking;
49      private final boolean allowMaskMismatch;
50  
51      /**
52       * Creates a new instance.
53       *
54       * @param webSocketURL
55       *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
56       *            sent to this URL.
57       * @param version
58       *            Version of web socket specification to use to connect to the server
59       * @param subprotocol
60       *            Sub protocol request sent to the server.
61       * @param allowExtensions
62       *            Allow extensions to be used in the reserved bits of the web socket frame
63       * @param customHeaders
64       *            Map of custom headers to add to the client request
65       * @param maxFramePayloadLength
66       *            Maximum length of a frame's payload
67       */
68      public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
69                                         boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
70          this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, true, false);
71      }
72  
73      /**
74       * Creates a new instance.
75       *
76       * @param webSocketURL
77       *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
78       *            sent to this URL.
79       * @param version
80       *            Version of web socket specification to use to connect to the server
81       * @param subprotocol
82       *            Sub protocol request sent to the server.
83       * @param allowExtensions
84       *            Allow extensions to be used in the reserved bits of the web socket frame
85       * @param customHeaders
86       *            Map of custom headers to add to the client request
87       * @param maxFramePayloadLength
88       *            Maximum length of a frame's payload
89       * @param performMasking
90       *            Whether to mask all written websocket frames. This must be set to true in order to be fully compatible
91       *            with the websocket specifications. Client applications that communicate with a non-standard server
92       *            which doesn't require masking might set this to false to achieve a higher performance.
93       * @param allowMaskMismatch
94       *            Allows to loosen the masking requirement on received frames. When this is set to false then also
95       *            frames which are not masked properly according to the standard will still be accepted.
96       */
97      public WebSocketClientHandshaker07(URI webSocketURL, WebSocketVersion version, String subprotocol,
98              boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
99              boolean performMasking, boolean allowMaskMismatch) {
100         super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
101         this.allowExtensions = allowExtensions;
102         this.performMasking = performMasking;
103         this.allowMaskMismatch = allowMaskMismatch;
104     }
105 
106     /**
107      * /**
108      * <p>
109      * Sends the opening request to the server:
110      * </p>
111      *
112      * <pre>
113      * GET /chat HTTP/1.1
114      * Host: server.example.com
115      * Upgrade: websocket
116      * Connection: Upgrade
117      * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
118      * Sec-WebSocket-Origin: http://example.com
119      * Sec-WebSocket-Protocol: chat, superchat
120      * Sec-WebSocket-Version: 7
121      * </pre>
122      *
123      */
124     @Override
125     protected FullHttpRequest newHandshakeRequest() {
126         // Get path
127         URI wsURL = uri();
128         String path = wsURL.getPath();
129         if (wsURL.getQuery() != null && !wsURL.getQuery().isEmpty()) {
130             path = wsURL.getPath() + '?' + wsURL.getQuery();
131         }
132 
133         if (path == null || path.isEmpty()) {
134             path = "/";
135         }
136 
137         // Get 16 bit nonce and base 64 encode it
138         byte[] nonce = WebSocketUtil.randomBytes(16);
139         String key = WebSocketUtil.base64(nonce);
140 
141         String acceptSeed = key + MAGIC_GUID;
142         byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
143         expectedChallengeResponseString = WebSocketUtil.base64(sha1);
144 
145         if (logger.isDebugEnabled()) {
146             logger.debug(
147                     "WebSocket version 07 client handshake key: {}, expected response: {}",
148                     key, expectedChallengeResponseString);
149         }
150 
151         // Format request
152         FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
153         HttpHeaders headers = request.headers();
154 
155         headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET)
156                .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
157                .add(HttpHeaderNames.SEC_WEBSOCKET_KEY, key)
158                .add(HttpHeaderNames.HOST, wsURL.getHost());
159 
160         int wsPort = wsURL.getPort();
161         String originValue = "http://" + wsURL.getHost();
162         if (wsPort != 80 && wsPort != 443) {
163             // if the port is not standard (80/443) its needed to add the port to the header.
164             // See http://tools.ietf.org/html/rfc6454#section-6.2
165             originValue = originValue + ':' + wsPort;
166         }
167         headers.add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, originValue);
168 
169         String expectedSubprotocol = expectedSubprotocol();
170         if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
171             headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
172         }
173 
174         headers.add(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "7");
175 
176         if (customHeaders != null) {
177             headers.add(customHeaders);
178         }
179         return request;
180     }
181 
182     /**
183      * <p>
184      * Process server response:
185      * </p>
186      *
187      * <pre>
188      * HTTP/1.1 101 Switching Protocols
189      * Upgrade: websocket
190      * Connection: Upgrade
191      * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
192      * Sec-WebSocket-Protocol: chat
193      * </pre>
194      *
195      * @param response
196      *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
197      * @throws WebSocketHandshakeException
198      */
199     @Override
200     protected void verify(FullHttpResponse response) {
201         final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
202         final HttpHeaders headers = response.headers();
203 
204         if (!response.status().equals(status)) {
205             throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status());
206         }
207 
208         CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
209         if (!HttpHeaderValues.WEBSOCKET.equalsIgnoreCase(upgrade)) {
210             throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade);
211         }
212 
213         CharSequence connection = headers.get(HttpHeaderNames.CONNECTION);
214         if (!HttpHeaderValues.UPGRADE.equalsIgnoreCase(connection)) {
215             throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection);
216         }
217 
218         CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT);
219         if (accept == null || !accept.equals(expectedChallengeResponseString)) {
220             throw new WebSocketHandshakeException(String.format(
221                     "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString));
222         }
223     }
224 
225     @Override
226     protected WebSocketFrameDecoder newWebsocketDecoder() {
227         return new WebSocket07FrameDecoder(false, allowExtensions, maxFramePayloadLength(), allowMaskMismatch);
228     }
229 
230     @Override
231     protected WebSocketFrameEncoder newWebSocketEncoder() {
232         return new WebSocket07FrameEncoder(performMasking);
233     }
234 }