| | | 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.Http.Headers |
| | | 9 | | { |
| | | 10 | | // According to the RFC, in places where a "parameter" is required, the value is mandatory |
| | | 11 | | // (e.g. Media-Type, Accept). However, we don't introduce a dedicated type for it. So NameValueHeaderValue supports |
| | | 12 | | // name-only values in addition to name/value pairs. |
| | | 13 | | public class NameValueHeaderValue : ICloneable |
| | | 14 | | { |
| | 1 | 15 | | private static readonly Func<NameValueHeaderValue> s_defaultNameValueCreator = CreateNameValue; |
| | | 16 | | |
| | 640537 | 17 | | private string _name = null!; // Name always set after default constructor used |
| | | 18 | | private string? _value; |
| | | 19 | | |
| | | 20 | | public string Name |
| | | 21 | | { |
| | 697482 | 22 | | get { return _name; } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public string? Value |
| | | 26 | | { |
| | 697482 | 27 | | get { return _value; } |
| | | 28 | | set |
| | 0 | 29 | | { |
| | 0 | 30 | | CheckValueFormat(value); |
| | 0 | 31 | | _value = value; |
| | 0 | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | 640537 | 35 | | internal NameValueHeaderValue() |
| | 640537 | 36 | | { |
| | 640537 | 37 | | } |
| | | 38 | | |
| | | 39 | | public NameValueHeaderValue(string name) |
| | 0 | 40 | | : this(name, null) |
| | 0 | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public NameValueHeaderValue(string name, string? value) |
| | 0 | 45 | | { |
| | 0 | 46 | | CheckNameValueFormat(name, value); |
| | | 47 | | |
| | 0 | 48 | | _name = name; |
| | 0 | 49 | | _value = value; |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | protected internal NameValueHeaderValue(NameValueHeaderValue source) |
| | 0 | 53 | | { |
| | 0 | 54 | | Debug.Assert(source != null); |
| | | 55 | | |
| | 0 | 56 | | _name = source._name; |
| | 0 | 57 | | _value = source._value; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public override int GetHashCode() |
| | 0 | 61 | | { |
| | 0 | 62 | | Debug.Assert(_name != null); |
| | | 63 | | |
| | 0 | 64 | | int nameHashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(_name); |
| | | 65 | | |
| | 0 | 66 | | if (!string.IsNullOrEmpty(_value)) |
| | 0 | 67 | | { |
| | | 68 | | // If we have a quoted-string, then just use the hash code. If we have a token, convert to lowercase |
| | | 69 | | // and retrieve the hash code. |
| | 0 | 70 | | if (_value[0] == '"') |
| | 0 | 71 | | { |
| | 0 | 72 | | return nameHashCode ^ _value.GetHashCode(); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return nameHashCode ^ StringComparer.OrdinalIgnoreCase.GetHashCode(_value); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | return nameHashCode; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 82 | | { |
| | 0 | 83 | | NameValueHeaderValue? other = obj as NameValueHeaderValue; |
| | | 84 | | |
| | 0 | 85 | | if (other == null) |
| | 0 | 86 | | { |
| | 0 | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if (!string.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 91 | | { |
| | 0 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // RFC2616: 14.20: unquoted tokens should use case-INsensitive comparison; quoted-strings should use |
| | | 96 | | // case-sensitive comparison. The RFC doesn't mention how to compare quoted-strings outside the "Expect" |
| | | 97 | | // header. We treat all quoted-strings the same: case-sensitive comparison. |
| | | 98 | | |
| | 0 | 99 | | if (string.IsNullOrEmpty(_value)) |
| | 0 | 100 | | { |
| | 0 | 101 | | return string.IsNullOrEmpty(other._value); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (_value[0] == '"') |
| | 0 | 105 | | { |
| | | 106 | | // We have a quoted string, so we need to do case-sensitive comparison. |
| | 0 | 107 | | return string.Equals(_value, other._value, StringComparison.Ordinal); |
| | | 108 | | } |
| | | 109 | | else |
| | 0 | 110 | | { |
| | 0 | 111 | | return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase); |
| | | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | public static NameValueHeaderValue Parse(string input) |
| | 0 | 116 | | { |
| | 0 | 117 | | int index = 0; |
| | 0 | 118 | | return (NameValueHeaderValue)GenericHeaderParser.SingleValueNameValueParser.ParseValue( |
| | 0 | 119 | | input, null, ref index); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out NameValueHeaderValue? par |
| | 0 | 123 | | { |
| | 0 | 124 | | int index = 0; |
| | 0 | 125 | | parsedValue = null; |
| | | 126 | | |
| | 0 | 127 | | if (GenericHeaderParser.SingleValueNameValueParser.TryParseValue(input, null, ref index, out object? output) |
| | 0 | 128 | | { |
| | 0 | 129 | | parsedValue = (NameValueHeaderValue)output!; |
| | 0 | 130 | | return true; |
| | | 131 | | } |
| | 0 | 132 | | return false; |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | public override string ToString() |
| | 279645 | 136 | | { |
| | 279645 | 137 | | if (!string.IsNullOrEmpty(_value)) |
| | 2400 | 138 | | { |
| | 2400 | 139 | | return _name + "=" + _value; |
| | | 140 | | } |
| | 277245 | 141 | | return _name; |
| | 279645 | 142 | | } |
| | | 143 | | |
| | | 144 | | private void AddToStringBuilder(StringBuilder sb) |
| | 766577 | 145 | | { |
| | 766577 | 146 | | if (GetType() != typeof(NameValueHeaderValue)) |
| | 0 | 147 | | { |
| | | 148 | | // If this is a derived instance, we need to give its |
| | | 149 | | // ToString a chance. |
| | 0 | 150 | | sb.Append(ToString()); |
| | 0 | 151 | | } |
| | | 152 | | else |
| | 766577 | 153 | | { |
| | | 154 | | // Otherwise, we can use the base behavior and avoid |
| | | 155 | | // the string concatenation. |
| | 766577 | 156 | | sb.Append(_name); |
| | 766577 | 157 | | if (!string.IsNullOrEmpty(_value)) |
| | 930 | 158 | | { |
| | 930 | 159 | | sb.Append('='); |
| | 930 | 160 | | sb.Append(_value); |
| | 930 | 161 | | } |
| | 766577 | 162 | | } |
| | 766577 | 163 | | } |
| | | 164 | | |
| | | 165 | | internal static void ToString(UnvalidatedObjectCollection<NameValueHeaderValue>? values, char separator, bool le |
| | | 166 | | StringBuilder destination) |
| | 826406 | 167 | | { |
| | 826406 | 168 | | Debug.Assert(destination != null); |
| | | 169 | | |
| | 826406 | 170 | | if ((values == null) || (values.Count == 0)) |
| | 627913 | 171 | | { |
| | 627913 | 172 | | return; |
| | | 173 | | } |
| | | 174 | | |
| | 2128633 | 175 | | foreach (var value in values) |
| | 766577 | 176 | | { |
| | 766577 | 177 | | if (leadingSeparator || (destination.Length > 0)) |
| | 761579 | 178 | | { |
| | 761579 | 179 | | destination.Append(separator); |
| | 761579 | 180 | | destination.Append(' '); |
| | 761579 | 181 | | } |
| | 766577 | 182 | | value.AddToStringBuilder(destination); |
| | 766577 | 183 | | } |
| | 826406 | 184 | | } |
| | | 185 | | |
| | | 186 | | internal static int GetHashCode(UnvalidatedObjectCollection<NameValueHeaderValue>? values) |
| | 0 | 187 | | { |
| | 0 | 188 | | if ((values == null) || (values.Count == 0)) |
| | 0 | 189 | | { |
| | 0 | 190 | | return 0; |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | int result = 0; |
| | 0 | 194 | | foreach (var value in values) |
| | 0 | 195 | | { |
| | 0 | 196 | | result ^= value.GetHashCode(); |
| | 0 | 197 | | } |
| | 0 | 198 | | return result; |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | internal static int GetNameValueLength(string input, int startIndex, out NameValueHeaderValue? parsedValue) |
| | 237688 | 202 | | { |
| | 237688 | 203 | | return GetNameValueLength(input, startIndex, s_defaultNameValueCreator, out parsedValue); |
| | 237688 | 204 | | } |
| | | 205 | | |
| | | 206 | | internal static int GetNameValueLength(string input, int startIndex, |
| | | 207 | | Func<NameValueHeaderValue> nameValueCreator, out NameValueHeaderValue? parsedValue) |
| | 641343 | 208 | | { |
| | 641343 | 209 | | Debug.Assert(input != null); |
| | 641343 | 210 | | Debug.Assert(startIndex >= 0); |
| | 641343 | 211 | | Debug.Assert(nameValueCreator != null); |
| | | 212 | | |
| | 641343 | 213 | | parsedValue = null; |
| | | 214 | | |
| | 641343 | 215 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 38 | 216 | | { |
| | 38 | 217 | | return 0; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | // Parse the name, i.e. <name> in name/value string "<name>=<value>". Caller must remove |
| | | 221 | | // leading whitespace. |
| | 641305 | 222 | | int nameLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 223 | | |
| | 641305 | 224 | | if (nameLength == 0) |
| | 430 | 225 | | { |
| | 430 | 226 | | return 0; |
| | | 227 | | } |
| | | 228 | | |
| | 640875 | 229 | | string name = input.Substring(startIndex, nameLength); |
| | 640875 | 230 | | int current = startIndex + nameLength; |
| | 640875 | 231 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 232 | | |
| | | 233 | | // Parse the separator between name and value |
| | 640875 | 234 | | if ((current == input.Length) || (input[current] != '=')) |
| | 638246 | 235 | | { |
| | | 236 | | // We only have a name and that's OK. Return. |
| | 638246 | 237 | | parsedValue = nameValueCreator(); |
| | 638246 | 238 | | parsedValue._name = name; |
| | 638246 | 239 | | current += HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespace |
| | 638246 | 240 | | return current - startIndex; |
| | | 241 | | } |
| | | 242 | | |
| | 2629 | 243 | | current++; // skip delimiter. |
| | 2629 | 244 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 245 | | |
| | | 246 | | // Parse the value, i.e. <value> in name/value string "<name>=<value>" |
| | 2629 | 247 | | int valueLength = GetValueLength(input, current); |
| | | 248 | | |
| | 2629 | 249 | | if (valueLength == 0) |
| | 338 | 250 | | { |
| | 338 | 251 | | return 0; // We have an invalid value. |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | // Use parameterless ctor to avoid double-parsing of name and value, i.e. skip public ctor validation. |
| | 2291 | 255 | | parsedValue = nameValueCreator(); |
| | 2291 | 256 | | parsedValue._name = name; |
| | 2291 | 257 | | parsedValue._value = input.Substring(current, valueLength); |
| | 2291 | 258 | | current += valueLength; |
| | 2291 | 259 | | current += HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespace |
| | 2291 | 260 | | return current - startIndex; |
| | 641343 | 261 | | } |
| | | 262 | | |
| | | 263 | | // Returns the length of a name/value list, separated by 'delimiter'. E.g. "a=b, c=d, e=f" adds 3 |
| | | 264 | | // name/value pairs to 'nameValueCollection' if 'delimiter' equals ','. |
| | | 265 | | internal static int GetNameValueListLength(string? input, int startIndex, char delimiter, |
| | | 266 | | UnvalidatedObjectCollection<NameValueHeaderValue> nameValueCollection) |
| | 122459 | 267 | | { |
| | 122459 | 268 | | Debug.Assert(nameValueCollection != null); |
| | 122459 | 269 | | Debug.Assert(startIndex >= 0); |
| | | 270 | | |
| | 122459 | 271 | | if ((string.IsNullOrEmpty(input)) || (startIndex >= input.Length)) |
| | 54 | 272 | | { |
| | 54 | 273 | | return 0; |
| | | 274 | | } |
| | | 275 | | |
| | 122405 | 276 | | int current = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex); |
| | 239958 | 277 | | while (true) |
| | 239958 | 278 | | { |
| | | 279 | | NameValueHeaderValue? parameter; |
| | 239958 | 280 | | int nameValueLength = NameValueHeaderValue.GetNameValueLength(input, current, |
| | 239958 | 281 | | s_defaultNameValueCreator, out parameter); |
| | | 282 | | |
| | 239958 | 283 | | if (nameValueLength == 0) |
| | 382 | 284 | | { |
| | 382 | 285 | | return 0; |
| | | 286 | | } |
| | | 287 | | |
| | 239576 | 288 | | nameValueCollection.Add(parameter!); |
| | 239576 | 289 | | current += nameValueLength; |
| | 239576 | 290 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 291 | | |
| | 239576 | 292 | | if ((current == input.Length) || (input[current] != delimiter)) |
| | 122023 | 293 | | { |
| | | 294 | | // We're done and we have at least one valid name/value pair. |
| | 122023 | 295 | | return current - startIndex; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // input[current] is 'delimiter'. Skip the delimiter and whitespace and try to parse again. |
| | 117553 | 299 | | current++; // skip delimiter. |
| | 117553 | 300 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 117553 | 301 | | } |
| | 122459 | 302 | | } |
| | | 303 | | |
| | | 304 | | internal static NameValueHeaderValue? Find(UnvalidatedObjectCollection<NameValueHeaderValue>? values, string nam |
| | 0 | 305 | | { |
| | 0 | 306 | | Debug.Assert((name != null) && (name.Length > 0)); |
| | | 307 | | |
| | 0 | 308 | | if ((values == null) || (values.Count == 0)) |
| | 0 | 309 | | { |
| | 0 | 310 | | return null; |
| | | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | foreach (var value in values) |
| | 0 | 314 | | { |
| | 0 | 315 | | if (string.Equals(value.Name, name, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 316 | | { |
| | 0 | 317 | | return value; |
| | | 318 | | } |
| | 0 | 319 | | } |
| | 0 | 320 | | return null; |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | internal static int GetValueLength(string input, int startIndex) |
| | 3579 | 324 | | { |
| | 3579 | 325 | | Debug.Assert(input != null); |
| | | 326 | | |
| | 3579 | 327 | | if (startIndex >= input.Length) |
| | 28 | 328 | | { |
| | 28 | 329 | | return 0; |
| | | 330 | | } |
| | | 331 | | |
| | 3551 | 332 | | int valueLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 333 | | |
| | 3551 | 334 | | if (valueLength == 0) |
| | 1003 | 335 | | { |
| | | 336 | | // A value can either be a token or a quoted string. Check if it is a quoted string. |
| | 1003 | 337 | | if (HttpRuleParser.GetQuotedStringLength(input, startIndex, out valueLength) != HttpParseResult.Parsed) |
| | 340 | 338 | | { |
| | | 339 | | // We have an invalid value. Reset the name and return. |
| | 340 | 340 | | return 0; |
| | | 341 | | } |
| | 663 | 342 | | } |
| | 3211 | 343 | | return valueLength; |
| | 3579 | 344 | | } |
| | | 345 | | |
| | | 346 | | private static void CheckNameValueFormat(string name, string? value) |
| | 0 | 347 | | { |
| | 0 | 348 | | HeaderUtilities.CheckValidToken(name); |
| | 0 | 349 | | CheckValueFormat(value); |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | private static void CheckValueFormat(string? value) |
| | 0 | 353 | | { |
| | | 354 | | // Either value is null/empty or a valid token/quoted string https://tools.ietf.org/html/rfc7230#section-3.2 |
| | 0 | 355 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 356 | | { |
| | 0 | 357 | | return; |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | // Trailing/leading space are not allowed |
| | 0 | 361 | | if (value.StartsWith(' ') || value.StartsWith('\t') || value.EndsWith(' ') || value.EndsWith('\t')) |
| | 0 | 362 | | { |
| | 0 | 363 | | ThrowFormatException(value); |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | if (value[0] == '"') |
| | 0 | 367 | | { |
| | 0 | 368 | | HttpParseResult parseResult = HttpRuleParser.GetQuotedStringLength(value, 0, out int valueLength); |
| | 0 | 369 | | if (parseResult != HttpParseResult.Parsed || valueLength != value.Length) |
| | 0 | 370 | | { |
| | 0 | 371 | | ThrowFormatException(value); |
| | 0 | 372 | | } |
| | 0 | 373 | | } |
| | 0 | 374 | | else if (HttpRuleParser.ContainsNewLineOrNull(value)) |
| | 0 | 375 | | { |
| | 0 | 376 | | ThrowFormatException(value); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | static void ThrowFormatException(string value) => |
| | 0 | 380 | | throw new FormatException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_heade |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | private static NameValueHeaderValue CreateNameValue() |
| | 476944 | 384 | | { |
| | 476944 | 385 | | return new NameValueHeaderValue(); |
| | 476944 | 386 | | } |
| | | 387 | | |
| | | 388 | | // Implement ICloneable explicitly to allow derived types to "override" the implementation. |
| | | 389 | | object ICloneable.Clone() |
| | 0 | 390 | | { |
| | 0 | 391 | | return new NameValueHeaderValue(this); |
| | 0 | 392 | | } |
| | | 393 | | } |
| | | 394 | | } |