Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit f161963

Browse files
committed
Don't set TE header for non-body responses
Resolves #952
1 parent efa37e5 commit f161963

File tree

1 file changed

+21
-20
lines changed
  • src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http

1 file changed

+21
-20
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -729,36 +729,37 @@ private void CreateResponseHeader(
729729

730730
if (_keepAlive && !responseHeaders.HasTransferEncoding && !responseHeaders.HasContentLength)
731731
{
732-
if (appCompleted)
732+
// Don't set the Content-Length or Transfer-Encoding headers
733+
// automatically for HEAD requests or 101, 204, 205, 304 responses.
734+
if (Method != "HEAD" && StatusCanHaveBody(StatusCode))
733735
{
734-
// Don't set the Content-Length or Transfer-Encoding headers
735-
// automatically for HEAD requests or 101, 204, 205, 304 responses.
736-
if (Method != "HEAD" && StatusCanHaveBody(StatusCode))
736+
if (appCompleted)
737737
{
738738
// Since the app has completed and we are only now generating
739739
// the headers we can safely set the Content-Length to 0.
740740
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
741741
}
742-
}
743-
else
744-
{
745-
// Note for future reference: never change this to set _autoChunk to true on HTTP/1.0
746-
// connections, even if we were to infer the client supports it because an HTTP/1.0 request
747-
// was received that used chunked encoding. Sending a chunked response to an HTTP/1.0
748-
// client would break compliance with RFC 7230 (section 3.3.1):
749-
//
750-
// A server MUST NOT send a response containing Transfer-Encoding unless the corresponding
751-
// request indicates HTTP/1.1 (or later).
752-
if (_httpVersion == HttpVersionType.Http11)
753-
{
754-
_autoChunk = true;
755-
responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked);
756-
}
757742
else
758743
{
759-
_keepAlive = false;
744+
// Note for future reference: never change this to set _autoChunk to true on HTTP/1.0
745+
// connections, even if we were to infer the client supports it because an HTTP/1.0 request
746+
// was received that used chunked encoding. Sending a chunked response to an HTTP/1.0
747+
// client would break compliance with RFC 7230 (section 3.3.1):
748+
//
749+
// A server MUST NOT send a response containing Transfer-Encoding unless the corresponding
750+
// request indicates HTTP/1.1 (or later).
751+
if (_httpVersion == HttpVersionType.Http11)
752+
{
753+
_autoChunk = true;
754+
responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked);
755+
}
760756
}
761757
}
758+
759+
if (!appCompleted && _httpVersion != HttpVersionType.Http11)
760+
{
761+
_keepAlive = false;
762+
}
762763
}
763764

764765
if (!_keepAlive && !hasConnection && _httpVersion != HttpVersionType.Http10)

0 commit comments

Comments
 (0)