HttpEndPoint
public sealed class HttpEndPoint : DnsEndPoint, ISupplier<UriBuilder>, IFunctional<Func<UriBuilder>>, IEquatable<HttpEndPoint>, IEqualityOperators<HttpEndPoint, HttpEndPoint, bool>, ISpanFormattable, IFormattable, IParsable<HttpEndPoint>, IUtf8SpanFormattable
Represents HTTP endpoint.
using DotNext.Buffers;
using DotNext.Runtime.CompilerServices;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
namespace DotNext.Net.Http
{
[NullableContext(1)]
[Nullable(0)]
public sealed class HttpEndPoint : DnsEndPoint, ISupplier<UriBuilder>, IFunctional<Func<UriBuilder>>, IEquatable<HttpEndPoint>, IFormattable
{
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();
}
[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;
}
[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()
{
return ToString(null, null);
}
[NullableContext(2)]
[return: Nullable(1)]
public string ToString(string format, IFormatProvider formatProvider)
{
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(5, 3);
val.AppendFormatted(Scheme);
val.AppendLiteral("://");
val.AppendFormatted(base.Host);
val.AppendLiteral(":");
val.AppendFormatted(base.Port.ToString(format, formatProvider));
val.AppendLiteral("/");
return val.ToStringAndClear();
}
bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
SpanWriter<char> writer = new SpanWriter<char>(destination);
bool result;
charsWritten = ((result = (writer.TryWrite(Scheme) && writer.TryWrite("://") && writer.TryWrite(base.Host) && writer.TryAdd(':') && ref writer.TryFormat<int>(base.Port, format, provider) && writer.TryAdd('/'))) ? writer.WrittenCount : 0);
return result;
}
unsafe bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
SpanWriter<byte> writer = new SpanWriter<byte>(destination);
bool result;
bytesWritten = ((result = (ref writer.TryEncodeAsUtf8(Scheme) && writer.TryWrite(new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.89CA4E50FF65BC5B2BDC3650452FE5770FEC546909392E103A951F810B19B3A5, 3)) && ref writer.TryEncodeAsUtf8(base.Host) && writer.TryAdd(58) && ref writer.TryFormat<int>(base.Port, format, provider) && writer.TryAdd(47))) ? writer.WrittenCount : 0);
return result;
}
[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;
}
static HttpEndPoint Parse(string s, IFormatProvider provider)
{
return new HttpEndPoint(new Uri(s));
}
static bool TryParse([NotNullWhen(true)] string s, IFormatProvider provider, [MaybeNullWhen(false)] out HttpEndPoint result)
{
return TryParse(s, out result);
}
}
}