netmash/NDB.Extensions.Swagger/ReverseProxy/ReverseProxyHelper.cs

39 lines
1.2 KiB
C#
Raw Normal View History

2020-12-20 22:31:53 +02:00
using Microsoft.AspNetCore.Http;
using System.Linq;
2023-01-12 01:01:36 +02:00
namespace Netmash.Extensions.Swagger.ReverseProxy
2020-12-20 22:31:53 +02:00
{
public static class ReverseProxyHelper
{
public static (string host, string basePath, string scheme) GetUrlComponents(HttpRequest request)
{
var host = ExtractHost(request);
var basePath = ExtractBasePath(request);
var scheme = ExtractScheme(request);
return (host, basePath, scheme);
}
private static string ExtractHost(HttpRequest request)
{
if (request.Headers.ContainsKey("X-Forwarded-Host"))
return request.Headers["X-Forwarded-Host"].First();
return request.Host.Value;
}
private static string ExtractBasePath(HttpRequest request)
{
if (request.Headers.ContainsKey("X-Forwarded-PathBase"))
return request.Headers["X-Forwarded-PathBase"].First();
return string.Empty;
}
private static string ExtractScheme(HttpRequest request)
{
return request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? request.Scheme;
}
}
}