From b91426ff33261522a3125c1cb36c9c197cc391dc Mon Sep 17 00:00:00 2001 From: Malik Baktiyarov Date: Fri, 16 Dec 2016 02:59:11 +0300 Subject: [PATCH] Added checkStartsWith option for WebSocketServerProtocolHandler Motivation: Enables optional .startsWith() matching of req.uri() with websocketPath. Modifications: New checkStartsWith boolean option with default false value added to both WebSocketServerProtocolHandler and WebSocketServerProtocolHandshakeHandler. req.uri() matching is based on this option. Result: By default old behavior matching via .equal() is preserved. To use checkStartsWith use constructor shortcut: new WebSocketServerProtocolHandler(websocketPath, true) or fill this flag on full form of constructor among other options. --- .../websocketx/WebSocketServerProtocolHandler.java | 13 ++++++++++++- .../WebSocketServerProtocolHandshakeHandler.java | 13 ++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java index 9cd59bde81..9dae5eb2a6 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java @@ -68,11 +68,16 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler { private final String subprotocols; private final boolean allowExtensions; private final int maxFramePayloadLength; + private final boolean checkStartsWith; public WebSocketServerProtocolHandler(String websocketPath) { this(websocketPath, null, false); } + public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith) { + this(websocketPath, null, false, 65536, checkStartsWith); + } + public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) { this(websocketPath, subprotocols, false); } @@ -83,10 +88,16 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler { public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, int maxFrameSize) { + this(websocketPath, subprotocols, allowExtensions, maxFrameSize, false); + } + + public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, + boolean allowExtensions, int maxFrameSize, boolean checkStartsWith) { this.websocketPath = websocketPath; this.subprotocols = subprotocols; this.allowExtensions = allowExtensions; maxFramePayloadLength = maxFrameSize; + this.checkStartsWith = checkStartsWith; } @Override @@ -96,7 +107,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler { // Add the WebSocketHandshakeHandler before this one. ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(), new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols, - allowExtensions, maxFramePayloadLength)); + allowExtensions, maxFramePayloadLength, checkStartsWith)); } } 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 c2fa1928da..086d713bd2 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 @@ -41,19 +41,26 @@ class WebSocketServerProtocolHandshakeHandler private final String subprotocols; private final boolean allowExtensions; private final int maxFramePayloadSize; + private final boolean checkStartsWith; WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols, - boolean allowExtensions, int maxFrameSize) { + boolean allowExtensions, int maxFrameSize, boolean checkStartsWith) { this.websocketPath = websocketPath; this.subprotocols = subprotocols; this.allowExtensions = allowExtensions; maxFramePayloadSize = maxFrameSize; + this.checkStartsWith = checkStartsWith; } @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - FullHttpRequest req = (FullHttpRequest) msg; - if (!websocketPath.equals(req.getUri())) { + final FullHttpRequest req = (FullHttpRequest) msg; + if (checkStartsWith) { + if (!req.getUri().startsWith(websocketPath)) { + ctx.fireChannelRead(msg); + return; + } + } else if (!req.getUri().equals(websocketPath)) { ctx.fireChannelRead(msg); return; }