| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/Headers/src |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics.Contracts; |
| | 9 | | using System.Globalization; |
| | 10 | | using System.Text; |
| | 11 | |
|
| | 12 | | #pragma warning disable IDE0008 // Use explicit type |
| | 13 | | #pragma warning disable IDE0017 // Simplify object initialization |
| | 14 | | #pragma warning disable IDE0018 // Inline declaration |
| | 15 | | #pragma warning disable IDE0019 // Use pattern matching |
| | 16 | | #pragma warning disable IDE0032 // Use auto property |
| | 17 | | #pragma warning disable IDE0051 // Private is unused |
| | 18 | | #pragma warning disable IDE0054 // Use compound assignment |
| | 19 | | #pragma warning disable IDE0059 // Unnecessary assignment |
| | 20 | | #pragma warning disable IDE1006 // Missing s_ prefix |
| | 21 | |
|
| | 22 | | namespace Azure.Core.Http.Multipart |
| | 23 | | { |
| | 24 | | // According to the RFC, in places where a "parameter" is required, the value is mandatory |
| | 25 | | // (e.g. Media-Type, Accept). However, we don't introduce a dedicated type for it. So NameValueHeaderValue supports |
| | 26 | | // name-only values in addition to name/value pairs. |
| | 27 | | internal class NameValueHeaderValue |
| | 28 | | { |
| 0 | 29 | | private static readonly HttpHeaderParser<NameValueHeaderValue> SingleValueParser |
| 0 | 30 | | = new GenericHeaderParser<NameValueHeaderValue>(false, GetNameValueLength); |
| 0 | 31 | | internal static readonly HttpHeaderParser<NameValueHeaderValue> MultipleValueParser |
| 0 | 32 | | = new GenericHeaderParser<NameValueHeaderValue>(true, GetNameValueLength); |
| | 33 | |
|
| | 34 | | private StringSegment _name; |
| | 35 | | private StringSegment _value; |
| | 36 | | private bool _isReadOnly; |
| | 37 | |
|
| 0 | 38 | | private NameValueHeaderValue() |
| | 39 | | { |
| | 40 | | // Used by the parser to create a new instance of this type. |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public NameValueHeaderValue(StringSegment name) |
| 0 | 44 | | : this(name, null) |
| | 45 | | { |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | public NameValueHeaderValue(StringSegment name, StringSegment value) |
| | 49 | | { |
| 0 | 50 | | CheckNameValueFormat(name, value); |
| | 51 | |
|
| 0 | 52 | | _name = name; |
| 0 | 53 | | _value = value; |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public StringSegment Name |
| | 57 | | { |
| 0 | 58 | | get { return _name; } |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public StringSegment Value |
| | 62 | | { |
| 0 | 63 | | get { return _value; } |
| | 64 | | set |
| | 65 | | { |
| 0 | 66 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| 0 | 67 | | CheckValueFormat(value); |
| 0 | 68 | | _value = value; |
| 0 | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | public bool IsReadOnly { get { return _isReadOnly; } } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Provides a copy of this object without the cost of re-validating the values. |
| | 76 | | /// </summary> |
| | 77 | | /// <returns>A copy.</returns> |
| | 78 | | public NameValueHeaderValue Copy() |
| | 79 | | { |
| 0 | 80 | | return new NameValueHeaderValue() |
| 0 | 81 | | { |
| 0 | 82 | | _name = _name, |
| 0 | 83 | | _value = _value |
| 0 | 84 | | }; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public NameValueHeaderValue CopyAsReadOnly() |
| | 88 | | { |
| 0 | 89 | | if (IsReadOnly) |
| | 90 | | { |
| 0 | 91 | | return this; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | return new NameValueHeaderValue() |
| 0 | 95 | | { |
| 0 | 96 | | _name = _name, |
| 0 | 97 | | _value = _value, |
| 0 | 98 | | _isReadOnly = true |
| 0 | 99 | | }; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | public override int GetHashCode() |
| | 103 | | { |
| | 104 | | Contract.Assert(_name != null); |
| | 105 | |
|
| 0 | 106 | | var nameHashCode = StringSegmentComparer.OrdinalIgnoreCase.GetHashCode(_name); |
| | 107 | |
|
| 0 | 108 | | if (!StringSegment.IsNullOrEmpty(_value)) |
| | 109 | | { |
| | 110 | | // If we have a quoted-string, then just use the hash code. If we have a token, convert to lowercase |
| | 111 | | // and retrieve the hash code. |
| 0 | 112 | | if (_value[0] == '"') |
| | 113 | | { |
| 0 | 114 | | return nameHashCode ^ _value.GetHashCode(); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | return nameHashCode ^ StringSegmentComparer.OrdinalIgnoreCase.GetHashCode(_value); |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | return nameHashCode; |
| | 121 | | } |
| | 122 | |
|
| | 123 | | public override bool Equals(object obj) |
| | 124 | | { |
| 0 | 125 | | var other = obj as NameValueHeaderValue; |
| | 126 | |
|
| 0 | 127 | | if (other == null) |
| | 128 | | { |
| 0 | 129 | | return false; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | if (!_name.Equals(other._name, StringComparison.OrdinalIgnoreCase)) |
| | 133 | | { |
| 0 | 134 | | return false; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | // RFC2616: 14.20: unquoted tokens should use case-INsensitive comparison; quoted-strings should use |
| | 138 | | // case-sensitive comparison. The RFC doesn't mention how to compare quoted-strings outside the "Expect" |
| | 139 | | // header. We treat all quoted-strings the same: case-sensitive comparison. |
| | 140 | |
|
| 0 | 141 | | if (StringSegment.IsNullOrEmpty(_value)) |
| | 142 | | { |
| 0 | 143 | | return StringSegment.IsNullOrEmpty(other._value); |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (_value[0] == '"') |
| | 147 | | { |
| | 148 | | // We have a quoted string, so we need to do case-sensitive comparison. |
| 0 | 149 | | return (_value.Equals(other._value, StringComparison.Ordinal)); |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 0 | 153 | | return (_value.Equals(other._value, StringComparison.OrdinalIgnoreCase)); |
| | 154 | | } |
| | 155 | | } |
| | 156 | |
|
| | 157 | | /* |
| | 158 | | public StringSegment GetUnescapedValue() |
| | 159 | | { |
| | 160 | | if (!HeaderUtilities.IsQuoted(_value)) |
| | 161 | | { |
| | 162 | | return _value; |
| | 163 | | } |
| | 164 | | return HeaderUtilities.UnescapeAsQuotedString(_value); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetAndEscapeValue(StringSegment value) |
| | 168 | | { |
| | 169 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| | 170 | | if (StringSegment.IsNullOrEmpty(value) || (GetValueLength(value, 0) == value.Length)) |
| | 171 | | { |
| | 172 | | _value = value; |
| | 173 | | } |
| | 174 | | else |
| | 175 | | { |
| | 176 | | Value = HeaderUtilities.EscapeAsQuotedString(value); |
| | 177 | | } |
| | 178 | | } |
| | 179 | | */ |
| | 180 | |
|
| | 181 | | public static NameValueHeaderValue Parse(StringSegment input) |
| | 182 | | { |
| 0 | 183 | | var index = 0; |
| 0 | 184 | | return SingleValueParser.ParseValue(input, ref index); |
| | 185 | | } |
| | 186 | |
|
| | 187 | | public static bool TryParse(StringSegment input, out NameValueHeaderValue parsedValue) |
| | 188 | | { |
| 0 | 189 | | var index = 0; |
| 0 | 190 | | return SingleValueParser.TryParseValue(input, ref index, out parsedValue); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | public static IList<NameValueHeaderValue> ParseList(IList<string> input) |
| | 194 | | { |
| 0 | 195 | | return MultipleValueParser.ParseValues(input); |
| | 196 | | } |
| | 197 | |
|
| | 198 | | public static IList<NameValueHeaderValue> ParseStrictList(IList<string> input) |
| | 199 | | { |
| 0 | 200 | | return MultipleValueParser.ParseStrictValues(input); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | public static bool TryParseList(IList<string> input, out IList<NameValueHeaderValue> parsedValues) |
| | 204 | | { |
| 0 | 205 | | return MultipleValueParser.TryParseValues(input, out parsedValues); |
| | 206 | | } |
| | 207 | |
|
| | 208 | | public static bool TryParseStrictList(IList<string> input, out IList<NameValueHeaderValue> parsedValues) |
| | 209 | | { |
| 0 | 210 | | return MultipleValueParser.TryParseStrictValues(input, out parsedValues); |
| | 211 | | } |
| | 212 | |
|
| | 213 | | public override string ToString() |
| | 214 | | { |
| 0 | 215 | | if (!StringSegment.IsNullOrEmpty(_value)) |
| | 216 | | { |
| 0 | 217 | | return _name + "=" + _value; |
| | 218 | | } |
| 0 | 219 | | return _name.ToString(); |
| | 220 | | } |
| | 221 | |
|
| | 222 | | internal static void ToString( |
| | 223 | | IList<NameValueHeaderValue> values, |
| | 224 | | char separator, |
| | 225 | | bool leadingSeparator, |
| | 226 | | StringBuilder destination) |
| | 227 | | { |
| | 228 | | Contract.Assert(destination != null); |
| | 229 | |
|
| 0 | 230 | | if ((values == null) || (values.Count == 0)) |
| | 231 | | { |
| 0 | 232 | | return; |
| | 233 | | } |
| | 234 | |
|
| 0 | 235 | | for (var i = 0; i < values.Count; i++) |
| | 236 | | { |
| 0 | 237 | | if (leadingSeparator || (destination.Length > 0)) |
| | 238 | | { |
| 0 | 239 | | destination.Append(separator); |
| 0 | 240 | | destination.Append(' '); |
| | 241 | | } |
| 0 | 242 | | destination.Append(values[i].Name.AsSpan()); |
| 0 | 243 | | if (!StringSegment.IsNullOrEmpty(values[i].Value)) |
| | 244 | | { |
| 0 | 245 | | destination.Append('='); |
| 0 | 246 | | destination.Append(values[i].Value.AsSpan()); |
| | 247 | | } |
| | 248 | | } |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | internal static string ToString(IList<NameValueHeaderValue> values, char separator, bool leadingSeparator) |
| | 252 | | { |
| 0 | 253 | | if ((values == null) || (values.Count == 0)) |
| | 254 | | { |
| 0 | 255 | | return null; |
| | 256 | | } |
| | 257 | |
|
| 0 | 258 | | var sb = new StringBuilder(); |
| | 259 | |
|
| 0 | 260 | | ToString(values, separator, leadingSeparator, sb); |
| | 261 | |
|
| 0 | 262 | | return sb.ToString(); |
| | 263 | | } |
| | 264 | |
|
| | 265 | | internal static int GetHashCode(IList<NameValueHeaderValue> values) |
| | 266 | | { |
| 0 | 267 | | if ((values == null) || (values.Count == 0)) |
| | 268 | | { |
| 0 | 269 | | return 0; |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | var result = 0; |
| 0 | 273 | | for (var i = 0; i < values.Count; i++) |
| | 274 | | { |
| 0 | 275 | | result = result ^ values[i].GetHashCode(); |
| | 276 | | } |
| 0 | 277 | | return result; |
| | 278 | | } |
| | 279 | |
|
| | 280 | | private static int GetNameValueLength(StringSegment input, int startIndex, out NameValueHeaderValue parsedValue) |
| | 281 | | { |
| | 282 | | Contract.Requires(input != null); |
| | 283 | | Contract.Requires(startIndex >= 0); |
| | 284 | |
|
| 0 | 285 | | parsedValue = null; |
| | 286 | |
|
| 0 | 287 | | if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 288 | | { |
| 0 | 289 | | return 0; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | // Parse the name, i.e. <name> in name/value string "<name>=<value>". Caller must remove |
| | 293 | | // leading whitespaces. |
| 0 | 294 | | var nameLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | 295 | |
|
| 0 | 296 | | if (nameLength == 0) |
| | 297 | | { |
| 0 | 298 | | return 0; |
| | 299 | | } |
| | 300 | |
|
| 0 | 301 | | var name = input.Subsegment(startIndex, nameLength); |
| 0 | 302 | | var current = startIndex + nameLength; |
| 0 | 303 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 304 | |
|
| | 305 | | // Parse the separator between name and value |
| 0 | 306 | | if ((current == input.Length) || (input[current] != '=')) |
| | 307 | | { |
| | 308 | | // We only have a name and that's OK. Return. |
| 0 | 309 | | parsedValue = new NameValueHeaderValue(); |
| 0 | 310 | | parsedValue._name = name; |
| 0 | 311 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces |
| 0 | 312 | | return current - startIndex; |
| | 313 | | } |
| | 314 | |
|
| 0 | 315 | | current++; // skip delimiter. |
| 0 | 316 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 317 | |
|
| | 318 | | // Parse the value, i.e. <value> in name/value string "<name>=<value>" |
| 0 | 319 | | int valueLength = GetValueLength(input, current); |
| | 320 | |
|
| | 321 | | // Value after the '=' may be empty |
| | 322 | | // Use parameterless ctor to avoid double-parsing of name and value, i.e. skip public ctor validation. |
| 0 | 323 | | parsedValue = new NameValueHeaderValue(); |
| 0 | 324 | | parsedValue._name = name; |
| 0 | 325 | | parsedValue._value = input.Subsegment(current, valueLength); |
| 0 | 326 | | current = current + valueLength; |
| 0 | 327 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespaces |
| 0 | 328 | | return current - startIndex; |
| | 329 | | } |
| | 330 | |
|
| | 331 | | // Returns the length of a name/value list, separated by 'delimiter'. E.g. "a=b, c=d, e=f" adds 3 |
| | 332 | | // name/value pairs to 'nameValueCollection' if 'delimiter' equals ','. |
| | 333 | | internal static int GetNameValueListLength( |
| | 334 | | StringSegment input, |
| | 335 | | int startIndex, |
| | 336 | | char delimiter, |
| | 337 | | IList<NameValueHeaderValue> nameValueCollection) |
| | 338 | | { |
| | 339 | | Contract.Requires(nameValueCollection != null); |
| | 340 | | Contract.Requires(startIndex >= 0); |
| | 341 | |
|
| 0 | 342 | | if ((StringSegment.IsNullOrEmpty(input)) || (startIndex >= input.Length)) |
| | 343 | | { |
| 0 | 344 | | return 0; |
| | 345 | | } |
| | 346 | |
|
| 0 | 347 | | var current = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex); |
| 0 | 348 | | while (true) |
| | 349 | | { |
| 0 | 350 | | NameValueHeaderValue parameter = null; |
| 0 | 351 | | var nameValueLength = GetNameValueLength(input, current, out parameter); |
| | 352 | |
|
| 0 | 353 | | if (nameValueLength == 0) |
| | 354 | | { |
| | 355 | | // There may be a trailing ';' |
| 0 | 356 | | return current - startIndex; |
| | 357 | | } |
| | 358 | |
|
| 0 | 359 | | nameValueCollection.Add(parameter); |
| 0 | 360 | | current = current + nameValueLength; |
| 0 | 361 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 362 | |
|
| 0 | 363 | | if ((current == input.Length) || (input[current] != delimiter)) |
| | 364 | | { |
| | 365 | | // We're done and we have at least one valid name/value pair. |
| 0 | 366 | | return current - startIndex; |
| | 367 | | } |
| | 368 | |
|
| | 369 | | // input[current] is 'delimiter'. Skip the delimiter and whitespaces and try to parse again. |
| 0 | 370 | | current++; // skip delimiter. |
| 0 | 371 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 372 | | } |
| | 373 | | } |
| | 374 | |
|
| | 375 | | public static NameValueHeaderValue Find(IList<NameValueHeaderValue> values, StringSegment name) |
| | 376 | | { |
| | 377 | | Contract.Requires((name != null) && (name.Length > 0)); |
| | 378 | |
|
| 0 | 379 | | if ((values == null) || (values.Count == 0)) |
| | 380 | | { |
| 0 | 381 | | return null; |
| | 382 | | } |
| | 383 | |
|
| 0 | 384 | | for (var i = 0; i < values.Count; i++) |
| | 385 | | { |
| 0 | 386 | | var value = values[i]; |
| 0 | 387 | | if (value.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) |
| | 388 | | { |
| 0 | 389 | | return value; |
| | 390 | | } |
| | 391 | | } |
| 0 | 392 | | return null; |
| | 393 | | } |
| | 394 | |
|
| | 395 | | internal static int GetValueLength(StringSegment input, int startIndex) |
| | 396 | | { |
| | 397 | | Contract.Requires(input != null); |
| | 398 | |
|
| 0 | 399 | | if (startIndex >= input.Length) |
| | 400 | | { |
| 0 | 401 | | return 0; |
| | 402 | | } |
| | 403 | |
|
| 0 | 404 | | var valueLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | 405 | |
|
| 0 | 406 | | if (valueLength == 0) |
| | 407 | | { |
| | 408 | | // A value can either be a token or a quoted string. Check if it is a quoted string. |
| 0 | 409 | | if (HttpRuleParser.GetQuotedStringLength(input, startIndex, out valueLength) != HttpParseResult.Parsed) |
| | 410 | | { |
| | 411 | | // We have an invalid value. Reset the name and return. |
| 0 | 412 | | return 0; |
| | 413 | | } |
| | 414 | | } |
| 0 | 415 | | return valueLength; |
| | 416 | | } |
| | 417 | |
|
| | 418 | | private static void CheckNameValueFormat(StringSegment name, StringSegment value) |
| | 419 | | { |
| 0 | 420 | | HeaderUtilities.CheckValidToken(name, nameof(name)); |
| 0 | 421 | | CheckValueFormat(value); |
| 0 | 422 | | } |
| | 423 | |
|
| | 424 | | private static void CheckValueFormat(StringSegment value) |
| | 425 | | { |
| | 426 | | // Either value is null/empty or a valid token/quoted string |
| 0 | 427 | | if (!(StringSegment.IsNullOrEmpty(value) || (GetValueLength(value, 0) == value.Length))) |
| | 428 | | { |
| 0 | 429 | | throw new FormatException(string.Format(CultureInfo.InvariantCulture, "The header value is invalid: '{0} |
| | 430 | | } |
| 0 | 431 | | } |
| | 432 | |
|
| | 433 | | private static NameValueHeaderValue CreateNameValue() |
| | 434 | | { |
| 0 | 435 | | return new NameValueHeaderValue(); |
| | 436 | | } |
| | 437 | | } |
| | 438 | | } |