HttpEndPoint
Represents HTTP endpoint.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
namespace DotNext.Net.Http
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class HttpEndPoint : DnsEndPoint, ISupplier<UriBuilder>, IEquatable<HttpEndPoint>
{
private const StringComparison HostNameComparison = StringComparison.OrdinalIgnoreCase;
public bool IsSecure { get; }
public string Scheme {
get {
if (!IsSecure)
return Uri.UriSchemeHttp;
return Uri.UriSchemeHttps;
}
}
public HttpEndPoint(Uri uri)
: base(uri.IdnHost, GetPort(uri, out bool secure), ToAddressFamily(uri.HostNameType))
{
IsSecure = secure;
}
public HttpEndPoint(string hostName, int port, bool secure, AddressFamily family = AddressFamily.Unspecified)
: base(hostName, port, family)
{
IsSecure = secure;
}
public HttpEndPoint(IPAddress address, int port, bool secure)
: base(address.ToString(), port, address.AddressFamily)
{
IsSecure = secure;
}
public HttpEndPoint(IPEndPoint address, bool secure)
: this(address.Address, address.Port, secure)
{
}
private static int GetPort(Uri uri, out bool secure)
{
secure = string.Equals(uri.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase);
if (!uri.IsDefaultPort)
return uri.Port;
if (!secure)
return 80;
return 443;
}
private static AddressFamily ToAddressFamily(UriHostNameType nameType)
{
switch (nameType) {
case UriHostNameType.IPv4:
return AddressFamily.InterNetwork;
case UriHostNameType.IPv6:
return AddressFamily.InterNetworkV6;
default:
return AddressFamily.Unspecified;
}
}
public UriBuilder CreateUriBuilder()
{
return new UriBuilder(Scheme, base.Host, base.Port);
}
UriBuilder ISupplier<UriBuilder>.Invoke()
{
return CreateUriBuilder();
}
public static explicit operator UriBuilder(HttpEndPoint endPoint)
{
return endPoint?.CreateUriBuilder();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public bool Equals([NotNullWhen(true)] HttpEndPoint other)
{
if ((object)other != null && string.Equals(base.Host, other.Host, StringComparison.OrdinalIgnoreCase) && base.Port == other.Port && IsSecure == other.IsSecure)
return AddressFamily == other.AddressFamily;
return false;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public override bool Equals([NotNullWhen(true)] object other)
{
return Equals(other as HttpEndPoint);
}
public static bool operator ==(HttpEndPoint x, HttpEndPoint y)
{
return object.Equals(x, y);
}
public static bool operator !=(HttpEndPoint x, HttpEndPoint y)
{
return !object.Equals(x, y);
}
public override int GetHashCode()
{
HashCode hashCode = default(HashCode);
hashCode.Add(base.Host, StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase));
hashCode.Add(base.Port);
hashCode.Add(IsSecure);
hashCode.Add(AddressFamily);
return hashCode.ToHashCode();
}
public override string ToString()
{
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(5, 3);
val.AppendFormatted(Scheme);
val.AppendLiteral("://");
val.AppendFormatted(base.Host);
val.AppendLiteral(":");
val.AppendFormatted<int>(base.Port);
val.AppendLiteral("/");
return val.ToStringAndClear();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static bool TryParse(string str, [NotNullWhen(true)] out HttpEndPoint result)
{
if (Uri.TryCreate(str, UriKind.Absolute, out Uri result2)) {
result = new HttpEndPoint(result2);
return true;
}
result = null;
return false;
}
}
}