| | | 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.Headers |
| | | 8 | | { |
| | | 9 | | public class ProductHeaderValue : ICloneable |
| | | 10 | | { |
| | | 11 | | private readonly string _name; |
| | | 12 | | private readonly string? _version; |
| | | 13 | | |
| | | 14 | | public string Name |
| | | 15 | | { |
| | 0 | 16 | | get { return _name; } |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | // We can't use the System.Version type, since a version can be e.g. "x11". |
| | | 20 | | public string? Version |
| | | 21 | | { |
| | 0 | 22 | | get { return _version; } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public ProductHeaderValue(string name) |
| | 48815 | 26 | | : this(name, null) |
| | 48815 | 27 | | { |
| | 48815 | 28 | | } |
| | | 29 | | |
| | 51391 | 30 | | public ProductHeaderValue(string name, string? version) |
| | 51391 | 31 | | { |
| | 51391 | 32 | | HeaderUtilities.CheckValidToken(name); |
| | | 33 | | |
| | 51391 | 34 | | if (!string.IsNullOrEmpty(version)) |
| | 2576 | 35 | | { |
| | 2576 | 36 | | HeaderUtilities.CheckValidToken(version); |
| | 2576 | 37 | | _version = version; |
| | 2576 | 38 | | } |
| | | 39 | | |
| | 51391 | 40 | | _name = name; |
| | 51391 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | private ProductHeaderValue(ProductHeaderValue source) |
| | 0 | 44 | | { |
| | 0 | 45 | | Debug.Assert(source != null); |
| | | 46 | | |
| | 0 | 47 | | _name = source._name; |
| | 0 | 48 | | _version = source._version; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public override string ToString() |
| | 79315 | 52 | | { |
| | 79315 | 53 | | if (string.IsNullOrEmpty(_version)) |
| | 75525 | 54 | | { |
| | 75525 | 55 | | return _name; |
| | | 56 | | } |
| | 3790 | 57 | | return _name + "/" + _version; |
| | 79315 | 58 | | } |
| | | 59 | | |
| | | 60 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 61 | | { |
| | 0 | 62 | | ProductHeaderValue? other = obj as ProductHeaderValue; |
| | | 63 | | |
| | 0 | 64 | | if (other == null) |
| | 0 | 65 | | { |
| | 0 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return string.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 70 | | string.Equals(_version, other._version, StringComparison.OrdinalIgnoreCase); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public override int GetHashCode() |
| | 0 | 74 | | { |
| | 0 | 75 | | int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_name); |
| | | 76 | | |
| | 0 | 77 | | if (!string.IsNullOrEmpty(_version)) |
| | 0 | 78 | | { |
| | 0 | 79 | | result ^= StringComparer.OrdinalIgnoreCase.GetHashCode(_version); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | return result; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public static ProductHeaderValue Parse(string input) |
| | 0 | 86 | | { |
| | 0 | 87 | | int index = 0; |
| | 0 | 88 | | return (ProductHeaderValue)GenericHeaderParser.SingleValueProductParser.ParseValue(input, null, ref index); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ProductHeaderValue? parse |
| | 0 | 92 | | { |
| | 0 | 93 | | int index = 0; |
| | 0 | 94 | | parsedValue = null; |
| | | 95 | | |
| | 0 | 96 | | if (GenericHeaderParser.SingleValueProductParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 97 | | { |
| | 0 | 98 | | parsedValue = (ProductHeaderValue)output!; |
| | 0 | 99 | | return true; |
| | | 100 | | } |
| | 0 | 101 | | return false; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static int GetProductLength(string input, int startIndex, out ProductHeaderValue? parsedValue) |
| | 51711 | 105 | | { |
| | 51711 | 106 | | Debug.Assert(startIndex >= 0); |
| | | 107 | | |
| | 51711 | 108 | | parsedValue = null; |
| | | 109 | | |
| | 51711 | 110 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 111 | | { |
| | 0 | 112 | | return 0; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Parse the name string: <name> in '<name>/<version>'. |
| | 51711 | 116 | | int nameLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 117 | | |
| | 51711 | 118 | | if (nameLength == 0) |
| | 294 | 119 | | { |
| | 294 | 120 | | return 0; |
| | | 121 | | } |
| | | 122 | | |
| | 51417 | 123 | | string name = input.Substring(startIndex, nameLength); |
| | 51417 | 124 | | int current = startIndex + nameLength; |
| | 51417 | 125 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 126 | | |
| | 51417 | 127 | | if ((current == input.Length) || (input[current] != '/')) |
| | 48815 | 128 | | { |
| | 48815 | 129 | | parsedValue = new ProductHeaderValue(name); |
| | 48815 | 130 | | return current - startIndex; |
| | | 131 | | } |
| | | 132 | | |
| | 2602 | 133 | | current++; // Skip '/' delimiter. |
| | 2602 | 134 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 135 | | |
| | | 136 | | // Parse the name string: <version> in '<name>/<version>'. |
| | 2602 | 137 | | int versionLength = HttpRuleParser.GetTokenLength(input, current); |
| | | 138 | | |
| | 2602 | 139 | | if (versionLength == 0) |
| | 26 | 140 | | { |
| | 26 | 141 | | return 0; // If there is a '/' separator it must be followed by a valid token. |
| | | 142 | | } |
| | | 143 | | |
| | 2576 | 144 | | string version = input.Substring(current, versionLength); |
| | | 145 | | |
| | 2576 | 146 | | current += versionLength; |
| | 2576 | 147 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 148 | | |
| | 2576 | 149 | | parsedValue = new ProductHeaderValue(name, version); |
| | 2576 | 150 | | return current - startIndex; |
| | 51711 | 151 | | } |
| | | 152 | | |
| | | 153 | | object ICloneable.Clone() |
| | 0 | 154 | | { |
| | 0 | 155 | | return new ProductHeaderValue(this); |
| | 0 | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |