diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java index cf453043be..a93396138a 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java @@ -25,7 +25,6 @@ import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.TooLongFrameException; -import io.netty.util.ReferenceCountUtil; import java.util.List; @@ -136,7 +135,7 @@ public class HttpObjectAggregator extends MessageToMessageDecoder { if (!m.getDecoderResult().isSuccess()) { removeTransferEncodingChunked(m); - out.add(ReferenceCountUtil.retain(m)); + out.add(toFullMessage(m)); this.currentMessage = null; return; } @@ -245,4 +244,25 @@ public class HttpObjectAggregator extends MessageToMessageDecoder { currentMessage = null; } } + + private static FullHttpMessage toFullMessage(HttpMessage msg) { + if (msg instanceof FullHttpMessage) { + return ((FullHttpMessage) msg).retain(); + } + + FullHttpMessage fullMsg; + if (msg instanceof HttpRequest) { + HttpRequest req = (HttpRequest) msg; + fullMsg = new DefaultFullHttpRequest( + req.getProtocolVersion(), req.getMethod(), req.getUri(), Unpooled.EMPTY_BUFFER, false); + } else if (msg instanceof HttpResponse) { + HttpResponse res = (HttpResponse) msg; + fullMsg = new DefaultFullHttpResponse( + res.getProtocolVersion(), res.getStatus(), Unpooled.EMPTY_BUFFER, false); + } else { + throw new IllegalStateException(); + } + + return fullMsg; + } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java index bed6a1f440..7c2ef17094 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import java.util.List; import static io.netty.util.ReferenceCountUtil.*; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; public class HttpObjectAggregatorTest { @@ -186,4 +187,22 @@ public class HttpObjectAggregatorTest { checkContentBuffer(aggratedMessage); assertNull(embedder.readInbound()); } + + @Test + public void testBadRequest() { + EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder(), new HttpObjectAggregator(1024 * 1024)); + ch.writeInbound(Unpooled.copiedBuffer("GET / HTTP/1.0 with extra\r\n", CharsetUtil.UTF_8)); + assertThat(ch.readInbound(), is(instanceOf(FullHttpRequest.class))); + assertNull(ch.readInbound()); + ch.finish(); + } + + @Test + public void testBadResponse() throws Exception { + EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(), new HttpObjectAggregator(1024 * 1024)); + ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.0 BAD_CODE Bad Server\r\n", CharsetUtil.UTF_8)); + assertThat(ch.readInbound(), is(instanceOf(FullHttpResponse.class))); + assertNull(ch.readInbound()); + ch.finish(); + } }