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

Commit 8ea6a1b

Browse files
committed
Zero contentLength msgbody doesn't need state
1 parent 487dcfc commit 8ea6a1b

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/MessageBody.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
1010
using Microsoft.Extensions.Internal;
11-
using Microsoft.Extensions.Primitives;
1211

1312
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
1413
{
1514
public abstract class MessageBody
1615
{
16+
private static readonly MessageBody _zeroContentLengthClose = new ForZeroContentLength(keepAlive: false);
17+
private static readonly MessageBody _zeroContentLengthKeepAlive = new ForZeroContentLength(keepAlive: true);
18+
1719
private readonly Frame _context;
1820
private bool _send100Continue = true;
1921

@@ -268,7 +270,13 @@ public static MessageBody For(
268270

269271
if (headers.ContentLength.HasValue)
270272
{
271-
return new ForContentLength(keepAlive, headers.ContentLength.Value, context);
273+
var contentLength = headers.ContentLength.Value;
274+
if (contentLength == 0)
275+
{
276+
return keepAlive ? _zeroContentLengthKeepAlive : _zeroContentLengthClose;
277+
}
278+
279+
return new ForContentLength(keepAlive, contentLength, context);
272280
}
273281

274282
// Avoid slowing down most common case
@@ -300,6 +308,28 @@ protected override ValueTask<ArraySegment<byte>> PeekAsync(CancellationToken can
300308
}
301309
}
302310

311+
private class ForZeroContentLength : MessageBody
312+
{
313+
public ForZeroContentLength(bool keepAlive)
314+
: base(null)
315+
{
316+
RequestKeepAlive = keepAlive;
317+
}
318+
319+
protected override ValueTask<ArraySegment<byte>> PeekAsync(CancellationToken cancellationToken)
320+
{
321+
return new ValueTask<ArraySegment<byte>>();
322+
}
323+
324+
protected override void OnConsumedBytes(int count)
325+
{
326+
if (count > 0)
327+
{
328+
throw new InvalidDataException("Consuming non-existant data");
329+
}
330+
}
331+
}
332+
303333
private class ForContentLength : MessageBody
304334
{
305335
private readonly long _contentLength;

0 commit comments

Comments
 (0)