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

Commit

Permalink
PR feedback.
Browse files Browse the repository at this point in the history
- Don't change Connection header if set
- Simplified code around calls to ParseConnection and
  GetFinalTransferCoding
- Add tests with both Transfer-Encoding and Content-Length set
  • Loading branch information
Cesar Blum Silveira committed Oct 31, 2016
1 parent 930e4dd commit 7763bf3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,9 @@ private void CreateResponseHeader(
{
var responseHeaders = FrameResponseHeaders;
var hasConnection = responseHeaders.HasConnection;
var connectionOptions = hasConnection ? FrameHeaders.ParseConnection(responseHeaders.HeaderConnection) : ConnectionOptions.None;
var connectionOptions = FrameHeaders.ParseConnection(responseHeaders.HeaderConnection);
var hasTransferEncoding = responseHeaders.HasTransferEncoding;
var transferCoding = hasTransferEncoding ? FrameHeaders.GetFinalTransferCoding(responseHeaders.HeaderTransferEncoding) : TransferCoding.None;
var transferCoding = FrameHeaders.GetFinalTransferCoding(responseHeaders.HeaderTransferEncoding);

var end = SocketOutput.ProducingStart();

Expand All @@ -830,11 +830,6 @@ private void CreateResponseHeader(
if (hasTransferEncoding && transferCoding != TransferCoding.Chunked)
{
_keepAlive = false;

if (hasConnection)
{
responseHeaders.SetRawConnection("close", _bytesConnectionClose);
}
}

// Set whether response can have body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,19 +849,61 @@ await connection.ReceiveEnd(
}

[Theory]
[InlineData("gzip", true)]
[InlineData("chunked, gzip", true)]
[InlineData("gzip", false)]
[InlineData("chunked, gzip", false)]
public async Task ConnectionClosedWhenChunkedIsNotFinalTransferCoding(string responseTransferEncoding, bool setResponseKeepAlive)
[InlineData("gzip")]
[InlineData("chunked, gzip")]
[InlineData("gzip")]
[InlineData("chunked, gzip")]
public async Task ConnectionClosedWhenChunkedIsNotFinalTransferCoding(string responseTransferEncoding)
{
using (var server = new TestServer(async httpContext =>
{
if (setResponseKeepAlive)
httpContext.Response.Headers["Transfer-Encoding"] = responseTransferEncoding;
await httpContext.Response.WriteAsync("hello, world");
}, new TestServiceContext()))
{
using (var connection = server.CreateConnection())
{
httpContext.Response.Headers["Connection"] = "keep-alive";
await connection.Send(
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
"HTTP/1.1 200 OK",
"Connection: close",
$"Date: {server.Context.DateHeaderValue}",
$"Transfer-Encoding: {responseTransferEncoding}",
"",
"hello, world");
}

using (var connection = server.CreateConnection())
{
await connection.Send(
"GET / HTTP/1.0",
"Connection: keep-alive",
"",
"");
await connection.ReceiveEnd(
"HTTP/1.1 200 OK",
"Connection: close",
$"Date: {server.Context.DateHeaderValue}",
$"Transfer-Encoding: {responseTransferEncoding}",
"",
"hello, world");
}
}
}

[Theory]
[InlineData("gzip")]
[InlineData("chunked, gzip")]
[InlineData("gzip")]
[InlineData("chunked, gzip")]
public async Task ConnectionClosedWhenChunkedIsNotFinalTransferCodingEvenIfConnectionKeepAliveSetInResponse(string responseTransferEncoding)
{
using (var server = new TestServer(async httpContext =>
{
httpContext.Response.Headers["Connection"] = "keep-alive";
httpContext.Response.Headers["Transfer-Encoding"] = responseTransferEncoding;
await httpContext.Response.WriteAsync("hello, world");
}, new TestServiceContext()))
Expand All @@ -874,7 +916,7 @@ await connection.Send(
"");
await connection.ReceiveEnd(
"HTTP/1.1 200 OK",
"Connection: close",
"Connection: keep-alive",
$"Date: {server.Context.DateHeaderValue}",
$"Transfer-Encoding: {responseTransferEncoding}",
"",
Expand All @@ -890,7 +932,7 @@ await connection.Send(
"");
await connection.ReceiveEnd(
"HTTP/1.1 200 OK",
"Connection: close",
"Connection: keep-alive",
$"Date: {server.Context.DateHeaderValue}",
$"Transfer-Encoding: {responseTransferEncoding}",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,57 @@ await connection.ReceiveForcedEnd(
"");
}

// Content-Length should not affect this
using (var connection = server.CreateConnection())
{
await connection.SendAll(
"POST / HTTP/1.1",
"Transfer-Encoding: not-chunked",
"Content-Length: 22",
"",
"C",
"hello, world",
"0",
"",
"");

await connection.ReceiveForcedEnd(
"HTTP/1.1 400 Bad Request",
"Connection: close",
$"Date: {testContext.DateHeaderValue}",
"Content-Length: 0",
"",
"");
}

using (var connection = server.CreateConnection())
{
await connection.SendAll(
"POST / HTTP/1.1",
"Transfer-Encoding: chunked, not-chunked",
"",
"C",
"hello, world",
"0",
"",
"");

await connection.ReceiveForcedEnd(
"HTTP/1.1 400 Bad Request",
"Connection: close",
$"Date: {testContext.DateHeaderValue}",
"Content-Length: 0",
"",
"");
}

// Content-Length should not affect this
using (var connection = server.CreateConnection())
{
await connection.SendAll(
"POST / HTTP/1.1",
"Transfer-Encoding: chunked, not-chunked",
"Content-Length: 22",
"",
"C",
"hello, world",
Expand Down

0 comments on commit 7763bf3

Please sign in to comment.