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

Commit eeac999

Browse files
authored
Only set Content-Length when serving body (#6886)
Addresses #6875
1 parent 83c3ac6 commit eeac999

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileResultExecutorBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
8989
// Assuming the request is not a range request, the Content-Length header is set to the length of the entire file.
9090
// If the request is a valid range request, this header is overwritten with the length of the range as part of the
9191
// range processing (see method SetContentLength).
92-
response.ContentLength = fileLength.Value;
92+
if (serveBody)
93+
{
94+
response.ContentLength = fileLength.Value;
95+
}
9396
if (HttpMethods.IsHead(request.Method) || HttpMethods.IsGet(request.Method))
9497
{
9598
if ((preconditionState == PreconditionState.Unspecified ||

test/Microsoft.AspNetCore.Mvc.Core.Test/FileContentResultTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
348348
var body = streamReader.ReadToEndAsync().Result;
349349
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
350350
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
351-
Assert.Equal(11, httpResponse.ContentLength);
351+
Assert.Null(httpResponse.ContentLength);
352352
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
353353
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
354354
Assert.Empty(body);
@@ -390,7 +390,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
390390
var body = streamReader.ReadToEndAsync().Result;
391391
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
392392
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
393-
Assert.Equal(11, httpResponse.ContentLength);
393+
Assert.Null(httpResponse.ContentLength);
394394
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
395395
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
396396
Assert.Empty(body);

test/Microsoft.AspNetCore.Mvc.Core.Test/FileStreamResultTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
338338
var body = streamReader.ReadToEndAsync().Result;
339339
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
340340
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
341-
Assert.Equal(11, httpResponse.ContentLength);
341+
Assert.Null(httpResponse.ContentLength);
342342
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
343343
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
344344
Assert.Empty(body);
@@ -381,7 +381,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
381381
var body = streamReader.ReadToEndAsync().Result;
382382
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
383383
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
384-
Assert.Equal(11, httpResponse.ContentLength);
384+
Assert.Null(httpResponse.ContentLength);
385385
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
386386
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
387387
Assert.Empty(body);

test/Microsoft.AspNetCore.Mvc.Core.Test/PhysicalFileResultTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
242242
var body = streamReader.ReadToEndAsync().Result;
243243
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
244244
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
245-
Assert.Equal(34, httpResponse.ContentLength);
245+
Assert.Null(httpResponse.ContentLength);
246246
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
247247
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
248248
Assert.Empty(body);
@@ -272,7 +272,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
272272
var body = streamReader.ReadToEndAsync().Result;
273273
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
274274
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
275-
Assert.Equal(34, httpResponse.ContentLength);
275+
Assert.Null(httpResponse.ContentLength);
276276
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
277277
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
278278
Assert.Empty(body);

test/Microsoft.AspNetCore.Mvc.Core.Test/VirtualFileResultTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
314314
var body = streamReader.ReadToEndAsync().Result;
315315
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
316316
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
317-
Assert.Equal(33, httpResponse.ContentLength);
317+
Assert.Null(httpResponse.ContentLength);
318318
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
319319
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
320320
Assert.Empty(body);
@@ -356,7 +356,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
356356
var body = streamReader.ReadToEndAsync().Result;
357357
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
358358
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
359-
Assert.Equal(33, httpResponse.ContentLength);
359+
Assert.Null(httpResponse.ContentLength);
360360
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
361361
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
362362
Assert.Empty(body);

0 commit comments

Comments
 (0)