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

Commit bd8b4d8

Browse files
authored
Port fix for #6875: Only set Content-Length when serving body (#6888)
Addresses #6887
1 parent 1ca5884 commit bd8b4d8

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
9090
// Assuming the request is not a range request, the Content-Length header is set to the length of the entire file.
9191
// If the request is a valid range request, this header is overwritten with the length of the range as part of the
9292
// range processing (see method SetContentLength).
93-
response.ContentLength = fileLength.Value;
94-
93+
if (serveBody)
94+
{
95+
response.ContentLength = fileLength.Value;
96+
}
97+
9598
if (enableRangeProcessing)
9699
{
97100
SetAcceptRangeHeader(context);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public async Task WriteFileAsync_PreconditionFailed()
376376
var streamReader = new StreamReader(httpResponse.Body);
377377
var body = streamReader.ReadToEndAsync().Result;
378378
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
379-
Assert.Equal(11, httpResponse.ContentLength);
379+
Assert.Null(httpResponse.ContentLength);
380380
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
381381
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
382382
Assert.Empty(body);
@@ -417,7 +417,7 @@ public async Task WriteFileAsync_NotModified()
417417
var streamReader = new StreamReader(httpResponse.Body);
418418
var body = streamReader.ReadToEndAsync().Result;
419419
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
420-
Assert.Equal(11, httpResponse.ContentLength);
420+
Assert.Null(httpResponse.ContentLength);
421421
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
422422
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
423423
Assert.Empty(body);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
366366
var streamReader = new StreamReader(httpResponse.Body);
367367
var body = streamReader.ReadToEndAsync().Result;
368368
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
369-
Assert.Equal(11, httpResponse.ContentLength);
369+
Assert.Null(httpResponse.ContentLength);
370370
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
371371
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
372372
Assert.Empty(body);
@@ -408,7 +408,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
408408
var streamReader = new StreamReader(httpResponse.Body);
409409
var body = streamReader.ReadToEndAsync().Result;
410410
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
411-
Assert.Equal(11, httpResponse.ContentLength);
411+
Assert.Null(httpResponse.ContentLength);
412412
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
413413
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
414414
Assert.Empty(body);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
241241
var body = streamReader.ReadToEndAsync().Result;
242242
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
243243
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
244-
Assert.Equal(34, httpResponse.ContentLength);
244+
Assert.Null(httpResponse.ContentLength);
245245
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
246246
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
247247
Assert.Empty(body);
@@ -271,7 +271,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
271271
var body = streamReader.ReadToEndAsync().Result;
272272
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
273273
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
274-
Assert.Equal(34, httpResponse.ContentLength);
274+
Assert.Null(httpResponse.ContentLength);
275275
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
276276
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
277277
Assert.Empty(body);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public async Task WriteFileAsync_RangeRequested_PreconditionFailed()
313313
var body = streamReader.ReadToEndAsync().Result;
314314
Assert.Equal(StatusCodes.Status412PreconditionFailed, httpResponse.StatusCode);
315315
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
316-
Assert.Equal(33, httpResponse.ContentLength);
316+
Assert.Null(httpResponse.ContentLength);
317317
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
318318
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
319319
Assert.Empty(body);
@@ -355,7 +355,7 @@ public async Task WriteFileAsync_RangeRequested_NotModified()
355355
var body = streamReader.ReadToEndAsync().Result;
356356
Assert.Equal(StatusCodes.Status304NotModified, httpResponse.StatusCode);
357357
Assert.Equal("bytes", httpResponse.Headers[HeaderNames.AcceptRanges]);
358-
Assert.Equal(33, httpResponse.ContentLength);
358+
Assert.Null(httpResponse.ContentLength);
359359
Assert.Empty(httpResponse.Headers[HeaderNames.ContentRange]);
360360
Assert.NotEmpty(httpResponse.Headers[HeaderNames.LastModified]);
361361
Assert.Empty(body);

0 commit comments

Comments
 (0)