Consumes request before closing connection (#2314)

This commit is contained in:
Alessio Franceschelli 2018-02-14 19:46:13 +00:00 committed by Chris Ross
parent b0673337e9
commit 9341f72b8d
4 changed files with 31 additions and 1 deletions

View File

@ -294,6 +294,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
RequestUpgrade = true;
}
public override bool IsEmpty => true;
protected override bool Read(ReadOnlyBuffer<byte> readableBuffer, PipeWriter writableBuffer, out SequencePosition consumed, out SequencePosition examined)
{
Copy(readableBuffer, writableBuffer);

View File

@ -533,7 +533,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
await ProduceEnd();
// ForZeroContentLength does not complete the reader nor the writer
if (!messageBody.IsEmpty && _keepAlive)
if (!messageBody.IsEmpty)
{
// Finish reading the request body in case the app did not.
await messageBody.ConsumeAsync();

View File

@ -12,6 +12,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
@ -792,6 +793,26 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
Assert.StartsWith(CoreStrings.NonNegativeNumberOrNullRequired, ex.Message);
}
[Fact]
public async Task ConsumesRequestWhenApplicationDoesNotConsumeIt()
{
var httpApplication = new DummyApplication(async context =>
{
var buffer = new byte[10];
await context.Response.Body.WriteAsync(buffer, 0, 10);
});
var mockMessageBody = new Mock<MessageBody>(null);
_http1Connection.NextMessageBody = mockMessageBody.Object;
var requestProcessingTask = _http1Connection.ProcessRequestsAsync(httpApplication);
var data = Encoding.ASCII.GetBytes("POST / HTTP/1.1\r\nHost:\r\nConnection: close\r\ncontent-length: 1\r\n\r\n");
await _application.Output.WriteAsync(data);
await requestProcessingTask.TimeoutAfter(TestConstants.DefaultTimeout);
mockMessageBody.Verify(body => body.ConsumeAsync(), Times.Once);
}
private static async Task WaitForCondition(TimeSpan timeout, Func<bool> condition)
{
const int MaxWaitLoop = 150;

View File

@ -25,9 +25,16 @@ namespace Microsoft.AspNetCore.Testing
set => _keepAlive = value;
}
public MessageBody NextMessageBody { private get; set; }
public Task ProduceEndAsync()
{
return ProduceEnd();
}
protected override MessageBody CreateMessageBody()
{
return NextMessageBody ?? base.CreateMessageBody();
}
}
}