FileStreamResult无法识别Asp.Net Core 2.2中的多个http范围 [英] FileStreamResult doesn't recognize multiple http ranges in Asp.Net Core 2.2

查看:362
本文介绍了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

abcdefghijklmnopqrstuvwxyz

您在return语句中看到的是我传递的 true 用于 enableRangeProcessing 。它在单范围请求的情况下按预期工作:

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范围:字节= 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 响应并包含文件的全部内容:

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

这是响应:

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

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);
}

这篇关于FileStreamResult无法识别Asp.Net Core 2.2中的多个http范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆