You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Extensions/EndpointExtensions.cs

54 lines
1.9 KiB

using System.Net;
namespace ZeroLevel.Extensions
{
public static class EndpointExtensions
{
public const string HTTP_SCHEMA = "http";
public const string HTTPS_SCHEMA = "https";
public static string ToHttpUrl(this EndPoint endPoint, string schema, string rawUrl = null!)
{
if (endPoint is IPEndPoint)
{
var ipEndPoint = endPoint as IPEndPoint;
return CreateHttpUrl(schema, ipEndPoint?.Address?.ToString() ?? string.Empty, ipEndPoint?.Port ?? 0,
rawUrl != null ? rawUrl.TrimStart('/') : string.Empty);
}
if (endPoint is DnsEndPoint)
{
var dnsEndpoint = endPoint as DnsEndPoint;
return CreateHttpUrl(schema, dnsEndpoint?.Host ?? string.Empty, dnsEndpoint?.Port ?? 0,
rawUrl != null ? rawUrl.TrimStart('/') : string.Empty);
}
return null!;
}
public static string ToHttpUrl(this EndPoint endPoint, string schema, string formatString,
params object[] args)
{
if (endPoint is IPEndPoint)
{
var ipEndPoint = endPoint as IPEndPoint;
return CreateHttpUrl(schema, ipEndPoint?.Address?.ToString() ?? string.Empty, ipEndPoint?.Port ?? 0,
string.Format(formatString.TrimStart('/'), args));
}
if (endPoint is DnsEndPoint)
{
var dnsEndpoint = endPoint as DnsEndPoint;
return CreateHttpUrl(schema, dnsEndpoint?.Host ?? string.Empty, dnsEndpoint?.Port ?? 0,
string.Format(formatString.TrimStart('/'), args));
}
return null!;
}
private static string CreateHttpUrl(string schema, string host, int port, string path)
{
return $"{schema}://{host}:{port}/{path}";
}
}
}

Powered by TurnKey Linux.