From 72a216625f16617579f0c0102a7d6624865148d6 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Fri, 10 Nov 2017 10:33:29 +0200 Subject: [PATCH] Correctly handle 205 Reset Content response with transfer-encoding Motivation: According to RFC 7231 the server may choose to: ``` indicate a zero-length payload for the response by including a Transfer-Encoding header field with a value of chunked and a message body consisting of a single chunk of zero-length ``` https://tools.ietf.org/html/rfc7231#page-53 In such cases the exception below appears during decoding phase: ``` java.lang.IllegalArgumentException: invalid version format: 0 at io.netty.handler.codec.http.HttpVersion.(HttpVersion.java:121) at io.netty.handler.codec.http.HttpVersion.valueOf(HttpVersion.java:76) at io.netty.handler.codec.http.HttpResponseDecoder.createMessage(HttpResponseDecoder.java:118) at io.netty.handler.codec.http.HttpObjectDecoder.decode(HttpObjectDecoder.java:219) ``` Modifications: HttpObjectDecoder.isContentAlwaysEmpty specifies content NOT empty when 205 Reset Content response Result: There is no `IllegalArgumentException: invalid version format: 0` when handling 205 Reset Content response with transfer-encoding --- .../handler/codec/http/HttpObjectDecoder.java | 2 +- .../codec/http/HttpResponseDecoderTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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 513d2aa415..22271d81d0 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 @@ -477,7 +477,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder { } switch (code) { - case 204: case 205: case 304: + case 204: case 304: return true; } } 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 f346de91c9..017dbd5ff9 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 @@ -368,6 +368,28 @@ public class HttpResponseDecoderTest { assertThat(ch.readInbound(), is(nullValue())); } + @Test + public void testResetContentResponseWithTransferEncoding() { + EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder()); + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( + "HTTP/1.1 205 Reset Content\r\n" + + "Transfer-Encoding: chunked\r\n" + + "\r\n" + + "0\r\n" + + "\r\n", + CharsetUtil.US_ASCII))); + + HttpResponse res = ch.readInbound(); + assertThat(res.protocolVersion(), sameInstance(HttpVersion.HTTP_1_1)); + assertThat(res.status(), is(HttpResponseStatus.RESET_CONTENT)); + + LastHttpContent lastContent = ch.readInbound(); + assertThat(lastContent.content().isReadable(), is(false)); + lastContent.release(); + + assertThat(ch.finish(), is(false)); + } + @Test public void testLastResponseWithTrailingHeader() { EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());