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.HttpHeaders;
19  
20  import java.net.URI;
21  
22  import static io.netty.handler.codec.http.websocketx.WebSocketVersion.*;
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      *            Allows to loosen the masking requirement on received frames. When this is set to false then also
104      *            frames which are not masked properly according to the standard will still be 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         if (version == V13) {
111             return new WebSocketClientHandshaker13(
112                     webSocketURL, V13, subprotocol, allowExtensions, customHeaders,
113                     maxFramePayloadLength, performMasking, allowMaskMismatch);
114         }
115         if (version == V08) {
116             return new WebSocketClientHandshaker08(
117                     webSocketURL, V08, subprotocol, allowExtensions, customHeaders,
118                     maxFramePayloadLength, performMasking, allowMaskMismatch);
119         }
120         if (version == V07) {
121             return new WebSocketClientHandshaker07(
122                     webSocketURL, V07, subprotocol, allowExtensions, customHeaders,
123                     maxFramePayloadLength, performMasking, allowMaskMismatch);
124         }
125         if (version == V00) {
126             return new WebSocketClientHandshaker00(
127                     webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength);
128         }
129 
130         throw new WebSocketHandshakeException("Protocol version " + version + " not supported.");
131     }
132 }