From 603cd03bfa7f8efeeb747e926cee1658be12004b Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 19 Jul 2018 14:18:57 -0700 Subject: [PATCH] Stop writing responses after the unexpected end of request content (#2734) --- src/Kestrel.Core/Internal/Http/Http1MessageBody.cs | 9 ++++++++- src/Kestrel.Core/Internal/Http/HttpProtocol.cs | 5 ----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Kestrel.Core/Internal/Http/Http1MessageBody.cs b/src/Kestrel.Core/Internal/Http/Http1MessageBody.cs index f11619d7f3..d2edbff7f8 100644 --- a/src/Kestrel.Core/Internal/Http/Http1MessageBody.cs +++ b/src/Kestrel.Core/Internal/Http/Http1MessageBody.cs @@ -91,8 +91,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http // Read() will have already have greedily consumed the entire request body if able. if (result.IsCompleted) { + // OnInputOrOutputCompleted() is an idempotent method that closes the connection. Sometimes + // input completion is observed here before the Input.OnWriterCompleted() callback is fired, + // so we call OnInputOrOutputCompleted() now to prevent a race in our tests where a 400 + // response is written after observing the unexpected end of request content instead of just + // closing the connection without a response as expected. + _context.OnInputOrOutputCompleted(); + // Treat any FIN from an upgraded request as expected. - // It's up to higher-level consumer (i.e. WebSocket middleware) to determine + // It's up to higher-level consumer (i.e. WebSocket middleware) to determine // if the end is actually expected based on higher-level framing. if (RequestUpgrade) { diff --git a/src/Kestrel.Core/Internal/Http/HttpProtocol.cs b/src/Kestrel.Core/Internal/Http/HttpProtocol.cs index e572f8b2c0..5518c0beee 100644 --- a/src/Kestrel.Core/Internal/Http/HttpProtocol.cs +++ b/src/Kestrel.Core/Internal/Http/HttpProtocol.cs @@ -1302,11 +1302,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http ? target.GetAsciiStringEscaped(Constants.MaxExceptionDetailSize) : string.Empty); - public void SetBadRequestState(RequestRejectionReason reason) - { - SetBadRequestState(BadHttpRequestException.GetException(reason)); - } - public void SetBadRequestState(BadHttpRequestException ex) { Log.ConnectionBadRequest(ConnectionId, ex);