2012-07-24 14:38:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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;
|
|
|
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
2012-09-10 07:22:35 +02:00
|
|
|
import io.netty.channel.ChannelFutureListener;
|
2012-07-24 14:38:39 +02:00
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
|
2013-03-28 06:57:04 +01:00
|
|
|
import io.netty.channel.ChannelStateHandler;
|
2012-09-10 07:18:26 +02:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
2013-02-09 12:32:49 +01:00
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2012-07-24 14:38:39 +02:00
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.util.AttributeKey;
|
|
|
|
|
2012-11-12 01:31:40 +01:00
|
|
|
import static io.netty.handler.codec.http.HttpVersion.*;
|
|
|
|
|
2012-07-24 14:38:39 +02:00
|
|
|
/**
|
2012-12-12 07:43:57 +01:00
|
|
|
* This handler does all the heavy lifting for you to run a websocket server.
|
|
|
|
*
|
|
|
|
* It takes care of websocket handshaking as well as processing of control frames (Close, Ping, Pong). Text and Binary
|
|
|
|
* data frames are passed to the next handler in the pipeline (implemented by you) for processing.
|
|
|
|
*
|
|
|
|
* See <tt>io.netty.example.http.websocketx.html5.WebSocketServer</tt> for usage.
|
|
|
|
*
|
|
|
|
* The implementation of this handler assumes that you just want to run a websocket server and not process other types
|
|
|
|
* HTTP requests (like GET and POST). If you wish to support both HTTP requests and websockets in the one server, refer
|
|
|
|
* to the <tt>io.netty.example.http.websocketx.server.WebSocketServer</tt> example.
|
2013-03-28 06:57:04 +01:00
|
|
|
*
|
|
|
|
* To know once a handshake was done you can intercept the
|
|
|
|
* {@link ChannelStateHandler#userEventTriggered(ChannelHandlerContext, Object)} and check if the event was of type
|
|
|
|
* {@link ServerHandshakeStateEvent#HANDSHAKE_COMPLETE}.
|
2012-07-24 14:38:39 +02:00
|
|
|
*/
|
2013-03-20 10:04:17 +01:00
|
|
|
public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
2012-07-24 14:38:39 +02:00
|
|
|
|
2013-03-28 06:57:04 +01:00
|
|
|
/**
|
|
|
|
* Events that are fired to notify about handshake status
|
|
|
|
*/
|
|
|
|
public enum ServerHandshakeStateEvent {
|
|
|
|
/**
|
|
|
|
* The Handshake was complete succesful and so the channel was upgraded to websockets
|
|
|
|
*/
|
|
|
|
HANDSHAKE_COMPLETE
|
|
|
|
}
|
|
|
|
|
2012-07-24 14:38:39 +02:00
|
|
|
private static final AttributeKey<WebSocketServerHandshaker> HANDSHAKER_ATTR_KEY =
|
|
|
|
new AttributeKey<WebSocketServerHandshaker>(WebSocketServerHandshaker.class.getName());
|
|
|
|
|
|
|
|
private final String websocketPath;
|
|
|
|
private final String subprotocols;
|
|
|
|
private final boolean allowExtensions;
|
|
|
|
|
|
|
|
public WebSocketServerProtocolHandler(String websocketPath) {
|
|
|
|
this(websocketPath, null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) {
|
|
|
|
this(websocketPath, subprotocols, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
|
|
|
|
this.websocketPath = websocketPath;
|
|
|
|
this.subprotocols = subprotocols;
|
|
|
|
this.allowExtensions = allowExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-04-05 15:46:18 +02:00
|
|
|
public void handlerAdded(ChannelHandlerContext ctx) {
|
2012-09-10 07:18:26 +02:00
|
|
|
ChannelPipeline cp = ctx.pipeline();
|
|
|
|
if (cp.get(WebSocketServerProtocolHandshakeHandler.class) == null) {
|
|
|
|
// Add the WebSocketHandshakeHandler before this one.
|
|
|
|
ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(),
|
|
|
|
new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols, allowExtensions));
|
|
|
|
}
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void messageReceived(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
|
|
|
|
if (frame instanceof CloseWebSocketFrame) {
|
2012-11-09 17:32:21 +01:00
|
|
|
WebSocketServerHandshaker handshaker = getHandshaker(ctx);
|
2013-02-10 05:31:31 +01:00
|
|
|
frame.retain();
|
2012-07-24 14:38:39 +02:00
|
|
|
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
|
|
|
|
return;
|
2012-11-12 01:31:40 +01:00
|
|
|
}
|
2013-03-20 10:04:17 +01:00
|
|
|
super.messageReceived(ctx, frame);
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
2012-09-10 07:22:35 +02:00
|
|
|
if (cause instanceof WebSocketHandshakeException) {
|
2013-01-16 05:22:50 +01:00
|
|
|
FullHttpResponse response = new DefaultFullHttpResponse(
|
|
|
|
HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
|
2012-09-10 07:22:35 +02:00
|
|
|
ctx.channel().write(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
|
} else {
|
2012-07-24 14:38:39 +02:00
|
|
|
ctx.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static WebSocketServerHandshaker getHandshaker(ChannelHandlerContext ctx) {
|
|
|
|
return ctx.attr(HANDSHAKER_ATTR_KEY).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setHandshaker(ChannelHandlerContext ctx, WebSocketServerHandshaker handshaker) {
|
|
|
|
ctx.attr(HANDSHAKER_ATTR_KEY).set(handshaker);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChannelHandler forbiddenHttpRequestResponder() {
|
2013-02-09 12:32:49 +01:00
|
|
|
return new ChannelInboundMessageHandlerAdapter<FullHttpRequest>() {
|
2012-07-24 14:38:39 +02:00
|
|
|
@Override
|
2013-02-09 12:32:49 +01:00
|
|
|
public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
|
|
|
|
FullHttpResponse response =
|
|
|
|
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
|
|
|
|
ctx.channel().write(response);
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|