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.channel.Channel;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelPromise;
21  import io.netty.handler.codec.http.DefaultFullHttpResponse;
22  import io.netty.handler.codec.http.HttpHeaderNames;
23  import io.netty.handler.codec.http.HttpUtil;
24  import io.netty.handler.codec.http.HttpRequest;
25  import io.netty.handler.codec.http.HttpResponse;
26  import io.netty.handler.codec.http.HttpResponseStatus;
27  import io.netty.handler.codec.http.HttpVersion;
28  import io.netty.util.internal.ObjectUtil;
29  
30  /**
31   * Auto-detects the version of the Web Socket protocol in use and creates a new proper
32   * {@link WebSocketServerHandshaker}.
33   */
34  public class WebSocketServerHandshakerFactory {
35  
36      private final String webSocketURL;
37  
38      private final String subprotocols;
39  
40      private final WebSocketDecoderConfig decoderConfig;
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. Null if sub protocols not supported.
50       * @param allowExtensions
51       *            Allow extensions to be used in the reserved bits of the web socket frame
52       */
53      public WebSocketServerHandshakerFactory(
54              String webSocketURL, String subprotocols, boolean allowExtensions) {
55          this(webSocketURL, subprotocols, allowExtensions, 65536);
56      }
57  
58      /**
59       * Constructor specifying the destination web socket location
60       *
61       * @param webSocketURL
62       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
63       *            Subsequent web socket frames will be sent to this URL.
64       * @param subprotocols
65       *            CSV of supported protocols. Null if sub protocols not supported.
66       * @param allowExtensions
67       *            Allow extensions to be used in the reserved bits of the web socket frame
68       * @param maxFramePayloadLength
69       *            Maximum allowable frame payload length. Setting this value to your application's
70       *            requirement may reduce denial of service attacks using long data frames.
71       */
72      public WebSocketServerHandshakerFactory(
73              String webSocketURL, String subprotocols, boolean allowExtensions,
74              int maxFramePayloadLength) {
75          this(webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, false);
76      }
77  
78      /**
79       * Constructor specifying the destination web socket location
80       *
81       * @param webSocketURL
82       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
83       *            Subsequent web socket frames will be sent to this URL.
84       * @param subprotocols
85       *            CSV of supported protocols. Null if sub protocols not supported.
86       * @param allowExtensions
87       *            Allow extensions to be used in the reserved bits of the web socket frame
88       * @param maxFramePayloadLength
89       *            Maximum allowable frame payload length. Setting this value to your application's
90       *            requirement may reduce denial of service attacks using long data frames.
91       * @param allowMaskMismatch
92       *            When set to true, frames which are not masked properly according to the standard will still be
93       *            accepted.
94       */
95      public WebSocketServerHandshakerFactory(
96              String webSocketURL, String subprotocols, boolean allowExtensions,
97              int maxFramePayloadLength, boolean allowMaskMismatch) {
98          this(webSocketURL, subprotocols, WebSocketDecoderConfig.newBuilder()
99              .allowExtensions(allowExtensions)
100             .maxFramePayloadLength(maxFramePayloadLength)
101             .allowMaskMismatch(allowMaskMismatch)
102             .build());
103     }
104 
105     /**
106      * Constructor specifying the destination web socket location
107      *
108      * @param webSocketURL
109      *            URL for web socket communications. e.g "ws://myhost.com/mypath".
110      *            Subsequent web socket frames will be sent to this URL.
111      * @param subprotocols
112      *            CSV of supported protocols. Null if sub protocols not supported.
113      * @param decoderConfig
114      *            Frames decoder options.
115      */
116     public WebSocketServerHandshakerFactory(
117             String webSocketURL, String subprotocols, WebSocketDecoderConfig decoderConfig) {
118         this.webSocketURL = webSocketURL;
119         this.subprotocols = subprotocols;
120         this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
121     }
122 
123     /**
124      * Instances a new handshaker
125      *
126      * @return A new WebSocketServerHandshaker for the requested web socket version. Null if web
127      *         socket version is not supported.
128      */
129     public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
130         return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig);
131     }
132 
133     /**
134      * Resolves the client's WebSocket protocol version from the HTTP request header,
135      * and creates the corresponding handshaker.
136      * This method is identical to {@link #newHandshaker(HttpRequest)},
137      * however it does not require a {@link WebSocketServerHandshakerFactory} instance allocation.
138      *
139      * @param req
140      *            The HTTP request that came from the client to upgrade the protocol to WebSocket.
141      * @param webSocketURL
142      *            URL for web socket communications. e.g "ws://myhost.com/mypath".
143      *            Subsequent web socket frames will be sent to this URL.
144      * @param subprotocols
145      *            CSV of supported protocols. Null if sub protocols not supported.
146      * @param decoderConfig
147      *            Frames decoder options.
148      * @return A new {@link WebSocketServerHandshaker} for the requested web socket version. Null if web
149      *         socket version is not supported.
150      * @throws NullPointerException if the {@code decoderConfig} is {@code null}.
151      */
152     public static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, String webSocketURL, String subprotocols,
153                                                               WebSocketDecoderConfig decoderConfig) {
154         ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
155         return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig);
156     }
157 
158     // Like resolveHandshaker(HttpRequest, String, String, WebSocketDecoderConfig) but defers URI construction.
159     static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, boolean isSecure, String host, String path,
160                                                        String subprotocols, WebSocketDecoderConfig decoderConfig) {
161         ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
162         return resolveHandshaker0(req, isSecure, host, path, subprotocols, decoderConfig);
163     }
164 
165     private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
166                                                                 String webSocketURL,
167                                                                 String subprotocols,
168                                                                 WebSocketDecoderConfig decoderConfig) {
169         CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
170         if (version != null) {
171             if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
172                 // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
173                 return new WebSocketServerHandshaker13(
174                         webSocketURL, subprotocols, decoderConfig);
175             } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
176                 // Version 8 of the wire protocol - version 10 of the draft hybi specification.
177                 return new WebSocketServerHandshaker08(
178                         webSocketURL, subprotocols, decoderConfig);
179             } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
180                 // Version 8 of the wire protocol - version 07 of the draft hybi specification.
181                 return new WebSocketServerHandshaker07(
182                         webSocketURL, subprotocols, decoderConfig);
183             } else {
184                 return null;
185             }
186         } else {
187             // Assume version 00 where version header was not specified
188             return new WebSocketServerHandshaker00(webSocketURL, subprotocols, decoderConfig);
189         }
190     }
191 
192     private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
193                                                                 boolean isSecure,
194                                                                 String host,
195                                                                 String path,
196                                                                 String subprotocols,
197                                                                 WebSocketDecoderConfig decoderConfig) {
198         CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
199         if (version != null) {
200             if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
201                 // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
202                 return new WebSocketServerHandshaker13(
203                         isSecure, host, path, subprotocols, decoderConfig);
204             } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
205                 // Version 8 of the wire protocol - version 10 of the draft hybi specification.
206                 return new WebSocketServerHandshaker08(
207                         isSecure, host, path, subprotocols, decoderConfig);
208             } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
209                 // Version 8 of the wire protocol - version 07 of the draft hybi specification.
210                 return new WebSocketServerHandshaker07(
211                         isSecure, host, path, subprotocols, decoderConfig);
212             } else {
213                 return null;
214             }
215         } else {
216             // Assume version 00 where version header was not specified
217             return new WebSocketServerHandshaker00(isSecure, host, path, subprotocols, decoderConfig);
218         }
219     }
220 
221     /**
222      * @deprecated use {@link #sendUnsupportedVersionResponse(Channel)}
223      */
224     @Deprecated
225     public static void sendUnsupportedWebSocketVersionResponse(Channel channel) {
226         sendUnsupportedVersionResponse(channel);
227     }
228 
229     /**
230      * Return that we need cannot support the web socket version
231      */
232     public static ChannelFuture sendUnsupportedVersionResponse(Channel channel) {
233         return sendUnsupportedVersionResponse(channel, channel.newPromise());
234     }
235 
236     /**
237      * Return that we need cannot support the web socket version
238      */
239     public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) {
240         HttpResponse res = new DefaultFullHttpResponse(
241                 HttpVersion.HTTP_1_1,
242                 HttpResponseStatus.UPGRADE_REQUIRED, channel.alloc().buffer(0));
243         res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
244         HttpUtil.setContentLength(res, 0);
245         return channel.writeAndFlush(res, promise);
246     }
247 }