From 507a765dfb078f446c445318e7b55ad9f7f5cbe0 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 18 Apr 2019 22:44:00 +0100 Subject: [PATCH] Lazy create StaticFileMiddleware statemachine (#9507) --- ...ft.AspNetCore.StaticFiles.netcoreapp3.0.cs | 1 - .../StaticFiles/src/StaticFileMiddleware.cs | 96 ++++++++++--------- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/Middleware/StaticFiles/ref/Microsoft.AspNetCore.StaticFiles.netcoreapp3.0.cs b/src/Middleware/StaticFiles/ref/Microsoft.AspNetCore.StaticFiles.netcoreapp3.0.cs index 5695c7befe..424801bb14 100644 --- a/src/Middleware/StaticFiles/ref/Microsoft.AspNetCore.StaticFiles.netcoreapp3.0.cs +++ b/src/Middleware/StaticFiles/ref/Microsoft.AspNetCore.StaticFiles.netcoreapp3.0.cs @@ -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 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 diff --git a/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs b/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs index fe9dcd0119..5f1dbb4e0f 100644 --- a/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs +++ b/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs @@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.StaticFiles /// /// /// - public async Task Invoke(HttpContext context) + public Task Invoke(HttpContext context) { var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider); @@ -95,52 +95,58 @@ namespace Microsoft.AspNetCore.StaticFiles else { // If we get here, we can try to serve the file - fileContext.ComprehendRequestHeaders(); - switch (fileContext.GetPreconditionState()) - { - case StaticFileContext.PreconditionState.Unspecified: - case StaticFileContext.PreconditionState.ShouldProcess: - if (fileContext.IsHeadMethod) - { - await fileContext.SendStatusAsync(Constants.Status200Ok); - return; - } - - try - { - if (fileContext.IsRangeRequest) - { - await fileContext.SendRangeAsync(); - return; - } - - await fileContext.SendAsync(); - _logger.FileServed(fileContext.SubPath, fileContext.PhysicalPath); - return; - } - catch (FileNotFoundException) - { - context.Response.Clear(); - } - break; - case StaticFileContext.PreconditionState.NotModified: - _logger.FileNotModified(fileContext.SubPath); - await fileContext.SendStatusAsync(Constants.Status304NotModified); - return; - - case StaticFileContext.PreconditionState.PreconditionFailed: - _logger.PreconditionFailed(fileContext.SubPath); - await fileContext.SendStatusAsync(Constants.Status412PreconditionFailed); - return; - - default: - var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString()); - Debug.Fail(exception.ToString()); - throw exception; - } + return ServeStaticFile(context, fileContext); } - await _next(context); + return _next(context); + } + + private async Task ServeStaticFile(HttpContext context, StaticFileContext fileContext) + { + fileContext.ComprehendRequestHeaders(); + switch (fileContext.GetPreconditionState()) + { + case StaticFileContext.PreconditionState.Unspecified: + case StaticFileContext.PreconditionState.ShouldProcess: + if (fileContext.IsHeadMethod) + { + await fileContext.SendStatusAsync(Constants.Status200Ok); + return; + } + + try + { + if (fileContext.IsRangeRequest) + { + await fileContext.SendRangeAsync(); + return; + } + + await fileContext.SendAsync(); + _logger.FileServed(fileContext.SubPath, fileContext.PhysicalPath); + return; + } + catch (FileNotFoundException) + { + context.Response.Clear(); + } + await _next(context); + return; + case StaticFileContext.PreconditionState.NotModified: + _logger.FileNotModified(fileContext.SubPath); + await fileContext.SendStatusAsync(Constants.Status304NotModified); + return; + + case StaticFileContext.PreconditionState.PreconditionFailed: + _logger.PreconditionFailed(fileContext.SubPath); + await fileContext.SendStatusAsync(Constants.Status412PreconditionFailed); + return; + + default: + var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString()); + Debug.Fail(exception.ToString()); + throw exception; + } } } }