Skip to content

Commit 603cd03

Browse files
authored
Stop writing responses after the unexpected end of request content (#2734)
1 parent b934e46 commit 603cd03

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Kestrel.Core/Internal/Http/Http1MessageBody.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,15 @@ private async Task PumpAsync()
9191
// Read() will have already have greedily consumed the entire request body if able.
9292
if (result.IsCompleted)
9393
{
94+
// OnInputOrOutputCompleted() is an idempotent method that closes the connection. Sometimes
95+
// input completion is observed here before the Input.OnWriterCompleted() callback is fired,
96+
// so we call OnInputOrOutputCompleted() now to prevent a race in our tests where a 400
97+
// response is written after observing the unexpected end of request content instead of just
98+
// closing the connection without a response as expected.
99+
_context.OnInputOrOutputCompleted();
100+
94101
// Treat any FIN from an upgraded request as expected.
95-
// It's up to higher-level consumer (i.e. WebSocket middleware) to determine
102+
// It's up to higher-level consumer (i.e. WebSocket middleware) to determine
96103
// if the end is actually expected based on higher-level framing.
97104
if (RequestUpgrade)
98105
{

src/Kestrel.Core/Internal/Http/HttpProtocol.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,11 +1302,6 @@ private BadHttpRequestException GetInvalidRequestTargetException(Span<byte> targ
13021302
? target.GetAsciiStringEscaped(Constants.MaxExceptionDetailSize)
13031303
: string.Empty);
13041304

1305-
public void SetBadRequestState(RequestRejectionReason reason)
1306-
{
1307-
SetBadRequestState(BadHttpRequestException.GetException(reason));
1308-
}
1309-
13101305
public void SetBadRequestState(BadHttpRequestException ex)
13111306
{
13121307
Log.ConnectionBadRequest(ConnectionId, ex);

0 commit comments

Comments
 (0)