| | | 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; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | internal abstract class BaseHeaderParser : HttpHeaderParser |
| | | 10 | | { |
| | | 11 | | protected BaseHeaderParser(bool supportsMultipleValues) |
| | 36 | 12 | | : base(supportsMultipleValues) |
| | 36 | 13 | | { |
| | 36 | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Parses a full header or a segment of a multi-value header. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="value">The header value string to parse.</param> |
| | | 20 | | /// <param name="startIndex">The index to begin parsing at.</param> |
| | | 21 | | /// <param name="storeValue"></param> |
| | | 22 | | /// <param name="parsedValue">The resulting value parsed.</param> |
| | | 23 | | /// <returns>If a value could be parsed, the number of characters used to parse that value. Otherwise, 0.</retur |
| | | 24 | | protected abstract int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 25 | | out object? parsedValue); |
| | | 26 | | |
| | | 27 | | #pragma warning disable CS8765 // Doesn't match overridden member nullable attribute on out parameter |
| | | 28 | | public sealed override bool TryParseValue(string? value, object? storeValue, ref int index, |
| | | 29 | | out object? parsedValue) |
| | | 30 | | #pragma warning restore CS8765 |
| | 982219 | 31 | | { |
| | 982219 | 32 | | parsedValue = null; |
| | | 33 | | |
| | | 34 | | // If multiple values are supported (i.e. list of values), then accept an empty string: The header may |
| | | 35 | | // be added multiple times to the request/response message. E.g. |
| | | 36 | | // Accept: text/xml; q=1 |
| | | 37 | | // Accept: |
| | | 38 | | // Accept: text/plain; q=0.2 |
| | 982219 | 39 | | if (string.IsNullOrEmpty(value) || (index == value.Length)) |
| | 280 | 40 | | { |
| | 280 | 41 | | return SupportsMultipleValues; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | bool separatorFound; |
| | 981939 | 45 | | int current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, SupportsMultipleValues, |
| | 981939 | 46 | | out separatorFound); |
| | | 47 | | |
| | 981939 | 48 | | if (separatorFound && !SupportsMultipleValues) |
| | 2 | 49 | | { |
| | 2 | 50 | | return false; // leading separators not allowed if we don't support multiple values. |
| | | 51 | | } |
| | | 52 | | |
| | 981937 | 53 | | if (current == value.Length) |
| | 138 | 54 | | { |
| | 138 | 55 | | if (SupportsMultipleValues) |
| | 132 | 56 | | { |
| | 132 | 57 | | index = current; |
| | 132 | 58 | | } |
| | 138 | 59 | | return SupportsMultipleValues; |
| | | 60 | | } |
| | | 61 | | |
| | 981799 | 62 | | int length = GetParsedValueLength(value, current, storeValue, out object? result); |
| | | 63 | | |
| | 981799 | 64 | | if (length == 0) |
| | 12856 | 65 | | { |
| | 12856 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 968943 | 69 | | current += length; |
| | 968943 | 70 | | current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, current, SupportsMultipleValues, |
| | 968943 | 71 | | out separatorFound); |
| | | 72 | | |
| | | 73 | | // If we support multiple values and we've not reached the end of the string, then we must have a separator. |
| | 968943 | 74 | | if ((separatorFound && !SupportsMultipleValues) || (!separatorFound && (current < value.Length))) |
| | 1356 | 75 | | { |
| | 1356 | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | 967587 | 79 | | index = current; |
| | 967587 | 80 | | parsedValue = result!; |
| | 967587 | 81 | | return true; |
| | 982219 | 82 | | } |
| | | 83 | | } |
| | | 84 | | } |