From d1b02257248adc250cc3ac61cb5efd179675519b Mon Sep 17 00:00:00 2001 From: Dmitriy Dumanskiy Date: Mon, 27 Feb 2017 18:40:02 +0200 Subject: [PATCH] Improvement : WebSocketServerProtocolHandshakeHandler.getWebSocketLocation now applies concat optimization and WebSocket path check moved to separated method. Motivation: Make code easier to read, make WebSocketServerProtocolHandshakeHandler.getWebSocketLocation method faster. Modification: WebSocket path check moved to separate method. Get header operation moved out from concat operation. Result: WebSocketServerProtocolHandshakeHandler.getWebSocketLocation is faster as OptimizeStringConcat could be applied. Code easier to read. --- .../WebSocketServerProtocolHandshakeHandler.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index fc341ce106..26e01f1860 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -62,12 +62,7 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { final FullHttpRequest req = (FullHttpRequest) msg; - if (checkStartsWith) { - if (!req.uri().startsWith(websocketPath)) { - ctx.fireChannelRead(msg); - return; - } - } else if (!req.uri().equals(websocketPath)) { + if (isNotWebSocketPath(req)) { ctx.fireChannelRead(msg); return; } @@ -110,6 +105,10 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt } } + private boolean isNotWebSocketPath(FullHttpRequest req) { + return checkStartsWith ? !req.uri().startsWith(websocketPath) : !req.uri().equals(websocketPath); + } + private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { @@ -123,6 +122,7 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt // SSL in use so use Secure WebSockets protocol = "wss"; } - return protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path; + String host = req.headers().get(HttpHeaderNames.HOST); + return protocol + "://" + host + path; } }