c# - FileStreamResult 无法识别 Asp.Net Core 2.2 中的多个 http 范围
问题描述
我有这样简单的端点:
[HttpGet]
public IActionResult GetFileDirect()
{
var path = ...; // path to the file
return File(System.IO.File.OpenRead(path), "text/plain", true);
}
目前文件内容:
abcdefghijklmnopqrstuvwxyz
正如您在 return 语句中看到的那样,我传递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
解决方案
我深入研究了比@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);
}
推荐阅读
- kubernetes - 尽管我有相同的选择器标签,但现有的 pod 不符合部署对象的条件
- amazon-web-services - 如何在可重用的 CDK 结构中打包 Lambda?
- postgresql - 带有 Postgres 的 Micronaut 数据 (JDBC) 使用 backtics 生成 SQL `
- python-3.x - 除了诊断之外,是否可以从 Seaborn Pairplot 中删除一个类?
- c# - 使用 SkiaSharp 进行轮播视图
- reactjs - Reactjs 电子邮件框架
- google-apps-script - 您可以在 gas 中创建电子表格并获取 ID 和 getActive 吗?
- sql - 用于连接子句的 TypeORM 多重条件
- python - Pip 无法使用 Python 3.8.5 安装 PyTorch
- c - 打印结构指针 c