2011-12-15 06:09:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 The Netty Project
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2012-05-23 20:42:10 +02: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
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.handler.codec.http.DefaultHttpResponse;
|
2012-05-23 20:42:10 +02:00
|
|
|
import io.netty.handler.codec.http.HttpChunkAggregator;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
2011-12-15 06:09:09 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
|
|
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
|
|
|
import io.netty.handler.codec.http.HttpResponse;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseEncoder;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Performs server side opening and closing handshakes for <a href="http://tools.ietf.org/html/rfc6455 ">RFC 6455</a>
|
|
|
|
* (originally web socket specification version <a
|
|
|
|
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" >draft-ietf-hybi-thewebsocketprotocol-
|
|
|
|
* 17</a>).
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker13.class);
|
|
|
|
|
|
|
|
public static final String WEBSOCKET_13_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
|
|
|
|
* 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
|
|
|
|
* 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:21:51 +02:00
|
|
|
public WebSocketServerHandshaker13(String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
|
2012-05-31 02:13:00 +02:00
|
|
|
super(WebSocketVersion.V13, 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-17">HyBi versions 13-17</a>. Versions 13-17
|
|
|
|
* 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: 13
|
|
|
|
* </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>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param req
|
|
|
|
* HTTP request
|
|
|
|
*/
|
|
|
|
@Override
|
2012-01-19 05:12:45 +01:00
|
|
|
public ChannelFuture handshake(Channel channel, HttpRequest req) {
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
2012-05-23 20:42:10 +02:00
|
|
|
logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.id()));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
String key = req.getHeader(Names.SEC_WEBSOCKET_KEY);
|
|
|
|
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_13_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 13 Server Handshake key: %s. Response: %s.", key, accept));
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS);
|
2011-12-15 12:25:40 +01:00
|
|
|
res.addHeader(Names.UPGRADE, WEBSOCKET.toLowerCase());
|
|
|
|
res.addHeader(Names.CONNECTION, Names.UPGRADE);
|
|
|
|
res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept);
|
|
|
|
String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
|
|
|
|
if (protocol != null) {
|
2012-01-19 05:12:45 +01:00
|
|
|
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectSubprotocol(protocol));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
ChannelFuture future = channel.write(res);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
// Upgrade the connection and send the handshake response.
|
2012-05-16 16:02:06 +02:00
|
|
|
ChannelPipeline p = channel.pipeline();
|
2012-02-04 16:59:04 +01:00
|
|
|
if (p.get(HttpChunkAggregator.class) != null) {
|
|
|
|
p.remove(HttpChunkAggregator.class);
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
p.replace(HttpRequestDecoder.class, "wsdecoder",
|
|
|
|
new WebSocket13FrameDecoder(true, allowExtensions, getMaxFramePayloadLength()));
|
2011-12-15 12:25:40 +01:00
|
|
|
p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
return future;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo back the closing frame and close the connection
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param frame
|
|
|
|
* Web Socket frame that was received
|
|
|
|
*/
|
|
|
|
@Override
|
2012-01-19 05:12:45 +01:00
|
|
|
public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) {
|
2011-12-15 12:25:40 +01:00
|
|
|
ChannelFuture f = channel.write(frame);
|
|
|
|
f.addListener(ChannelFutureListener.CLOSE);
|
2012-01-19 05:12:45 +01:00
|
|
|
return f;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-12-15 06:09:09 +01:00
|
|
|
|
|
|
|
}
|