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