| | | 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.Globalization; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace 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 | | |
| | 0 | 18 | | public static AltSvcHeaderValue Clear { get; } = new AltSvcHeaderValue(ClearString, host: null, port: 0, maxAge: |
| | | 19 | | |
| | 0 | 20 | | public AltSvcHeaderValue(string alpnProtocolName, string? host, int port, TimeSpan maxAge, bool persist) |
| | 0 | 21 | | { |
| | 0 | 22 | | AlpnProtocolName = alpnProtocolName; |
| | 0 | 23 | | Host = host; |
| | 0 | 24 | | Port = port; |
| | 0 | 25 | | MaxAge = maxAge; |
| | 0 | 26 | | Persist = persist; |
| | 0 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | 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> |
| | 0 | 35 | | public string? Host { get; } |
| | | 36 | | |
| | 0 | 37 | | 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> |
| | 0 | 44 | | 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 |
| | 0 | 51 | | public bool Persist { get; } |
| | | 52 | | |
| | | 53 | | public override string ToString() |
| | 0 | 54 | | { |
| | 0 | 55 | | if (ReferenceEquals(Clear, this)) |
| | 0 | 56 | | { |
| | 0 | 57 | | return ClearString; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | var sb = new ValueStringBuilder(stackalloc char[256]); |
| | | 61 | | |
| | 0 | 62 | | sb.Append(AlpnProtocolName); |
| | 0 | 63 | | sb.Append("=\""); |
| | 0 | 64 | | if (Host != null) |
| | 0 | 65 | | { |
| | 0 | 66 | | sb.Append(Host); |
| | 0 | 67 | | } |
| | 0 | 68 | | sb.Append(':'); |
| | 0 | 69 | | sb.AppendSpanFormattable((uint)Port); |
| | 0 | 70 | | sb.Append('"'); |
| | | 71 | | |
| | 0 | 72 | | if (MaxAge != TimeSpan.FromTicks(AltSvcHeaderParser.DefaultMaxAgeTicks)) |
| | 0 | 73 | | { |
| | 0 | 74 | | sb.Append("; ma="); |
| | 0 | 75 | | sb.AppendSpanFormattable(MaxAge.Ticks / TimeSpan.TicksPerSecond, provider: CultureInfo.InvariantCulture) |
| | 0 | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | if (Persist) |
| | 0 | 79 | | { |
| | 0 | 80 | | sb.Append("; persist=1"); |
| | 0 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | return sb.ToString(); |
| | 0 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |