btcpayserver/BTCPayServer/Hosting/HeadersOverrideMiddleware.cs

33 lines
1.0 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
2019-10-10 02:49:03 +02:00
using System.Threading.Tasks;
2020-06-28 10:55:27 +02:00
using BTCPayServer.Configuration;
2019-10-10 02:49:03 +02:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace BTCPayServer.Hosting
{
public class HeadersOverrideMiddleware
{
readonly RequestDelegate _Next;
readonly string overrideXForwardedProto;
2019-10-10 02:49:03 +02:00
public HeadersOverrideMiddleware(RequestDelegate next,
IConfiguration options)
{
_Next = next ?? throw new ArgumentNullException(nameof(next));
overrideXForwardedProto = options.GetOrDefault<string>("xforwardedproto", null);
}
public async Task Invoke(HttpContext httpContext)
{
if (!string.IsNullOrEmpty(overrideXForwardedProto))
{
if (!httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase))
{
httpContext.Request.Headers["X-Forwarded-Proto"] = overrideXForwardedProto;
}
2019-10-10 02:49:03 +02:00
}
await _Next(httpContext);
}
}
}