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 if (version == V13) {
78 return new WebSocketClientHandshaker13(
79 webSocketURL, V13, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
80 }
81 if (version == V08) {
82 return new WebSocketClientHandshaker08(
83 webSocketURL, V08, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
84 }
85 if (version == V07) {
86 return new WebSocketClientHandshaker07(
87 webSocketURL, V07, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength);
88 }
89 if (version == V00) {
90 return new WebSocketClientHandshaker00(
91 webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength);
92 }
93
94 throw new WebSocketHandshakeException("Protocol version " + version + " not supported.");
95 }
96 }