StaticFileMiddleware中间件如何处理针对文件请求

我们通过《以Web的形式发布静态文件》和《条件请求与区间请求》中的实例演示,以及上面针对条件请求和区间请求的介绍,从提供的功能和特性的角度对这个名为StaticFileMiddleware的中间进行了全面的介绍,接下来我们将更近一步,将从实现原理的角度来进一步认识这个中间件。 [本文已经同步到《ASP.NET Core框架揭秘》之中]

目录 
一、StaticFileMiddleware
二、ContentTypeProvider
三、利用配置指定StaticFileOptions
四、实现原理

一、StaticFileMiddleware

不过在此之前,我们先来看看StaticFileMiddleware这个类型的定义。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     public StaticFileMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<StaticFileOptions> options, ILoggerFactory loggerFactory);
  1. 4:     public Task Invoke(HttpContext context);
  1. 5: }

如上面的代码片段所示,除了作为“下一个中间件”的next参数之前,StaticFileMiddleware的构造函数还包含三个参数。其中hostingEnv和loggerFactory这两个参数分别表示当前执行环境和用来创建Logger的工厂,最重要的options参数表示为这个中间件指定的配置选项,至于具体可以提供怎样的配置选项,我们只需要看看 StaticFileOptions这个类型提供了怎样的属性成员。

  1. 1: public class StaticFileOptions : SharedOptionsBase
  1. 2: {
  1. 3:     public IContentTypeProvider                  ContentTypeProvider { get; set; }
  1. 4:     public string                                DefaultContentType { get; set; }
  1. 5:     public bool                                  ServeUnknownFileTypes { get; set; }
  1. 6:     public Action<StaticFileResponseContext>     OnPrepareResponse { get; set; }   
  1. 7: 
  1. 8:     public StaticFileOptions();
  1. 9:     public StaticFileOptions(SharedOptions sharedOptions);
  1. 10: }
  1. 11: 
  1. 12: public abstract class SharedOptionsBase
  1. 13: {
  1. 14:     protected SharedOptionsBase(SharedOptions sharedOptions);
  1. 15:     public IFileProvider     FileProvider { get; set; }
  1. 16:     public PathString        RequestPath { get; set; }
  1. 17: }
  1. 18: 
  1. 19: public class SharedOptions
  1. 20: {
  1. 21:     public IFileProvider     FileProvider { get; set; }
  1. 22:     public PathString        RequestPath { get; set; }
  1. 23: }
  1. 24: 
  1. 25: public class StaticFileResponseContext
  1. 26: {
  1. 27:     public HttpContext       Context { get; }
  1. 28:     public IFileInfo         File { get; }
  1. 29: }

如上面的代码片段所示,StaticFileOptions继承自抽象类型SharedOptionsBase,后者实际上体现的是两个路径之间的映射关系,一个是HTTP请求采用的路径,另一个则是文件的物理地址,后者体现为一个FileProvider对象。不过也正是因为文件的读取是通过这个FileProvider来完成的,而FileProvider未必就一定对应着具体的物理文件,所以StaticFileMiddleware并不限于针对专门处理“物理文件”。

直接定义在StaticFileOptions中的前三个类型都与媒体类型的解析有关,其中ContentTypeProvider属性返回一个根据请求相对地址进行媒体类型的ContentTypeProvider对象。如果这个ContentTypeProvider不能正确解析出目标文件的媒体类型,我们可以利用DefaultContentType设置一个默认媒体类型。但是只有将另一个名为ServeUnknownFileTypes的属性设置为True的情况下,媒体类型不能正常识别的请求采用使用这个默认设置的媒体类型。

StaticFileOptions还具有一个OnPrepareResponse属性,它返回一个Action<StaticFileResponseContext>类型的委托对象,我们可以为这属性指定的委托对象来对最终的响应进行定制。至于作为委托输入参数的是一个类型为StaticFileResponseContext的对象,我们利用它可以获得当前的HTTP上下文和目标文件。

针对StaticFileMiddleware这个中间件的注册一般都是调用针对ApplicationBuilder的UseStaticFiles扩展方法来完成的。具体来说,一共具有三个UseStaticFiles方法重载供我们选择,如下所示的代码片段展示了这三个扩展方法的实现。

  1. 1: public static class StaticFileExtensions
  1. 2: {
  1. 3:     public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder app)
  1. 4:     {
  1. 5:         return app.UseMiddleware<StaticFileMiddleware>();
  1. 6:     }
  1. 7: 
  1. 8:     public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder app,StaticFileOptions options)
  1. 9:     {     
  1. 10:         return app.UseMiddleware<StaticFileMiddleware>(Options.Create<StaticFileOptions>(options));
  1. 11:     }
  1. 12: 
  1. 13:     public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder app, string requestPath)
  1. 14:     {      
  1. 15:         StaticFileOptions options = new StaticFileOptions
  1. 16:         {
  1. 17:             RequestPath = new PathString(requestPath)
  1. 18:         };
  1. 19:         return app.UseStaticFiles(options);
  1. 20:     }
  1. 21: }

二、ContentTypeProvider

StaticFileMiddleware针对物理文件请求的处理并不仅仅限于完成文件内容的响应,还需要针对文件的格式解析出正确的媒体类型。对于客户端来说,如果无法确定媒体类型,获取的文件就像是一步无法解码的天书,毫无意义。StaticFileMiddleware利用指定的ContentTypeProvider来解析媒体类型,ContentTypeProvider是我们对实现了IContentTypeProvider接口的所有类型以及对应对象的统称。如下面的代码片段所示,IContentTypeProvider接口定义了唯一的方法TryGetContentType根据当前请求的相对路径来解析这个作为输出参数的媒体类型。

  1. 1: public interface IContentTypeProvider
  1. 2: {
  1. 3:     bool TryGetContentType(string subpath, out string contentType);
  1. 4: }

StaticFileMiddleware默认使用的ContentProvider是一个具有如下定义的FileExtensionContentTypeProvider对象。顾名思义,FileExtensionContentTypeProvider利用物理文件的扩展名来解析对应的媒体类型,它利用其Mappings属性表示的字典维护了一个扩展名与媒体类型之间的映射关系。我们常用的数百种标准的文件扩展名和对应的媒体类型之间的映射关系都会保存在爱这个字典中。如果我们发布的文件具有一些特殊的扩展名,或者我们需要现有的某些扩展名映射为不同的媒体类型,这些通过添加或者修改映射关系来实现。

  1. 1: public class FileExtensionContentTypeProvider : IContentTypeProvider
  1. 2: {
  1. 3:     public IDictionary<string, string> Mappings { get; }
  1. 4: 
  1. 5:     public FileExtensionContentTypeProvider();
  1. 6:     public FileExtensionContentTypeProvider(IDictionary<string, string> mapping);
  1. 7: 
  1. 8:     public bool TryGetContentType(string subpath, out string contentType);
  1. 9: }

三、利用配置指定StaticFileOptions

由于StaticFileMiddleware的构造函数用来设置相关选项的options参数类型为IOptions<StaticFileOptions>,所以我们可以根据Options模式将StaticFileOptions对象承载的部分选项定义在配置文件中。比如我们利用如下所示的一个JSON文件开启了针对未知文件类型的支持,并设置了默认使用媒体类型(“application/octet-stream”),这两个配置项对应着StaticFileOptions的同名属性。

  1. 1: {
  1. 2:   "serveUnknownFileTypes"    : true,
  1. 3:   "defaultContentType"       : "application/octet-stream"
  1. 4: }

有了这个配置文件(假设文件名为“StaticFileOptions.json”),我们就可以按照如下的方式加载它并生成对应的Configuration对象,然后采用Options模式特有的编程模式实现与StaticFileOptions类型的映射。这样的配置将会自动应用到注册的StaticFileMiddleware中间件上。

  1. 1: public class Program
  1. 2: {
  1. 3:     public static void Main()
  1. 4:     {
  1. 5:         IConfiguration config = new ConfigurationBuilder()
  1. 6:             .AddJsonFile("StaticFileOptions.json")
  1. 7:             .Build();
  1. 8: 
  1. 9:         new WebHostBuilder()
  1. 10:             .UseContentRoot(Directory.GetCurrentDirectory())
  1. 11:             .UseKestrel()
  1. 12:             .ConfigureServices(svsc=>svsc.Configure<StaticFileOptions>(config))
  1. 13:             .Configure(app=>app.UseStaticFiles())
  1. 14:             .Build()
  1. 15:             .Run();
  1. 16:     }
  1. 17: }

对于上面这样的应用,所有未知文件类型都将自动映射为“application/octet-stream”媒体类型。如果使用浏览器请求一个未知类型的文件(比如前面演示的“~/wwwroot/img/ dophin1.img”),目标文件将以如下图所示的形式以一个附件的形式被下载。

四、实现原理

为了上读者朋友们对针对静态文件的请求在StaticFileMiddleware中间件的处理具有更加深刻的认识,接下来我们会采用相对简单的代码来重新定义这个中间件。这部分作为选修内容供有兴趣的读者朋友阅读,忽略这些内容不会影响对后续内容的理解。这个模拟中间件具有与StaticFileMiddleware相同的能力,它能够将目标文件的内容采用正确的媒体类型响应给客户端,同时能够处理条件请求和区间请求。

StaticFileMiddleware中间处理针对静态文件请求的整个处理流程大体上可以划分为如上图所示的三个步骤:

  • 获取目标文件:中间件根据请求的路径获取目标文件,并解析出正确的媒体类型。在此之前,中间件还会验证请求采用的方法,它支持GET和HEAD请求。除此之外,中间件还会获取文件最后被修改的时间,然后根据这个时间戳和文件内容的长度生成一个签名,响应消息的Last-odified和ETag报头的内容来源于此。
  • 条件请求解析:获取与条件请求相关的四个报头(If-Match、If-None-Match、If-Modified-Since和If-Unmodified-Since)的值,根据HTTP规范计算出最终的条件状态。
  • 响应请求:如果是区间请求,中间件会获取相关的报头(Range和If-Range)解析出正确的内容区间。中间件最终根据上面计算的条件状态和区间相关信息设置响应的响应报头,并根据需要响应整个文件的内容或者指定区间的内容。

接下来我们按照上述的这个流程来重新定义这个StaticFileMiddleware,不过在此之前先来了解一下我们预先定义的几个辅助性的扩展方法。如下面代码片段所示,扩展方法UseMethods用于判指定的请求是否采用指定的HTTP方法,而TryGetSubpath用于解析请求的目标文件的相对路径。TryGetContentType方法会根据指定的StaticFileOptions携带的ContentTypeProvider解析出正确的媒体类型,而TryGetFileInfo则根据指定的路径获取描述目标文件的FileInfo对象。至于最后的IsRangeRequest方法,它会根据是否携带Rang报头判断指定的请求是否是一个区间请求。

  1. 1: public static class Extensions
  1. 2: {
  1. 3:     public static bool UseMethods(this HttpContext context, params string[] methods)
  1. 4:     {
  1. 5:         return methods.Contains(context.Request.Method, StringComparer.OrdinalIgnoreCase);
  1. 6:     }
  1. 7: 
  1. 8:     public static bool TryGetSubpath(this HttpContext context, string requestPath, out PathString subpath)
  1. 9:     {
  1. 10:         return new PathString(context.Request.Path).StartsWithSegments(requestPath, out subpath);
  1. 11:     }
  1. 12: 
  1. 13:     public static bool TryGetContentType(this StaticFileOptions options, PathString subpath, out string contentType)
  1. 14:     {
  1. 15:         return options.ContentTypeProvider.TryGetContentType(subpath.Value, out contentType) ||(!string.IsNullOrEmpty(contentType = options.DefaultContentType) && options.ServeUnknownFileTypes);
  1. 16:     }
  1. 17: 
  1. 18:     public static bool TryGetFileInfo(this StaticFileOptions options, PathString subpath, out IFileInfo fileInfo)
  1. 19:     {
  1. 20:         return (fileInfo = options.FileProvider.GetFileInfo(subpath.Value)).Exists;
  1. 21:     }
  1. 22: 
  1. 23:     public static bool IsRangeRequest(this HttpContext context)
  1. 24:     {
  1. 25:         return context.Request.GetTypedHeaders().Range != null;
  1. 26:     }
  1. 27: }

如下所示的模拟类型 StaticFileMiddleware的定义。如果指定的StaticFileOptions没有提供FileProvider,我们会默认使用指向WebRoot目录的那个PhysicalFileProvider。如果一个具体的ContentTypeProvider没有显式指定,我们使用的则是一个FileExtensionContentTypeProvider对象。这两个默认值分别解释了两个问题,为什么请求的静态文件将WebRoot作为默认的根目录,以及为什么目标文件的扩展名决定响应的媒体类型。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     private RequestDelegate         _next;
  1. 4:     private StaticFileOptions       _options;
  1. 5: 
  1. 6:     public StaticFileMiddleware(RequestDelegate next, IHostingEnvironment env, IOptions<StaticFileOptions> options)
  1. 7:     {
  1. 8:         _next                        = next;
  1. 9:         _options                     = options.Value;
  1. 10:         _options.FileProvider        = _options.FileProvider??env.WebRootFileProvider;
  1. 11:         _options.ContentTypeProvider = _options.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
  1. 12:     }
  1. 13:     ...
  1. 14: }

我们上述的三个步骤分别实现在三个对应的方法(TryGetFileInfo、ResolvePreconditionState和SendResponseAsync)中,所以StaticFileMiddleware的Invoke方法按照如下的方式先后调用这三个方法完整对整个请求的处理。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     public async Task Invoke(HttpContext context)
  1. 4:     {
  1. 5:         IFileInfo              fileInfo;
  1. 6:         string                 contentType;
  1. 7:         DateTimeOffset?        lastModified;
  1. 8:         EntityTagHeaderValue   etag;
  1. 9: 
  1. 10:         if (this.TryGetFileInfo(context, out contentType, out fileInfo, out lastModified, out etag))
  1. 11:         {
  1. 12:             PreconditionState preconditionState = this.GetPreconditionState(context, lastModified.Value, etag);
  1. 13:             await this.SendResponseAsync(preconditionState, context, etag, lastModified.Value, contentType, fileInfo);
  1. 14:             return;        
  1. 15:         }
  1. 16:         await _next(context);
  1. 17:     }   
  1. 18:     ...
  1. 19: }

接下来我们的重点就集中到上述这三个方法的实现上。我们首先看看TryGetFileInfo方法是如何根据请求的路径获得描述目标文件的FileInfo对象的。如下面的代码片段所示,如果目标文件存在,这个方法除了将目标文件的FileInfo对象作为输出参数返回之外,与这个文件相关的数据(媒体类型、最后修改时间戳和封装签名的ETag)。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     public bool TryGetFileInfo(HttpContext context, out string contentType, out IFileInfo fileInfo, out DateTimeOffset? lastModified, out EntityTagHeaderValue etag)
  1. 4:     {
  1. 5:         contentType = null;
  1. 6:         fileInfo = null;
  1. 7:         PathString subpath;
  1. 8: 
  1. 9:         if (context.UseMethods("GET", "HEAD") &&context.TryGetSubpath(_options.RequestPath, out subpath) &&_options.TryGetContentType(subpath, out contentType) &&_options.TryGetFileInfo(subpath, out fileInfo))
  1. 10:         {
  1. 11:             DateTimeOffset last = fileInfo.LastModified;
  1. 12:             long etagHash       = last.ToFileTime() ^ fileInfo.Length;
  1. 13:             etag                = new EntityTagHeaderValue('\"' + Convert.ToString(etagHash, 16) + '\"');
  1. 14:             lastModified        = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, last.Offset).ToUniversalTime();
  1. 15:             return true;
  1. 16:         }
  1. 17: 
  1. 18:         etag             = null;
  1. 19:         lastModified     = null;
  1. 20:         return false;
  1. 21:     }
  1. 22: }

方法 GetPreconditionState旨在获取与条件请求相关的四个报头(If-Match、If-None-Match、If-Modified-Since和If-Unmodified-Since)的值,并通过与目标文件当前的状态进行比较,进而得到一个最终的检验结果。针对这四个请求报头的检验最终会产生四种可能的结果,所以我们定义了如下一个PreconditionState枚举来表示它们。

  1. 1: private enum PreconditionState
  1. 2: {
  1. 3:     Unspecified        = 0,
  1. 4:     NotModified        = 1,
  1. 5:     ShouldProcess      = 2,
  1. 6:     PreconditionFailed = 3,
  1. 7: }

对于定义在这个枚举类型中的四个选项,Unspecified表示请求中不包含这四个报头。如果将请求报头If-None-Match的值与当前文件签名进行比较,或者将请求If-Modified-Since报头的值与文件最后修改时间进行比较确定目标文件不曾改变,检验结果对应的枚举值为NotModified,反之对应的枚举值为ShouldProcess。如果目标文件当前的状态不满足If-Match或者If-Unmodified-Since报头表示的条件,检验结果对应的枚举值为PreconditionFailed,反之对应的枚举值为ShouldProcess。如果请求携带多个报头,针对它们会得出不同的检验结果,那么值最大的那个将最为最终的结果。如下面的代码片段所示,GetPreconditionState方法正是通过这样的逻辑得到这个标识最终条件检验结果的PreconditionState枚举。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     private PreconditionState GetPreconditionState(HttpContext context, DateTimeOffset lastModified, EntityTagHeaderValue etag)
  1. 4:     {
  1. 5:         PreconditionState ifMatch,ifNonematch, ifModifiedSince, ifUnmodifiedSince;
  1. 6:         ifMatch = ifNonematch = ifModifiedSince = ifUnmodifiedSince = PreconditionState.Unspecified;
  1. 7: 
  1. 8:         RequestHeaders requestHeaders = context.Request.GetTypedHeaders();
  1. 9:         //If-Match:ShouldProcess or PreconditionFailed
  1. 10:         if (requestHeaders.IfMatch != null)
  1. 11:         {
  1. 12:             ifMatch = requestHeaders.IfMatch.Any(it => it.Equals(EntityTagHeaderValue.Any) || it.Compare(etag, true))
  1. 13:                 ? PreconditionState.ShouldProcess
  1. 14:                 : PreconditionState.PreconditionFailed;
  1. 15:         }
  1. 16: 
  1. 17:         //If-None-Match:NotModified or ShouldProcess
  1. 18:         if (requestHeaders.IfNoneMatch != null)
  1. 19:         {
  1. 20:             ifNonematch = requestHeaders.IfNoneMatch.Any(it => it.Equals(EntityTagHeaderValue.Any) || it.Compare(etag, true))
  1. 21:                 ? PreconditionState.NotModified
  1. 22:                 : PreconditionState.ShouldProcess;
  1. 23:         }
  1. 24: 
  1. 25:         //If-Modified-Since: ShouldProcess or NotModified
  1. 26:         if (requestHeaders.IfModifiedSince.HasValue)
  1. 27:         {
  1. 28:             ifModifiedSince = requestHeaders.IfModifiedSince < lastModified
  1. 29:                 ? PreconditionState.ShouldProcess
  1. 30:                 : PreconditionState.NotModified;
  1. 31:         }
  1. 32: 
  1. 33:         //If-Unmodified-Since: ShouldProcess or PreconditionFailed
  1. 34:         if (requestHeaders.IfUnmodifiedSince.HasValue)
  1. 35:         {
  1. 36:             ifUnmodifiedSince = requestHeaders.IfUnmodifiedSince > lastModified
  1. 37:                 ? PreconditionState.ShouldProcess
  1. 38:                 : PreconditionState.PreconditionFailed;
  1. 39:         }
  1. 40: 
  1. 41:         //Return maximum.
  1. 42:         return new PreconditionState[] { ifMatch, ifNonematch, ifModifiedSince, ifUnmodifiedSince }.Max(); 
  1. 43:     }
  1. 44:     ...
  1. 45: }

针对静态文件的处理最终实现在SendResponseAsync方法中,这个方法最终会设置相应的响应报头和状态码,如果需要还会将目标文件的内容写入到响应报文的主体。为响应选择怎样的状态码,设置哪些报头,以及响应内容的选择除了决定于GetPreconditionState方法返回的条件检验结果外,与区间请求相关的两个报头(Range和If-Range)也是决定因素之一。为了我们定义了如下这个TryGetRanges方法来解析这两个报头并计算出正确的区间。

  1. 1: public class StaticFileMiddleware
  1. 2: {
  1. 3:     private bool TryGetRanges(HttpContext context, DateTimeOffset lastModified, EntityTagHeaderValue etag, long length, out IEnumerable<RangeItemHeaderValue> ranges)       
  1. 4:     {
  1. 5:         ranges = null;
  1. 6:         RequestHeaders requestHeaders = context.Request.GetTypedHeaders();
  1. 7: 
  1. 8:         //Check If-Range
  1. 9:         RangeConditionHeaderValue ifRange = requestHeaders.IfRange;
  1. 10:         if (ifRange != null)
  1. 11:         {
  1. 12:             bool ignore = (ifRange.EntityTag != null && !ifRange.EntityTag.Compare(etag, true)) ||(ifRange.LastModified.HasValue && ifRange.LastModified < lastModified);
  1. 13:             if (ignore)
  1. 14:             {
  1. 15:                 return false;
  1. 16:             }
  1. 17:         }
  1. 18: 
  1. 19:         List<RangeItemHeaderValue> list = new List<RangeItemHeaderValue>();
  1. 20:         foreach (var it in requestHeaders.Range.Ranges)
  1. 21:         {
  1. 22:             //Range:{from}-{to} Or {from}-
  1. 23:             if (it.From.HasValue)
  1. 24:             {
  1. 25:                 if (it.From.Value < length - 1)
  1. 26:                 {
  1. 27:                     long to = it.To.HasValue ? Math.Min(it.To.Value, length - 1) : length - 1;
  1. 28:                     list.Add(new RangeItemHeaderValue(it.From.Value, to));
  1. 29:                 }
  1. 30:             }
  1. 31:             //Range:-{size}
  1. 32:             else if (it.To.Value != 0)
  1. 33:             {
  1. 34:                 long size = Math.Min(length, it.To.Value);
  1. 35:                 list.Add(new RangeItemHeaderValue(length - size, length - 1));
  1. 36:             }
  1. 37:         }
  1. 38:         return ( ranges = list) != null;
  1. 39:     }
  1. 40:    
  1. 41: }

如上面的代码片段所示,TryGetRanges方法会先获取If-Range报头的值,并与目标位文件当前的状态进行比较。如果当前状态不满足If-Range报头表示的条件,这种情况意味着目标文件内容发生变化,那么请求Range报头携带的区间信息将自动被忽略。至于Range报头携带的值,考虑到它具有不同的表现形式(比如“bytes={from}-{to}”、“bytes={from}-”或者“bytes=-{size}”)以及指定的端点是否超出目标文件长度,这个方法定义了相应的逻辑来检验区间定义的合法性以及计算出正确的区间范围。

对于区间请求,TryGetRanges的返回值表示目标文件的当前状态是否满足If-Range携带的条件相匹配。由于HTTP规范并未限制Range报头中设置的区间数量,所以这个方法通过输出参数返回的区间信息是一个元素类型为RangeItemHeaderValue的集合。如果集合为空,表示设置的区间不符合要求。

实现在SendResponseAsync方法中针对请求的处理不外乎指定响应状态码、设置响应报头和写入响应主体内。我们将前两个工作实现在HttpContext如下这个扩展方法SetResponseHeaders中。该方法会我们指定的响应状态码应用到指定的HttpContext,并设置相应的响应报头。

  1. 1: public static class Extensions
  1. 2: {
  1. 3:         public static void SetResponseHeaders(this HttpContext context, int statusCode, EntityTagHeaderValue etag, DateTimeOffset lastModified, string contentType,long contentLength, RangeItemHeaderValue range = null)
  1. 4:         {
  1. 5:             context.Response.StatusCode = statusCode;
  1. 6:             var responseHeaders = context.Response.GetTypedHeaders();
  1. 7:             if (statusCode < 400)
  1. 8:             {
  1. 9:                 responseHeaders.ETag                                   = etag;
  1. 10:                 responseHeaders.LastModified                           = lastModified;
  1. 11:                 context.Response.ContentType                           = contentType;
  1. 12:                 context.Response.Headers[HeaderNames.AcceptRanges]     = "bytes";
  1. 13:             }
  1. 14:             if (statusCode == 200)
  1. 15:             {
  1. 16:                 context.Response.ContentLength = contentLength;
  1. 17:             }
  1. 18: 
  1. 19:             if (statusCode == 416)
  1. 20:             {
  1. 21:                 responseHeaders.ContentRange = new ContentRangeHeaderValue(contentLength);
  1. 22:             }
  1. 23: 
  1. 24:             if (statusCode == 206 && range != null)
  1. 25:             {
  1. 26:                 responseHeaders.ContentRange = new ContentRangeHeaderValue(range.From.Value, range.To.Value, contentLength);
  1. 27:             }       
  1. 28:       }
  1. 29: }

如上面的代码片段所示,对于所有非错误类型的响应(主要指“200 OK”、“206 partial Content”和“304 Not Modified”),除了表示媒体类型的Content-Type报头之外,它们还具有三个额外的报头(Last-Modified、ETag和Accept-Range)。针对区间请求的两种响应(“206 partial Content”和“416 Range Not Satisfiable”),它们都具有一个Content-Range报头。

如下所示的是 SendResponseAsync方法的完整定义。它会根据条件请求和区间请求的解析结果来决定最终采用的响应状态码。响应状态和相关响应报头的设置通过调用上面这个SetResponseHeaders方法来完成。对于状态码为“200 OK”或者“206 Partial Content”的响应,这个方法会分别将整个文件内容或者指定区间的内容写入到响应报文的主体部分。至于文件的内容的读取,我们直接可以利用表示目标文件的FileInfo的CreateReadStream方法创建的读取文件输出流来实现。

  1. 1: public class StaticFileMiddleware
  1. 2: {  
  1. 3:     private async Task SendResponseAsync(PreconditionState state, HttpContext context, EntityTagHeaderValue etag, DateTimeOffset lastModified, string contentType, IFileInfo fileInfo)
  1. 4:     {
  1. 5:         switch (state)
  1. 6:         {
  1. 7:             //304 Not Modified
  1. 8:             case PreconditionState.NotModified:
  1. 9:                 {
  1. 10:                     context.SetResponseHeaders(304, etag, lastModified, contentType, fileInfo.Length);
  1. 11:                     break;
  1. 12:                 }
  1. 13:             //416 Precondition Failded
  1. 14:             case PreconditionState.PreconditionFailed:
  1. 15:                 {
  1. 16:                     context.SetResponseHeaders(412, etag, lastModified, contentType,fileInfo.Length);
  1. 17:                     break;
  1. 18:                 }
  1. 19:             case PreconditionState.Unspecified:
  1. 20:             case PreconditionState.ShouldProcess:
  1. 21:                 {
  1. 22:                     //200 OK
  1. 23:                     if (context.UseMethods("HEAD"))
  1. 24:                     {
  1. 25:                         context.SetResponseHeaders(200, etag, lastModified, contentType, fileInfo.Length);
  1. 26:                         return;
  1. 27:                     }
  1. 28: 
  1. 29:                     IEnumerable<RangeItemHeaderValue> ranges;
  1. 30:                     if (context.IsRangeRequest() && this.TryGetRanges(context, lastModified, etag, fileInfo.Length, out ranges))
  1. 31:                     {
  1. 32:                         RangeItemHeaderValue range = ranges.FirstOrDefault();
  1. 33:                         //416
  1. 34:                         if (null == range)
  1. 35:                         {
  1. 36:                             context.SetResponseHeaders(416, etag, lastModified,
  1. 37:                                 contentType, fileInfo.Length);
  1. 38:                             return;
  1. 39:                         }
  1. 40:                         else
  1. 41:                         {
  1. 42:                             //206 Partial Content
  1. 43:                             context.SetResponseHeaders(206, etag, lastModified, contentType, fileInfo.Length, range);
  1. 44:                             context.Response.GetTypedHeaders().ContentRange = new ContentRangeHeaderValue(range.From.Value, range.To.Value, fileInfo.Length);
  1. 45:                             using (Stream stream = fileInfo.CreateReadStream())
  1. 46:                             {
  1. 47:                                 stream.Seek(range.From.Value, SeekOrigin.Begin);
  1. 48:                                 await StreamCopyOperation.CopyToAsync(stream, context.Response.Body, range.To - range.From + 1, context.RequestAborted);
  1. 49:                             }
  1. 50:                             return;
  1. 51:                         }
  1. 52:                     }
  1. 53:                     //200 OK
  1. 54:                     context.SetResponseHeaders(200, etag, lastModified, contentType, fileInfo.Length);
  1. 55:                     using (Stream stream = fileInfo.CreateReadStream())
  1. 56:                     {
  1. 57:                         await StreamCopyOperation.CopyToAsync(stream, context.Response.Body, fileInfo.Length, context.RequestAborted);
  1. 58:                     }
  1. 59:                     break;
  1. 60:                 }
  1. 61:         }
  1. 62:     }
  1. 63: }

ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件
ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求
ASP.NET Core应用针对静态文件请求的处理[3]: StaticFileMiddleware中间件如何处理针对文件请求
ASP.NET Core应用针对静态文件请求的处理[4]: DirectoryBrowserMiddleware中间件如何呈现目录结构
ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面

作者:蒋金楠 
微信公众账号:大内老A
微博:www.weibo.com/artech

StaticFileMiddleware中间件如何处理针对文件请求的更多相关文章

  1. ASP.NET Core应用针对静态文件请求的处理[3]: StaticFileMiddleware中间件如何处理针对文件请求

    我们通过<以Web的形式发布静态文件>和<条件请求与区间请求>中的实例演示,以及上面针对条件请求和区间请求的介绍,从提供的功能和特性的角度对这个名为StaticFileMidd ...

  2. ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面

    DefaultFilesMiddleware中间件的目的在于将目标目录下的默认文件作为响应内容.我们知道,如果直接请求的就是这个默认文件,那么前面介绍的StaticFileMiddleware中间件会 ...

  3. ASP.NET Core应用针对静态文件请求的处理[4]: DirectoryBrowserMiddleware中间件如何呈现目录结构

    和StaticFileMiddleware中间件一样,DirectoryBrowserMiddleware中间本质上还是定义了一个请求地址与某个物理目录之间的映射关系,而目标目录体现为一个FilePr ...

  4. ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求

    通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...

  5. ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件

    虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...

  6. Django中间件如何处理请求

    Django中间件 在http请求 到达视图函数之前   和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. Django1.9版本以后中间件的执行流程 1. ...

  7. IIS 是如何处理 ASP.NET 请求的

    #main{ width:1250px; } #mainContent{ width:915px } img#imgTop{ max-width:850px; } Web 服务器 VS Web 应用程 ...

  8. JS&CSS文件请求合并及压缩处理研究(一)

    在我们日常的网站开发工作中,一个页面难免会引用到各种样式及脚本文件.了解Web开发的朋友们都知道,页面引用的每一个: <link href="style.css" rel=& ...

  9. ASP.NET Core应用的错误处理[4]:StatusCodePagesMiddleware中间件如何针对响应码呈现错误页面

    StatusCodePagesMiddleware中间件与ExceptionHandlerMiddleware中间件比较类似,它们都是在后续请求处理过程中“出错”的情况下利用一个错误处理器来完成最终的 ...

随机推荐

  1. 同步推是如何给未越狱的IOS设备安装任意IPA的?

    工作准备: 1. 准备一台MAC 2. 拥有一份299企业证书, 然后按照下面步骤操作: 1. 把xxxx.ipa改成xxx.zip, 解压缩得到Payload文件夹 2. 替换Payload里的em ...

  2. Android 自定义带刻度的seekbar

    自定义带刻度的seekbar 1.布局 <span style="font-family:SimHei;font-size:18px;"><com.imibaby ...

  3. Android根据APP包名启动应用

    public void openApp(String packageName, Context context) { PackageManager packageManager = context.g ...

  4. STL--双端队列(deque)和链表(list)

    双端队列(deque容器类): #include<deque>与vector 类似,支持随机访问和快速插入删除,它在容器中某一位置上的操作所花费的是线性时间. 与vector不同的是:de ...

  5. 【读书笔记】iOS-自动释放池

    一,NSObject类提供了一个autorelease方法: -(id)autorelease; 该方法预先设定了一条将来在某个时间发送的release消息,其返回值是接收消息的对象.retain消息 ...

  6. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  7. 后台API服务的设计考虑

    我在<写在最前>里说过,后台API的文档至关重要.不过,文档只是外在表现形式,设计才是真正的灵魂.我在这篇博文主要介绍的就是我在后台开发过程中,设计API时的考虑.我只说他是考虑,因为很多 ...

  8. mac:ssh登陆总是需要输入钥匙串密码解决

    方法1: finder -> 左上角:前往->按住option建->多出一个资源->KeyChains->第一个文件夹(删除掉次文件夹) 然后重启即可 方法2: ssh- ...

  9. nginx服务器中的安全配置

    一.关闭SELinux 安全增强型Linux(SELinux)的是一个Linux内核的功能,它提供支持访问控制的安全政策保护机制. 但是,SELinux带来的附加安全性和使用复杂性上不成比例,性价比不 ...

  10. 关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造)

    关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造) 摘要:在中文搜索中的标点.符号往往也是有语义的,比如我们要搜索“C++”或是“C#”,我们不希望搜索出来的全是“C”吧?那样对程 ...