Always send HTTP/1.1 responses (#792).

This commit is contained in:
Cesar Blum Silveira 2016-05-16 16:11:37 -07:00
parent 2cd86a2724
commit 3e841ccba1
6 changed files with 30 additions and 25 deletions

View File

@ -30,12 +30,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
private static readonly byte[] _bytesConnectionClose = Encoding.ASCII.GetBytes("\r\nConnection: close");
private static readonly byte[] _bytesConnectionKeepAlive = Encoding.ASCII.GetBytes("\r\nConnection: keep-alive");
private static readonly byte[] _bytesTransferEncodingChunked = Encoding.ASCII.GetBytes("\r\nTransfer-Encoding: chunked");
private static readonly byte[] _bytesHttpVersion1_0 = Encoding.ASCII.GetBytes("HTTP/1.0 ");
private static readonly byte[] _bytesHttpVersion1_1 = Encoding.ASCII.GetBytes("HTTP/1.1 ");
private static readonly byte[] _bytesContentLengthZero = Encoding.ASCII.GetBytes("\r\nContent-Length: 0");
private static readonly byte[] _bytesSpace = Encoding.ASCII.GetBytes(" ");
private static readonly byte[] _bytesEndHeaders = Encoding.ASCII.GetBytes("\r\n\r\n");
private static readonly int _httpVersionLength = "HTTP/1.*".Length;
private static Vector<byte> _vectorCRs = new Vector<byte>((byte)'\r');
private static Vector<byte> _vectorColons = new Vector<byte>((byte)':');
@ -707,6 +705,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
}
else
{
// Note for future reference: never change this to set _autoChunk to true on HTTP/1.0
// connections, even if we were to infer the client supports it because an HTTP/1.0 request
// was received that used chunked encoding. Sending a chunked response to an HTTP/1.0
// client would break compliance with RFC 7230 (section 3.3.1):
//
// A server MUST NOT send a response containing Transfer-Encoding unless the corresponding
// request indicates HTTP/1.1 (or later).
if (_httpVersion == HttpVersionType.Http1_1)
{
_autoChunk = true;
@ -728,7 +733,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
responseHeaders.SetRawConnection("keep-alive", _bytesConnectionKeepAlive);
}
end.CopyFrom(_httpVersion == HttpVersionType.Http1_1 ? _bytesHttpVersion1_1 : _bytesHttpVersion1_0);
end.CopyFrom(_bytesHttpVersion1_1);
end.CopyFrom(statusBytes);
responseHeaders.CopyTo(ref end);
end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length);

View File

@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
var receiveTask = Task.Run(async () =>
{
await connection.Receive(
"HTTP/1.0 400 Bad Request",
"HTTP/1.1 400 Bad Request",
"");
await connection.ReceiveStartsWith("Date: ");
await connection.ReceiveForcedEnd(

View File

@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"0",
"");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Hello World");
}
@ -105,13 +105,13 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Goodbye");
await connection.Receive(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Connection: keep-alive",
"Content-Length: 11",
"",
"Hello World");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Content-Length: 7",
"",
"Goodbye");

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
// "?" changes to "!"
await connection.SendEnd(sendString);
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Hello World!");
}
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Hello World?");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Hello World!");
}

View File

@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Theory]
[MemberData(nameof(ConnectionFilterData))]
public async Task Http10(ServiceContext testContext)
public async Task Http10RequestReceivesHttp11Response(ServiceContext testContext)
{
using (var server = new TestServer(App, testContext))
{
@ -132,7 +132,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Hello World");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Hello World");
}
@ -268,7 +268,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Hello World");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Hello World");
}
@ -291,12 +291,12 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Goodbye");
await connection.Receive(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Connection: keep-alive",
"Content-Length: 0",
"\r\n");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Content-Length: 7",
"",
"Goodbye");
@ -322,12 +322,12 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Goodbye");
await connection.Receive(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Connection: keep-alive",
"Content-Length: 0",
"\r\n");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"Goodbye");
}
@ -351,13 +351,13 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"Goodbye");
await connection.Receive(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Connection: keep-alive",
"Content-Length: 11",
"",
"Hello World");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Content-Length: 7",
"",
"Goodbye");
@ -408,7 +408,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"GET / HTTP/1.0",
"\r\n");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"\r\n");
}
}
@ -433,7 +433,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"HTTP/1.1 200 OK",
"Content-Length: 0",
"",
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Connection: keep-alive",
"Content-Length: 0",
"",
@ -469,7 +469,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"");
await connection.ReceiveEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"",
"");
}
@ -729,7 +729,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
await connection.SendEnd(
"GET /");
await connection.Receive(
"HTTP/1.0 400 Bad Request",
"HTTP/1.1 400 Bad Request",
"");
await connection.ReceiveStartsWith("Date:");
await connection.ReceiveForcedEnd(
@ -749,7 +749,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"HTTP/1.1 200 OK",
"Content-Length: 0",
"",
"HTTP/1.0 400 Bad Request",
"HTTP/1.1 400 Bad Request",
"");
await connection.ReceiveStartsWith("Date:");
await connection.ReceiveForcedEnd(
@ -1063,7 +1063,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"");
await connection.ReceiveForcedEnd(
"HTTP/1.0 200 OK",
"HTTP/1.1 200 OK",
"Content-Length: 11",
"",
"Hello World");

View File

@ -240,7 +240,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
var reader = new StreamReader(stream);
var line = await reader.ReadLineAsync();
Assert.Equal("HTTP/1.0 200 OK", line);
Assert.Equal("HTTP/1.1 200 OK", line);
}
}
}