Fix a leak in WebSocketServerProtocolHandshakeHandler

- Related: #1975
This commit is contained in:
Trustin Lee 2014-02-06 20:57:55 -08:00
parent 50f8cc98d1
commit c01f08d306

View File

@ -41,7 +41,7 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelHandlerAdapter {
private final String subprotocols; private final String subprotocols;
private final boolean allowExtensions; private final boolean allowExtensions;
public WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols, WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
boolean allowExtensions) { boolean allowExtensions) {
this.websocketPath = websocketPath; this.websocketPath = websocketPath;
this.subprotocols = subprotocols; this.subprotocols = subprotocols;
@ -51,32 +51,36 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelHandlerAdapter {
@Override @Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
FullHttpRequest req = (FullHttpRequest) msg; FullHttpRequest req = (FullHttpRequest) msg;
if (req.getMethod() != GET) { try {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); if (req.getMethod() != GET) {
return; sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
} return;
}
final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols, allowExtensions); getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols, allowExtensions);
final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) { if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else { } else {
final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req); final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req);
handshakeFuture.addListener(new ChannelFutureListener() { handshakeFuture.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) { if (!future.isSuccess()) {
ctx.fireExceptionCaught(future.cause()); ctx.fireExceptionCaught(future.cause());
} else { } else {
ctx.fireUserEventTriggered( ctx.fireUserEventTriggered(
WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE); WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
}
} }
} });
}); WebSocketServerProtocolHandler.setHandshaker(ctx, handshaker);
WebSocketServerProtocolHandler.setHandshaker(ctx, handshaker); ctx.pipeline().replace(this, "WS403Responder",
ctx.pipeline().replace(this, "WS403Responder", WebSocketServerProtocolHandler.forbiddenHttpRequestResponder());
WebSocketServerProtocolHandler.forbiddenHttpRequestResponder()); }
} finally {
req.release();
} }
} }