diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index 76825a573f..38cde0d839 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -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); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java index 45c8f2e4a0..27e97ebbdf 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker07.java @@ -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); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java index 30ca2bc18b..aa6f369677 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java @@ -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); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java index 27192b964c..0ed048e52b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java @@ -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); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java index 09510c3287..62ced3ee31 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00Test.java @@ -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)); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java index 63004eb2a6..4b48d0898e 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08Test.java @@ -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); } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java index 3410d31e28..da8854ae00 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13Test.java @@ -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); } }