diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java index 5569cb3876..3c467b0254 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java @@ -362,8 +362,14 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder 0) { + // Keep on consuming as otherwise we may trigger an DecoderException, + // other handler will replace this codec with the upgraded protocol codec to + // take the traffic over at some point then. + // See https://github.com/netty/netty/issues/2173 + out.add(buffer.readBytes(actualReadableBytes())); + } break; } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java index 98c8c27cd7..f2a6c44bf7 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java @@ -478,6 +478,33 @@ public class HttpResponseDecoderTest { assertThat(ch.readInbound(), is(nullValue())); } + // See https://github.com/netty/netty/issues/2173 + @Test + public void testWebSocketResponseWithDataFollowing() { + byte[] data = ("HTTP/1.1 101 WebSocket Protocol Handshake\r\n" + + "Upgrade: WebSocket\r\n" + + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Origin: http://localhost:8080\r\n" + + "Sec-WebSocket-Location: ws://localhost/some/path\r\n" + + "\r\n" + + "1234567812345678").getBytes(); + byte[] otherData = {1, 2, 3, 4}; + + EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder()); + ch.writeInbound(Unpooled.wrappedBuffer(data, otherData)); + + HttpResponse res = (HttpResponse) ch.readInbound(); + assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1)); + assertThat(res.getStatus(), is(HttpResponseStatus.SWITCHING_PROTOCOLS)); + HttpContent content = (HttpContent) ch.readInbound(); + assertThat(content.content().readableBytes(), is(16)); + content.release(); + + assertThat(ch.finish(), is(true)); + + assertEquals(ch.readInbound(), Unpooled.wrappedBuffer(otherData)); + } + @Test public void testGarbageHeaders() { // A response without headers - from https://github.com/netty/netty/issues/2103