[#1818] Pass through message as they are when no compression is needed

This commit is contained in:
Norman Maurer 2013-09-09 11:32:37 +02:00
parent ffab456aca
commit 0065006824
2 changed files with 57 additions and 8 deletions

View File

@ -109,9 +109,6 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
if (isFull) { if (isFull) {
// Pass through the full response with empty content and continue waiting for the the next resp. // Pass through the full response with empty content and continue waiting for the the next resp.
if (!((ByteBufHolder) res).content().isReadable()) { if (!((ByteBufHolder) res).content().isReadable()) {
// Set the content length to 0.
res.headers().remove(Names.TRANSFER_ENCODING);
res.headers().set(Names.CONTENT_LENGTH, "0");
out.add(ReferenceCountUtil.retain(res)); out.add(ReferenceCountUtil.retain(res));
break; break;
} }
@ -123,9 +120,6 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
// If unable to encode, pass through. // If unable to encode, pass through.
if (result == null) { if (result == null) {
if (isFull) { if (isFull) {
// Set the content length.
res.headers().remove(Names.TRANSFER_ENCODING);
res.headers().set(Names.CONTENT_LENGTH, ((ByteBufHolder) res).content().readableBytes());
out.add(ReferenceCountUtil.retain(res)); out.add(ReferenceCountUtil.retain(res));
} else { } else {
out.add(res); out.add(res);

View File

@ -101,6 +101,39 @@ public class HttpContentEncoderTest {
assertThat(ch.readOutbound(), is(nullValue())); assertThat(ch.readOutbound(), is(nullValue()));
} }
@Test
public void testChunkedContentWithTrailingHeader() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED);
ch.writeOutbound(res);
assertEncodedResponse(ch);
ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[3])));
ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[2])));
LastHttpContent content = new DefaultLastHttpContent(Unpooled.wrappedBuffer(new byte[1]));
content.trailingHeaders().set("X-Test", "Netty");
ch.writeOutbound(content);
HttpContent chunk;
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3"));
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2"));
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1"));
assertThat(chunk, is(instanceOf(HttpContent.class)));
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().isReadable(), is(false));
assertThat(chunk, is(instanceOf(LastHttpContent.class)));
assertEquals("Netty", ((LastHttpContent) chunk).trailingHeaders().get("X-Test"));
assertThat(ch.readOutbound(), is(nullValue()));
}
@Test @Test
public void testFullContent() throws Exception { public void testFullContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder()); EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
@ -161,8 +194,6 @@ public class HttpContentEncoderTest {
res = (FullHttpResponse) o; res = (FullHttpResponse) o;
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue())); assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue()));
// Length must be set to 0.
assertThat(res.headers().get(Names.CONTENT_LENGTH), is("0"));
// Content encoding shouldn't be modified. // Content encoding shouldn't be modified.
assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue())); assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue()));
assertThat(res.content().readableBytes(), is(0)); assertThat(res.content().readableBytes(), is(0));
@ -171,6 +202,30 @@ public class HttpContentEncoderTest {
assertThat(ch.readOutbound(), is(nullValue())); assertThat(ch.readOutbound(), is(nullValue()));
} }
@Test
public void testEmptyFullContentWithTrailer() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER);
res.trailingHeaders().set("X-Test", "Netty");
ch.writeOutbound(res);
Object o = ch.readOutbound();
assertThat(o, is(instanceOf(FullHttpResponse.class)));
res = (FullHttpResponse) o;
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue()));
// Content encoding shouldn't be modified.
assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue()));
assertThat(res.content().readableBytes(), is(0));
assertThat(res.content().toString(CharsetUtil.US_ASCII), is(""));
assertEquals("Netty", res.trailingHeaders().get("X-Test"));
assertThat(ch.readOutbound(), is(nullValue()));
}
private static void assertEncodedResponse(EmbeddedChannel ch) { private static void assertEncodedResponse(EmbeddedChannel ch) {
Object o = ch.readOutbound(); Object o = ch.readOutbound();
assertThat(o, is(instanceOf(HttpResponse.class))); assertThat(o, is(instanceOf(HttpResponse.class)));