Not throw an exception if subprotocol is not supported but just drop the header as stated in the RFC's
This commit is contained in:
parent
6b790f1868
commit
407e12d99b
@ -136,7 +136,9 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
|
|||||||
if (subprotocols != null) {
|
if (subprotocols != null) {
|
||||||
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
||||||
if (selectedSubprotocol == null) {
|
if (selectedSubprotocol == null) {
|
||||||
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug(String.format("Requested subprotocol(s) not supported: %s.", subprotocols));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.headers().add(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
res.headers().add(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,9 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
|
|||||||
if (subprotocols != null) {
|
if (subprotocols != null) {
|
||||||
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
||||||
if (selectedSubprotocol == null) {
|
if (selectedSubprotocol == null) {
|
||||||
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug(String.format("Requested subprotocol(s) not supported: %s.", subprotocols));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,9 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
|
|||||||
if (subprotocols != null) {
|
if (subprotocols != null) {
|
||||||
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
||||||
if (selectedSubprotocol == null) {
|
if (selectedSubprotocol == null) {
|
||||||
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug(String.format("Requested subprotocol(s) not supported: %s.", subprotocols));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
||||||
}
|
}
|
||||||
|
@ -118,8 +118,9 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
|
|||||||
if (subprotocols != null) {
|
if (subprotocols != null) {
|
||||||
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
||||||
if (selectedSubprotocol == null) {
|
if (selectedSubprotocol == null) {
|
||||||
throw new WebSocketHandshakeException(
|
if (logger.isDebugEnabled()) {
|
||||||
"Requested subprotocol(s) not supported: " + subprotocols);
|
logger.debug(String.format("Requested subprotocol(s) not supported: %s.", subprotocols));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,15 @@ public class WebSocketServerHandshaker00Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformOpeningHandshake() {
|
public void testPerformOpeningHandshake() {
|
||||||
|
testPerformOpeningHandshake0(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
|
||||||
|
testPerformOpeningHandshake0(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testPerformOpeningHandshake0(boolean subProtocol) {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(
|
EmbeddedChannel ch = new EmbeddedChannel(
|
||||||
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
||||||
|
|
||||||
@ -53,15 +62,24 @@ public class WebSocketServerHandshaker00Test {
|
|||||||
req.headers().set(Names.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00");
|
req.headers().set(Names.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00");
|
||||||
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
||||||
|
|
||||||
new WebSocketServerHandshaker00(
|
if (subProtocol) {
|
||||||
"ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
|
new WebSocketServerHandshaker00(
|
||||||
|
"ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
} else {
|
||||||
|
new WebSocketServerHandshaker00(
|
||||||
|
"ws://example.com/chat", null, Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
}
|
||||||
|
|
||||||
EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
|
EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
ch2.writeInbound(ch.readOutbound());
|
ch2.writeInbound(ch.readOutbound());
|
||||||
HttpResponse res = (HttpResponse) ch2.readInbound();
|
HttpResponse res = (HttpResponse) ch2.readInbound();
|
||||||
|
|
||||||
Assert.assertEquals("ws://example.com/chat", res.headers().get(Names.SEC_WEBSOCKET_LOCATION));
|
Assert.assertEquals("ws://example.com/chat", res.headers().get(Names.SEC_WEBSOCKET_LOCATION));
|
||||||
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
if (subProtocol) {
|
||||||
|
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
} else {
|
||||||
|
Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
}
|
||||||
LastHttpContent content = (LastHttpContent) ch2.readInbound();
|
LastHttpContent content = (LastHttpContent) ch2.readInbound();
|
||||||
|
|
||||||
Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
|
Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
|
||||||
|
@ -37,6 +37,15 @@ public class WebSocketServerHandshaker08Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformOpeningHandshake() {
|
public void testPerformOpeningHandshake() {
|
||||||
|
testPerformOpeningHandshake0(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
|
||||||
|
testPerformOpeningHandshake0(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testPerformOpeningHandshake0(boolean subProtocol) {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(
|
EmbeddedChannel ch = new EmbeddedChannel(
|
||||||
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
||||||
|
|
||||||
@ -50,8 +59,13 @@ public class WebSocketServerHandshaker08Test {
|
|||||||
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
||||||
req.headers().set(Names.SEC_WEBSOCKET_VERSION, "8");
|
req.headers().set(Names.SEC_WEBSOCKET_VERSION, "8");
|
||||||
|
|
||||||
new WebSocketServerHandshaker08(
|
if (subProtocol) {
|
||||||
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
|
new WebSocketServerHandshaker08(
|
||||||
|
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
} else {
|
||||||
|
new WebSocketServerHandshaker08(
|
||||||
|
"ws://example.com/chat", null, false, Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
}
|
||||||
|
|
||||||
ByteBuf resBuf = (ByteBuf) ch.readOutbound();
|
ByteBuf resBuf = (ByteBuf) ch.readOutbound();
|
||||||
|
|
||||||
@ -61,7 +75,11 @@ public class WebSocketServerHandshaker08Test {
|
|||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
|
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
|
||||||
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
if (subProtocol) {
|
||||||
|
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
} else {
|
||||||
|
Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
}
|
||||||
ReferenceCountUtil.release(res);
|
ReferenceCountUtil.release(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,15 @@ public class WebSocketServerHandshaker13Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformOpeningHandshake() {
|
public void testPerformOpeningHandshake() {
|
||||||
|
testPerformOpeningHandshake0(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
|
||||||
|
testPerformOpeningHandshake0(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testPerformOpeningHandshake0(boolean subProtocol) {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(
|
EmbeddedChannel ch = new EmbeddedChannel(
|
||||||
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
|
||||||
|
|
||||||
@ -50,8 +59,13 @@ public class WebSocketServerHandshaker13Test {
|
|||||||
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
|
||||||
req.headers().set(Names.SEC_WEBSOCKET_VERSION, "13");
|
req.headers().set(Names.SEC_WEBSOCKET_VERSION, "13");
|
||||||
|
|
||||||
new WebSocketServerHandshaker13(
|
if (subProtocol) {
|
||||||
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
|
new WebSocketServerHandshaker13(
|
||||||
|
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
} else {
|
||||||
|
new WebSocketServerHandshaker13(
|
||||||
|
"ws://example.com/chat", null, false, Integer.MAX_VALUE).handshake(ch, req);
|
||||||
|
}
|
||||||
|
|
||||||
ByteBuf resBuf = (ByteBuf) ch.readOutbound();
|
ByteBuf resBuf = (ByteBuf) ch.readOutbound();
|
||||||
|
|
||||||
@ -61,7 +75,11 @@ public class WebSocketServerHandshaker13Test {
|
|||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
|
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(Names.SEC_WEBSOCKET_ACCEPT));
|
||||||
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
if (subProtocol) {
|
||||||
|
Assert.assertEquals("chat", res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
} else {
|
||||||
|
Assert.assertNull(res.headers().get(Names.SEC_WEBSOCKET_PROTOCOL));
|
||||||
|
}
|
||||||
ReferenceCountUtil.release(res);
|
ReferenceCountUtil.release(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user