< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 38
Coverable lines: 38
Total lines: 81
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%440%
Equals(...)0%440%
Equals(...)0%220%
GetHashCode()100%110%
ToString()0%220%
op_Equality(...)0%220%
op_Inequality(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpAuthority.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text;
 7
 8namespace System.Net.Http
 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
 015        public string IdnHost { get; }
 016        public string HostValue { get; }
 017        public int Port { get; }
 18
 019        public HttpAuthority(string host, int port)
 020        {
 021            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
 024            var builder = new UriBuilder(Uri.UriSchemeHttp, host, port);
 025            Uri uri = builder.Uri;
 26
 027            if (uri.HostNameType == UriHostNameType.IPv6)
 028            {
 29                // This includes brackets for IPv6 and ScopeId for IPv6 LLA so Connect works.
 030                IdnHost = $"[{uri.IdnHost}]";
 31                // This is bracket enclosed IPv6 without ScopeID for LLA
 032                HostValue = uri.Host;
 033            }
 34            else
 035            {
 36                // IPv4 address, dns or puny encoded name
 037                HostValue = IdnHost = uri.IdnHost;
 038            }
 39
 040            if (!Ascii.IsValid(HostValue))
 041            {
 042                Debug.Assert(uri.HostNameType == UriHostNameType.Basic);
 43
 44                // This is not a DNS host and it contains non-ASCII characters.
 45                // Uri failed to Punycode encode it, likely because one of the labels was too long for DNS.
 046                throw new HttpRequestException(SR.net_http_request_invalid_host_punycode);
 47            }
 48
 049            Port = port;
 050        }
 51
 52        public bool Equals([NotNullWhen(true)] HttpAuthority? other)
 053        {
 054            return other != null && string.Equals(IdnHost, other.IdnHost) && Port == other.Port;
 055        }
 56
 57        public override bool Equals([NotNullWhen(true)] object? obj)
 058        {
 059            return obj is HttpAuthority other && Equals(other);
 060        }
 61
 62        public override int GetHashCode()
 063        {
 064            return HashCode.Combine(IdnHost, Port);
 065        }
 66
 67        // For diagnostics
 68        public override string ToString()
 069        {
 070            return IdnHost != null ? $"{IdnHost}:{Port}" : "<empty>";
 071        }
 72
 73        public static bool operator ==(HttpAuthority? left, HttpAuthority? right)
 074        {
 075            return left is null ? right is null : left.Equals(right);
 076        }
 77        public static bool operator !=(HttpAuthority? left, HttpAuthority? right)
 078            => !(left == right);
 79    }
 80}
 81