Skip to content
This repository was archived by the owner on Mar 19, 2019. It is now read-only.

Commit 8042ee8

Browse files
committed
#339 Don't send chunked responses for HEAD requests
1 parent 0e4405a commit 8042ee8

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

src/Microsoft.AspNetCore.Server.HttpSys/RequestProcessing/Response.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ internal HttpApi.HTTP_FLAGS ComputeHeaders(long writeCount, bool endOfRequest =
384384
// The application is performing it's own chunking.
385385
_boundaryType = BoundaryType.PassThrough;
386386
}
387-
else if (endOfRequest && !(isHeadRequest && statusCanHaveBody)) // HEAD requests should always end without a body. Assume a GET response would have a body.
387+
else if (endOfRequest)
388388
{
389-
if (statusCanHaveBody)
389+
if (!isHeadRequest && statusCanHaveBody)
390390
{
391391
Headers[HttpKnownHeaderNames.ContentLength] = Constants.Zero;
392392
}

test/Microsoft.AspNetCore.Server.HttpSys.FunctionalTests/Listener/ResponseHeaderTests.cs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
namespace Microsoft.AspNetCore.Server.HttpSys.Listener
1616
{
17-
public class ResponseHeaderTests
17+
public class ResponseHeaderTests : IDisposable
1818
{
19+
private HttpClient _client = new HttpClient();
20+
21+
void IDisposable.Dispose()
22+
{
23+
_client.Dispose();
24+
}
25+
1926
[ConditionalFact]
2027
public async Task ResponseHeaders_11Request_ServerSendsDefaultHeaders()
2128
{
@@ -74,12 +81,18 @@ public async Task ResponseHeaders_11HeadRequest_ServerSendsDefaultHeaders()
7481

7582
HttpResponseMessage response = await responseTask;
7683
response.EnsureSuccessStatusCode();
77-
Assert.Equal(3, response.Headers.Count());
78-
Assert.True(response.Headers.TransferEncodingChunked.Value);
84+
Assert.Equal(2, response.Headers.Count());
85+
Assert.False(response.Headers.TransferEncodingChunked.HasValue);
7986
Assert.True(response.Headers.Date.HasValue);
8087
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers.Server.ToString());
8188
Assert.False(response.Content.Headers.Contains("Content-Length"));
8289
Assert.Equal(0, response.Content.Headers.Count());
90+
91+
// Send a second request to check for connection corruption.
92+
responseTask = SendHeadRequestAsync(address);
93+
context = await server.AcceptAsync(Utilities.DefaultTimeout);
94+
context.Dispose();
95+
response = await responseTask;
8396
}
8497
}
8598

@@ -103,6 +116,12 @@ public async Task ResponseHeaders_10HeadRequest_ServerSendsDefaultHeaders()
103116
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers.Server.ToString());
104117
Assert.False(response.Content.Headers.Contains("Content-Length"));
105118
Assert.Equal(0, response.Content.Headers.Count());
119+
120+
// Send a second request to check for connection corruption.
121+
responseTask = SendHeadRequestAsync(address);
122+
context = await server.AcceptAsync(Utilities.DefaultTimeout);
123+
context.Dispose();
124+
response = await responseTask;
106125
}
107126
}
108127

@@ -126,6 +145,12 @@ public async Task ResponseHeaders_11HeadRequestWithContentLength_Success()
126145
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers.Server.ToString());
127146
Assert.Equal(1, response.Content.Headers.Count());
128147
Assert.Equal(20, response.Content.Headers.ContentLength);
148+
149+
// Send a second request to check for connection corruption.
150+
responseTask = SendHeadRequestAsync(address);
151+
context = await server.AcceptAsync(Utilities.DefaultTimeout);
152+
context.Dispose();
153+
response = await responseTask;
129154
}
130155
}
131156

@@ -172,6 +197,12 @@ public async Task ResponseHeaders_11HeadRequestStatusCodeWithoutBody_NoContentLe
172197
Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers.Server.ToString());
173198
Assert.False(response.Content.Headers.Contains("Content-Length"));
174199
Assert.Equal(0, response.Content.Headers.Count());
200+
201+
// Send a second request to check for connection corruption.
202+
responseTask = SendHeadRequestAsync(address);
203+
context = await server.AcceptAsync(Utilities.DefaultTimeout);
204+
context.Dispose();
205+
response = await responseTask;
175206
}
176207
}
177208

@@ -484,32 +515,26 @@ public async Task AddingControlCharactersToHeadersThrows(string key, string valu
484515

485516
private async Task<HttpResponseMessage> SendRequestAsync(string uri, bool usehttp11 = true, bool sendKeepAlive = false)
486517
{
487-
using (HttpClient client = new HttpClient())
518+
var request = new HttpRequestMessage(HttpMethod.Get, uri);
519+
if (!usehttp11)
488520
{
489-
var request = new HttpRequestMessage(HttpMethod.Get, uri);
490-
if (!usehttp11)
491-
{
492-
request.Version = new Version(1, 0);
493-
}
494-
if (sendKeepAlive)
495-
{
496-
request.Headers.Add("Connection", "Keep-Alive");
497-
}
498-
return await client.SendAsync(request);
521+
request.Version = new Version(1, 0);
522+
}
523+
if (sendKeepAlive)
524+
{
525+
request.Headers.Add("Connection", "Keep-Alive");
499526
}
527+
return await _client.SendAsync(request);
500528
}
501529

502530
private async Task<HttpResponseMessage> SendHeadRequestAsync(string uri, bool usehttp11 = true)
503531
{
504-
using (HttpClient client = new HttpClient())
532+
var request = new HttpRequestMessage(HttpMethod.Head, uri);
533+
if (!usehttp11)
505534
{
506-
var request = new HttpRequestMessage(HttpMethod.Head, uri);
507-
if (!usehttp11)
508-
{
509-
request.Version = new Version(1, 0);
510-
}
511-
return await client.SendAsync(request);
535+
request.Version = new Version(1, 0);
512536
}
537+
return await _client.SendAsync(request);
513538
}
514539
}
515540
}

0 commit comments

Comments
 (0)