From d0ddb068bea92f75fa0c783e97065ecbe3b2e04c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 13 Sep 2018 13:35:31 -0700 Subject: [PATCH] Return FormCollection.Empty when Content-Length is 0 (#1038) * Return FormCollection.Empty when Content-Length is 0 Fixes https://github.com/aspnet/Mvc/issues/5631 --- .../Features/FormFeature.cs | 5 +++++ .../Features/FormFeatureTests.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs b/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs index 02c1bd7a..865e183f 100644 --- a/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs +++ b/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs @@ -131,6 +131,11 @@ private async Task InnerReadFormAsync(CancellationToken cancell cancellationToken.ThrowIfCancellationRequested(); + if (_request.ContentLength == 0) + { + return FormCollection.Empty; + } + if (_options.BufferBody) { _request.EnableRewind(_options.MemoryBufferThreshold, _options.BufferBodyLengthLimit); diff --git a/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs b/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs index 591f46a4..cfa8b021 100644 --- a/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs +++ b/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs @@ -12,6 +12,23 @@ namespace Microsoft.AspNetCore.Http.Features { public class FormFeatureTests { + [Fact] + public async Task ReadFormAsync_0ContentLength_ReturnsEmptyForm() + { + var context = new DefaultHttpContext(); + var responseFeature = new FakeResponseFeature(); + context.Features.Set(responseFeature); + context.Request.ContentType = MultipartContentType; + context.Request.ContentLength = 0; + + var formFeature = new FormFeature(context.Request, new FormOptions()); + context.Features.Set(formFeature); + + var formCollection = await context.Request.ReadFormAsync(); + + Assert.Same(FormCollection.Empty, formCollection); + } + [Theory] [InlineData(true)] [InlineData(false)]