Do not trigger inboundBufferUpdated unnecessarily / Fix a bug in WebSocketServerProtocolHandlerTest / Fix a bug in forbiddenHttpRequestResponder()

This commit is contained in:
Trustin Lee 2013-02-09 20:32:49 +09:00
parent 51daf2a6a2
commit 2ac7983471
3 changed files with 9 additions and 20 deletions

View File

@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
@ -86,7 +87,6 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler
} }
ctx.nextInboundMessageBuffer().add(frame); ctx.nextInboundMessageBuffer().add(frame);
ctx.fireInboundBufferUpdated();
} }
@Override @Override
@ -102,11 +102,7 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler
@Override @Override
protected void freeInboundMessage(WebSocketFrame msg) throws Exception { protected void freeInboundMessage(WebSocketFrame msg) throws Exception {
if (msg instanceof PingWebSocketFrame || msg instanceof CloseWebSocketFrame) { // Do not free; other handlers will.
// Will be freed once wrote back
return;
}
super.freeInboundMessage(msg);
} }
static WebSocketServerHandshaker getHandshaker(ChannelHandlerContext ctx) { static WebSocketServerHandshaker getHandshaker(ChannelHandlerContext ctx) {
@ -118,19 +114,13 @@ public class WebSocketServerProtocolHandler extends ChannelInboundMessageHandler
} }
static ChannelHandler forbiddenHttpRequestResponder() { static ChannelHandler forbiddenHttpRequestResponder() {
return new ChannelInboundMessageHandlerAdapter<Object>() { return new ChannelInboundMessageHandlerAdapter<FullHttpRequest>() {
@Override @Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception { public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
if (!(msg instanceof WebSocketFrame)) { FullHttpResponse response =
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN); ctx.channel().write(response);
ctx.channel().write(response);
} else {
ctx.nextInboundMessageBuffer().add(msg);
ctx.fireInboundBufferUpdated();
}
} }
}; };
} }
} }

View File

@ -24,7 +24,7 @@ import io.netty.channel.ChannelOperationHandlerAdapter;
import io.netty.channel.ChannelOutboundMessageHandler; import io.netty.channel.ChannelOutboundMessageHandler;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedMessageChannel; import io.netty.channel.embedded.EmbeddedMessageChannel;
import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
@ -59,7 +59,7 @@ public class WebSocketServerProtocolHandlerTest {
writeUpgradeRequest(ch); writeUpgradeRequest(ch);
assertEquals(SWITCHING_PROTOCOLS, ((HttpResponse) ch.outboundMessageBuffer().poll()).getStatus()); assertEquals(SWITCHING_PROTOCOLS, ((HttpResponse) ch.outboundMessageBuffer().poll()).getStatus());
ch.writeInbound(new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test")); ch.writeInbound(new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/test"));
assertEquals(FORBIDDEN, ((HttpResponse) ch.outboundMessageBuffer().poll()).getStatus()); assertEquals(FORBIDDEN, ((HttpResponse) ch.outboundMessageBuffer().poll()).getStatus());
} }

View File

@ -55,7 +55,6 @@ public final class SocksServerHandler extends ChannelInboundMessageHandlerAdapte
ctx.pipeline().addLast(SocksServerConnectHandler.getName(), new SocksServerConnectHandler()); ctx.pipeline().addLast(SocksServerConnectHandler.getName(), new SocksServerConnectHandler());
ctx.pipeline().remove(this); ctx.pipeline().remove(this);
ctx.nextInboundMessageBuffer().add(socksRequest); ctx.nextInboundMessageBuffer().add(socksRequest);
ctx.fireInboundBufferUpdated();
} else { } else {
ctx.close(); ctx.close();
} }