[英]FileStreamResult doesn't recognize multiple http ranges in Asp.Net Core 2.2
我有這樣簡單的端點:
[HttpGet]
public IActionResult GetFileDirect()
{
var path = ...; // path to the file
return File(System.IO.File.OpenRead(path), "text/plain", true);
}
目前文件內容:
abcdefghijklmnopqrstuvwxyz
正如您在 return 語句中看到的那樣,我為enableRangeProcessing
傳遞了true
。 在單范圍請求的情況下,它可以按預期工作:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.