Created
December 7, 2017 20:18
-
-
Save tpeczek/58fdae48b8ed46a5efa4d122b2c11616 to your computer and use it in GitHub Desktop.
ASP.NET Core middleware for POST Tunneling support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Options; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Http.Features; | |
namespace Demo.AspNetCore.PostTunneling.Middlewares | |
{ | |
public class PostTunnelingMiddleware | |
{ | |
private readonly RequestDelegate _next; | |
private readonly string _headerName; | |
private readonly HashSet<string> _allowedMethods; | |
public PostTunnelingMiddleware(RequestDelegate next, IOptions<PostTunnelingOptions> options) | |
{ | |
_next = next ?? throw new ArgumentNullException(nameof(next)); | |
if (options?.Value == null) | |
{ | |
throw new ArgumentNullException(nameof(options)); | |
} | |
_headerName = options.Value.HeaderName; | |
_allowedMethods = new HashSet<string>(); | |
if (options.Value.AllowedMethods != null) | |
{ | |
foreach (string allowedMethod in options.Value.AllowedMethods) | |
{ | |
_allowedMethods.Add(allowedMethod.ToUpper()); | |
} | |
} | |
} | |
public Task Invoke(HttpContext context) | |
{ | |
if (HttpMethods.IsPost(context.Request.Method)) | |
{ | |
if (context.Request.Headers.ContainsKey(_headerName)) | |
{ | |
string tunelledMethod = context.Request.Headers[_headerName]; | |
if (_allowedMethods.Contains(tunelledMethod)) | |
{ | |
IHttpRequestFeature httpRequestFeature = context.Features.Get<IHttpRequestFeature>(); | |
httpRequestFeature.Method = tunelledMethod; | |
} | |
} | |
} | |
return _next(context); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.Extensions.Options; | |
using Microsoft.AspNetCore.Builder; | |
namespace Demo.AspNetCore.PostTunneling.Middlewares | |
{ | |
public static class PostTunnelingMiddlewareExtensions | |
{ | |
public static IApplicationBuilder UsePostTunneling(this IApplicationBuilder app) | |
{ | |
if (app == null) | |
{ | |
throw new ArgumentNullException(nameof(app)); | |
} | |
return app.UseMiddleware<PostTunnelingMiddleware>(); | |
} | |
public static IApplicationBuilder UsePostTunneling(this IApplicationBuilder app, PostTunnelingOptions options) | |
{ | |
if (app == null) | |
{ | |
throw new ArgumentNullException(nameof(app)); | |
} | |
if (options == null) | |
{ | |
throw new ArgumentNullException(nameof(options)); | |
} | |
return app.UseMiddleware<PostTunnelingMiddleware>(Options.Create(options)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace Demo.AspNetCore.PostTunneling.Middlewares | |
{ | |
public class PostTunnelingOptions | |
{ | |
public string HeaderName { get; set; } | |
public IEnumerable<string> AllowedMethods { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description --> https://www.tpeczek.com/2017/12/post-tunneling-middleware-for-aspnet.html