| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http |
| | | 8 | | { |
| | | 9 | | |
| | | 10 | | internal sealed class HttpAuthority : IEquatable<HttpAuthority> |
| | | 11 | | { |
| | | 12 | | // ALPN Protocol Name should also be part of an authority, but we are special-casing for HTTP/3, so this can be |
| | | 13 | | // public string AlpnProtocolName { get; } |
| | | 14 | | |
| | 0 | 15 | | public string IdnHost { get; } |
| | 0 | 16 | | public string HostValue { get; } |
| | 0 | 17 | | public int Port { get; } |
| | | 18 | | |
| | 0 | 19 | | public HttpAuthority(string host, int port) |
| | 0 | 20 | | { |
| | 0 | 21 | | Debug.Assert(host != null); |
| | | 22 | | |
| | | 23 | | // This is very rarely called, but could be optimized to avoid the URI-specific stuff by bringing in DomainN |
| | 0 | 24 | | var builder = new UriBuilder(Uri.UriSchemeHttp, host, port); |
| | 0 | 25 | | Uri uri = builder.Uri; |
| | | 26 | | |
| | 0 | 27 | | if (uri.HostNameType == UriHostNameType.IPv6) |
| | 0 | 28 | | { |
| | | 29 | | // This includes brackets for IPv6 and ScopeId for IPv6 LLA so Connect works. |
| | 0 | 30 | | IdnHost = $"[{uri.IdnHost}]"; |
| | | 31 | | // This is bracket enclosed IPv6 without ScopeID for LLA |
| | 0 | 32 | | HostValue = uri.Host; |
| | 0 | 33 | | } |
| | | 34 | | else |
| | 0 | 35 | | { |
| | | 36 | | // IPv4 address, dns or puny encoded name |
| | 0 | 37 | | HostValue = IdnHost = uri.IdnHost; |
| | 0 | 38 | | } |
| | 0 | 39 | | Port = port; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public bool Equals([NotNullWhen(true)] HttpAuthority? other) |
| | 0 | 43 | | { |
| | 0 | 44 | | return other != null && string.Equals(IdnHost, other.IdnHost) && Port == other.Port; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 48 | | { |
| | 0 | 49 | | return obj is HttpAuthority other && Equals(other); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public override int GetHashCode() |
| | 0 | 53 | | { |
| | 0 | 54 | | return HashCode.Combine(IdnHost, Port); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | // For diagnostics |
| | | 58 | | public override string ToString() |
| | 0 | 59 | | { |
| | 0 | 60 | | return IdnHost != null ? $"{IdnHost}:{Port}" : "<empty>"; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public static bool operator ==(HttpAuthority? left, HttpAuthority? right) |
| | 0 | 64 | | { |
| | 0 | 65 | | return left is null ? right is null : left.Equals(right); |
| | 0 | 66 | | } |
| | | 67 | | public static bool operator !=(HttpAuthority? left, HttpAuthority? right) |
| | 0 | 68 | | => !(left == right); |
| | | 69 | | } |
| | | 70 | | } |