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

Commit d50bcf1

Browse files
author
Cesar Blum Silveira
committed
Chunk non-keepalive HTTP/1.1 responses.
1 parent ec6a664 commit d50bcf1

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ private void CreateResponseHeader(
781781
// the headers we can safely set the Content-Length to 0.
782782
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
783783
}
784-
else if (_keepAlive)
784+
else
785785
{
786786
// Note for future reference: never change this to set _autoChunk to true on HTTP/1.0
787787
// connections, even if we were to infer the client supports it because an HTTP/1.0 request

test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedResponseTests.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Testing;
910
using Xunit;
1011

@@ -67,9 +68,8 @@ public async Task ResponsesAreNotChunkedAutomaticallyForHttp10Requests(TestServi
6768
{
6869
using (var server = new TestServer(async httpContext =>
6970
{
70-
var response = httpContext.Response;
71-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello "), 0, 6);
72-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("World!"), 0, 6);
71+
await httpContext.Response.WriteAsync("Hello ");
72+
await httpContext.Response.WriteAsync("World!");
7373
}, testContext))
7474
{
7575
using (var connection = server.CreateConnection())
@@ -89,13 +89,14 @@ await connection.ReceiveEnd(
8989
}
9090
}
9191

92-
public async Task ResponsesAreNotChunkedAutomaticallyForHttp11NonKeepAliveRequests(TestServiceContext testContext)
92+
[Theory]
93+
[MemberData(nameof(ConnectionFilterData))]
94+
public async Task ResponsesAreChunkedAutomaticallyForHttp11NonKeepAliveRequests(TestServiceContext testContext)
9395
{
9496
using (var server = new TestServer(async httpContext =>
9597
{
96-
var response = httpContext.Response;
97-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello "), 0, 6);
98-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("World!"), 0, 6);
98+
await httpContext.Response.WriteAsync("Hello ");
99+
await httpContext.Response.WriteAsync("World!");
99100
}, testContext))
100101
{
101102
using (var connection = server.CreateConnection())
@@ -109,22 +110,28 @@ await connection.ReceiveEnd(
109110
"HTTP/1.1 200 OK",
110111
"Connection: close",
111112
$"Date: {testContext.DateHeaderValue}",
113+
"Transfer-Encoding: chunked",
112114
"",
113-
"Hello World!");
115+
"6",
116+
"Hello ",
117+
"6",
118+
"World!",
119+
"0",
120+
"",
121+
"");
114122
}
115123
}
116124
}
117125

118126
[Theory]
119127
[MemberData(nameof(ConnectionFilterData))]
120-
public async Task SettingConnectionCloseHeaderInAppDisablesChunking(TestServiceContext testContext)
128+
public async Task SettingConnectionCloseHeaderInAppDoesNotDisableChunking(TestServiceContext testContext)
121129
{
122130
using (var server = new TestServer(async httpContext =>
123131
{
124-
var response = httpContext.Response;
125-
response.Headers["Connection"] = "close";
126-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("Hello "), 0, 6);
127-
await response.Body.WriteAsync(Encoding.ASCII.GetBytes("World!"), 0, 6);
132+
httpContext.Response.Headers["Connection"] = "close";
133+
await httpContext.Response.WriteAsync("Hello ");
134+
await httpContext.Response.WriteAsync("World!");
128135
}, testContext))
129136
{
130137
using (var connection = server.CreateConnection())
@@ -137,8 +144,15 @@ await connection.ReceiveEnd(
137144
"HTTP/1.1 200 OK",
138145
"Connection: close",
139146
$"Date: {testContext.DateHeaderValue}",
147+
"Transfer-Encoding: chunked",
140148
"",
141-
"Hello World!");
149+
"6",
150+
"Hello ",
151+
"6",
152+
"World!",
153+
"0",
154+
"",
155+
"");
142156
}
143157
}
144158
}

0 commit comments

Comments
 (0)