2011-09-26 14:51:15 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2011-09-26 14:51:15 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2011-09-26 14:51:15 +02:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-26 14:51:15 +02:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2011-09-26 14:51:15 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http.websocketx;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2011-12-11 00:47:07 +01:00
|
|
|
import io.netty.channel.Channel;
|
2013-01-14 16:52:30 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultHttpResponseHeader;
|
2012-05-31 02:13:00 +02:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
2013-01-14 16:52:30 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequestHeader;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseHeader;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.handler.codec.http.HttpVersion;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-15 12:25:40 +01:00
|
|
|
* Instances the appropriate handshake class to use for servers
|
2011-09-26 14:51:15 +02:00
|
|
|
*/
|
|
|
|
public class WebSocketServerHandshakerFactory {
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
private final String webSocketURL;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final String subprotocols;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final boolean allowExtensions;
|
2011-10-16 08:01:24 +02:00
|
|
|
|
2012-05-31 02:21:51 +02:00
|
|
|
private final int maxFramePayloadLength;
|
2012-05-31 02:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location
|
|
|
|
*
|
|
|
|
* @param webSocketURL
|
2012-06-08 12:28:12 +02:00
|
|
|
* URL for web socket communications. e.g "ws://myhost.com/mypath".
|
|
|
|
* Subsequent web socket frames will be sent to this URL.
|
2012-05-31 02:13:00 +02:00
|
|
|
* @param subprotocols
|
|
|
|
* CSV of supported protocols. Null if sub protocols not supported.
|
|
|
|
* @param allowExtensions
|
|
|
|
* Allow extensions to be used in the reserved bits of the web socket frame
|
|
|
|
*/
|
|
|
|
public WebSocketServerHandshakerFactory(
|
|
|
|
String webSocketURL, String subprotocols, boolean allowExtensions) {
|
|
|
|
this(webSocketURL, subprotocols, allowExtensions, 65536);
|
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location
|
2012-05-31 02:13:00 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param webSocketURL
|
2012-06-08 12:28:12 +02:00
|
|
|
* URL for web socket communications. e.g "ws://myhost.com/mypath".
|
|
|
|
* Subsequent web socket frames will be sent to this URL.
|
2012-01-19 05:12:45 +01:00
|
|
|
* @param subprotocols
|
2011-12-15 12:25:40 +01:00
|
|
|
* CSV of supported protocols. Null if sub protocols not supported.
|
|
|
|
* @param allowExtensions
|
|
|
|
* Allow extensions to be used in the reserved bits of the web socket frame
|
2012-05-31 02:13:00 +02:00
|
|
|
* @param maxFramePayloadLength
|
2012-06-08 12:28:12 +02:00
|
|
|
* Maximum allowable frame payload length. Setting this value to your application's
|
|
|
|
* requirement may reduce denial of service attacks using long data frames.
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2012-05-31 02:13:00 +02:00
|
|
|
public WebSocketServerHandshakerFactory(
|
|
|
|
String webSocketURL, String subprotocols, boolean allowExtensions,
|
2012-05-31 02:21:51 +02:00
|
|
|
int maxFramePayloadLength) {
|
2011-12-15 12:25:40 +01:00
|
|
|
this.webSocketURL = webSocketURL;
|
2012-01-19 05:12:45 +01:00
|
|
|
this.subprotocols = subprotocols;
|
2011-12-15 12:25:40 +01:00
|
|
|
this.allowExtensions = allowExtensions;
|
2012-05-31 02:13:00 +02:00
|
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Instances a new handshaker
|
2012-05-31 02:13:00 +02:00
|
|
|
*
|
2012-06-08 12:28:12 +02:00
|
|
|
* @return A new WebSocketServerHandshaker for the requested web socket version. Null if web
|
|
|
|
* socket version is not supported.
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2013-01-14 16:52:30 +01:00
|
|
|
public WebSocketServerHandshaker newHandshaker(HttpRequestHeader req) {
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
String version = req.getHeader(Names.SEC_WEBSOCKET_VERSION);
|
|
|
|
if (version != null) {
|
|
|
|
if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
|
|
|
|
// Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
|
2012-06-08 12:28:12 +02:00
|
|
|
return new WebSocketServerHandshaker13(
|
|
|
|
webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
} else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
|
|
|
|
// Version 8 of the wire protocol - version 10 of the draft hybi specification.
|
2012-06-08 12:28:12 +02:00
|
|
|
return new WebSocketServerHandshaker08(
|
|
|
|
webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
|
2012-12-14 23:59:18 +01:00
|
|
|
} else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
|
|
|
|
// Version 8 of the wire protocol - version 07 of the draft hybi specification.
|
|
|
|
return new WebSocketServerHandshaker07(
|
|
|
|
webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Assume version 00 where version header was not specified
|
2012-05-31 02:13:00 +02:00
|
|
|
return new WebSocketServerHandshaker00(webSocketURL, subprotocols, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Return that we need cannot not support the web socket version
|
2012-05-31 02:13:00 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
*/
|
2012-11-09 23:54:33 +01:00
|
|
|
public static void sendUnsupportedWebSocketVersionResponse(Channel channel) {
|
2013-01-14 16:52:30 +01:00
|
|
|
HttpResponseHeader res = new DefaultHttpResponseHeader(
|
2012-01-19 05:12:45 +01:00
|
|
|
HttpVersion.HTTP_1_1,
|
|
|
|
HttpResponseStatus.SWITCHING_PROTOCOLS);
|
2011-12-15 12:25:40 +01:00
|
|
|
res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED);
|
|
|
|
res.setHeader(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
|
|
|
|
channel.write(res);
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
}
|