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

Add EnableRangeProcessingSwitch for FileContentResult and Fil… #6839

Merged
merged 3 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public virtual Task ExecuteAsync(ActionContext context, FileContentResult result
throw new ArgumentNullException(nameof(result));
}

AppContext.TryGetSwitch(EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch);
var (range, rangeLength, serveBody) = SetHeadersAndLog(
context,
result,
result.FileContents.Length,
result.LastModified,
result.EntityTag);
result.EntityTag,
enableRangeProcessingSwitch);

if (!serveBody)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
{
public class FileResultExecutorBase
{
internal const string EnableRangeProcessingSwitch = "Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing";

private const string AcceptRangeHeaderValue = "bytes";

protected const int BufferSize = 64 * 1024;
Expand All @@ -41,7 +43,8 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
FileResult result,
long? fileLength,
DateTimeOffset? lastModified = null,
EntityTagHeaderValue etag = null)
EntityTagHeaderValue etag = null,
bool enableRangeProcessing = true)
{
if (context == null)
{
Expand Down Expand Up @@ -84,19 +87,23 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)

if (fileLength.HasValue)
{
SetAcceptRangeHeader(context);
// 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 (HttpMethods.IsHead(request.Method) || HttpMethods.IsGet(request.Method))

if (enableRangeProcessing)
{
if ((preconditionState == PreconditionState.Unspecified ||
preconditionState == PreconditionState.ShouldProcess))
SetAcceptRangeHeader(context);
if (HttpMethods.IsHead(request.Method) || HttpMethods.IsGet(request.Method))
{
if (IfRangeValid(context, httpRequestHeaders, lastModified, etag))
if ((preconditionState == PreconditionState.Unspecified ||
preconditionState == PreconditionState.ShouldProcess))
{
return SetRangeHeaders(context, httpRequestHeaders, fileLength.Value);
if (IfRangeValid(context, httpRequestHeaders, lastModified, etag))
{
return SetRangeHeaders(context, httpRequestHeaders, fileLength.Value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public virtual Task ExecuteAsync(ActionContext context, FileStreamResult result)
fileLength = result.FileStream.Length;
}

AppContext.TryGetSwitch(EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch);
var (range, rangeLength, serveBody) = SetHeadersAndLog(
context,
result,
fileLength,
result.LastModified,
result.EntityTag);
result.EntityTag,
enableRangeProcessingSwitch);

if (!serveBody)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Mvc.Core.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Mvc.FunctionalTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
78 changes: 53 additions & 25 deletions test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task WriteFileAsync_CopiesBuffer_ToOutputStream()
[InlineData(6, 10, "World", 5)]
[InlineData(null, 5, "World", 5)]
[InlineData(6, null, "World", 5)]
public async Task WriteFileAsync_PreconditionStateShouldProcess_WritesRangeRequested(long? start, long? end, string expectedString, long contentLength)
public async Task WriteFileAsync_PreconditionStateShouldProcess_RangeRequestIgnored_WritesRangeRequested_IfRangeProcessingOn(long? start, long? end, string expectedString, long contentLength)
{
// Arrange
var contentType = "text/plain";
Expand Down Expand Up @@ -134,18 +134,29 @@ public async Task WriteFileAsync_PreconditionStateShouldProcess_WritesRangeReque
httpResponse.Body.Seek(0, SeekOrigin.Begin);
var streamReader = new StreamReader(httpResponse.Body);
var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
var contentRange = new ContentRangeHeaderValue(start.Value, end.Value, byteArray.Length);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]);
Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]);
Assert.Equal(contentLength, httpResponse.ContentLength);
Assert.Equal(expectedString, body);

if (AppContext.TryGetSwitch(FileResultExecutorBase.EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch)
&& enableRangeProcessingSwitch)
{
Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
var contentRange = new ContentRangeHeaderValue(start.Value, end.Value, byteArray.Length);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(contentLength, httpResponse.ContentLength);
Assert.Equal(expectedString, body);
}
else
{
Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Equal("Hello World", body);
}
}

[Fact]
public async Task WriteFileAsync_IfRangeHeaderValid_WritesRequestedRange()
public async Task WriteFileAsync_IfRangeHeaderValid_WritesRangeRequest_IfRangeProcessingOn()
{
// Arrange
var contentType = "text/plain";
Expand Down Expand Up @@ -179,18 +190,29 @@ public async Task WriteFileAsync_IfRangeHeaderValid_WritesRequestedRange()
httpResponse.Body.Seek(0, SeekOrigin.Begin);
var streamReader = new StreamReader(httpResponse.Body);
var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
var contentRange = new ContentRangeHeaderValue(0, 4, byteArray.Length);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]);
Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]);
Assert.Equal(5, httpResponse.ContentLength);
Assert.Equal("Hello", body);

if (AppContext.TryGetSwitch(FileResultExecutorBase.EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch)
&& enableRangeProcessingSwitch)
{
Assert.Equal(StatusCodes.Status206PartialContent, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
var contentRange = new ContentRangeHeaderValue(0, 4, byteArray.Length);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(5, httpResponse.ContentLength);
Assert.Equal("Hello", body);
}
else
{
Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Equal("Hello World", body);
}
}

[Fact]
public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestedIgnored()
public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestIgnored()
{
// Arrange
var contentType = "text/plain";
Expand Down Expand Up @@ -225,7 +247,6 @@ public async Task WriteFileAsync_IfRangeHeaderInvalid_RangeRequestedIgnored()
var streamReader = new StreamReader(httpResponse.Body);
var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]);
Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]);
Assert.Equal("Hello World", body);
Expand Down Expand Up @@ -265,7 +286,6 @@ public async Task WriteFileAsync_PreconditionStateUnspecified_RangeRequestIgnore
var body = streamReader.ReadToEndAsync().Result;
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]);
Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]);
Assert.Equal("Hello World", body);
Expand Down Expand Up @@ -303,16 +323,26 @@ public async Task WriteFileAsync_PreconditionStateUnspecified_RangeRequestedNotS
var streamReader = new StreamReader(httpResponse.Body);
var body = streamReader.ReadToEndAsync().Result;
var contentRange = new ContentRangeHeaderValue(byteArray.Length);
Assert.Equal(StatusCodes.Status416RangeNotSatisfiable, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Equal(lastModified.ToString("R"), httpResponse.Headers[HeaderNames.LastModified]);
Assert.Equal(entityTag.ToString(), httpResponse.Headers[HeaderNames.ETag]);
Assert.Empty(body);

if (AppContext.TryGetSwitch(FileResultExecutorBase.EnableRangeProcessingSwitch, out var enableRangeProcessingSwitch)
&& enableRangeProcessingSwitch)
{
Assert.Equal(StatusCodes.Status416RangeNotSatisfiable, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(contentRange.ToString(), httpResponse.Headers[HeaderNames.ContentRange]);
Assert.Empty(body);
}
else
{
Assert.Equal(StatusCodes.Status200OK, httpResponse.StatusCode);
Assert.Equal("Hello World", body);
}
}

[Fact]
public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
public async Task WriteFileAsync_PreconditionFailed()
{
// Arrange
var contentType = "text/plain";
Expand Down Expand Up @@ -346,15 +376,14 @@ 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("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Empty(body);
}

[Fact]
public async Task WriteFileAsync_RangeRequested_NotModified()
public async Task WriteFileAsync_NotModified()
{
// Arrange
var contentType = "text/plain";
Expand Down Expand Up @@ -388,7 +417,6 @@ 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("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Expand Down
Loading