From 5b3dbadc86a198371e4190777560528d4e743dc3 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Mon, 25 Sep 2017 15:29:15 -0700 Subject: [PATCH] Port fix for #6875: Only set Content-Length when serving body --- .../Internal/FileResultExecutorBase.cs | 7 +++++-- .../FileContentResultTest.cs | 4 ++-- .../FileStreamResultTest.cs | 4 ++-- .../PhysicalFileResultTest.cs | 4 ++-- .../VirtualFileResultTest.cs | 4 ++-- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs index d010fb8d17..78a8a31fb1 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs @@ -90,8 +90,11 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody) // Assuming the request is not a range request, the Content-Length header is set to the length of the entire file. // If the request is a valid range request, this header is overwritten with the length of the range as part of the // range processing (see method SetContentLength). - response.ContentLength = fileLength.Value; - + if (serveBody) + { + response.ContentLength = fileLength.Value; + } + if (enableRangeProcessing) { SetAcceptRangeHeader(context); diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs index 4127d3b875..3df8cd7558 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs @@ -376,7 +376,7 @@ public async Task WriteFileAsync_PreconditionFailed() var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); - Assert.Equal(11, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); @@ -417,7 +417,7 @@ public async Task WriteFileAsync_NotModified() var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); - Assert.Equal(11, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/FileStreamResultTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/FileStreamResultTest.cs index 7677676cef..67137b0d67 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/FileStreamResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/FileStreamResultTest.cs @@ -366,7 +366,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed() var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); - Assert.Equal(11, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); @@ -408,7 +408,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified() var streamReader = new StreamReader(httpResponse.Body); var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); - Assert.Equal(11, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/PhysicalFileResultTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/PhysicalFileResultTest.cs index 77e82bf686..7b91bb1af7 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/PhysicalFileResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/PhysicalFileResultTest.cs @@ -241,7 +241,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed() var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); - Assert.Equal(34, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); @@ -271,7 +271,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified() var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); - Assert.Equal(34, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/VirtualFileResultTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/VirtualFileResultTest.cs index 07b5f7e72c..2f8f10ba21 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/VirtualFileResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/VirtualFileResultTest.cs @@ -313,7 +313,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed() var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); - Assert.Equal(33, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body); @@ -355,7 +355,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified() var body = streamReader.ReadToEndAsync().Result; Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); - Assert.Equal(33, httpResponse.ContentLength); + Assert.Null(httpResponse.ContentLength); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.Empty(body);