Skip to content

Commit b360abc

Browse files
authored
Allow HTTP responses without reason-phrase (#14183)
Motivation: #14180 introduced some changes which broke the handling of HTTP responses without a reason-phrase. This change fixes it by correctly following the RFC Modifications: Correctly handle responses without reason-phrase Result: Fixes regression introduced by #14180
1 parent a6bf424 commit b360abc

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,15 @@ private String[] splitInitialLine(ByteBuf asciiBuffer) {
950950

951951
byte lastByte = asciiBytes[end - 1];
952952
if (isControlOrWhitespaceAsciiChar(lastByte)) {
953-
// There should no extra control or whitespace char.
954-
// See https://datatracker.ietf.org/doc/html/rfc2616#section-5.1
955-
throw new IllegalArgumentException("Illegal character in request line: 0x" + Integer.toHexString(lastByte));
953+
if (isDecodingRequest() || !isOWS(lastByte)) {
954+
// There should no extra control or whitespace char in case of a request.
955+
// In case of a response there might be a SP if there is no reason-phrase given.
956+
// See
957+
// - https://datatracker.ietf.org/doc/html/rfc2616#section-5.1
958+
// - https://datatracker.ietf.org/doc/html/rfc9112#name-status-line
959+
throw new IllegalArgumentException(
960+
"Illegal character in request line: 0x" + Integer.toHexString(lastByte));
961+
}
956962
}
957963

958964
final int aStart = findNonSPLenient(asciiBytes, startContent, end);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,20 @@ public void testHttpMessageDecoderResult() {
10451045
assertFalse(channel.finish());
10461046
}
10471047

1048+
@Test
1049+
public void testStatusWithoutReasonPhrase() {
1050+
String responseStr = "HTTP/1.1 200 \r\n" +
1051+
"Content-Length: 0\r\n\r\n";
1052+
EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseDecoder());
1053+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(responseStr, CharsetUtil.US_ASCII)));
1054+
HttpResponse response = channel.readInbound();
1055+
assertTrue(response.decoderResult().isSuccess());
1056+
assertEquals(HttpResponseStatus.OK, response.status());
1057+
HttpContent c = channel.readInbound();
1058+
c.release();
1059+
assertFalse(channel.finish());
1060+
}
1061+
10481062
@Test
10491063
public void testHeaderNameStartsWithControlChar1c() {
10501064
testHeaderNameStartsWithControlChar(0x1c);

0 commit comments

Comments
 (0)