| | | 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 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Security |
| | | 9 | | { |
| | | 10 | | public readonly struct SslApplicationProtocol : IEquatable<SslApplicationProtocol> |
| | | 11 | | { |
| | 0 | 12 | | private static readonly Encoding s_utf8 = Encoding.GetEncoding(Encoding.UTF8.CodePage, EncoderFallback.Exception |
| | 0 | 13 | | private static readonly byte[] s_http3Utf8 = "h3"u8.ToArray(); |
| | 0 | 14 | | private static readonly byte[] s_http2Utf8 = "h2"u8.ToArray(); |
| | 0 | 15 | | private static readonly byte[] s_http11Utf8 = "http/1.1"u8.ToArray(); |
| | | 16 | | |
| | | 17 | | // Refer to IANA on ApplicationProtocols: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensio |
| | | 18 | | /// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 3.0.</summary> |
| | 0 | 19 | | public static readonly SslApplicationProtocol Http3 = new SslApplicationProtocol(s_http3Utf8, copy: false); |
| | | 20 | | /// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 2.0.</summary> |
| | 0 | 21 | | public static readonly SslApplicationProtocol Http2 = new SslApplicationProtocol(s_http2Utf8, copy: false); |
| | | 22 | | /// <summary>Defines a <see cref="SslApplicationProtocol"/> instance for HTTP 1.1.</summary> |
| | 0 | 23 | | public static readonly SslApplicationProtocol Http11 = new SslApplicationProtocol(s_http11Utf8, copy: false); |
| | | 24 | | |
| | | 25 | | private readonly byte[] _readOnlyProtocol; |
| | | 26 | | |
| | | 27 | | internal SslApplicationProtocol(byte[] protocol, bool copy) |
| | 0 | 28 | | { |
| | 0 | 29 | | Debug.Assert(protocol != null); |
| | | 30 | | |
| | | 31 | | // RFC 7301 states protocol size <= 255 bytes. |
| | 0 | 32 | | if (protocol.Length == 0 || protocol.Length > 255) |
| | 0 | 33 | | { |
| | 0 | 34 | | throw new ArgumentException(SR.net_ssl_app_protocol_invalid, nameof(protocol)); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | _readOnlyProtocol = copy ? |
| | 0 | 38 | | protocol.AsSpan().ToArray() : |
| | 0 | 39 | | protocol; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public SslApplicationProtocol(byte[] protocol) : |
| | 0 | 43 | | this(protocol ?? throw new ArgumentNullException(nameof(protocol)), copy: true) |
| | 0 | 44 | | { |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public SslApplicationProtocol(string protocol) : |
| | 0 | 48 | | this(s_utf8.GetBytes(protocol ?? throw new ArgumentNullException(nameof(protocol))), copy: false) |
| | 0 | 49 | | { |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | public ReadOnlyMemory<byte> Protocol => _readOnlyProtocol; |
| | | 53 | | |
| | | 54 | | public bool Equals(SslApplicationProtocol other) => |
| | 0 | 55 | | ((ReadOnlySpan<byte>)_readOnlyProtocol).SequenceEqual(other._readOnlyProtocol); |
| | | 56 | | |
| | 0 | 57 | | public override bool Equals([NotNullWhen(true)] object? obj) => obj is SslApplicationProtocol protocol && Equals |
| | | 58 | | |
| | | 59 | | public override int GetHashCode() |
| | 0 | 60 | | { |
| | 0 | 61 | | byte[] arr = _readOnlyProtocol; |
| | 0 | 62 | | if (arr == null) |
| | 0 | 63 | | { |
| | 0 | 64 | | return 0; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | int hash = 0; |
| | 0 | 68 | | for (int i = 0; i < arr.Length; i++) |
| | 0 | 69 | | { |
| | 0 | 70 | | hash = ((hash << 5) + hash) ^ arr[i]; |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return hash; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public override string ToString() |
| | 0 | 77 | | { |
| | 0 | 78 | | byte[] arr = _readOnlyProtocol; |
| | | 79 | | try |
| | 0 | 80 | | { |
| | 0 | 81 | | return |
| | 0 | 82 | | arr is null ? string.Empty : |
| | 0 | 83 | | ReferenceEquals(arr, s_http3Utf8) ? "h3" : |
| | 0 | 84 | | ReferenceEquals(arr, s_http2Utf8) ? "h2" : |
| | 0 | 85 | | ReferenceEquals(arr, s_http11Utf8) ? "http/1.1" : |
| | 0 | 86 | | s_utf8.GetString(arr); |
| | | 87 | | } |
| | 0 | 88 | | catch |
| | 0 | 89 | | { |
| | | 90 | | // In case of decoding errors, return the byte values as hex string. |
| | 0 | 91 | | char[] byteChars = new char[arr.Length * 5]; |
| | 0 | 92 | | int index = 0; |
| | | 93 | | |
| | 0 | 94 | | for (int i = 0; i < byteChars.Length; i += 5) |
| | 0 | 95 | | { |
| | 0 | 96 | | byte b = arr[index++]; |
| | 0 | 97 | | byteChars[i] = '0'; |
| | 0 | 98 | | byteChars[i + 1] = 'x'; |
| | 0 | 99 | | byteChars[i + 2] = HexConverter.ToCharLower(b >> 4); |
| | 0 | 100 | | byteChars[i + 3] = HexConverter.ToCharLower(b); |
| | 0 | 101 | | byteChars[i + 4] = ' '; |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | return new string(byteChars, 0, byteChars.Length - 1); |
| | | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public static bool operator ==(SslApplicationProtocol left, SslApplicationProtocol right) => |
| | 0 | 109 | | left.Equals(right); |
| | | 110 | | |
| | | 111 | | public static bool operator !=(SslApplicationProtocol left, SslApplicationProtocol right) => |
| | 0 | 112 | | !(left == right); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |