Skip to content

Commit

Permalink
[#1818] Pass through message as they are when no compression is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Sep 9, 2013
1 parent 198dde3 commit c89f1b0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
if (isFull) {
// Pass through the full response with empty content and continue waiting for the the next resp.
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));
break;
}
Expand All @@ -123,9 +120,6 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
// If unable to encode, pass through.
if (result == null) {
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));
} else {
out.add(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ public void testChunkedContent() throws Exception {
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
public void testFullContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
Expand Down Expand Up @@ -161,8 +194,6 @@ public void testEmptyFullContent() throws Exception {
res = (FullHttpResponse) o;
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.
assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue()));
assertThat(res.content().readableBytes(), is(0));
Expand All @@ -171,6 +202,30 @@ public void testEmptyFullContent() throws Exception {
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) {
Object o = ch.readOutbound();
assertThat(o, is(instanceOf(HttpResponse.class)));
Expand Down

0 comments on commit c89f1b0

Please sign in to comment.