Skip to content

Commit

Permalink
Not throw an exception if subprotocol is not supported but just drop …
Browse files Browse the repository at this point in the history
…the header as stated in the RFC's
  • Loading branch information
Norman Maurer committed Jan 26, 2014
1 parent 491b2fd commit f122118
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -53,15 +62,25 @@ public void testPerformOpeningHandshake() {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -50,8 +59,13 @@ public void testPerformOpeningHandshake() {
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();

Expand All @@ -61,7 +75,11 @@ public void testPerformOpeningHandshake() {

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -50,8 +59,13 @@ public void testPerformOpeningHandshake() {
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();

Expand All @@ -61,7 +75,11 @@ public void testPerformOpeningHandshake() {

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);
}
}

0 comments on commit f122118

Please sign in to comment.