【问题标题】:FileStreamResult doesn't recognize multiple http ranges in Asp.Net Core 2.2FileStreamResult 无法识别 Asp.Net Core 2.2 中的多个 http 范围
【发布时间】:2019-11-02 17:51:31
【问题描述】:

我有这么简单的端点:

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

当前文件内容:

abcdefghijklmnopqrstuvwxyz

正如您在返回语句中看到的,我将 true 传递给 enableRangeProcessing。在单范围请求的情况下,它可以按预期工作:

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

回复如下:

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

但是,在多范围请求的情况下,它不会考虑任何范围,并会返回带有文件全部内容的Ok 响应:

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

回复如下:

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

【问题讨论】:

标签: c# asp.net-core curl .net-core http-range


【解决方案1】:

我比@Nkosi 更深入地研究了源代码,以找到解析范围的地方,请看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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多