| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | internal sealed class CacheControlHeaderParser : BaseHeaderParser |
| | | 10 | | { |
| | 1 | 11 | | internal static readonly CacheControlHeaderParser Parser = new CacheControlHeaderParser(); |
| | | 12 | | |
| | | 13 | | // The Cache-Control header is special: It is a header supporting a list of values, but we represent the list |
| | | 14 | | // as _one_ instance of CacheControlHeaderValue. I.e we set 'SupportsMultipleValues' to 'true' since it is |
| | | 15 | | // OK to have multiple Cache-Control headers in a request/response message. However, after parsing all |
| | | 16 | | // Cache-Control headers, only one instance of CacheControlHeaderValue is created (if all headers contain valid |
| | | 17 | | // values, otherwise we may have multiple strings containing the invalid values). |
| | | 18 | | private CacheControlHeaderParser() |
| | 1 | 19 | | : base(true) |
| | 1 | 20 | | { |
| | 1 | 21 | | } |
| | | 22 | | |
| | | 23 | | protected override int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 24 | | out object? parsedValue) |
| | 5134 | 25 | | { |
| | 5134 | 26 | | CacheControlHeaderValue? temp = null; |
| | 5134 | 27 | | bool isInvalidValue = true; |
| | 5134 | 28 | | if (storeValue is List<object> list) |
| | 0 | 29 | | { |
| | 0 | 30 | | foreach (object item in list) |
| | 0 | 31 | | { |
| | 0 | 32 | | if (item is not HttpHeaders.InvalidValue) |
| | 0 | 33 | | { |
| | 0 | 34 | | isInvalidValue = false; |
| | 0 | 35 | | temp = item as CacheControlHeaderValue; |
| | 0 | 36 | | break; |
| | | 37 | | } |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | else |
| | 5134 | 41 | | { |
| | 5134 | 42 | | if (storeValue is not HttpHeaders.InvalidValue) |
| | 5134 | 43 | | { |
| | 5134 | 44 | | isInvalidValue = false; |
| | 5134 | 45 | | temp = storeValue as CacheControlHeaderValue; |
| | 5134 | 46 | | } |
| | 5134 | 47 | | } |
| | 5134 | 48 | | Debug.Assert(isInvalidValue || storeValue == null || temp != null, "'storeValue' is not of type CacheControl |
| | | 49 | | |
| | 5134 | 50 | | int resultLength = CacheControlHeaderValue.GetCacheControlLength(value, startIndex, temp, out temp); |
| | | 51 | | |
| | 5134 | 52 | | parsedValue = temp; |
| | 5134 | 53 | | return resultLength; |
| | 5134 | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |