Skip to content

Commit 0244e35

Browse files
committed
Fix a bug where HttpObjectDecoder produces LastHttpContent after entering BAD_MESSAGE state
- Fixes netty#2103 - Added LastHttpContent.EMPTY_LAST_CONTENT.toString() for athestic reasons.
1 parent 5dd5e6a commit 0244e35

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ private HttpMessage invalidMessage(Exception cause) {
438438
message = createInvalidMessage();
439439
message.setDecoderResult(DecoderResult.failure(cause));
440440
}
441-
return message;
441+
442+
HttpMessage ret = message;
443+
message = null;
444+
return ret;
442445
}
443446

444447
private HttpContent invalidChunk(Exception cause) {

codec-http/src/main/java/io/netty/handler/codec/http/LastHttpContent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public boolean release() {
8383
public boolean release(int decrement) {
8484
return false;
8585
}
86+
87+
@Override
88+
public String toString() {
89+
return "EmptyLastHttpContent";
90+
}
8691
};
8792

8893
HttpHeaders trailingHeaders();

codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,36 @@ public void testWebSocketResponse() {
478478

479479
assertThat(ch.readInbound(), is(nullValue()));
480480
}
481+
482+
@Test
483+
public void testGarbageHeaders() {
484+
// A response without headers - from https://github.com/netty/netty/issues/2103
485+
byte[] data = ("<html>\r\n" +
486+
"<head><title>400 Bad Request</title></head>\r\n" +
487+
"<body bgcolor=\"white\">\r\n" +
488+
"<center><h1>400 Bad Request</h1></center>\r\n" +
489+
"<hr><center>nginx/1.1.19</center>\r\n" +
490+
"</body>\r\n" +
491+
"</html>\r\n").getBytes();
492+
493+
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
494+
495+
ch.writeInbound(Unpooled.wrappedBuffer(data));
496+
497+
// Garbage input should generate the 999 Unknown response.
498+
HttpResponse res = ch.readInbound();
499+
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_0));
500+
assertThat(res.getStatus().code(), is(999));
501+
assertThat(res.getDecoderResult().isFailure(), is(true));
502+
assertThat(res.getDecoderResult().isFinished(), is(true));
503+
assertThat(ch.readInbound(), is(nullValue()));
504+
505+
// More garbage should not generate anything (i.e. the decoder discards anything beyond this point.)
506+
ch.writeInbound(Unpooled.wrappedBuffer(data));
507+
assertThat(ch.readInbound(), is(nullValue()));
508+
509+
// Closing the connection should not generate anything since the protocol has been violated.
510+
ch.finish();
511+
assertThat(ch.readInbound(), is(nullValue()));
512+
}
481513
}

0 commit comments

Comments
 (0)