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.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  
29  /**
30   * Auto-detects the version of the Web Socket protocol in use and creates a new proper
31   * {@link WebSocketServerHandshaker}.
32   */
33  public class WebSocketServerHandshakerFactory {
34  
35      private final String webSocketURL;
36  
37      private final String subprotocols;
38  
39      private final boolean allowExtensions;
40  
41      private final int maxFramePayloadLength;
42  
43      private final boolean allowMaskMismatch;
44  
45      /**
46       * Constructor specifying the destination web socket location
47       *
48       * @param webSocketURL
49       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
50       *            Subsequent web socket frames will be sent to this URL.
51       * @param subprotocols
52       *            CSV of supported protocols. Null if sub protocols not supported.
53       * @param allowExtensions
54       *            Allow extensions to be used in the reserved bits of the web socket frame
55       */
56      public WebSocketServerHandshakerFactory(
57              String webSocketURL, String subprotocols, boolean allowExtensions) {
58          this(webSocketURL, subprotocols, allowExtensions, 65536);
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. Null if sub protocols not supported.
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       */
75      public WebSocketServerHandshakerFactory(
76              String webSocketURL, String subprotocols, boolean allowExtensions,
77              int maxFramePayloadLength) {
78          this(webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, false);
79      }
80  
81      /**
82       * Constructor specifying the destination web socket location
83       *
84       * @param webSocketURL
85       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
86       *            Subsequent web socket frames will be sent to this URL.
87       * @param subprotocols
88       *            CSV of supported protocols. Null if sub protocols not supported.
89       * @param allowExtensions
90       *            Allow extensions to be used in the reserved bits of the web socket frame
91       * @param maxFramePayloadLength
92       *            Maximum allowable frame payload length. Setting this value to your application's
93       *            requirement may reduce denial of service attacks using long data frames.
94       * @param allowMaskMismatch
95       *            When set to true, frames which are not masked properly according to the standard will still be
96       *            accepted.
97       */
98      public WebSocketServerHandshakerFactory(
99              String webSocketURL, String subprotocols, boolean allowExtensions,
100             int maxFramePayloadLength, boolean allowMaskMismatch) {
101         this.webSocketURL = webSocketURL;
102         this.subprotocols = subprotocols;
103         this.allowExtensions = allowExtensions;
104         this.maxFramePayloadLength = maxFramePayloadLength;
105         this.allowMaskMismatch = allowMaskMismatch;
106     }
107 
108     /**
109      * Instances a new handshaker
110      *
111      * @return A new WebSocketServerHandshaker for the requested web socket version. Null if web
112      *         socket version is not supported.
113      */
114     public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
115 
116         CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
117         if (version != null) {
118             if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
119                 // Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
120                 return new WebSocketServerHandshaker13(
121                         webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch);
122             } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
123                 // Version 8 of the wire protocol - version 10 of the draft hybi specification.
124                 return new WebSocketServerHandshaker08(
125                         webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch);
126             } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
127                 // Version 8 of the wire protocol - version 07 of the draft hybi specification.
128                 return new WebSocketServerHandshaker07(
129                         webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch);
130             } else {
131                 return null;
132             }
133         } else {
134             // Assume version 00 where version header was not specified
135             return new WebSocketServerHandshaker00(webSocketURL, subprotocols, maxFramePayloadLength);
136         }
137     }
138 
139     /**
140      * @deprecated use {@link #sendUnsupportedVersionResponse(Channel)}
141      */
142     @Deprecated
143     public static void sendUnsupportedWebSocketVersionResponse(Channel channel) {
144         sendUnsupportedVersionResponse(channel);
145     }
146 
147     /**
148      * Return that we need cannot not support the web socket version
149      */
150     public static ChannelFuture sendUnsupportedVersionResponse(Channel channel) {
151         return sendUnsupportedVersionResponse(channel, channel.newPromise());
152     }
153 
154     /**
155      * Return that we need cannot not support the web socket version
156      */
157     public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) {
158         HttpResponse res = new DefaultFullHttpResponse(
159                 HttpVersion.HTTP_1_1,
160                 HttpResponseStatus.UPGRADE_REQUIRED);
161         res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
162         HttpUtil.setContentLength(res, 0);
163         return channel.writeAndFlush(res, promise);
164     }
165 }