Set Content-Length and increase BufferSize (#6347)

Addresses #6045
This commit is contained in:
Jass Bagga 2017-06-01 11:27:49 -07:00 committed by GitHub
parent c1a14f22cc
commit a0d9b08f58
5 changed files with 22 additions and 17 deletions

View File

@ -19,8 +19,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
{ {
private const string AcceptRangeHeaderValue = "bytes"; private const string AcceptRangeHeaderValue = "bytes";
// default buffer size as defined in BufferedStream type protected const int BufferSize = 64 * 1024;
protected const int BufferSize = 0x1000;
public FileResultExecutorBase(ILogger logger) public FileResultExecutorBase(ILogger logger)
{ {
@ -40,9 +39,9 @@ namespace Microsoft.AspNetCore.Mvc.Internal
protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody) SetHeadersAndLog( protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody) SetHeadersAndLog(
ActionContext context, ActionContext context,
FileResult result, long? FileResult result,
fileLength, DateTimeOffset? long? fileLength,
lastModified = null, DateTimeOffset? lastModified = null,
EntityTagHeaderValue etag = null) EntityTagHeaderValue etag = null)
{ {
if (context == null) if (context == null)
@ -57,15 +56,20 @@ namespace Microsoft.AspNetCore.Mvc.Internal
SetContentType(context, result); SetContentType(context, result);
SetContentDispositionHeader(context, result); SetContentDispositionHeader(context, result);
Logger.FileResultExecuting(result.FileDownloadName); Logger.FileResultExecuting(result.FileDownloadName);
if (fileLength.HasValue)
{
SetAcceptRangeHeader(context);
}
var request = context.HttpContext.Request; var request = context.HttpContext.Request;
var httpRequestHeaders = request.GetTypedHeaders(); var httpRequestHeaders = request.GetTypedHeaders();
var response = context.HttpContext.Response; var response = context.HttpContext.Response;
var httpResponseHeaders = response.GetTypedHeaders(); var httpResponseHeaders = response.GetTypedHeaders();
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 (lastModified.HasValue) if (lastModified.HasValue)
{ {
httpResponseHeaders.LastModified = lastModified; httpResponseHeaders.LastModified = lastModified;
@ -276,6 +280,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
fileLength.Value); fileLength.Value);
response.StatusCode = StatusCodes.Status206PartialContent; response.StatusCode = StatusCodes.Status206PartialContent;
// Overwrite the Content-Length header for valid range requests with the range length.
var rangeLength = SetContentLength(context, range); var rangeLength = SetContentLength(context, range);
return (range, rangeLength, serveBody: true); return (range, rangeLength, serveBody: true);
} }

View File

@ -308,9 +308,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }
@ -350,9 +350,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }

View File

@ -297,9 +297,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }
@ -340,9 +340,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(11, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }

View File

@ -208,9 +208,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(34, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }
@ -238,9 +238,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(34, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }

View File

@ -270,9 +270,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(33, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }
@ -312,9 +312,9 @@ namespace Microsoft.AspNetCore.Mvc
var body = streamReader.ReadToEndAsync().Result; var body = streamReader.ReadToEndAsync().Result;
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode); Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]); Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
Assert.Equal(33, httpResponse.ContentLength);
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]); Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]); Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
Assert.Null(httpResponse.ContentLength);
Assert.Empty(body); Assert.Empty(body);
} }