mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 05:54:26 +01:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace BTCPayServer.Abstractions;
|
|
|
|
public record RequestBaseUrl(string Scheme, HostString Host, PathString PathBase)
|
|
{
|
|
public static RequestBaseUrl FromUrl(Uri url)
|
|
=> new RequestBaseUrl(url.Scheme, new HostString(url.Authority), new PathString(url.AbsolutePath));
|
|
|
|
public static RequestBaseUrl FromUrl(string url)
|
|
{
|
|
if (TryFromUrl(url, out var result))
|
|
return result;
|
|
throw new FormatException("Invalid RequestBaseUrl");
|
|
}
|
|
public static bool TryFromUrl(string url, [MaybeNullWhen(false)] out RequestBaseUrl result)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(url);
|
|
result = null;
|
|
if (!Uri.TryCreate(url, UriKind.Absolute, out var o))
|
|
return false;
|
|
result = FromUrl(o);
|
|
return true;
|
|
}
|
|
|
|
public RequestBaseUrl(HttpRequest request) : this(request.Scheme, request.Host, request.PathBase)
|
|
{
|
|
|
|
}
|
|
|
|
public override string ToString()
|
|
=> string.Concat(
|
|
Scheme,
|
|
"://",
|
|
Host.ToUriComponent(),
|
|
PathBase.ToUriComponent());
|
|
}
|