Updating to new options pattern

This commit is contained in:
John Luo 2016-01-05 20:47:19 -08:00
parent a9f4969cfc
commit 5e9bbc5650
17 changed files with 101 additions and 178 deletions

View File

@ -17,9 +17,9 @@ namespace StaticFilesSample
// Displays all log levels
factory.AddConsole(LogLevel.Debug);
app.UseFileServer(options =>
app.UseFileServer(new FileServerOptions
{
options.EnableDirectoryBrowsing = true;
EnableDirectoryBrowsing = true
});
}

View File

@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder
{
@ -24,7 +25,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseDefaultFiles(options => { });
return app.UseMiddleware<DefaultFilesMiddleware>();
}
/// <summary>
@ -40,30 +41,10 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseDefaultFiles(options => { options.RequestPath = new PathString(requestPath); });
}
/// <summary>
/// Enables default file mapping with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder app, Action<DefaultFilesOptions> configureOptions)
{
if (app == null)
return app.UseDefaultFiles(new DefaultFilesOptions
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new DefaultFilesOptions();
configureOptions(options);
return app.UseMiddleware<DefaultFilesMiddleware>(options);
RequestPath = new PathString(requestPath)
});
}
/// <summary>
@ -83,7 +64,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<DefaultFilesMiddleware>(options);
return app.UseMiddleware<DefaultFilesMiddleware>(Options.Create(options));
}
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.StaticFiles
@ -26,7 +27,7 @@ namespace Microsoft.AspNet.StaticFiles
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration options for this middleware.</param>
public DefaultFilesMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, DefaultFilesOptions options)
public DefaultFilesMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<DefaultFilesOptions> options)
{
if (next == null)
{
@ -42,12 +43,11 @@ namespace Microsoft.AspNet.StaticFiles
{
throw new ArgumentNullException(nameof(options));
}
options.ResolveFileProvider(hostingEnv);
_next = next;
_options = options;
_matchUrl = options.RequestPath;
_options = options.Value;
_options.ResolveFileProvider(hostingEnv);
_matchUrl = _options.RequestPath;
}
/// <summary>

View File

@ -4,7 +4,7 @@
using System.Collections.Generic;
using Microsoft.AspNet.StaticFiles.Infrastructure;
namespace Microsoft.AspNet.StaticFiles
namespace Microsoft.AspNet.Builder
{
/// <summary>
/// Options for selecting default file names.

View File

@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder
{
@ -24,7 +25,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseDirectoryBrowser(options => { });
return app.UseMiddleware<DirectoryBrowserMiddleware>();
}
/// <summary>
@ -40,30 +41,10 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseDirectoryBrowser(options => { options.RequestPath = new PathString(requestPath); });
}
/// <summary>
/// Enable directory browsing with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser(this IApplicationBuilder app, Action<DirectoryBrowserOptions> configureOptions)
{
if (app == null)
return app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new DirectoryBrowserOptions();
configureOptions(options);
return app.UseMiddleware<DirectoryBrowserMiddleware>(options);
RequestPath = new PathString(requestPath)
});
}
/// <summary>
@ -83,7 +64,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<DirectoryBrowserMiddleware>(options);
return app.UseMiddleware<DirectoryBrowserMiddleware>(Options.Create(options));
}
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.StaticFiles
@ -25,7 +26,7 @@ namespace Microsoft.AspNet.StaticFiles
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration for this middleware.</param>
public DirectoryBrowserMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, DirectoryBrowserOptions options)
public DirectoryBrowserMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<DirectoryBrowserOptions> options)
{
if (next == null)
{
@ -42,15 +43,15 @@ namespace Microsoft.AspNet.StaticFiles
throw new ArgumentNullException(nameof(options));
}
if (options.Formatter == null)
if (options.Value.Formatter == null)
{
throw new ArgumentException(Resources.Args_NoFormatter);
}
options.ResolveFileProvider(hostingEnv);
_next = next;
_options = options;
_matchUrl = options.RequestPath;
_options = options.Value;
_options.ResolveFileProvider(hostingEnv);
_matchUrl = _options.RequestPath;
}
/// <summary>

View File

@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.StaticFiles;
using Microsoft.AspNet.StaticFiles.Infrastructure;
namespace Microsoft.AspNet.StaticFiles
namespace Microsoft.AspNet.Builder
{
/// <summary>
/// Directory browsing options

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseFileServer(options => { });
return app.UseFileServer(new FileServerOptions());
}
/// <summary>
@ -41,7 +41,10 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseFileServer(options => { options.EnableDirectoryBrowsing = enableDirectoryBrowsing; });
return app.UseFileServer(new FileServerOptions
{
EnableDirectoryBrowsing = enableDirectoryBrowsing
});
}
/// <summary>
@ -62,40 +65,10 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(requestPath));
}
return app.UseFileServer(options => { options.RequestPath = new PathString(requestPath); });
}
/// <summary>
/// Enable all static file middleware with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer(this IApplicationBuilder app, Action<FileServerOptions> configureOptions)
{
if (app == null)
return app.UseFileServer(new FileServerOptions
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var fileServerOptions = new FileServerOptions();
configureOptions(fileServerOptions);
if (fileServerOptions.EnableDefaultFiles)
{
app = app.UseDefaultFiles(options => { options = fileServerOptions.DefaultFilesOptions; });
}
if (fileServerOptions.EnableDirectoryBrowsing)
{
app = app.UseDirectoryBrowser(options => { options = fileServerOptions.DirectoryBrowserOptions; });
}
return app.UseStaticFiles(options => { options = fileServerOptions.StaticFileOptions; });
RequestPath = new PathString(requestPath)
});
}
/// <summary>
@ -117,12 +90,12 @@ namespace Microsoft.AspNet.Builder
if (options.EnableDefaultFiles)
{
app = app.UseDefaultFiles(options.DefaultFilesOptions);
app.UseDefaultFiles(options.DefaultFilesOptions);
}
if (options.EnableDirectoryBrowsing)
{
app = app.UseDirectoryBrowser(options.DirectoryBrowserOptions);
app.UseDirectoryBrowser(options.DirectoryBrowserOptions);
}
return app.UseStaticFiles(options.StaticFileOptions);

View File

@ -3,7 +3,7 @@
using Microsoft.AspNet.StaticFiles.Infrastructure;
namespace Microsoft.AspNet.StaticFiles
namespace Microsoft.AspNet.Builder
{
/// <summary>
/// Options for all of the static file middleware components

View File

@ -7,6 +7,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;

View File

@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder
{
@ -24,7 +25,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseStaticFiles(options => { });
return app.UseMiddleware<StaticFileMiddleware>();
}
/// <summary>
@ -40,30 +41,10 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app));
}
return app.UseStaticFiles(options => { options.RequestPath = new PathString(requestPath); });
}
/// <summary>
/// Enables static file serving with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder app, Action<StaticFileOptions> configureOptions)
{
if (app == null)
return app.UseStaticFiles(new StaticFileOptions
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new StaticFileOptions();
configureOptions(options);
return app.UseMiddleware<StaticFileMiddleware>(options);
RequestPath = new PathString(requestPath)
});
}
/// <summary>
@ -83,7 +64,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(options));
}
return app.UseMiddleware<StaticFileMiddleware>(options);
return app.UseMiddleware<StaticFileMiddleware>(Options.Create(options));
}
}
}

View File

@ -4,9 +4,11 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.StaticFiles
{
@ -26,7 +28,7 @@ namespace Microsoft.AspNet.StaticFiles
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The configuration options.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
public StaticFileMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, StaticFileOptions options, ILoggerFactory loggerFactory)
public StaticFileMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions<StaticFileOptions> options, ILoggerFactory loggerFactory)
{
if (next == null)
{
@ -48,15 +50,15 @@ namespace Microsoft.AspNet.StaticFiles
throw new ArgumentNullException(nameof(loggerFactory));
}
if (options.ContentTypeProvider == null)
if (options.Value.ContentTypeProvider == null)
{
throw new ArgumentException(Resources.Args_NoContentTypeProvider);
}
options.ResolveFileProvider(hostingEnv);
_next = next;
_options = options;
_matchUrl = options.RequestPath;
_options = options.Value;
_options.ResolveFileProvider(hostingEnv);
_matchUrl = _options.RequestPath;
_logger = loggerFactory.CreateLogger<StaticFileMiddleware>();
}

View File

@ -2,9 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.StaticFiles;
using Microsoft.AspNet.StaticFiles.Infrastructure;
namespace Microsoft.AspNet.StaticFiles
namespace Microsoft.AspNet.Builder
{
/// <summary>
/// Options for serving static files

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.StaticFiles
public async Task NullArguments()
{
// No exception, default provided
StaticFilesTestServer.Create(app => app.UseDefaultFiles(options => { options.FileProvider = null; }));
StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = null }));
// PathString(null) is OK.
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles((string)null));
@ -56,10 +56,10 @@ namespace Microsoft.AspNet.StaticFiles
{
var server = StaticFilesTestServer.Create(app =>
{
app.UseDefaultFiles(options =>
app.UseDefaultFiles(new DefaultFilesOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
});
app.Run(context => context.Response.WriteAsync(context.Request.Path.Value));
});
@ -95,10 +95,10 @@ namespace Microsoft.AspNet.StaticFiles
{
var server = StaticFilesTestServer.Create(app =>
{
app.UseDefaultFiles(options =>
app.UseDefaultFiles(new DefaultFilesOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
});
app.Run(context => context.Response.WriteAsync(context.Request.Path.Value));
});
@ -132,10 +132,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}));
var response = await server.CreateRequest(requestUrl + queryString).GetAsync();
@ -170,10 +170,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvder = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseDefaultFiles(new DefaultFilesOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvder;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvder
}));
var response = await server.CreateRequest(requestUrl).GetAsync();

View File

@ -23,12 +23,12 @@ namespace Microsoft.AspNet.StaticFiles
public async Task NullArguments()
{
Assert.Throws<ArgumentException>(() => StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options => { options.Formatter = null; }),
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions { Formatter = null }),
services => services.AddDirectoryBrowser()));
// No exception, default provided
StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options => { options.FileProvider = null; }),
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = null }),
services => services.AddDirectoryBrowser());
// PathString(null) is OK.
@ -66,10 +66,10 @@ namespace Microsoft.AspNet.StaticFiles
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options =>
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}),
services => services.AddDirectoryBrowser());
var response = await server.CreateRequest(requestUrl).GetAsync();
@ -103,10 +103,10 @@ namespace Microsoft.AspNet.StaticFiles
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options =>
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}),
services => services.AddDirectoryBrowser());
var response = await server.CreateRequest(requestUrl).GetAsync();
@ -145,10 +145,10 @@ namespace Microsoft.AspNet.StaticFiles
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options =>
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}),
services => services.AddDirectoryBrowser());
@ -184,10 +184,10 @@ namespace Microsoft.AspNet.StaticFiles
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options =>
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}),
services => services.AddDirectoryBrowser());
@ -220,10 +220,10 @@ namespace Microsoft.AspNet.StaticFiles
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(
app => app.UseDirectoryBrowser(options =>
app => app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}),
services => services.AddDirectoryBrowser());

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;

View File

@ -32,10 +32,10 @@ namespace Microsoft.AspNet.StaticFiles
[Fact]
public async Task NullArguments()
{
Assert.Throws<ArgumentException>(() => StaticFilesTestServer.Create(app => app.UseStaticFiles(options => { options.ContentTypeProvider = null; })));
Assert.Throws<ArgumentException>(() => StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = null })));
// No exception, default provided
StaticFilesTestServer.Create(app => app.UseStaticFiles(options => { options.FileProvider = null; }));
StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions { FileProvider = null }));
// PathString(null) is OK.
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles((string)null));
@ -52,10 +52,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}));
var response = await server.CreateRequest(requestUrl).GetAsync();
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
@ -89,10 +89,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}));
var response = await server.CreateRequest(requestUrl).GetAsync();
@ -113,10 +113,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}));
var response = await server.CreateRequest(requestUrl).PostAsync();
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
@ -133,10 +133,10 @@ namespace Microsoft.AspNet.StaticFiles
{
using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir)))
{
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(options =>
var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(new StaticFileOptions
{
options.RequestPath = new PathString(baseUrl);
options.FileProvider = fileProvider;
RequestPath = new PathString(baseUrl),
FileProvider = fileProvider
}));
var response = await server.CreateRequest(requestUrl).SendAsync("HEAD");