| | 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.Linq; |
| | 11 | | using System.Text; |
| | 12 | |
|
| | 13 | | #pragma warning disable IDE0008 // Use explicit type |
| | 14 | | #pragma warning disable IDE0017 // initialization can be simplified |
| | 15 | | #pragma warning disable IDE0019 // Use pattern matching |
| | 16 | | #pragma warning disable IDE0032 // Use auto property |
| | 17 | | #pragma warning disable IDE0034 // Default expression can be simplified |
| | 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 | | /// <summary> |
| | 25 | | /// Representation of the media type header. See <see href="https://tools.ietf.org/html/rfc6838">Media Type Specific |
| | 26 | | /// </summary> |
| | 27 | | internal class MediaTypeHeaderValue |
| | 28 | | { |
| | 29 | | private const string BoundaryString = "boundary"; |
| | 30 | | private const string CharsetString = "charset"; |
| | 31 | | private const string MatchesAllString = "*/*"; |
| | 32 | | private const string QualityString = "q"; |
| | 33 | | private const string WildcardString = "*"; |
| | 34 | |
|
| | 35 | | private const char ForwardSlashCharacter = '/'; |
| | 36 | | private const char PeriodCharacter = '.'; |
| | 37 | | private const char PlusCharacter = '+'; |
| | 38 | |
|
| 0 | 39 | | private static readonly char[] PeriodCharacterArray = new char[] { PeriodCharacter }; |
| | 40 | |
|
| 0 | 41 | | private static readonly HttpHeaderParser<MediaTypeHeaderValue> SingleValueParser |
| 0 | 42 | | = new GenericHeaderParser<MediaTypeHeaderValue>(false, GetMediaTypeLength); |
| 0 | 43 | | private static readonly HttpHeaderParser<MediaTypeHeaderValue> MultipleValueParser |
| 0 | 44 | | = new GenericHeaderParser<MediaTypeHeaderValue>(true, GetMediaTypeLength); |
| | 45 | |
|
| | 46 | | // Use a collection instead of a dictionary since we may have multiple parameters with the same name. |
| | 47 | | private ObjectCollection<NameValueHeaderValue> _parameters; |
| | 48 | | private StringSegment _mediaType; |
| | 49 | | private bool _isReadOnly; |
| | 50 | |
|
| 0 | 51 | | private MediaTypeHeaderValue() |
| | 52 | | { |
| | 53 | | // Used by the parser to create a new instance of this type. |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Initializes a <see cref="MediaTypeHeaderValue"/> instance. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="mediaType">A <see cref="StringSegment"/> representation of a media type. |
| | 60 | | /// The text provided must be a single media type without parameters. </param> |
| 0 | 61 | | public MediaTypeHeaderValue(StringSegment mediaType) |
| | 62 | | { |
| 0 | 63 | | CheckMediaTypeFormat(mediaType, nameof(mediaType)); |
| 0 | 64 | | _mediaType = mediaType; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Initializes a <see cref="MediaTypeHeaderValue"/> instance. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="mediaType">A <see cref="StringSegment"/> representation of a media type. |
| | 71 | | /// The text provided must be a single media type without parameters. </param> |
| | 72 | | /// <param name="quality">The <see cref="double"/> with the quality of the media type.</param> |
| | 73 | | public MediaTypeHeaderValue(StringSegment mediaType, double quality) |
| 0 | 74 | | : this(mediaType) |
| | 75 | | { |
| 0 | 76 | | Quality = quality; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Gets or sets the value of the charset parameter. Returns <see cref="StringSegment.Empty"/> |
| | 81 | | /// if there is no charset. |
| | 82 | | /// </summary> |
| | 83 | | public StringSegment Charset |
| | 84 | | { |
| | 85 | | get |
| | 86 | | { |
| 0 | 87 | | return NameValueHeaderValue.Find(_parameters, CharsetString)?.Value.Value; |
| | 88 | | } |
| | 89 | | set |
| | 90 | | { |
| 0 | 91 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| | 92 | | // We don't prevent a user from setting whitespace-only charsets. Like we can't prevent a user from |
| | 93 | | // setting a non-existing charset. |
| 0 | 94 | | var charsetParameter = NameValueHeaderValue.Find(_parameters, CharsetString); |
| 0 | 95 | | if (StringSegment.IsNullOrEmpty(value)) |
| | 96 | | { |
| | 97 | | // Remove charset parameter |
| 0 | 98 | | if (charsetParameter != null) |
| | 99 | | { |
| 0 | 100 | | Parameters.Remove(charsetParameter); |
| | 101 | | } |
| | 102 | | } |
| | 103 | | else |
| | 104 | | { |
| 0 | 105 | | if (charsetParameter != null) |
| | 106 | | { |
| 0 | 107 | | charsetParameter.Value = value; |
| | 108 | | } |
| | 109 | | else |
| | 110 | | { |
| 0 | 111 | | Parameters.Add(new NameValueHeaderValue(CharsetString, value)); |
| | 112 | | } |
| | 113 | | } |
| 0 | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Gets or sets the value of the Encoding parameter. Setting the Encoding will set |
| | 119 | | /// the <see cref="Charset"/> to <see cref="Encoding.WebName"/>. |
| | 120 | | /// </summary> |
| | 121 | | public Encoding Encoding |
| | 122 | | { |
| | 123 | | get |
| | 124 | | { |
| 0 | 125 | | var charset = Charset; |
| 0 | 126 | | if (!StringSegment.IsNullOrEmpty(charset)) |
| | 127 | | { |
| | 128 | | try |
| | 129 | | { |
| 0 | 130 | | return Encoding.GetEncoding(charset.Value); |
| | 131 | | } |
| 0 | 132 | | catch (ArgumentException) |
| | 133 | | { |
| | 134 | | // Invalid or not supported |
| 0 | 135 | | } |
| | 136 | | } |
| 0 | 137 | | return null; |
| 0 | 138 | | } |
| | 139 | | set |
| | 140 | | { |
| 0 | 141 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| 0 | 142 | | if (value == null) |
| | 143 | | { |
| 0 | 144 | | Charset = null; |
| | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 0 | 148 | | Charset = value.WebName; |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Gets or sets the value of the boundary parameter. Returns <see cref="StringSegment.Empty"/> |
| | 155 | | /// if there is no boundary. |
| | 156 | | /// </summary> |
| | 157 | | public StringSegment Boundary |
| | 158 | | { |
| | 159 | | get |
| | 160 | | { |
| 0 | 161 | | return NameValueHeaderValue.Find(_parameters, BoundaryString)?.Value ?? default(StringSegment); |
| | 162 | | } |
| | 163 | | set |
| | 164 | | { |
| 0 | 165 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| 0 | 166 | | var boundaryParameter = NameValueHeaderValue.Find(_parameters, BoundaryString); |
| 0 | 167 | | if (StringSegment.IsNullOrEmpty(value)) |
| | 168 | | { |
| | 169 | | // Remove charset parameter |
| 0 | 170 | | if (boundaryParameter != null) |
| | 171 | | { |
| 0 | 172 | | Parameters.Remove(boundaryParameter); |
| | 173 | | } |
| | 174 | | } |
| | 175 | | else |
| | 176 | | { |
| 0 | 177 | | if (boundaryParameter != null) |
| | 178 | | { |
| 0 | 179 | | boundaryParameter.Value = value; |
| | 180 | | } |
| | 181 | | else |
| | 182 | | { |
| 0 | 183 | | Parameters.Add(new NameValueHeaderValue(BoundaryString, value)); |
| | 184 | | } |
| | 185 | | } |
| 0 | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// Gets or sets the media type's parameters. Returns an empty <see cref="IList{T}"/> |
| | 191 | | /// if there are no parameters. |
| | 192 | | /// </summary> |
| | 193 | | public IList<NameValueHeaderValue> Parameters |
| | 194 | | { |
| | 195 | | get |
| | 196 | | { |
| 0 | 197 | | if (_parameters == null) |
| | 198 | | { |
| 0 | 199 | | if (IsReadOnly) |
| | 200 | | { |
| 0 | 201 | | _parameters = ObjectCollection<NameValueHeaderValue>.EmptyReadOnlyCollection; |
| | 202 | | } |
| | 203 | | else |
| | 204 | | { |
| 0 | 205 | | _parameters = new ObjectCollection<NameValueHeaderValue>(); |
| | 206 | | } |
| | 207 | | } |
| 0 | 208 | | return _parameters; |
| | 209 | | } |
| | 210 | | } |
| | 211 | |
|
| | 212 | | /// <summary> |
| | 213 | | /// Gets or sets the value of the quality parameter. Returns null |
| | 214 | | /// if there is no quality. |
| | 215 | | /// </summary> |
| | 216 | | public double? Quality |
| | 217 | | { |
| 0 | 218 | | get { return HeaderUtilities.GetQuality(_parameters); } |
| | 219 | | set |
| | 220 | | { |
| 0 | 221 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| 0 | 222 | | HeaderUtilities.SetQuality(Parameters, value); |
| 0 | 223 | | } |
| | 224 | | } |
| | 225 | |
|
| | 226 | | /// <summary> |
| | 227 | | /// Gets or sets the value of the media type. Returns <see cref="StringSegment.Empty"/> |
| | 228 | | /// if there is no media type. |
| | 229 | | /// </summary> |
| | 230 | | /// <example> |
| | 231 | | /// For the media type <c>"application/json"</c>, the property gives the value |
| | 232 | | /// <c>"application/json"</c>. |
| | 233 | | /// </example> |
| | 234 | | public StringSegment MediaType |
| | 235 | | { |
| 0 | 236 | | get { return _mediaType; } |
| | 237 | | set |
| | 238 | | { |
| 0 | 239 | | HeaderUtilities.ThrowIfReadOnly(IsReadOnly); |
| 0 | 240 | | CheckMediaTypeFormat(value, nameof(value)); |
| 0 | 241 | | _mediaType = value; |
| 0 | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| | 245 | | /// <summary> |
| | 246 | | /// Gets the type of the <see cref="MediaTypeHeaderValue"/>. |
| | 247 | | /// </summary> |
| | 248 | | /// <example> |
| | 249 | | /// For the media type <c>"application/json"</c>, the property gives the value <c>"application"</c>. |
| | 250 | | /// </example> |
| | 251 | | /// <remarks>See <see href="https://tools.ietf.org/html/rfc6838#section-4.2">Naming Requirements</see> for more |
| | 252 | | public StringSegment Type |
| | 253 | | { |
| | 254 | | get |
| | 255 | | { |
| 0 | 256 | | return _mediaType.Subsegment(0, _mediaType.IndexOf(ForwardSlashCharacter)); |
| | 257 | | } |
| | 258 | | } |
| | 259 | |
|
| | 260 | | /// <summary> |
| | 261 | | /// Gets the subtype of the <see cref="MediaTypeHeaderValue"/>. |
| | 262 | | /// </summary> |
| | 263 | | /// <example> |
| | 264 | | /// For the media type <c>"application/vnd.example+json"</c>, the property gives the value |
| | 265 | | /// <c>"vnd.example+json"</c>. |
| | 266 | | /// </example> |
| | 267 | | /// <remarks>See <see href="https://tools.ietf.org/html/rfc6838#section-4.2">Naming Requirements</see> for more |
| | 268 | | public StringSegment SubType |
| | 269 | | { |
| | 270 | | get |
| | 271 | | { |
| 0 | 272 | | return _mediaType.Subsegment(_mediaType.IndexOf(ForwardSlashCharacter) + 1); |
| | 273 | | } |
| | 274 | | } |
| | 275 | |
|
| | 276 | | /// <summary> |
| | 277 | | /// Gets subtype of the <see cref="MediaTypeHeaderValue"/>, excluding any structured syntax suffix. Returns <see |
| | 278 | | /// if there is no subtype without suffix. |
| | 279 | | /// </summary> |
| | 280 | | /// <example> |
| | 281 | | /// For the media type <c>"application/vnd.example+json"</c>, the property gives the value |
| | 282 | | /// <c>"vnd.example"</c>. |
| | 283 | | /// </example> |
| | 284 | | public StringSegment SubTypeWithoutSuffix |
| | 285 | | { |
| | 286 | | get |
| | 287 | | { |
| 0 | 288 | | var subType = SubType; |
| 0 | 289 | | var startOfSuffix = subType.LastIndexOf(PlusCharacter); |
| 0 | 290 | | if (startOfSuffix == -1) |
| | 291 | | { |
| 0 | 292 | | return subType; |
| | 293 | | } |
| | 294 | | else |
| | 295 | | { |
| 0 | 296 | | return subType.Subsegment(0, startOfSuffix); |
| | 297 | | } |
| | 298 | | } |
| | 299 | | } |
| | 300 | |
|
| | 301 | | /// <summary> |
| | 302 | | /// Gets the structured syntax suffix of the <see cref="MediaTypeHeaderValue"/> if it has one. |
| | 303 | | /// See <see href="https://tools.ietf.org/html/rfc6838#section-4.8">The RFC documentation on structured syntaxes |
| | 304 | | /// </summary> |
| | 305 | | /// <example> |
| | 306 | | /// For the media type <c>"application/vnd.example+json"</c>, the property gives the value |
| | 307 | | /// <c>"json"</c>. |
| | 308 | | /// </example> |
| | 309 | | public StringSegment Suffix |
| | 310 | | { |
| | 311 | | get |
| | 312 | | { |
| 0 | 313 | | var subType = SubType; |
| 0 | 314 | | var startOfSuffix = subType.LastIndexOf(PlusCharacter); |
| 0 | 315 | | if (startOfSuffix == -1) |
| | 316 | | { |
| 0 | 317 | | return default(StringSegment); |
| | 318 | | } |
| | 319 | | else |
| | 320 | | { |
| 0 | 321 | | return subType.Subsegment(startOfSuffix + 1); |
| | 322 | | } |
| | 323 | | } |
| | 324 | | } |
| | 325 | |
|
| | 326 | | /// <summary> |
| | 327 | | /// Get a <see cref="IList{T}"/> of facets of the <see cref="MediaTypeHeaderValue"/>. Facets are a |
| | 328 | | /// period separated list of StringSegments in the <see cref="SubTypeWithoutSuffix"/>. |
| | 329 | | /// See <see href="https://tools.ietf.org/html/rfc6838#section-3">The RFC documentation on facets.</see> |
| | 330 | | /// </summary> |
| | 331 | | /// <example> |
| | 332 | | /// For the media type <c>"application/vnd.example+json"</c>, the property gives the value: |
| | 333 | | /// <c>{"vnd", "example"}</c> |
| | 334 | | /// </example> |
| | 335 | | public IEnumerable<StringSegment> Facets |
| | 336 | | { |
| | 337 | | get |
| | 338 | | { |
| 0 | 339 | | return SubTypeWithoutSuffix.Split(PeriodCharacterArray); |
| | 340 | | } |
| | 341 | | } |
| | 342 | |
|
| | 343 | | /// <summary> |
| | 344 | | /// Gets whether this <see cref="MediaTypeHeaderValue"/> matches all types. |
| | 345 | | /// </summary> |
| 0 | 346 | | public bool MatchesAllTypes => MediaType.Equals(MatchesAllString, StringComparison.Ordinal); |
| | 347 | |
|
| | 348 | | /// <summary> |
| | 349 | | /// Gets whether this <see cref="MediaTypeHeaderValue"/> matches all subtypes. |
| | 350 | | /// </summary> |
| | 351 | | /// <example> |
| | 352 | | /// For the media type <c>"application/*"</c>, this property is <c>true</c>. |
| | 353 | | /// </example> |
| | 354 | | /// <example> |
| | 355 | | /// For the media type <c>"application/json"</c>, this property is <c>false</c>. |
| | 356 | | /// </example> |
| 0 | 357 | | public bool MatchesAllSubTypes => SubType.Equals(WildcardString, StringComparison.Ordinal); |
| | 358 | |
|
| | 359 | | /// <summary> |
| | 360 | | /// Gets whether this <see cref="MediaTypeHeaderValue"/> matches all subtypes, ignoring any structured syntax su |
| | 361 | | /// </summary> |
| | 362 | | /// <example> |
| | 363 | | /// For the media type <c>"application/*+json"</c>, this property is <c>true</c>. |
| | 364 | | /// </example> |
| | 365 | | /// <example> |
| | 366 | | /// For the media type <c>"application/vnd.example+json"</c>, this property is <c>false</c>. |
| | 367 | | /// </example> |
| | 368 | | public bool MatchesAllSubTypesWithoutSuffix => |
| 0 | 369 | | SubTypeWithoutSuffix.Equals(WildcardString, StringComparison.OrdinalIgnoreCase); |
| | 370 | |
|
| | 371 | | /// <summary> |
| | 372 | | /// Gets whether the <see cref="MediaTypeHeaderValue"/> is readonly. |
| | 373 | | /// </summary> |
| | 374 | | public bool IsReadOnly |
| | 375 | | { |
| 0 | 376 | | get { return _isReadOnly; } |
| | 377 | | } |
| | 378 | |
|
| | 379 | | /// <summary> |
| | 380 | | /// Gets a value indicating whether this <see cref="MediaTypeHeaderValue"/> is a subset of |
| | 381 | | /// <paramref name="otherMediaType"/>. A "subset" is defined as the same or a more specific media type |
| | 382 | | /// according to the precedence described in https://www.ietf.org/rfc/rfc2068.txt section 14.1, Accept. |
| | 383 | | /// </summary> |
| | 384 | | /// <param name="otherMediaType">The <see cref="MediaTypeHeaderValue"/> to compare.</param> |
| | 385 | | /// <returns> |
| | 386 | | /// A value indicating whether this <see cref="MediaTypeHeaderValue"/> is a subset of |
| | 387 | | /// <paramref name="otherMediaType"/>. |
| | 388 | | /// </returns> |
| | 389 | | /// <remarks> |
| | 390 | | /// For example "multipart/mixed; boundary=1234" is a subset of "multipart/mixed; boundary=1234", |
| | 391 | | /// "multipart/mixed", "multipart/*", and "*/*" but not "multipart/mixed; boundary=2345" or |
| | 392 | | /// "multipart/message; boundary=1234". |
| | 393 | | /// </remarks> |
| | 394 | | public bool IsSubsetOf(MediaTypeHeaderValue otherMediaType) |
| | 395 | | { |
| 0 | 396 | | if (otherMediaType == null) |
| | 397 | | { |
| 0 | 398 | | return false; |
| | 399 | | } |
| | 400 | |
|
| | 401 | | // "text/plain" is a subset of "text/plain", "text/*" and "*/*". "*/*" is a subset only of "*/*". |
| 0 | 402 | | return MatchesType(otherMediaType) && |
| 0 | 403 | | MatchesSubtype(otherMediaType) && |
| 0 | 404 | | MatchesParameters(otherMediaType); |
| | 405 | | } |
| | 406 | |
|
| | 407 | | /// <summary> |
| | 408 | | /// Performs a deep copy of this object and all of it's NameValueHeaderValue sub components, |
| | 409 | | /// while avoiding the cost of re-validating the components. |
| | 410 | | /// </summary> |
| | 411 | | /// <returns>A deep copy.</returns> |
| | 412 | | public MediaTypeHeaderValue Copy() |
| | 413 | | { |
| 0 | 414 | | var other = new MediaTypeHeaderValue(); |
| 0 | 415 | | other._mediaType = _mediaType; |
| | 416 | |
|
| 0 | 417 | | if (_parameters != null) |
| | 418 | | { |
| 0 | 419 | | other._parameters = new ObjectCollection<NameValueHeaderValue>( |
| 0 | 420 | | _parameters.Select(item => item.Copy())); |
| | 421 | | } |
| 0 | 422 | | return other; |
| | 423 | | } |
| | 424 | |
|
| | 425 | | /// <summary> |
| | 426 | | /// Performs a deep copy of this object and all of it's NameValueHeaderValue sub components, |
| | 427 | | /// while avoiding the cost of re-validating the components. This copy is read-only. |
| | 428 | | /// </summary> |
| | 429 | | /// <returns>A deep, read-only, copy.</returns> |
| | 430 | | public MediaTypeHeaderValue CopyAsReadOnly() |
| | 431 | | { |
| 0 | 432 | | if (IsReadOnly) |
| | 433 | | { |
| 0 | 434 | | return this; |
| | 435 | | } |
| | 436 | |
|
| 0 | 437 | | var other = new MediaTypeHeaderValue(); |
| 0 | 438 | | other._mediaType = _mediaType; |
| 0 | 439 | | if (_parameters != null) |
| | 440 | | { |
| 0 | 441 | | other._parameters = new ObjectCollection<NameValueHeaderValue>( |
| 0 | 442 | | _parameters.Select(item => item.CopyAsReadOnly()), isReadOnly: true); |
| | 443 | | } |
| 0 | 444 | | other._isReadOnly = true; |
| 0 | 445 | | return other; |
| | 446 | | } |
| | 447 | |
|
| | 448 | | public override string ToString() |
| | 449 | | { |
| 0 | 450 | | var builder = new StringBuilder(); |
| 0 | 451 | | builder.Append(_mediaType.AsSpan()); |
| 0 | 452 | | NameValueHeaderValue.ToString(_parameters, separator: ';', leadingSeparator: true, destination: builder); |
| 0 | 453 | | return builder.ToString(); |
| | 454 | | } |
| | 455 | |
|
| | 456 | | public override bool Equals(object obj) |
| | 457 | | { |
| 0 | 458 | | var other = obj as MediaTypeHeaderValue; |
| | 459 | |
|
| 0 | 460 | | if (other == null) |
| | 461 | | { |
| 0 | 462 | | return false; |
| | 463 | | } |
| | 464 | |
|
| 0 | 465 | | return _mediaType.Equals(other._mediaType, StringComparison.OrdinalIgnoreCase) && |
| 0 | 466 | | HeaderUtilities.AreEqualCollections(_parameters, other._parameters); |
| | 467 | | } |
| | 468 | |
|
| | 469 | | public override int GetHashCode() |
| | 470 | | { |
| | 471 | | // The media-type string is case-insensitive. |
| 0 | 472 | | return StringSegmentComparer.OrdinalIgnoreCase.GetHashCode(_mediaType) ^ NameValueHeaderValue.GetHashCode(_p |
| | 473 | | } |
| | 474 | |
|
| | 475 | | /// <summary> |
| | 476 | | /// Takes a media type and parses it into the <see cref="MediaTypeHeaderValue" /> and its associated parameters. |
| | 477 | | /// </summary> |
| | 478 | | /// <param name="input">The <see cref="StringSegment"/> with the media type.</param> |
| | 479 | | /// <returns>The parsed <see cref="MediaTypeHeaderValue"/>.</returns> |
| | 480 | | public static MediaTypeHeaderValue Parse(StringSegment input) |
| | 481 | | { |
| 0 | 482 | | var index = 0; |
| 0 | 483 | | return SingleValueParser.ParseValue(input, ref index); |
| | 484 | | } |
| | 485 | |
|
| | 486 | | /// <summary> |
| | 487 | | /// Takes a media type, which can include parameters, and parses it into the <see cref="MediaTypeHeaderValue" /> |
| | 488 | | /// </summary> |
| | 489 | | /// <param name="input">The <see cref="StringSegment"/> with the media type. The media type constructed here mus |
| | 490 | | /// <param name="parsedValue">The parsed <see cref="MediaTypeHeaderValue"/></param> |
| | 491 | | /// <returns>True if the value was successfully parsed.</returns> |
| | 492 | | public static bool TryParse(StringSegment input, out MediaTypeHeaderValue parsedValue) |
| | 493 | | { |
| 0 | 494 | | var index = 0; |
| 0 | 495 | | return SingleValueParser.TryParseValue(input, ref index, out parsedValue); |
| | 496 | | } |
| | 497 | |
|
| | 498 | | /// <summary> |
| | 499 | | /// Takes an <see cref="IList{T}"/> of <see cref="string"/> and parses it into the <see cref="MediaTypeHeaderVal |
| | 500 | | /// </summary> |
| | 501 | | /// <param name="inputs">A list of media types</param> |
| | 502 | | /// <returns>The parsed <see cref="MediaTypeHeaderValue"/>.</returns> |
| | 503 | | public static IList<MediaTypeHeaderValue> ParseList(IList<string> inputs) |
| | 504 | | { |
| 0 | 505 | | return MultipleValueParser.ParseValues(inputs); |
| | 506 | | } |
| | 507 | |
|
| | 508 | | /// <summary> |
| | 509 | | /// Takes an <see cref="IList{T}"/> of <see cref="string"/> and parses it into the <see cref="MediaTypeHeaderVal |
| | 510 | | /// Throws if there is invalid data in a string. |
| | 511 | | /// </summary> |
| | 512 | | /// <param name="inputs">A list of media types</param> |
| | 513 | | /// <returns>The parsed <see cref="MediaTypeHeaderValue"/>.</returns> |
| | 514 | | public static IList<MediaTypeHeaderValue> ParseStrictList(IList<string> inputs) |
| | 515 | | { |
| 0 | 516 | | return MultipleValueParser.ParseStrictValues(inputs); |
| | 517 | | } |
| | 518 | |
|
| | 519 | | /// <summary> |
| | 520 | | /// Takes an <see cref="IList{T}"/> of <see cref="string"/> and parses it into the <see cref="MediaTypeHeaderVal |
| | 521 | | /// </summary> |
| | 522 | | /// <param name="inputs">A list of media types</param> |
| | 523 | | /// <param name="parsedValues">The parsed <see cref="MediaTypeHeaderValue"/>.</param> |
| | 524 | | /// <returns>True if the value was successfully parsed.</returns> |
| | 525 | | public static bool TryParseList(IList<string> inputs, out IList<MediaTypeHeaderValue> parsedValues) |
| | 526 | | { |
| 0 | 527 | | return MultipleValueParser.TryParseValues(inputs, out parsedValues); |
| | 528 | | } |
| | 529 | |
|
| | 530 | | /// <summary> |
| | 531 | | /// Takes an <see cref="IList{T}"/> of <see cref="string"/> and parses it into the <see cref="MediaTypeHeaderVal |
| | 532 | | /// </summary> |
| | 533 | | /// <param name="inputs">A list of media types</param> |
| | 534 | | /// <param name="parsedValues">The parsed <see cref="MediaTypeHeaderValue"/>.</param> |
| | 535 | | /// <returns>True if the value was successfully parsed.</returns> |
| | 536 | | public static bool TryParseStrictList(IList<string> inputs, out IList<MediaTypeHeaderValue> parsedValues) |
| | 537 | | { |
| 0 | 538 | | return MultipleValueParser.TryParseStrictValues(inputs, out parsedValues); |
| | 539 | | } |
| | 540 | |
|
| | 541 | | private static int GetMediaTypeLength(StringSegment input, int startIndex, out MediaTypeHeaderValue parsedValue) |
| | 542 | | { |
| | 543 | | Contract.Requires(startIndex >= 0); |
| | 544 | |
|
| 0 | 545 | | parsedValue = null; |
| | 546 | |
|
| 0 | 547 | | if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 548 | | { |
| 0 | 549 | | return 0; |
| | 550 | | } |
| | 551 | |
|
| | 552 | | // Caller must remove leading whitespace. If not, we'll return 0. |
| 0 | 553 | | var mediaTypeLength = MediaTypeHeaderValue.GetMediaTypeExpressionLength(input, startIndex, out var mediaType |
| | 554 | |
|
| 0 | 555 | | if (mediaTypeLength == 0) |
| | 556 | | { |
| 0 | 557 | | return 0; |
| | 558 | | } |
| | 559 | |
|
| 0 | 560 | | var current = startIndex + mediaTypeLength; |
| 0 | 561 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| 0 | 562 | | MediaTypeHeaderValue mediaTypeHeader = null; |
| | 563 | |
|
| | 564 | | // If we're not done and we have a parameter delimiter, then we have a list of parameters. |
| 0 | 565 | | if ((current < input.Length) && (input[current] == ';')) |
| | 566 | | { |
| 0 | 567 | | mediaTypeHeader = new MediaTypeHeaderValue(); |
| 0 | 568 | | mediaTypeHeader._mediaType = mediaType; |
| | 569 | |
|
| 0 | 570 | | current++; // skip delimiter. |
| 0 | 571 | | var parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';', |
| 0 | 572 | | mediaTypeHeader.Parameters); |
| | 573 | |
|
| 0 | 574 | | parsedValue = mediaTypeHeader; |
| 0 | 575 | | return current + parameterLength - startIndex; |
| | 576 | | } |
| | 577 | |
|
| | 578 | | // We have a media type without parameters. |
| 0 | 579 | | mediaTypeHeader = new MediaTypeHeaderValue(); |
| 0 | 580 | | mediaTypeHeader._mediaType = mediaType; |
| 0 | 581 | | parsedValue = mediaTypeHeader; |
| 0 | 582 | | return current - startIndex; |
| | 583 | | } |
| | 584 | |
|
| | 585 | | private static int GetMediaTypeExpressionLength(StringSegment input, int startIndex, out StringSegment mediaType |
| | 586 | | { |
| | 587 | | Contract.Requires((input != null) && (input.Length > 0) && (startIndex < input.Length)); |
| | 588 | |
|
| | 589 | | // This method just parses the "type/subtype" string, it does not parse parameters. |
| 0 | 590 | | mediaType = null; |
| | 591 | |
|
| | 592 | | // Parse the type, i.e. <type> in media type string "<type>/<subtype>; param1=value1; param2=value2" |
| 0 | 593 | | var typeLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | 594 | |
|
| 0 | 595 | | if (typeLength == 0) |
| | 596 | | { |
| 0 | 597 | | return 0; |
| | 598 | | } |
| | 599 | |
|
| 0 | 600 | | var current = startIndex + typeLength; |
| 0 | 601 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 602 | |
|
| | 603 | | // Parse the separator between type and subtype |
| 0 | 604 | | if ((current >= input.Length) || (input[current] != '/')) |
| | 605 | | { |
| 0 | 606 | | return 0; |
| | 607 | | } |
| 0 | 608 | | current++; // skip delimiter. |
| 0 | 609 | | current = current + HttpRuleParser.GetWhitespaceLength(input, current); |
| | 610 | |
|
| | 611 | | // Parse the subtype, i.e. <subtype> in media type string "<type>/<subtype>; param1=value1; param2=value2" |
| 0 | 612 | | var subtypeLength = HttpRuleParser.GetTokenLength(input, current); |
| | 613 | |
|
| 0 | 614 | | if (subtypeLength == 0) |
| | 615 | | { |
| 0 | 616 | | return 0; |
| | 617 | | } |
| | 618 | |
|
| | 619 | | // If there is no whitespace between <type> and <subtype> in <type>/<subtype> get the media type using |
| | 620 | | // one Substring call. Otherwise get substrings for <type> and <subtype> and combine them. |
| 0 | 621 | | var mediaTypeLength = current + subtypeLength - startIndex; |
| 0 | 622 | | if (typeLength + subtypeLength + 1 == mediaTypeLength) |
| | 623 | | { |
| 0 | 624 | | mediaType = input.Subsegment(startIndex, mediaTypeLength); |
| | 625 | | } |
| | 626 | | else |
| | 627 | | { |
| 0 | 628 | | mediaType = input.Substring(startIndex, typeLength) + ForwardSlashCharacter + input.Substring(current, s |
| | 629 | | } |
| | 630 | |
|
| 0 | 631 | | return mediaTypeLength; |
| | 632 | | } |
| | 633 | |
|
| | 634 | | private static void CheckMediaTypeFormat(StringSegment mediaType, string parameterName) |
| | 635 | | { |
| 0 | 636 | | if (StringSegment.IsNullOrEmpty(mediaType)) |
| | 637 | | { |
| 0 | 638 | | throw new ArgumentException("An empty string is not allowed.", parameterName); |
| | 639 | | } |
| | 640 | |
|
| | 641 | | // When adding values using strongly typed objects, no leading/trailing LWS (whitespace) is allowed. |
| | 642 | | // Also no LWS between type and subtype is allowed. |
| 0 | 643 | | var mediaTypeLength = GetMediaTypeExpressionLength(mediaType, 0, out var tempMediaType); |
| 0 | 644 | | if ((mediaTypeLength == 0) || (tempMediaType.Length != mediaType.Length)) |
| | 645 | | { |
| 0 | 646 | | throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Invalid media type '{0}'.", media |
| | 647 | | } |
| 0 | 648 | | } |
| | 649 | |
|
| | 650 | | private bool MatchesType(MediaTypeHeaderValue set) |
| | 651 | | { |
| 0 | 652 | | return set.MatchesAllTypes || |
| 0 | 653 | | set.Type.Equals(Type, StringComparison.OrdinalIgnoreCase); |
| | 654 | | } |
| | 655 | |
|
| | 656 | | private bool MatchesSubtype(MediaTypeHeaderValue set) |
| | 657 | | { |
| 0 | 658 | | if (set.MatchesAllSubTypes) |
| | 659 | | { |
| 0 | 660 | | return true; |
| | 661 | | } |
| | 662 | |
|
| 0 | 663 | | if (set.Suffix.HasValue) |
| | 664 | | { |
| 0 | 665 | | if (Suffix.HasValue) |
| | 666 | | { |
| 0 | 667 | | return MatchesSubtypeWithoutSuffix(set) && MatchesSubtypeSuffix(set); |
| | 668 | | } |
| | 669 | | else |
| | 670 | | { |
| 0 | 671 | | return false; |
| | 672 | | } |
| | 673 | | } |
| | 674 | | else |
| | 675 | | { |
| | 676 | | // If this subtype or suffix matches the subtype of the set, |
| | 677 | | // it is considered a subtype. |
| | 678 | | // Ex: application/json > application/val+json |
| 0 | 679 | | return MatchesEitherSubtypeOrSuffix(set); |
| | 680 | | } |
| | 681 | | } |
| | 682 | |
|
| | 683 | | private bool MatchesSubtypeWithoutSuffix(MediaTypeHeaderValue set) |
| | 684 | | { |
| 0 | 685 | | return set.MatchesAllSubTypesWithoutSuffix || |
| 0 | 686 | | set.SubTypeWithoutSuffix.Equals(SubTypeWithoutSuffix, StringComparison.OrdinalIgnoreCase); |
| | 687 | | } |
| | 688 | |
|
| | 689 | | private bool MatchesEitherSubtypeOrSuffix(MediaTypeHeaderValue set) |
| | 690 | | { |
| 0 | 691 | | return set.SubType.Equals(SubType, StringComparison.OrdinalIgnoreCase) || |
| 0 | 692 | | set.SubType.Equals(Suffix, StringComparison.OrdinalIgnoreCase); |
| | 693 | | } |
| | 694 | |
|
| | 695 | | private bool MatchesParameters(MediaTypeHeaderValue set) |
| | 696 | | { |
| 0 | 697 | | if (set._parameters != null && set._parameters.Count != 0) |
| | 698 | | { |
| | 699 | | // Make sure all parameters in the potential superset are included locally. Fine to have additional |
| | 700 | | // parameters locally; they make this one more specific. |
| 0 | 701 | | foreach (var parameter in set._parameters) |
| | 702 | | { |
| 0 | 703 | | if (parameter.Name.Equals(WildcardString, StringComparison.OrdinalIgnoreCase)) |
| | 704 | | { |
| | 705 | | // A parameter named "*" has no effect on media type matching, as it is only used as an indicati |
| | 706 | | // that the entire media type string should be treated as a wildcard. |
| | 707 | | continue; |
| | 708 | | } |
| | 709 | |
|
| 0 | 710 | | if (parameter.Name.Equals(QualityString, StringComparison.OrdinalIgnoreCase)) |
| | 711 | | { |
| | 712 | | // "q" and later parameters are not involved in media type matching. Quoting the RFC: The first |
| | 713 | | // "q" parameter (if any) separates the media-range parameter(s) from the accept-params. |
| 0 | 714 | | break; |
| | 715 | | } |
| | 716 | |
|
| 0 | 717 | | var localParameter = NameValueHeaderValue.Find(_parameters, parameter.Name); |
| 0 | 718 | | if (localParameter == null) |
| | 719 | | { |
| | 720 | | // Not found. |
| 0 | 721 | | return false; |
| | 722 | | } |
| | 723 | |
|
| 0 | 724 | | if (!StringSegment.Equals(parameter.Value, localParameter.Value, StringComparison.OrdinalIgnoreCase) |
| | 725 | | { |
| 0 | 726 | | return false; |
| | 727 | | } |
| | 728 | | } |
| | 729 | | } |
| 0 | 730 | | return true; |
| 0 | 731 | | } |
| | 732 | |
|
| | 733 | | private bool MatchesSubtypeSuffix(MediaTypeHeaderValue set) |
| | 734 | | { |
| | 735 | | // We don't have support for wildcards on suffixes alone (e.g., "application/entity+*") |
| | 736 | | // because there's no clear use case for it. |
| 0 | 737 | | return set.Suffix.Equals(Suffix, StringComparison.OrdinalIgnoreCase); |
| | 738 | | } |
| | 739 | | } |
| | 740 | | } |