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:
Norman Maurer 2014-01-23 15:02:38 +01:00
parent 491b2fd69a
commit f122118bf7
7 changed files with 76 additions and 14 deletions

View File

@ -136,7 +136,9 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
if (subprotocols != null) {
String selectedSubprotocol = selectSubprotocol(subprotocols);
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 {
res.headers().add(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
}

View File

@ -123,7 +123,9 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
if (subprotocols != null) {
String selectedSubprotocol = selectSubprotocol(subprotocols);
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 {
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
}

View File

@ -122,7 +122,9 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
if (subprotocols != null) {
String selectedSubprotocol = selectSubprotocol(subprotocols);
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 {
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
}

View File

@ -119,8 +119,9 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
if (subprotocols != null) {
String selectedSubprotocol = selectSubprotocol(subprotocols);
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 {
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
}

View File

@ -39,6 +39,15 @@ public class WebSocketServerHandshaker00Test {
@Test
public void testPerformOpeningHandshake() {
testPerformOpeningHandshake0(true);
}
@Test
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
testPerformOpeningHandshake0(false);
}
private static void testPerformOpeningHandshake0(boolean subProtocol) {
EmbeddedChannel ch = new EmbeddedChannel(
new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
@ -53,15 +62,25 @@ public class WebSocketServerHandshaker00Test {
req.headers().set(Names.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00");
req.headers().set(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
new WebSocketServerHandshaker00(
"ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
if (subProtocol) {
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());
ch2.writeInbound(ch.readOutbound());
HttpResponse res = ch2.readInbound();
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 = ch2.readInbound();
Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));

View File

@ -37,6 +37,15 @@ public class WebSocketServerHandshaker08Test {
@Test
public void testPerformOpeningHandshake() {
testPerformOpeningHandshake0(true);
}
@Test
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
testPerformOpeningHandshake0(false);
}
private static void testPerformOpeningHandshake0(boolean subProtocol) {
EmbeddedChannel ch = new EmbeddedChannel(
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_VERSION, "8");
new WebSocketServerHandshaker08(
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
if (subProtocol) {
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 = ch.readOutbound();
@ -61,7 +75,11 @@ public class WebSocketServerHandshaker08Test {
Assert.assertEquals(
"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);
}
}

View File

@ -37,6 +37,15 @@ public class WebSocketServerHandshaker13Test {
@Test
public void testPerformOpeningHandshake() {
testPerformOpeningHandshake0(true);
}
@Test
public void testPerformOpeningHandshakeSubProtocolNotSupported() {
testPerformOpeningHandshake0(false);
}
private static void testPerformOpeningHandshake0(boolean subProtocol) {
EmbeddedChannel ch = new EmbeddedChannel(
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_VERSION, "13");
new WebSocketServerHandshaker13(
"ws://example.com/chat", "chat", false, Integer.MAX_VALUE).handshake(ch, req);
if (subProtocol) {
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 = ch.readOutbound();
@ -61,7 +75,11 @@ public class WebSocketServerHandshaker13Test {
Assert.assertEquals(
"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);
}
}