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.
This commit is contained in:
parent
cfd8fb10db
commit
16ddf460a6
@ -102,11 +102,16 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
|||||||
private final boolean allowExtensions;
|
private final boolean allowExtensions;
|
||||||
private final int maxFramePayloadLength;
|
private final int maxFramePayloadLength;
|
||||||
private final boolean allowMaskMismatch;
|
private final boolean allowMaskMismatch;
|
||||||
|
private final boolean checkStartsWith;
|
||||||
|
|
||||||
public WebSocketServerProtocolHandler(String websocketPath) {
|
public WebSocketServerProtocolHandler(String websocketPath) {
|
||||||
this(websocketPath, null, false);
|
this(websocketPath, null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith) {
|
||||||
|
this(websocketPath, null, false, 65536, false, checkStartsWith);
|
||||||
|
}
|
||||||
|
|
||||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) {
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) {
|
||||||
this(websocketPath, subprotocols, false);
|
this(websocketPath, subprotocols, false);
|
||||||
}
|
}
|
||||||
@ -122,11 +127,17 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
|||||||
|
|
||||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||||
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
||||||
|
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||||
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
|
||||||
this.websocketPath = websocketPath;
|
this.websocketPath = websocketPath;
|
||||||
this.subprotocols = subprotocols;
|
this.subprotocols = subprotocols;
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
maxFramePayloadLength = maxFrameSize;
|
maxFramePayloadLength = maxFrameSize;
|
||||||
this.allowMaskMismatch = allowMaskMismatch;
|
this.allowMaskMismatch = allowMaskMismatch;
|
||||||
|
this.checkStartsWith = checkStartsWith;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -136,7 +147,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
|||||||
// Add the WebSocketHandshakeHandler before this one.
|
// Add the WebSocketHandshakeHandler before this one.
|
||||||
ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(),
|
ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(),
|
||||||
new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols,
|
new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols,
|
||||||
allowExtensions, maxFramePayloadLength, allowMaskMismatch));
|
allowExtensions, maxFramePayloadLength, allowMaskMismatch, checkStartsWith));
|
||||||
}
|
}
|
||||||
if (cp.get(Utf8FrameValidator.class) == null) {
|
if (cp.get(Utf8FrameValidator.class) == null) {
|
||||||
// Add the UFT8 checking before this one.
|
// Add the UFT8 checking before this one.
|
||||||
|
@ -42,20 +42,32 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt
|
|||||||
private final boolean allowExtensions;
|
private final boolean allowExtensions;
|
||||||
private final int maxFramePayloadSize;
|
private final int maxFramePayloadSize;
|
||||||
private final boolean allowMaskMismatch;
|
private final boolean allowMaskMismatch;
|
||||||
|
private final boolean checkStartsWith;
|
||||||
|
|
||||||
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
||||||
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
||||||
|
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
||||||
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
|
||||||
this.websocketPath = websocketPath;
|
this.websocketPath = websocketPath;
|
||||||
this.subprotocols = subprotocols;
|
this.subprotocols = subprotocols;
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
maxFramePayloadSize = maxFrameSize;
|
maxFramePayloadSize = maxFrameSize;
|
||||||
this.allowMaskMismatch = allowMaskMismatch;
|
this.allowMaskMismatch = allowMaskMismatch;
|
||||||
|
this.checkStartsWith = checkStartsWith;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
final FullHttpRequest req = (FullHttpRequest) msg;
|
final FullHttpRequest req = (FullHttpRequest) msg;
|
||||||
if (!websocketPath.equals(req.uri())) {
|
if (checkStartsWith) {
|
||||||
|
if (!req.uri().startsWith(websocketPath)) {
|
||||||
|
ctx.fireChannelRead(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (!req.uri().equals(websocketPath)) {
|
||||||
ctx.fireChannelRead(msg);
|
ctx.fireChannelRead(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user