-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
StringWithQualityHeaderValue.cs
268 lines (220 loc) · 9.42 KB
/
StringWithQualityHeaderValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using Microsoft.Extensions.Primitives;
namespace Microsoft.Net.Http.Headers;
/// <summary>
/// A string header value with an optional quality.
/// </summary>
public class StringWithQualityHeaderValue
{
private static readonly HttpHeaderParser<StringWithQualityHeaderValue> SingleValueParser
= new GenericHeaderParser<StringWithQualityHeaderValue>(false, GetStringWithQualityLength);
private static readonly HttpHeaderParser<StringWithQualityHeaderValue> MultipleValueParser
= new GenericHeaderParser<StringWithQualityHeaderValue>(true, GetStringWithQualityLength);
private StringSegment _value;
private double? _quality;
private StringWithQualityHeaderValue()
{
// Used by the parser to create a new instance of this type.
}
/// <summary>
/// Initializes a new instance of <see cref="StringWithQualityHeaderValue"/>.
/// </summary>
/// <param name="value">The <see cref="StringSegment"/> used to initialize the new instance.</param>
public StringWithQualityHeaderValue(StringSegment value)
{
HeaderUtilities.CheckValidToken(value, nameof(value));
_value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="StringWithQualityHeaderValue"/>.
/// </summary>
/// <param name="value">The <see cref="StringSegment"/> used to initialize the new instance.</param>
/// <param name="quality">The quality factor.</param>
public StringWithQualityHeaderValue(StringSegment value, double quality)
{
HeaderUtilities.CheckValidToken(value, nameof(value));
if ((quality < 0) || (quality > 1))
{
throw new ArgumentOutOfRangeException(nameof(quality));
}
_value = value;
_quality = quality;
}
/// <summary>
/// Gets the string header value.
/// </summary>
public StringSegment Value => _value;
/// <summary>
/// Gets the quality factor.
/// </summary>
public double? Quality => _quality;
/// <inheritdoc />
public override string ToString()
{
if (_quality.HasValue)
{
return _value + "; q=" + _quality.GetValueOrDefault().ToString("0.0##", NumberFormatInfo.InvariantInfo);
}
return _value.ToString();
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
var other = obj as StringWithQualityHeaderValue;
if (other == null)
{
return false;
}
if (!StringSegment.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (_quality.HasValue)
{
// Note that we don't consider double.Epsilon here. We really consider two values equal if they're
// actually equal. This makes sure that we also get the same hashcode for two values considered equal
// by Equals().
return other._quality.HasValue && (_quality.GetValueOrDefault() == other._quality.Value);
}
// If we don't have a quality value, then 'other' must also have no quality assigned in order to be
// considered equal.
return !other._quality.HasValue;
}
/// <inheritdoc />
public override int GetHashCode()
{
var result = StringSegmentComparer.OrdinalIgnoreCase.GetHashCode(_value);
if (_quality.HasValue)
{
result = result ^ _quality.GetValueOrDefault().GetHashCode();
}
return result;
}
/// <summary>
/// Parses the specified <paramref name="input"/> as a <see cref="StringWithQualityHeaderValue"/>.
/// </summary>
/// <param name="input">The value to parse.</param>
/// <returns>The parsed value.</returns>
public static StringWithQualityHeaderValue Parse(StringSegment input)
{
var index = 0;
return SingleValueParser.ParseValue(input, ref index)!;
}
/// <summary>
/// Attempts to parse the specified <paramref name="input"/> as a <see cref="StringWithQualityHeaderValue"/>.
/// </summary>
/// <param name="input">The value to parse.</param>
/// <param name="parsedValue">The parsed value.</param>
/// <returns><see langword="true"/> if input is a valid <see cref="StringWithQualityHeaderValue"/>, otherwise <see langword="false"/>.</returns>
public static bool TryParse(StringSegment input, [NotNullWhen(true)] out StringWithQualityHeaderValue? parsedValue)
{
var index = 0;
return SingleValueParser.TryParseValue(input, ref index, out parsedValue);
}
/// <summary>
/// Parses a sequence of inputs as a sequence of <see cref="StringWithQualityHeaderValue"/> values.
/// </summary>
/// <param name="input">The values to parse.</param>
/// <returns>The parsed values.</returns>
public static IList<StringWithQualityHeaderValue> ParseList(IList<string>? input)
{
return MultipleValueParser.ParseValues(input);
}
/// <summary>
/// Parses a sequence of inputs as a sequence of <see cref="StringWithQualityHeaderValue"/> values using string parsing rules.
/// </summary>
/// <param name="input">The values to parse.</param>
/// <returns>The parsed values.</returns>
public static IList<StringWithQualityHeaderValue> ParseStrictList(IList<string>? input)
{
return MultipleValueParser.ParseStrictValues(input);
}
/// <summary>
/// Attempts to parse the sequence of values as a sequence of <see cref="StringWithQualityHeaderValue"/>.
/// </summary>
/// <param name="input">The values to parse.</param>
/// <param name="parsedValues">The parsed values.</param>
/// <returns><see langword="true"/> if all inputs are valid <see cref="StringWithQualityHeaderValue"/>, otherwise <see langword="false"/>.</returns>
public static bool TryParseList(IList<string>? input, [NotNullWhen(true)] out IList<StringWithQualityHeaderValue>? parsedValues)
{
return MultipleValueParser.TryParseValues(input, out parsedValues);
}
/// <summary>
/// Attempts to parse the sequence of values as a sequence of <see cref="StringWithQualityHeaderValue"/> using string parsing rules.
/// </summary>
/// <param name="input">The values to parse.</param>
/// <param name="parsedValues">The parsed values.</param>
/// <returns><see langword="true"/> if all inputs are valid <see cref="StringWithQualityHeaderValue"/>, otherwise <see langword="false"/>.</returns>
public static bool TryParseStrictList(IList<string>? input, [NotNullWhen(true)] out IList<StringWithQualityHeaderValue>? parsedValues)
{
return MultipleValueParser.TryParseStrictValues(input, out parsedValues);
}
private static int GetStringWithQualityLength(StringSegment input, int startIndex, out StringWithQualityHeaderValue? parsedValue)
{
Contract.Requires(startIndex >= 0);
parsedValue = null;
if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length))
{
return 0;
}
// Parse the value string: <value> in '<value>; q=<quality>'
var valueLength = HttpRuleParser.GetTokenLength(input, startIndex);
if (valueLength == 0)
{
return 0;
}
StringWithQualityHeaderValue result = new StringWithQualityHeaderValue();
result._value = input.Subsegment(startIndex, valueLength);
var current = startIndex + valueLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
if ((current == input.Length) || (input[current] != ';'))
{
parsedValue = result;
return current - startIndex; // we have a valid token, but no quality.
}
current++; // skip ';' separator
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
// If we found a ';' separator, it must be followed by a quality information
if (!TryReadQuality(input, result, ref current))
{
return 0;
}
parsedValue = result;
return current - startIndex;
}
private static bool TryReadQuality(StringSegment input, StringWithQualityHeaderValue result, ref int index)
{
var current = index;
// See if we have a quality value by looking for "q"
if ((current == input.Length) || ((input[current] != 'q') && (input[current] != 'Q')))
{
return false;
}
current++; // skip 'q' identifier
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
// If we found "q" it must be followed by "="
if ((current == input.Length) || (input[current] != '='))
{
return false;
}
current++; // skip '=' separator
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
if (current == input.Length)
{
return false;
}
if (!HeaderUtilities.TryParseQualityDouble(input, current, out var quality, out var qualityLength))
{
return false;
}
result._quality = quality;
current = current + qualityLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
index = current;
return true;
}
}