Remove GetService calls in Static Files
This commit is contained in:
parent
f78abb5bcf
commit
2f7e31ab5b
|
|
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
||||||
|
|
@ -21,6 +22,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
private readonly DefaultFilesOptions _options;
|
private readonly DefaultFilesOptions _options;
|
||||||
private readonly PathString _matchUrl;
|
private readonly PathString _matchUrl;
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly IFileProvider _fileProvider;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the DefaultFilesMiddleware.
|
/// Creates a new instance of the DefaultFilesMiddleware.
|
||||||
|
|
@ -47,7 +49,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
_options.ResolveFileProvider(hostingEnv);
|
_fileProvider = _options.FileProvider ?? Helpers.ResolveFileProvider(hostingEnv);
|
||||||
_matchUrl = _options.RequestPath;
|
_matchUrl = _options.RequestPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,14 +66,14 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
|
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
|
||||||
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath))
|
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath))
|
||||||
{
|
{
|
||||||
var dirContents = _options.FileProvider.GetDirectoryContents(subpath.Value);
|
var dirContents = _fileProvider.GetDirectoryContents(subpath.Value);
|
||||||
if (dirContents.Exists)
|
if (dirContents.Exists)
|
||||||
{
|
{
|
||||||
// Check if any of our default files exist.
|
// Check if any of our default files exist.
|
||||||
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
|
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
|
||||||
{
|
{
|
||||||
string defaultFile = _options.DefaultFileNames[matchIndex];
|
string defaultFile = _options.DefaultFileNames[matchIndex];
|
||||||
var file = _options.FileProvider.GetFileInfo(subpath + defaultFile);
|
var file = _fileProvider.GetFileInfo(subpath + defaultFile);
|
||||||
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
|
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
|
||||||
if (file.Exists)
|
if (file.Exists)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Options for selecting default file names.
|
/// Options for selecting default file names.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DefaultFilesOptions : SharedOptionsBase<DefaultFilesOptions>
|
public class DefaultFilesOptions : SharedOptionsBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Configuration for the DefaultFilesMiddleware.
|
/// Configuration for the DefaultFilesMiddleware.
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
|
@ -20,14 +21,17 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
private readonly DirectoryBrowserOptions _options;
|
private readonly DirectoryBrowserOptions _options;
|
||||||
private readonly PathString _matchUrl;
|
private readonly PathString _matchUrl;
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly IDirectoryFormatter _formatter;
|
||||||
|
private readonly IFileProvider _fileProvider;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the SendFileMiddleware.
|
/// Creates a new instance of the SendFileMiddleware.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the pipeline.</param>
|
/// <param name="next">The next middleware in the pipeline.</param>
|
||||||
/// <param name="hostingEnv">The <see cref="IHostingEnvironment"/> used by this middleware.</param>
|
/// <param name="hostingEnv">The <see cref="IHostingEnvironment"/> used by this middleware.</param>
|
||||||
|
/// <param name="encoder">The <see cref="HtmlEncoder"/> used by the default <see cref="HtmlDirectoryFormatter"/>.</param>
|
||||||
/// <param name="options">The configuration for this middleware.</param>
|
/// <param name="options">The configuration for this middleware.</param>
|
||||||
public DirectoryBrowserMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<DirectoryBrowserOptions> options)
|
public DirectoryBrowserMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, HtmlEncoder encoder, IOptions<DirectoryBrowserOptions> options)
|
||||||
{
|
{
|
||||||
if (next == null)
|
if (next == null)
|
||||||
{
|
{
|
||||||
|
|
@ -39,19 +43,20 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
throw new ArgumentNullException(nameof(hostingEnv));
|
throw new ArgumentNullException(nameof(hostingEnv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (encoder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(encoder));
|
||||||
|
}
|
||||||
|
|
||||||
if (options == null)
|
if (options == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(options));
|
throw new ArgumentNullException(nameof(options));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.Value.Formatter == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException(Resources.Args_NoFormatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
_options.ResolveFileProvider(hostingEnv);
|
_fileProvider = _options.FileProvider ?? Helpers.ResolveFileProvider(hostingEnv);
|
||||||
|
_formatter = options.Value.Formatter ?? new HtmlDirectoryFormatter(encoder);
|
||||||
_matchUrl = _options.RequestPath;
|
_matchUrl = _options.RequestPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +83,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
return Constants.CompletedTask;
|
return Constants.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _options.Formatter.GenerateContentAsync(context, contents);
|
return _formatter.GenerateContentAsync(context, contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _next(context);
|
return _next(context);
|
||||||
|
|
@ -86,7 +91,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents)
|
private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents)
|
||||||
{
|
{
|
||||||
contents = _options.FileProvider.GetDirectoryContents(subpath.Value);
|
contents = _fileProvider.GetDirectoryContents(subpath.Value);
|
||||||
return contents.Exists;
|
return contents.Exists;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Directory browsing options
|
/// Directory browsing options
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DirectoryBrowserOptions : SharedOptionsBase<DirectoryBrowserOptions>
|
public class DirectoryBrowserOptions : SharedOptionsBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enabled directory browsing for all request paths
|
/// Enabled directory browsing for all request paths
|
||||||
|
|
@ -26,7 +26,6 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
public DirectoryBrowserOptions(SharedOptions sharedOptions)
|
public DirectoryBrowserOptions(SharedOptions sharedOptions)
|
||||||
: base(sharedOptions)
|
: base(sharedOptions)
|
||||||
{
|
{
|
||||||
Formatter = new HtmlDirectoryFormatter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.StaticFiles
|
namespace Microsoft.AspNetCore.StaticFiles
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Options for all of the static file middleware components
|
/// Options for all of the static file middleware components
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class FileServerOptions : SharedOptionsBase<FileServerOptions>
|
public class FileServerOptions : SharedOptionsBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a combined options class for all of the static file middleware components.
|
/// Creates a combined options class for all of the static file middleware components.
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,23 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.StaticFiles
|
namespace Microsoft.AspNetCore.StaticFiles
|
||||||
{
|
{
|
||||||
internal static class Helpers
|
internal static class Helpers
|
||||||
{
|
{
|
||||||
|
internal static IFileProvider ResolveFileProvider(IHostingEnvironment hostingEnv)
|
||||||
|
{
|
||||||
|
if (hostingEnv.WebRootFileProvider == null) {
|
||||||
|
throw new InvalidOperationException("Missing FileProvider.");
|
||||||
|
}
|
||||||
|
return hostingEnv.WebRootFileProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static bool IsGetOrHeadMethod(string method)
|
internal static bool IsGetOrHeadMethod(string method)
|
||||||
{
|
{
|
||||||
return IsGetMethod(method) || IsHeadMethod(method);
|
return IsGetMethod(method) || IsHeadMethod(method);
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,15 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
private HtmlEncoder _htmlEncoder;
|
private HtmlEncoder _htmlEncoder;
|
||||||
|
|
||||||
|
public HtmlDirectoryFormatter(HtmlEncoder encoder)
|
||||||
|
{
|
||||||
|
if (encoder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(encoder));
|
||||||
|
}
|
||||||
|
_htmlEncoder = encoder;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates an HTML view for a directory.
|
/// Generates an HTML view for a directory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -37,11 +46,6 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
throw new ArgumentNullException(nameof(contents));
|
throw new ArgumentNullException(nameof(contents));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_htmlEncoder == null)
|
|
||||||
{
|
|
||||||
_htmlEncoder = context.RequestServices.GetRequiredService<HtmlEncoder>();
|
|
||||||
}
|
|
||||||
|
|
||||||
context.Response.ContentType = TextHtmlUtf8;
|
context.Response.ContentType = TextHtmlUtf8;
|
||||||
|
|
||||||
if (Helpers.IsHeadMethod(context.Request.Method))
|
if (Helpers.IsHeadMethod(context.Request.Method))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
|
|
@ -11,8 +10,7 @@ namespace Microsoft.AspNetCore.StaticFiles.Infrastructure
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Options common to several middleware components
|
/// Options common to several middleware components
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the subclass</typeparam>
|
public abstract class SharedOptionsBase
|
||||||
public abstract class SharedOptionsBase<T>
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an new instance of the SharedOptionsBase.
|
/// Creates an new instance of the SharedOptionsBase.
|
||||||
|
|
@ -50,17 +48,5 @@ namespace Microsoft.AspNetCore.StaticFiles.Infrastructure
|
||||||
get { return SharedOptions.FileProvider; }
|
get { return SharedOptions.FileProvider; }
|
||||||
set { SharedOptions.FileProvider = value; }
|
set { SharedOptions.FileProvider = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ResolveFileProvider(IHostingEnvironment hostingEnv)
|
|
||||||
{
|
|
||||||
if (FileProvider == null)
|
|
||||||
{
|
|
||||||
FileProvider = hostingEnv.WebRootFileProvider;
|
|
||||||
if (FileProvider == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Missing FileProvider.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,22 +10,6 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
private static readonly ResourceManager _resourceManager
|
private static readonly ResourceManager _resourceManager
|
||||||
= new ResourceManager("Microsoft.AspNetCore.StaticFiles.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
= new ResourceManager("Microsoft.AspNetCore.StaticFiles.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// No IContentTypeProvider was specified.
|
|
||||||
/// </summary>
|
|
||||||
internal static string Args_NoContentTypeProvider
|
|
||||||
{
|
|
||||||
get { return GetString("Args_NoContentTypeProvider"); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// No IContentTypeProvider was specified.
|
|
||||||
/// </summary>
|
|
||||||
internal static string FormatArgs_NoContentTypeProvider()
|
|
||||||
{
|
|
||||||
return GetString("Args_NoContentTypeProvider");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// No formatter provided.
|
/// No formatter provided.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -117,9 +117,6 @@
|
||||||
<resheader name="writer">
|
<resheader name="writer">
|
||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</resheader>
|
||||||
<data name="Args_NoContentTypeProvider" xml:space="preserve">
|
|
||||||
<value>No IContentTypeProvider was specified.</value>
|
|
||||||
</data>
|
|
||||||
<data name="Args_NoFormatter" xml:space="preserve">
|
<data name="Args_NoFormatter" xml:space="preserve">
|
||||||
<value>No formatter provided.</value>
|
<value>No formatter provided.</value>
|
||||||
</data>
|
</data>
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
private readonly HttpRequest _request;
|
private readonly HttpRequest _request;
|
||||||
private readonly HttpResponse _response;
|
private readonly HttpResponse _response;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
private readonly IFileProvider _fileProvider;
|
||||||
|
private readonly IContentTypeProvider _contentTypeProvider;
|
||||||
private string _method;
|
private string _method;
|
||||||
private bool _isGet;
|
private bool _isGet;
|
||||||
private bool _isHead;
|
private bool _isHead;
|
||||||
|
|
@ -47,7 +49,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
private IList<RangeItemHeaderValue> _ranges;
|
private IList<RangeItemHeaderValue> _ranges;
|
||||||
|
|
||||||
public StaticFileContext(HttpContext context, StaticFileOptions options, PathString matchUrl, ILogger logger)
|
public StaticFileContext(HttpContext context, StaticFileOptions options, PathString matchUrl, ILogger logger, IFileProvider fileProvider, IContentTypeProvider contentTypeProvider)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_options = options;
|
_options = options;
|
||||||
|
|
@ -57,6 +59,8 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_requestHeaders = _request.GetTypedHeaders();
|
_requestHeaders = _request.GetTypedHeaders();
|
||||||
_responseHeaders = _response.GetTypedHeaders();
|
_responseHeaders = _response.GetTypedHeaders();
|
||||||
|
_fileProvider = fileProvider;
|
||||||
|
_contentTypeProvider = contentTypeProvider;
|
||||||
|
|
||||||
_method = null;
|
_method = null;
|
||||||
_isGet = false;
|
_isGet = false;
|
||||||
|
|
@ -118,7 +122,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
public bool LookupContentType()
|
public bool LookupContentType()
|
||||||
{
|
{
|
||||||
if (_options.ContentTypeProvider.TryGetContentType(_subPath.Value, out _contentType))
|
if (_contentTypeProvider.TryGetContentType(_subPath.Value, out _contentType))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -134,7 +138,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
public bool LookupFileInfo()
|
public bool LookupFileInfo()
|
||||||
{
|
{
|
||||||
_fileInfo = _options.FileProvider.GetFileInfo(_subPath.Value);
|
_fileInfo = _fileProvider.GetFileInfo(_subPath.Value);
|
||||||
if (_fileInfo.Exists)
|
if (_fileInfo.Exists)
|
||||||
{
|
{
|
||||||
_length = _fileInfo.Length;
|
_length = _fileInfo.Length;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
|
@ -21,6 +22,8 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
private readonly PathString _matchUrl;
|
private readonly PathString _matchUrl;
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
private readonly IFileProvider _fileProvider;
|
||||||
|
private readonly IContentTypeProvider _contentTypeProvider;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the StaticFileMiddleware.
|
/// Creates a new instance of the StaticFileMiddleware.
|
||||||
|
|
@ -51,14 +54,10 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
throw new ArgumentNullException(nameof(loggerFactory));
|
throw new ArgumentNullException(nameof(loggerFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.Value.ContentTypeProvider == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException(Resources.Args_NoContentTypeProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
_options.ResolveFileProvider(hostingEnv);
|
_contentTypeProvider = options.Value.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
|
||||||
|
_fileProvider = _options.FileProvider ?? Helpers.ResolveFileProvider(hostingEnv);
|
||||||
_matchUrl = _options.RequestPath;
|
_matchUrl = _options.RequestPath;
|
||||||
_logger = loggerFactory.CreateLogger<StaticFileMiddleware>();
|
_logger = loggerFactory.CreateLogger<StaticFileMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +69,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public Task Invoke(HttpContext context)
|
public Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger);
|
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);
|
||||||
|
|
||||||
if (!fileContext.ValidateMethod())
|
if (!fileContext.ValidateMethod())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Options for serving static files
|
/// Options for serving static files
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class StaticFileOptions : SharedOptionsBase<StaticFileOptions>
|
public class StaticFileOptions : SharedOptionsBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defaults to all request paths
|
/// Defaults to all request paths
|
||||||
|
|
@ -25,8 +25,6 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
/// <param name="sharedOptions"></param>
|
/// <param name="sharedOptions"></param>
|
||||||
public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions)
|
public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions)
|
||||||
{
|
{
|
||||||
ContentTypeProvider = new FileExtensionContentTypeProvider();
|
|
||||||
|
|
||||||
OnPrepareResponse = _ => { };
|
OnPrepareResponse = _ => { };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,10 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task NullArguments()
|
public async Task NullArguments()
|
||||||
{
|
{
|
||||||
Assert.Throws<ArgumentException>(() => StaticFilesTestServer.Create(
|
// No exception, default provided
|
||||||
|
StaticFilesTestServer.Create(
|
||||||
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions { Formatter = null }),
|
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions { Formatter = null }),
|
||||||
services => services.AddDirectoryBrowser()));
|
services => services.AddDirectoryBrowser());
|
||||||
|
|
||||||
// No exception, default provided
|
// No exception, default provided
|
||||||
StaticFilesTestServer.Create(
|
StaticFilesTestServer.Create(
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var options = new StaticFileOptions();
|
var options = new StaticFileOptions();
|
||||||
options.FileProvider = new TestFileProvider();
|
var context = new StaticFileContext(new DefaultHttpContext(), options, PathString.Empty, NullLogger.Instance, new TestFileProvider(), new FileExtensionContentTypeProvider());
|
||||||
var context = new StaticFileContext(new DefaultHttpContext(), options, PathString.Empty, NullLogger.Instance);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var validateResult = context.ValidatePath();
|
var validateResult = context.ValidatePath();
|
||||||
|
|
@ -43,11 +42,10 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
{
|
{
|
||||||
LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
|
LastModified = new DateTimeOffset(2014, 1, 2, 3, 4, 5, TimeSpan.Zero)
|
||||||
});
|
});
|
||||||
options.FileProvider = fileProvider;
|
|
||||||
var pathString = new PathString("/test");
|
var pathString = new PathString("/test");
|
||||||
var httpContext = new DefaultHttpContext();
|
var httpContext = new DefaultHttpContext();
|
||||||
httpContext.Request.Path = new PathString("/test/foo.txt");
|
httpContext.Request.Path = new PathString("/test/foo.txt");
|
||||||
var context = new StaticFileContext(httpContext, options, pathString, NullLogger.Instance);
|
var context = new StaticFileContext(httpContext, options, pathString, NullLogger.Instance, fileProvider, new FileExtensionContentTypeProvider());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
context.ValidatePath();
|
context.ValidatePath();
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,8 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task NullArguments()
|
public async Task NullArguments()
|
||||||
{
|
{
|
||||||
Assert.Throws<ArgumentException>(() => StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = null })));
|
// No exception, default provided
|
||||||
|
StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = null }));
|
||||||
|
|
||||||
// No exception, default provided
|
// No exception, default provided
|
||||||
StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { FileProvider = null }));
|
StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { FileProvider = null }));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue