| | | 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.Globalization; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | public class StringWithQualityHeaderValue : ICloneable |
| | | 11 | | { |
| | | 12 | | private const double NotSetSentinel = double.PositiveInfinity; |
| | | 13 | | |
| | | 14 | | private readonly string _value; |
| | | 15 | | private readonly double _quality; |
| | | 16 | | |
| | 0 | 17 | | public string Value => _value; |
| | | 18 | | |
| | 0 | 19 | | public double? Quality => _quality == NotSetSentinel ? null : _quality; |
| | | 20 | | |
| | 6504 | 21 | | public StringWithQualityHeaderValue(string value) |
| | 6504 | 22 | | { |
| | 6504 | 23 | | HeaderUtilities.CheckValidToken(value); |
| | | 24 | | |
| | 6504 | 25 | | _value = value; |
| | 6504 | 26 | | _quality = NotSetSentinel; |
| | 6504 | 27 | | } |
| | | 28 | | |
| | 679 | 29 | | public StringWithQualityHeaderValue(string value, double quality) |
| | 679 | 30 | | { |
| | 679 | 31 | | HeaderUtilities.CheckValidToken(value); |
| | | 32 | | |
| | 679 | 33 | | ArgumentOutOfRangeException.ThrowIfNegative(quality); |
| | 679 | 34 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(quality, 1.0); |
| | | 35 | | |
| | 679 | 36 | | _value = value; |
| | 679 | 37 | | _quality = quality; |
| | 679 | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | private StringWithQualityHeaderValue(StringWithQualityHeaderValue source) |
| | 0 | 41 | | { |
| | 0 | 42 | | Debug.Assert(source != null); |
| | | 43 | | |
| | 0 | 44 | | _value = source._value; |
| | 0 | 45 | | _quality = source._quality; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public override string ToString() => |
| | 11135 | 49 | | _quality == NotSetSentinel |
| | 11135 | 50 | | ? _value |
| | 11135 | 51 | | : string.Create(CultureInfo.InvariantCulture, stackalloc char[128], $"{_value}; q={_quality:0.0##}"); |
| | | 52 | | |
| | | 53 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 54 | | obj is StringWithQualityHeaderValue other && |
| | 0 | 55 | | string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 56 | | // Note that we don't consider double.Epsilon here. We really consider two values equal if they're |
| | 0 | 57 | | // actually equal. This makes sure that we also get the same hashcode for two values considered equal |
| | 0 | 58 | | // by Equals(). |
| | 0 | 59 | | _quality == other._quality; |
| | | 60 | | |
| | | 61 | | public override int GetHashCode() => |
| | 0 | 62 | | HashCode.Combine(StringComparer.OrdinalIgnoreCase.GetHashCode(_value), _quality); |
| | | 63 | | |
| | | 64 | | public static StringWithQualityHeaderValue Parse(string input) |
| | 0 | 65 | | { |
| | 0 | 66 | | int index = 0; |
| | 0 | 67 | | return (StringWithQualityHeaderValue)GenericHeaderParser.SingleValueStringWithQualityParser.ParseValue( |
| | 0 | 68 | | input, null, ref index); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out StringWithQualityHeaderVa |
| | 0 | 72 | | { |
| | 0 | 73 | | int index = 0; |
| | 0 | 74 | | parsedValue = null; |
| | | 75 | | |
| | 0 | 76 | | if (GenericHeaderParser.SingleValueStringWithQualityParser.TryParseValue( |
| | 0 | 77 | | input, null, ref index, out object? output)) |
| | 0 | 78 | | { |
| | 0 | 79 | | parsedValue = (StringWithQualityHeaderValue)output!; |
| | 0 | 80 | | return true; |
| | | 81 | | } |
| | 0 | 82 | | return false; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | internal static int GetStringWithQualityLength(string? input, int startIndex, out object? parsedValue) |
| | 7331 | 86 | | { |
| | 7331 | 87 | | Debug.Assert(startIndex >= 0); |
| | | 88 | | |
| | 7331 | 89 | | parsedValue = null; |
| | | 90 | | |
| | 7331 | 91 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 92 | | { |
| | 0 | 93 | | return 0; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // Parse the value string: <value> in '<value>; q=<quality>' |
| | 7331 | 97 | | int valueLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 98 | | |
| | 7331 | 99 | | if (valueLength == 0) |
| | 14 | 100 | | { |
| | 14 | 101 | | return 0; |
| | | 102 | | } |
| | | 103 | | |
| | 7317 | 104 | | string value = input.Substring(startIndex, valueLength); |
| | 7317 | 105 | | int current = startIndex + valueLength; |
| | 7317 | 106 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 107 | | |
| | 7317 | 108 | | if ((current == input.Length) || (input[current] != ';')) |
| | 6504 | 109 | | { |
| | 6504 | 110 | | parsedValue = new StringWithQualityHeaderValue(value); |
| | 6504 | 111 | | return current - startIndex; // we have a valid token, but no quality. |
| | | 112 | | } |
| | | 113 | | |
| | 813 | 114 | | current++; // skip ';' separator |
| | 813 | 115 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 116 | | |
| | | 117 | | // If we found a ';' separator, it must be followed by a quality information |
| | 813 | 118 | | if (!TryReadQuality(input, out double quality, ref current)) |
| | 134 | 119 | | { |
| | 134 | 120 | | return 0; |
| | | 121 | | } |
| | | 122 | | |
| | 679 | 123 | | parsedValue = new StringWithQualityHeaderValue(value, quality); |
| | 679 | 124 | | return current - startIndex; |
| | 7331 | 125 | | } |
| | | 126 | | |
| | | 127 | | private static bool TryReadQuality(string input, out double quality, ref int index) |
| | 813 | 128 | | { |
| | 813 | 129 | | int current = index; |
| | 813 | 130 | | quality = default; |
| | | 131 | | |
| | | 132 | | // See if we have a quality value by looking for "q" |
| | 813 | 133 | | if ((current == input.Length) || ((input[current] != 'q') && (input[current] != 'Q'))) |
| | 96 | 134 | | { |
| | 96 | 135 | | return false; |
| | | 136 | | } |
| | | 137 | | |
| | 717 | 138 | | current++; // skip 'q' identifier |
| | 717 | 139 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 140 | | |
| | | 141 | | // If we found "q" it must be followed by "=" |
| | 717 | 142 | | if ((current == input.Length) || (input[current] != '=')) |
| | 4 | 143 | | { |
| | 4 | 144 | | return false; |
| | | 145 | | } |
| | | 146 | | |
| | 713 | 147 | | current++; // skip '=' separator |
| | 713 | 148 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 149 | | |
| | 713 | 150 | | if (current == input.Length) |
| | 2 | 151 | | { |
| | 2 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 711 | 155 | | int qualityLength = HttpRuleParser.GetNumberLength(input, current, true); |
| | | 156 | | |
| | 711 | 157 | | if (qualityLength == 0) |
| | 2 | 158 | | { |
| | 2 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 709 | 162 | | if (!double.TryParse(input.AsSpan(current, qualityLength), NumberStyles.AllowDecimalPoint, |
| | 709 | 163 | | NumberFormatInfo.InvariantInfo, out quality)) |
| | 0 | 164 | | { |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 709 | 168 | | if ((quality < 0) || (quality > 1)) |
| | 30 | 169 | | { |
| | 30 | 170 | | return false; |
| | | 171 | | } |
| | | 172 | | |
| | 679 | 173 | | current += qualityLength; |
| | 679 | 174 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 175 | | |
| | 679 | 176 | | index = current; |
| | 679 | 177 | | return true; |
| | 813 | 178 | | } |
| | | 179 | | |
| | | 180 | | object ICloneable.Clone() |
| | 0 | 181 | | { |
| | 0 | 182 | | return new StringWithQualityHeaderValue(this); |
| | 0 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |