2011-12-15 06:09:09 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2011-12-15 06:09:09 +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:
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-15 06:09:09 +01:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.handler.codec.http.websocketx;
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
import io.netty.channel.ChannelInboundByteHandler;
|
|
|
|
import io.netty.channel.ChannelOutboundMessageHandler;
|
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
2013-03-07 10:57:27 +01:00
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2013-02-08 19:54:05 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
2012-01-19 05:12:45 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
2011-12-15 06:09:09 +01:00
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
2012-12-03 11:58:13 +01:00
|
|
|
import static io.netty.handler.codec.http.HttpHeaders.Values.*;
|
|
|
|
import static io.netty.handler.codec.http.HttpVersion.*;
|
|
|
|
|
2011-12-15 06:09:09 +01:00
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Performs server side opening and closing handshakes for web socket specification version <a
|
|
|
|
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
|
|
|
|
* 10</a>
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final boolean allowExtensions;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location
|
2012-05-23 20:42:10 +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
|
|
|
|
* @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-06-08 12:28:12 +02:00
|
|
|
public WebSocketServerHandshaker08(
|
|
|
|
String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
|
2012-05-31 02:13:00 +02:00
|
|
|
super(WebSocketVersion.V08, webSocketURL, subprotocols, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
this.allowExtensions = allowExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Handle the web socket handshake for the web socket specification <a href=
|
|
|
|
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi version 8 to 10</a>. Version 8, 9 and
|
|
|
|
* 10 share the same wire protocol.
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <p>
|
|
|
|
* Browser request to the server:
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <pre>
|
|
|
|
* GET /chat HTTP/1.1
|
|
|
|
* Host: server.example.com
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
|
|
* Sec-WebSocket-Origin: http://example.com
|
|
|
|
* Sec-WebSocket-Protocol: chat, superchat
|
|
|
|
* Sec-WebSocket-Version: 8
|
|
|
|
* </pre>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <p>
|
|
|
|
* Server response:
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <pre>
|
|
|
|
* HTTP/1.1 101 Switching Protocols
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
|
|
|
* Sec-WebSocket-Protocol: chat
|
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
@Override
|
2013-03-07 10:57:27 +01:00
|
|
|
protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
|
|
|
|
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-02-08 19:54:05 +01:00
|
|
|
if (headers != null) {
|
|
|
|
res.headers().add(headers);
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
2011-12-15 12:25:40 +01:00
|
|
|
if (key == null) {
|
2012-01-19 05:12:45 +01:00
|
|
|
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
String acceptSeed = key + WEBSOCKET_08_ACCEPT_GUID;
|
2012-01-19 05:12:45 +01:00
|
|
|
byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
|
|
|
|
String accept = WebSocketUtil.base64(sha1);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug(String.format("WS Version 8 Server Handshake key: %s. Response: %s.", key, accept));
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase());
|
|
|
|
res.headers().add(Names.CONNECTION, Names.UPGRADE);
|
|
|
|
res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept);
|
|
|
|
String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
|
2012-05-12 13:05:15 +02:00
|
|
|
if (subprotocols != null) {
|
|
|
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
|
|
|
if (selectedSubprotocol == null) {
|
|
|
|
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
|
|
|
|
} else {
|
2013-01-16 05:22:50 +01:00
|
|
|
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
2012-05-12 13:05:15 +02:00
|
|
|
}
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2013-03-07 10:57:27 +01:00
|
|
|
return res;
|
|
|
|
}
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
@Override
|
|
|
|
protected ChannelInboundByteHandler newWebsocketDecoder() {
|
|
|
|
return new WebSocket08FrameDecoder(true, allowExtensions, maxFramePayloadLength());
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-07 10:57:27 +01:00
|
|
|
protected ChannelOutboundMessageHandler<WebSocketFrame> newWebSocketEncoder() {
|
|
|
|
return new WebSocket08FrameEncoder(false);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-12-15 06:09:09 +01:00
|
|
|
}
|