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.DefaultFullHttpResponse;
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.HttpResponseStatus;
25  import io.netty.util.CharsetUtil;
26  
27  import static io.netty.handler.codec.http.HttpVersion.*;
28  
29  /**
30   * <p>
31   * Performs server side opening and closing handshakes for web socket specification version <a
32   * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
33   * 10</a>
34   * </p>
35   */
36  public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
37  
38      public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
39  
40      private final boolean allowExtensions;
41      private final boolean allowMaskMismatch;
42  
43      /**
44       * Constructor specifying the destination web socket location
45       *
46       * @param webSocketURL
47       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
48       *            Subsequent web socket frames will be sent to this URL.
49       * @param subprotocols
50       *            CSV of supported protocols
51       * @param allowExtensions
52       *            Allow extensions to be used in the reserved bits of the web socket frame
53       * @param maxFramePayloadLength
54       *            Maximum allowable frame payload length. Setting this value to your application's
55       *            requirement may reduce denial of service attacks using long data frames.
56       */
57      public WebSocketServerHandshaker08(
58              String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
59          this(webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, false);
60      }
61  
62      /**
63       * Constructor specifying the destination web socket location
64       *
65       * @param webSocketURL
66       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
67       *            Subsequent web socket frames will be sent to this URL.
68       * @param subprotocols
69       *            CSV of supported protocols
70       * @param allowExtensions
71       *            Allow extensions to be used in the reserved bits of the web socket frame
72       * @param maxFramePayloadLength
73       *            Maximum allowable frame payload length. Setting this value to your application's
74       *            requirement may reduce denial of service attacks using long data frames.
75       * @param allowMaskMismatch
76       *            Allows to loosen the masking requirement on received frames. When this is set to false then also
77       *            frames which are not masked properly according to the standard will still be accepted.
78       */
79      public WebSocketServerHandshaker08(
80              String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength,
81              boolean allowMaskMismatch) {
82          super(WebSocketVersion.V08, webSocketURL, subprotocols, maxFramePayloadLength);
83          this.allowExtensions = allowExtensions;
84          this.allowMaskMismatch = allowMaskMismatch;
85      }
86  
87      /**
88       * <p>
89       * Handle the web socket handshake for the web socket specification <a href=
90       * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi version 8 to 10</a>. Version 8, 9 and
91       * 10 share the same wire protocol.
92       * </p>
93       *
94       * <p>
95       * Browser request to the server:
96       * </p>
97       *
98       * <pre>
99       * GET /chat HTTP/1.1
100      * Host: server.example.com
101      * Upgrade: websocket
102      * Connection: Upgrade
103      * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
104      * Sec-WebSocket-Origin: http://example.com
105      * Sec-WebSocket-Protocol: chat, superchat
106      * Sec-WebSocket-Version: 8
107      * </pre>
108      *
109      * <p>
110      * Server response:
111      * </p>
112      *
113      * <pre>
114      * HTTP/1.1 101 Switching Protocols
115      * Upgrade: websocket
116      * Connection: Upgrade
117      * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
118      * Sec-WebSocket-Protocol: chat
119      * </pre>
120      */
121     @Override
122     protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
123         FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
124 
125         if (headers != null) {
126             res.headers().add(headers);
127         }
128 
129         CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY);
130         if (key == null) {
131             throw new WebSocketHandshakeException("not a WebSocket request: missing key");
132         }
133         String acceptSeed = key + WEBSOCKET_08_ACCEPT_GUID;
134         byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
135         String accept = WebSocketUtil.base64(sha1);
136 
137         if (logger.isDebugEnabled()) {
138             logger.debug("WebSocket version 08 server handshake key: {}, response: {}", key, accept);
139         }
140 
141         res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
142         res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
143         res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept);
144         String subprotocols = req.headers().getAndConvert(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
145         if (subprotocols != null) {
146             String selectedSubprotocol = selectSubprotocol(subprotocols);
147             if (selectedSubprotocol == null) {
148                 if (logger.isDebugEnabled()) {
149                     logger.debug("Requested subprotocol(s) not supported: {}", subprotocols);
150                 }
151             } else {
152                 res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
153             }
154         }
155         return res;
156     }
157 
158     @Override
159     protected WebSocketFrameDecoder newWebsocketDecoder() {
160         return new WebSocket08FrameDecoder(true, allowExtensions, maxFramePayloadLength(), allowMaskMismatch);
161     }
162 
163     @Override
164     protected WebSocketFrameEncoder newWebSocketEncoder() {
165         return new WebSocket08FrameEncoder(false);
166     }
167 }