Do not send Content-Length: 0 on 101 responses.

Motivation:

During code read of the Netty codebase I noticed that the Netty
HttpServerUpgradeHandler unconditionally sets a Content-Length: 0
header on 101 Switching Protocols responses. This explicitly
contravenes RFC 7230 Section 3.3.2 (Content-Length), which notes
that:

    A server MUST NOT send a Content-Length header field in any
    response with a status code of 1xx (Informational) or 204
    (No Content).

While it is unlikely that any client will ever be confused by
this behaviour, there is no reason to contravene this part of the
specification.

Modifications:

Removed the line of code setting the header field and changed the
only test that expected it to be there.

Result:

When performing the server portion of HTTP upgrade, the 101
Switching Protocols response will no longer contain a
Content-Length: 0 header field.
This commit is contained in:
Cory Benfield 2017-10-13 17:53:02 -07:00 committed by Norman Maurer
parent 16b1dbdf92
commit 1b0a545921
2 changed files with 1 additions and 3 deletions

View File

@ -350,7 +350,6 @@ public class HttpServerUpgradeHandler extends HttpObjectAggregator {
Unpooled.EMPTY_BUFFER, false); Unpooled.EMPTY_BUFFER, false);
res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
res.headers().add(HttpHeaderNames.UPGRADE, upgradeProtocol); res.headers().add(HttpHeaderNames.UPGRADE, upgradeProtocol);
res.headers().add(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);
return res; return res;
} }

View File

@ -142,8 +142,7 @@ public class CleartextHttp2ServerUpgradeHandlerTest {
String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" +
"connection: upgrade\r\n" + "connection: upgrade\r\n" +
"upgrade: h2c\r\n" + "upgrade: h2c\r\n\r\n";
"content-length: 0\r\n\r\n";
ByteBuf responseBuffer = channel.readOutbound(); ByteBuf responseBuffer = channel.readOutbound();
assertEquals(expectedHttpResponse, responseBuffer.toString(CharsetUtil.UTF_8)); assertEquals(expectedHttpResponse, responseBuffer.toString(CharsetUtil.UTF_8));
responseBuffer.release(); responseBuffer.release();