FileStreamResult doesn't recognize multiple http ranges in Asp.Net Core 2.2

1.3k views Asked by At

I have such simple endpoint:

[HttpGet]
public IActionResult GetFileDirect()
{
     var path = ...; // path to the file
     return File(System.IO.File.OpenRead(path), "text/plain", true);
}

Currently the content of the file:

abcdefghijklmnopqrstuvwxyz

As you see in the return statement I am passing true for enableRangeProcessing. And it works as expected in case of single range request:

curl -H Range:bytes=0-8 http://localhost:65318/api/File -i

Here is the response:

HTTP/1.1 206 Partial Content
Content-Length: 9
Content-Type: text/plain
Content-Range: bytes 0-8/26
Accept-Ranges: bytes
Server: Kestrel
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcY2ViaXlcRGVza3RvcFxSYW5nZVdlYlxSYW5nZVdlYlxhcGlcRmlsZQ==?=
X-Powered-By: ASP.NET
Date: Sat, 02 Nov 2019 17:46:49 GMT

abcdefghi

But, in case of multi range request, it just won't consider any range and will return Ok response with full content of the file:

curl -H  Range:bytes=0-8,12-15 http://localhost:65318/api/File -i

Here is the response:

HTTP/1.1 200 OK
Content-Length: 26
Content-Type: text/plain
Accept-Ranges: bytes
Server: Kestrel
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcY2ViaXlcRGVza3RvcFxSYW5nZVdlYlxSYW5nZVdlYlxhcGlcRmlsZQ==?=
X-Powered-By: ASP.NET
Date: Sat, 02 Nov 2019 17:49:37 GMT

abcdefghijklmnopqrstuvwxyz
1

There are 1 answers

0
vladimir On BEST ANSWER

I dived to source code a little deeper than @Nkosi to find the place where ranges are parsed, look at AspNetCore - RangeHelper.cs

if (rawRangeHeader.Count > 1 || rawRangeHeader[0].IndexOf(',') >= 0)
{
    logger.LogDebug("Multiple ranges are not supported.");

    // The spec allows for multiple ranges but we choose not to support them because the client may request
    // very strange ranges (e.g. each byte separately, overlapping ranges, etc.) that could negatively
    // impact the server. Ignore the header and serve the response normally.               
    return (false, null);
}