< Summary

Information
Class: System.Net.Http.Headers.AltSvcHeaderValue
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\AltSvcHeaderValue.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 86
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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(...)100%110%
ToString()0%880%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\AltSvcHeaderValue.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.Globalization;
 5using System.Text;
 6
 7namespace System.Net.Http.Headers
 8{
 9    /// <remarks>
 10    /// Kept internal for now:
 11    /// A user depending on this strongly-typed header is dubious, as Alt-Svc values can also be received via the ALTSVC
 12    /// This type does not conform to the typical API for header values, and should be updated if ever made public.
 13    /// </remarks>
 14    internal sealed class AltSvcHeaderValue
 15    {
 16        public const string ClearString = "clear";
 17
 018        public static AltSvcHeaderValue Clear { get; } = new AltSvcHeaderValue(ClearString, host: null, port: 0, maxAge:
 19
 020        public AltSvcHeaderValue(string alpnProtocolName, string? host, int port, TimeSpan maxAge, bool persist)
 021        {
 022            AlpnProtocolName = alpnProtocolName;
 023            Host = host;
 024            Port = port;
 025            MaxAge = maxAge;
 026            Persist = persist;
 027        }
 28
 029        public string AlpnProtocolName { get; }
 30
 31        /// <summary>
 32        /// The name of the host serving this alternate service.
 33        /// If null, the alternate service is on the same host this header was received from.
 34        /// </summary>
 035        public string? Host { get; }
 36
 037        public int Port { get; }
 38
 39        /// <summary>
 40        /// The time span this alternate service is valid for.
 41        /// If not specified by the header, defaults to 24 hours.
 42        /// </summary>
 43        /// <remarks>TODO: if made public, should this be defaulted or nullable?</remarks>
 044        public TimeSpan MaxAge { get; }
 45
 46        /// <summary>
 47        /// If true, the service should persist across network changes.
 48        /// Otherwise, the service should be invalidated if a network change is detected.
 49        /// </summary>
 50        /// <remarks>TODO: if made public, this should be made internal as Persist is left open-ended and can be non-boo
 051        public bool Persist { get; }
 52
 53        public override string ToString()
 054        {
 055            if (ReferenceEquals(Clear, this))
 056            {
 057                return ClearString;
 58            }
 59
 060            var sb = new ValueStringBuilder(stackalloc char[256]);
 61
 062            sb.Append(AlpnProtocolName);
 063            sb.Append("=\"");
 064            if (Host != null)
 065            {
 066                sb.Append(Host);
 067            }
 068            sb.Append(':');
 069            sb.AppendSpanFormattable((uint)Port);
 070            sb.Append('"');
 71
 072            if (MaxAge != TimeSpan.FromTicks(AltSvcHeaderParser.DefaultMaxAgeTicks))
 073            {
 074                sb.Append("; ma=");
 075                sb.AppendSpanFormattable(MaxAge.Ticks / TimeSpan.TicksPerSecond, provider: CultureInfo.InvariantCulture)
 076            }
 77
 078            if (Persist)
 079            {
 080                sb.Append("; persist=1");
 081            }
 82
 083            return sb.ToString();
 084        }
 85    }
 86}