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

Commit

Permalink
Don't automatically set Content-Length: 0 for HEAD requests and 304 r…
Browse files Browse the repository at this point in the history
…esponses
  • Loading branch information
halter73 committed Aug 26, 2015
1 parent 6975923 commit ce5276e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,16 @@ public void ProduceEnd(Exception ex)

if (appCompleted && !hasTransferEncoding && !hasContentLength)
{
// Since the app has completed and we are only now generating
// the headers we can safely set the Content-Length to 0.
writer.Write("Content-Length: 0\r\n");
hasContentLength = true;
// Don't set the Content-Length or Transfer-Encoding headers
// automatically for HEAD requests or 304 responses.
if (Method != "HEAD" && StatusCode != 304)
{
// Since the app has completed and we are only now generating
// the headers we can safely set the Content-Length to 0.
writer.Write("Content-Length: 0\r\n");
}
}

if (_keepAlive && !hasTransferEncoding && !hasContentLength)
else if (_keepAlive && !hasTransferEncoding && !hasContentLength)
{
if (HttpVersion == "HTTP/1.1")
{
Expand All @@ -514,6 +517,7 @@ public void ProduceEnd(Exception ex)
_keepAlive = false;
}
}

if (_keepAlive == false && hasConnection == false && HttpVersion == "HTTP/1.1")
{
writer.Write("Connection: close\r\n\r\n");
Expand Down
49 changes: 48 additions & 1 deletion test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public async Task DisconnectingClient()
}

[Fact]
public async Task EmptyResponseBodyHandledCorrectlyWithoutAnyWrites()
public async Task ZeroContentLengthSetAutomaticallyAfterNoWrites()
{
using (var server = new TestServer(frame =>
{
Expand All @@ -400,6 +400,53 @@ public async Task EmptyResponseBodyHandledCorrectlyWithoutAnyWrites()
}
}

[Fact]
public async Task ZeroContentLengthNotSetAutomaticallyForHeadRequests()
{
using (var server = new TestServer(frame =>
{
frame.ResponseHeaders.Clear();
return Task.FromResult<object>(null);
}))
{
using (var connection = new TestConnection())
{
await connection.SendEnd(
"HEAD / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
"HTTP/1.1 200 OK",
"",
"");
}
}
}

[Fact]
public async Task ZeroContentLengthNotSetAutomaticallyFor304Responses()
{
using (var server = new TestServer(frame =>
{
frame.ResponseHeaders.Clear();
frame.StatusCode = 304;
return Task.FromResult<object>(null);
}))
{
using (var connection = new TestConnection())
{
await connection.SendEnd(
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
"HTTP/1.1 304 Not Modified",
"",
"");
}
}
}

[Fact]
public async Task ThrowingResultsIn500Response()
{
Expand Down

0 comments on commit ce5276e

Please sign in to comment.