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.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 * 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, V13, subprotocol, allowExtensions, customHeaders, 148 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis); 149 } 150 if (version == V08) { 151 return new WebSocketClientHandshaker08( 152 webSocketURL, V08, subprotocol, allowExtensions, customHeaders, 153 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis); 154 } 155 if (version == V07) { 156 return new WebSocketClientHandshaker07( 157 webSocketURL, V07, subprotocol, allowExtensions, customHeaders, 158 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis); 159 } 160 if (version == V00) { 161 return new WebSocketClientHandshaker00( 162 webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength, forceCloseTimeoutMillis); 163 } 164 165 throw new WebSocketClientHandshakeException("Protocol version " + version + " not supported."); 166 } 167 168 /** 169 * Creates a new handshaker. 170 * 171 * @param webSocketURL 172 * URL for web socket communications. e.g "ws://myhost.com/mypath". 173 * Subsequent web socket frames will be sent to this URL. 174 * @param version 175 * Version of web socket specification to use to connect to the server 176 * @param subprotocol 177 * Sub protocol request sent to the server. Null if no sub-protocol support is required. 178 * @param allowExtensions 179 * Allow extensions to be used in the reserved bits of the web socket frame 180 * @param customHeaders 181 * Custom HTTP headers to send during the handshake 182 * @param maxFramePayloadLength 183 * Maximum allowable frame payload length. Setting this value to your application's 184 * requirement may reduce denial of service attacks using long data frames. 185 * @param performMasking 186 * Whether to mask all written websocket frames. This must be set to true in order to be fully compatible 187 * with the websocket specifications. Client applications that communicate with a non-standard server 188 * which doesn't require masking might set this to false to achieve a higher performance. 189 * @param allowMaskMismatch 190 * When set to true, frames which are not masked properly according to the standard will still be 191 * accepted. 192 * @param forceCloseTimeoutMillis 193 * Close the connection if it was not closed by the server after timeout specified 194 * @param absoluteUpgradeUrl 195 * Use an absolute url for the Upgrade request, typically when connecting through an HTTP proxy over 196 * clear HTTP 197 */ 198 public static WebSocketClientHandshaker newHandshaker( 199 URI webSocketURL, WebSocketVersion version, String subprotocol, 200 boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, 201 boolean performMasking, boolean allowMaskMismatch, long forceCloseTimeoutMillis, boolean absoluteUpgradeUrl) { 202 if (version == V13) { 203 return new WebSocketClientHandshaker13( 204 webSocketURL, V13, subprotocol, allowExtensions, customHeaders, 205 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl); 206 } 207 if (version == V08) { 208 return new WebSocketClientHandshaker08( 209 webSocketURL, V08, subprotocol, allowExtensions, customHeaders, 210 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl); 211 } 212 if (version == V07) { 213 return new WebSocketClientHandshaker07( 214 webSocketURL, V07, subprotocol, allowExtensions, customHeaders, 215 maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis, absoluteUpgradeUrl); 216 } 217 if (version == V00) { 218 return new WebSocketClientHandshaker00( 219 webSocketURL, V00, subprotocol, customHeaders, 220 maxFramePayloadLength, forceCloseTimeoutMillis, absoluteUpgradeUrl); 221 } 222 223 throw new WebSocketClientHandshakeException("Protocol version " + version + " not supported."); 224 } 225 }