Change the IHostingEnvironment parameter to IFileProvider for top-level IIS and Apache extension methods

Also fixup some xmldocs
This commit is contained in:
Nate McMaster 2016-09-06 11:07:41 -07:00
parent d2913edf61
commit d2fb2cd776
No known key found for this signature in database
GPG Key ID: BD729980AA6A21BD
9 changed files with 45 additions and 52 deletions

View File

@ -11,14 +11,14 @@ namespace RewriteSample
{ {
public class Startup public class Startup
{ {
public void Configure(IApplicationBuilder app, IHostingEnvironment hostingEnvironment) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
var options = new RewriteOptions() var options = new RewriteOptions()
.AddRedirect("(.*)/$", "$1") .AddRedirect("(.*)/$", "$1")
.AddRewrite(@"app/(\d+)", "app?id=$1") .AddRewrite(@"app/(\d+)", "app?id=$1")
.AddRedirectToHttps(302) .AddRedirectToHttps(302)
.AddIISUrlRewrite(hostingEnvironment, "UrlRewrite.xml") .AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml")
.AddApacheModRewrite(hostingEnvironment, "Rewrite.txt"); .AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt");
app.UseRewriter(options); app.UseRewriter(options);

View File

@ -3,50 +3,45 @@
using System; using System;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite; using Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
{ {
/// <summary> /// <summary>
/// Apache mod_rewrite extensions on top of the <see cref="RewriteOptions"/> /// Extensions for adding Apache mod_rewrite rules to <see cref="RewriteOptions"/>
/// </summary> /// </summary>
public static class ApacheModRewriteOptionsExtensions public static class ApacheModRewriteOptionsExtensions
{ {
/// <summary> /// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules. /// Add rules from an Apache mod_rewrite file
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="hostingEnvironment">The Hosting Environment</param> /// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
/// <param name="filePath">The path to the file containing mod_rewrite rules.</param> /// <param name="filePath">The path to the file containing mod_rewrite rules.</param>
public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, IHostingEnvironment hostingEnvironment, string filePath) public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, IFileProvider fileProvider, string filePath)
{ {
if (options == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
if (hostingEnvironment == null) if (fileProvider == null)
{ {
throw new ArgumentNullException(nameof(hostingEnvironment)); throw new ArgumentNullException(nameof(fileProvider));
} }
if (string.IsNullOrEmpty(filePath)) var fileInfo = fileProvider.GetFileInfo(filePath);
{ using (var stream = fileInfo.CreateReadStream())
throw new ArgumentException(nameof(filePath));
}
var path = Path.Combine(hostingEnvironment.ContentRootPath, filePath);
using (var stream = File.OpenRead(path))
{ {
return options.AddApacheModRewrite(new StreamReader(stream)); return options.AddApacheModRewrite(new StreamReader(stream));
} }
} }
/// <summary> /// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules. /// Add rules from an Apache mod_rewrite file
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="reader">A stream of mod_rewrite rules.</param> /// <param name="reader">A stream of mod_rewrite rules.</param>
public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, TextReader reader) public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, TextReader reader)
{ {

View File

@ -3,50 +3,46 @@
using System; using System;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite; using Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
{ {
/// <summary> /// <summary>
/// IIS Url rewrite extensions on top of the <see cref="RewriteOptions"/> /// Extensions for adding IIS Url Rewrite rules to <see cref="RewriteOptions"/>
/// </summary> /// </summary>
public static class IISUrlRewriteOptionsExtensions public static class IISUrlRewriteOptionsExtensions
{ {
/// <summary> /// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules. /// Add rules from a IIS config file containing Url Rewrite rules
/// </summary> /// </summary>
/// <param name="options">The UrlRewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="hostingEnvironment"></param> /// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
/// <param name="filePath">The path to the file containing urlrewrite rules.</param> /// <param name="filePath">The path to the file containing UrlRewrite rules.</param>
public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, IHostingEnvironment hostingEnvironment, string filePath) public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, IFileProvider fileProvider, string filePath)
{ {
if (options == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
if (hostingEnvironment == null) if (fileProvider == null)
{ {
throw new ArgumentNullException(nameof(hostingEnvironment)); throw new ArgumentNullException(nameof(fileProvider));
} }
if (string.IsNullOrEmpty(filePath)) var file = fileProvider.GetFileInfo(filePath);
{
throw new ArgumentException(nameof(filePath));
}
var path = Path.Combine(hostingEnvironment.ContentRootPath, filePath); using (var stream = file.CreateReadStream())
using (var stream = File.OpenRead(path))
{ {
return AddIISUrlRewrite(options, new StreamReader(stream)); return AddIISUrlRewrite(options, new StreamReader(stream));
} }
} }
/// <summary> /// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules. /// Add rules from a IIS config file containing Url Rewrite rules
/// </summary> /// </summary>
/// <param name="options">The UrlRewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="reader">The text reader stream.</param> /// <param name="reader">The text reader stream.</param>
public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, TextReader reader) public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, TextReader reader)
{ {
@ -66,6 +62,7 @@ namespace Microsoft.AspNetCore.Rewrite
{ {
options.Rules.Add(rule); options.Rules.Add(rule);
} }
return options; return options;
} }
} }

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Builder
/// <summary> /// <summary>
/// Checks if a given Url matches rules and conditions, and modifies the HttpContext on match. /// Checks if a given Url matches rules and conditions, and modifies the HttpContext on match.
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app">The <see cref="IApplicationBuilder"/></param>
/// <param name="options">Options for rewrite.</param> /// <param name="options">Options for rewrite.</param>
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseRewriter(this IApplicationBuilder app, RewriteOptions options) public static IApplicationBuilder UseRewriter(this IApplicationBuilder app, RewriteOptions options)

View File

@ -14,7 +14,7 @@ using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
{ {
/// <summary> /// <summary>
/// Represents a middleware that rewrites urls imported from mod_rewrite, UrlRewrite, and code. /// Represents a middleware that rewrites urls
/// </summary> /// </summary>
public class RewriteMiddleware public class RewriteMiddleware
{ {

View File

@ -3,6 +3,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Hosting;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
{ {
@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Rewrite
public IList<Rule> Rules { get; } = new List<Rule>(); public IList<Rule> Rules { get; } = new List<Rule>();
/// <summary> /// <summary>
/// Gets and sets the File Provider for file and directory checks. /// Gets and sets the File Provider for file and directory checks. Defaults to <see cref="IHostingEnvironment.WebRootFileProvider"/>
/// </summary> /// </summary>
public IFileProvider StaticFileProvider { get; set; } public IFileProvider StaticFileProvider { get; set; }
} }

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Adds a rule to the current rules. /// Adds a rule to the current rules.
/// </summary> /// </summary>
/// <param name="options">The UrlRewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="rule">A rule to be added to the current rules.</param> /// <param name="rule">A rule to be added to the current rules.</param>
/// <returns>The Rewrite options.</returns> /// <returns>The Rewrite options.</returns>
public static RewriteOptions Add(this RewriteOptions options, Rule rule) public static RewriteOptions Add(this RewriteOptions options, Rule rule)
@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Adds a rule to the current rules. /// Adds a rule to the current rules.
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="applyRule">A Func that checks and applies the rule.</param> /// <param name="applyRule">A Func that checks and applies the rule.</param>
/// <returns></returns> /// <returns></returns>
public static RewriteOptions Add(this RewriteOptions options, Action<RewriteContext> applyRule) public static RewriteOptions Add(this RewriteOptions options, Action<RewriteContext> applyRule)
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Rewrites the path if the regex matches the HttpContext's PathString /// Rewrites the path if the regex matches the HttpContext's PathString
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param> /// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace HttpContext with.</param> /// <param name="replacement">If the regex matches, what to replace HttpContext with.</param>
/// <returns>The Rewrite options.</returns> /// <returns>The Rewrite options.</returns>
@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Rewrites the path if the regex matches the HttpContext's PathString /// Rewrites the path if the regex matches the HttpContext's PathString
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param> /// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param> /// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <param name="stopProcessing">If the regex matches, conditionally stop processing other rules.</param> /// <param name="stopProcessing">If the regex matches, conditionally stop processing other rules.</param>
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Redirect the request if the regex matches the HttpContext's PathString /// Redirect the request if the regex matches the HttpContext's PathString
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param> /// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param> /// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <returns>The Rewrite options.</returns> /// <returns>The Rewrite options.</returns>
@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Redirect the request if the regex matches the HttpContext's PathString /// Redirect the request if the regex matches the HttpContext's PathString
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param> /// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param> /// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <param name="statusCode">The status code to add to the response.</param> /// <param name="statusCode">The status code to add to the response.</param>
@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// Redirect a request to https if the incoming request is http, with returning a 301 /// Redirect a request to https if the incoming request is http, with returning a 301
/// status code for permanently redirected. /// status code for permanently redirected.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <returns></returns> /// <returns></returns>
public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options) public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options)
{ {
@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Redirect a request to https if the incoming request is http /// Redirect a request to https if the incoming request is http
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options) public static RewriteOptions AddRedirectToHttps(this RewriteOptions options)
{ {
return AddRedirectToHttps(options, statusCode: 302, sslPort: null); return AddRedirectToHttps(options, statusCode: 302, sslPort: null);
@ -110,7 +110,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Redirect a request to https if the incoming request is http /// Redirect a request to https if the incoming request is http
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="statusCode">The status code to add to the response.</param> /// <param name="statusCode">The status code to add to the response.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode) public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode)
{ {
@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary> /// <summary>
/// Redirect a request to https if the incoming request is http /// Redirect a request to https if the incoming request is http
/// </summary> /// </summary>
/// <param name="options">The Rewrite options.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="statusCode">The status code to add to the response.</param> /// <param name="statusCode">The status code to add to the response.</param>
/// <param name="sslPort">The SSL port to add to the response.</param> /// <param name="sslPort">The SSL port to add to the response.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode, int? sslPort) public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode, int? sslPort)

View File

@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.Rewrite
{ {
/// <summary> /// <summary>
/// Applies the rule. /// Applies the rule.
/// Implementations of ApplyRule should set the value for RewriteContext.Results /// Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result"/>
/// (defaults to RuleTermination.Continue) /// (defaults to <see cref="RuleTermination.Continue"/> )
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
public abstract void ApplyRule(RewriteContext context); public abstract void ApplyRule(RewriteContext context);