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    *   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.netty5.handler.codec.http.websocketx;
17  
18  import io.netty5.handler.codec.http.HttpHeaders;
19  
20  import java.net.URI;
21  
22  import static io.netty5.handler.codec.http.websocketx.WebSocketVersion.V13;
23  
24  /**
25   * Creates a new {@link WebSocketClientHandshaker} of desired protocol version.
26   */
27  public final class WebSocketClientHandshakerFactory {
28  
29      /**
30       * Private constructor so this static class cannot be instanced.
31       */
32      private WebSocketClientHandshakerFactory() {
33      }
34  
35      /**
36       * Creates a new handshaker.
37       *
38       * @param webSocketURL
39       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
40       *            Subsequent web socket frames will be sent to this URL.
41       * @param version
42       *            Version of web socket specification to use to connect to the server
43       * @param subprotocol
44       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
45       * @param allowExtensions
46       *            Allow extensions to be used in the reserved bits of the web socket frame
47       * @param customHeaders
48       *            Custom HTTP headers to send during the handshake
49       */
50      public static WebSocketClientHandshaker newHandshaker(
51              URI webSocketURL, WebSocketVersion version, String subprotocol,
52              boolean allowExtensions, HttpHeaders customHeaders) {
53          return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders, 65536);
54      }
55  
56      /**
57       * Creates a new handshaker.
58       *
59       * @param webSocketURL
60       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
61       *            Subsequent web socket frames will be sent to this URL.
62       * @param version
63       *            Version of web socket specification to use to connect to the server
64       * @param subprotocol
65       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
66       * @param allowExtensions
67       *            Allow extensions to be used in the reserved bits of the web socket frame
68       * @param customHeaders
69       *            Custom HTTP headers to send during the handshake
70       * @param maxFramePayloadLength
71       *            Maximum allowable frame payload length. Setting this value to your application's
72       *            requirement may reduce denial of service attacks using long data frames.
73       */
74      public static WebSocketClientHandshaker newHandshaker(
75              URI webSocketURL, WebSocketVersion version, String subprotocol,
76              boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
77          return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders,
78                               maxFramePayloadLength, true, false);
79      }
80  
81      /**
82       * Creates a new handshaker.
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 version
88       *            Version of web socket specification to use to connect to the server
89       * @param subprotocol
90       *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
91       * @param allowExtensions
92       *            Allow extensions to be used in the reserved bits of the web socket frame
93       * @param customHeaders
94       *            Custom HTTP headers to send during the handshake
95       * @param maxFramePayloadLength
96       *            Maximum allowable frame payload length. Setting this value to your application's
97       *            requirement may reduce denial of service attacks using long data frames.
98       * @param performMasking
99       *            Whether to mask all written websocket frames. This must be set to true in order to be fully compatible
100      *            with the websocket specifications. Client applications that communicate with a non-standard server
101      *            which doesn't require masking might set this to false to achieve a higher performance.
102      * @param allowMaskMismatch
103      *            When set to true, frames which are not masked properly according to the standard will still be
104      *            accepted.
105      */
106     public static WebSocketClientHandshaker newHandshaker(
107             URI webSocketURL, WebSocketVersion version, String subprotocol,
108             boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
109             boolean performMasking, boolean allowMaskMismatch) {
110         return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders,
111                 maxFramePayloadLength, performMasking, allowMaskMismatch, -1);
112     }
113 
114     /**
115      * Creates a new handshaker.
116      *
117      * @param webSocketURL
118      *            URL for web socket communications. e.g "ws://myhost.com/mypath".
119      *            Subsequent web socket frames will be sent to this URL.
120      * @param version
121      *            Version of web socket specification to use to connect to the server
122      * @param subprotocol
123      *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
124      * @param allowExtensions
125      *            Allow extensions to be used in the reserved bits of the web socket frame
126      * @param customHeaders
127      *            Custom HTTP headers to send during the handshake
128      * @param maxFramePayloadLength
129      *            Maximum allowable frame payload length. Setting this value to your application's
130      *            requirement may reduce denial of service attacks using long data frames.
131      * @param performMasking
132      *            Whether to mask all written websocket frames. This must be set to true in order to be fully compatible
133      *            with the websocket specifications. Client applications that communicate with a non-standard server
134      *            which doesn't require masking might set this to false to achieve a higher performance.
135      * @param allowMaskMismatch
136      *            When set to true, frames which are not masked properly according to the standard will still be
137      *            accepted.
138      * @param forceCloseTimeoutMillis
139      *            Close the connection if it was not closed by the server after timeout specified
140      */
141     public static WebSocketClientHandshaker newHandshaker(
142             URI webSocketURL, WebSocketVersion version, String subprotocol,
143             boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
144             boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis) {
145         if (version == V13) {
146             return new WebSocketClientHandshaker13(
147                     webSocketURL, subprotocol, allowExtensions, customHeaders,
148                     maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis);
149         }
150 
151         throw new WebSocketClientHandshakeException("Protocol version " + version + " not supported.");
152     }
153 
154     /**
155      * Creates a new handshaker.
156      *
157      * @param webSocketURL
158      *            URL for web socket communications. e.g "ws://myhost.com/mypath".
159      *            Subsequent web socket frames will be sent to this URL.
160      * @param version
161      *            Version of web socket specification to use to connect to the server
162      * @param subprotocol
163      *            Sub protocol request sent to the server. Null if no sub-protocol support is required.
164      * @param allowExtensions
165      *            Allow extensions to be used in the reserved bits of the web socket frame
166      * @param customHeaders
167      *            Custom HTTP headers to send during the handshake
168      * @param maxFramePayloadLength
169      *            Maximum allowable frame payload length. Setting this value to your application's
170      *            requirement may reduce denial of service attacks using long data frames.
171      * @param performMasking
172      *            Whether to mask all written websocket frames. This must be set to true in order to be fully compatible
173      *            with the websocket specifications. Client applications that communicate with a non-standard server
174      *            which doesn't require masking might set this to false to achieve a higher performance.
175      * @param allowMaskMismatch
176      *            When set to true, frames which are not masked properly according to the standard will still be
177      *            accepted.
178      * @param forceCloseTimeoutMillis
179      *            Close the connection if it was not closed by the server after timeout specified
180      * @param  absoluteUpgradeUrl
181      *            Use an absolute url for the Upgrade request, typically when connecting through an HTTP proxy over
182      *            clear HTTP
183      */
184     public static WebSocketClientHandshaker newHandshaker(
185         URI webSocketURL, WebSocketVersion version, String subprotocol,
186         boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
187         boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis, boolean absoluteUpgradeUrl) {
188         if (version == V13) {
189             return new WebSocketClientHandshaker13(
190                 webSocketURL, subprotocol, allowExtensions, customHeaders,
191                 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl);
192         }
193 
194         throw new WebSocketClientHandshakeException("Protocol version " + version + " not supported.");
195     }
196 }