39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using System.Linq;
|
|
|
|
namespace NDB.Extensions.Swagger.ReverseProxy
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|