Updating to new options pattern

This commit is contained in:
John Luo 2016-01-07 13:45:40 -08:00
parent 92fff3ccf3
commit d1a0edb87e
10 changed files with 60 additions and 37 deletions

View File

@ -11,9 +11,9 @@ namespace HttpOverridesSample
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
app.UseIISPlatformHandler(); app.UseIISPlatformHandler();
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.All; ForwardedOptions = ForwardedHeaders.All
}); });
app.UseHttpMethodOverride(); app.UseHttpMethodOverride();

View File

@ -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.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.HttpOverrides; using Microsoft.AspNet.HttpOverrides;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -19,7 +20,8 @@ namespace Microsoft.AspNet.Builder
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
return builder.Use(next => new HttpMethodOverrideMiddleware(next, new HttpMethodOverrideOptions()).Invoke);
return builder.UseMiddleware<HttpMethodOverrideMiddleware>();
} }
/// <summary> /// <summary>
@ -28,19 +30,18 @@ namespace Microsoft.AspNet.Builder
/// <param name="builder"></param> /// <param name="builder"></param>
/// <param name="formFieldInput">Denotes the element that contains the name of the resulting method type.</param> /// <param name="formFieldInput">Denotes the element that contains the name of the resulting method type.</param>
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder builder, Action<HttpMethodOverrideOptions> configureOptions) public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder builder, HttpMethodOverrideOptions options)
{ {
if (builder == null) if (builder == null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
if (configureOptions == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(configureOptions)); throw new ArgumentNullException(nameof(options));
} }
var options = new HttpMethodOverrideOptions();
configureOptions(options); return builder.UseMiddleware<HttpMethodOverrideMiddleware>(Options.Create(options));
return builder.Use(next => new HttpMethodOverrideMiddleware(next, options).Invoke);
} }
} }
} }

View File

@ -3,7 +3,9 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.HttpOverrides namespace Microsoft.AspNet.HttpOverrides
{ {
@ -13,7 +15,7 @@ namespace Microsoft.AspNet.HttpOverrides
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly HttpMethodOverrideOptions _options; private readonly HttpMethodOverrideOptions _options;
public HttpMethodOverrideMiddleware(RequestDelegate next, HttpMethodOverrideOptions options) public HttpMethodOverrideMiddleware(RequestDelegate next, IOptions<HttpMethodOverrideOptions> options)
{ {
if (next == null) if (next == null)
{ {
@ -24,7 +26,7 @@ namespace Microsoft.AspNet.HttpOverrides
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
_next = next; _next = next;
_options = options; _options = options.Value;
} }
public async Task Invoke(HttpContext context) public async Task Invoke(HttpContext context)

View File

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.HttpOverrides namespace Microsoft.AspNet.Builder
{ {
public class HttpMethodOverrideOptions public class HttpMethodOverrideOptions
{ {

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.HttpOverrides; using Microsoft.AspNet.HttpOverrides;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -14,19 +15,34 @@ namespace Microsoft.AspNet.Builder
/// <param name="builder"></param> /// <param name="builder"></param>
/// <param name="options">Enables the different override options.</param> /// <param name="options">Enables the different override options.</param>
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseOverrideHeaders(this IApplicationBuilder builder, Action<OverrideHeaderOptions> configureOptions) public static IApplicationBuilder UseOverrideHeaders(this IApplicationBuilder builder)
{ {
if (builder == null) if (builder == null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
if (configureOptions == null)
return builder.UseMiddleware<OverrideHeaderMiddleware>();
}
/// <summary>
/// Forwards proxied headers onto current request
/// </summary>
/// <param name="builder"></param>
/// <param name="options">Enables the different override options.</param>
/// <returns></returns>
public static IApplicationBuilder UseOverrideHeaders(this IApplicationBuilder builder, OverrideHeaderOptions options)
{
if (builder == null)
{ {
throw new ArgumentNullException(nameof(configureOptions)); throw new ArgumentNullException(nameof(builder));
} }
var options = new OverrideHeaderOptions(); if (options == null)
configureOptions(options); {
return builder.Use(next => new OverrideHeaderMiddleware(next, options).Invoke); throw new ArgumentNullException(nameof(options));
}
return builder.UseMiddleware<OverrideHeaderMiddleware>(Options.Create(options));
} }
} }
} }

View File

@ -4,7 +4,9 @@
using System; using System;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.HttpOverrides namespace Microsoft.AspNet.HttpOverrides
{ {
@ -19,7 +21,7 @@ namespace Microsoft.AspNet.HttpOverrides
private readonly OverrideHeaderOptions _options; private readonly OverrideHeaderOptions _options;
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
public OverrideHeaderMiddleware(RequestDelegate next, OverrideHeaderOptions options) public OverrideHeaderMiddleware(RequestDelegate next, IOptions<OverrideHeaderOptions> options)
{ {
if (next == null) if (next == null)
{ {
@ -30,7 +32,7 @@ namespace Microsoft.AspNet.HttpOverrides
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
_options = options; _options = options.Value;
_next = next; _next = next;
} }

View File

@ -1,7 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.HttpOverrides using Microsoft.AspNet.HttpOverrides;
namespace Microsoft.AspNet.Builder
{ {
public class OverrideHeaderOptions public class OverrideHeaderOptions
{ {

View File

@ -10,7 +10,8 @@
"url": "git://github.com/aspnet/basicmiddleware" "url": "git://github.com/aspnet/basicmiddleware"
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Http.Extensions": "1.0.0-*" "Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.Extensions.Options": "1.0.0-*"
}, },
"frameworks": { "frameworks": {
"net451": { }, "net451": { },

View File

@ -20,9 +20,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.XForwardedFor; ForwardedOptions = ForwardedHeaders.XForwardedFor
}); });
app.Run(context => app.Run(context =>
{ {
@ -47,9 +47,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.XForwardedFor; ForwardedOptions = ForwardedHeaders.XForwardedFor
}); });
app.Run(context => app.Run(context =>
{ {
@ -74,9 +74,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.XForwardedHost; ForwardedOptions = ForwardedHeaders.XForwardedHost
}); });
app.Run(context => app.Run(context =>
{ {
@ -101,9 +101,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.XForwardedProto; ForwardedOptions = ForwardedHeaders.XForwardedProto
}); });
app.Run(context => app.Run(context =>
{ {
@ -135,9 +135,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.All; ForwardedOptions = ForwardedHeaders.All
}); });
app.Run(context => app.Run(context =>
{ {
@ -166,9 +166,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.None; ForwardedOptions = ForwardedHeaders.None
}); });
app.Run(context => app.Run(context =>
{ {
@ -197,9 +197,9 @@ namespace Microsoft.AspNet.HttpOverrides
var builder = new WebApplicationBuilder() var builder = new WebApplicationBuilder()
.Configure(app => .Configure(app =>
{ {
app.UseOverrideHeaders(options => app.UseOverrideHeaders(new OverrideHeaderOptions
{ {
options.ForwardedOptions = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; ForwardedOptions = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
}); });
app.Run(context => app.Run(context =>
{ {