Lazy create StaticFileMiddleware statemachine (#9507)

This commit is contained in:
Ben Adams 2019-04-18 22:44:00 +01:00 committed by Chris Ross
parent c84e37f30d
commit 507a765dfb
2 changed files with 51 additions and 46 deletions

View File

@ -103,7 +103,6 @@ namespace Microsoft.AspNetCore.StaticFiles
public partial class StaticFileMiddleware
{
public StaticFileMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.StaticFileOptions> options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
[System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
}
public partial class StaticFileResponseContext

View File

@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.StaticFiles
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
public Task Invoke(HttpContext context)
{
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);
@ -95,6 +95,14 @@ namespace Microsoft.AspNetCore.StaticFiles
else
{
// If we get here, we can try to serve the file
return ServeStaticFile(context, fileContext);
}
return _next(context);
}
private async Task ServeStaticFile(HttpContext context, StaticFileContext fileContext)
{
fileContext.ComprehendRequestHeaders();
switch (fileContext.GetPreconditionState())
{
@ -122,7 +130,8 @@ namespace Microsoft.AspNetCore.StaticFiles
{
context.Response.Clear();
}
break;
await _next(context);
return;
case StaticFileContext.PreconditionState.NotModified:
_logger.FileNotModified(fileContext.SubPath);
await fileContext.SendStatusAsync(Constants.Status304NotModified);
@ -139,8 +148,5 @@ namespace Microsoft.AspNetCore.StaticFiles
throw exception;
}
}
await _next(context);
}
}
}