| | | 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; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | internal sealed class GenericHeaderParser : BaseHeaderParser |
| | | 10 | | { |
| | | 11 | | private delegate int GetParsedValueLengthDelegate(string value, int startIndex, out object? parsedValue); |
| | | 12 | | |
| | | 13 | | #region Parser instances |
| | | 14 | | |
| | 1 | 15 | | internal static readonly GenericHeaderParser HostParser = new GenericHeaderParser(false, ParseHost, StringCompar |
| | 1 | 16 | | internal static readonly GenericHeaderParser TokenListParser = new GenericHeaderParser(true, ParseTokenList, Str |
| | 1 | 17 | | internal static readonly GenericHeaderParser SingleValueNameValueWithParametersParser = new GenericHeaderParser( |
| | 1 | 18 | | internal static readonly GenericHeaderParser MultipleValueNameValueWithParametersParser = new GenericHeaderParse |
| | 1 | 19 | | internal static readonly GenericHeaderParser SingleValueNameValueParser = new GenericHeaderParser(false, ParseNa |
| | 1 | 20 | | internal static readonly GenericHeaderParser MultipleValueNameValueParser = new GenericHeaderParser(true, ParseN |
| | 1 | 21 | | internal static readonly GenericHeaderParser SingleValueParserWithoutValidation = new GenericHeaderParser(false, |
| | 1 | 22 | | internal static readonly GenericHeaderParser SingleValueProductParser = new GenericHeaderParser(false, ParseProd |
| | 1 | 23 | | internal static readonly GenericHeaderParser MultipleValueProductParser = new GenericHeaderParser(true, ParsePro |
| | 1 | 24 | | internal static readonly GenericHeaderParser RangeConditionParser = new GenericHeaderParser(false, RangeConditio |
| | 1 | 25 | | internal static readonly GenericHeaderParser SingleValueAuthenticationParser = new GenericHeaderParser(false, Au |
| | 1 | 26 | | internal static readonly GenericHeaderParser MultipleValueAuthenticationParser = new GenericHeaderParser(true, A |
| | 1 | 27 | | internal static readonly GenericHeaderParser RangeParser = new GenericHeaderParser(false, RangeHeaderValue.GetRa |
| | 1 | 28 | | internal static readonly GenericHeaderParser RetryConditionParser = new GenericHeaderParser(false, RetryConditio |
| | 1 | 29 | | internal static readonly GenericHeaderParser ContentRangeParser = new GenericHeaderParser(false, ContentRangeHea |
| | 1 | 30 | | internal static readonly GenericHeaderParser ContentDispositionParser = new GenericHeaderParser(false, ContentDi |
| | 1 | 31 | | internal static readonly GenericHeaderParser SingleValueStringWithQualityParser = new GenericHeaderParser(false, |
| | 1 | 32 | | internal static readonly GenericHeaderParser MultipleValueStringWithQualityParser = new GenericHeaderParser(true |
| | 1 | 33 | | internal static readonly GenericHeaderParser SingleValueEntityTagParser = new GenericHeaderParser(false, ParseSi |
| | 1 | 34 | | internal static readonly GenericHeaderParser MultipleValueEntityTagParser = new GenericHeaderParser(true, ParseM |
| | 1 | 35 | | internal static readonly GenericHeaderParser SingleValueViaParser = new GenericHeaderParser(false, ViaHeaderValu |
| | 1 | 36 | | internal static readonly GenericHeaderParser MultipleValueViaParser = new GenericHeaderParser(true, ViaHeaderVal |
| | 1 | 37 | | internal static readonly GenericHeaderParser SingleValueWarningParser = new GenericHeaderParser(false, WarningHe |
| | 1 | 38 | | internal static readonly GenericHeaderParser MultipleValueWarningParser = new GenericHeaderParser(true, WarningH |
| | | 39 | | |
| | | 40 | | #endregion |
| | | 41 | | |
| | | 42 | | private readonly GetParsedValueLengthDelegate _getParsedValueLength; |
| | | 43 | | private readonly IEqualityComparer? _comparer; |
| | | 44 | | |
| | | 45 | | public override IEqualityComparer? Comparer |
| | | 46 | | { |
| | 0 | 47 | | get { return _comparer; } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private GenericHeaderParser(bool supportsMultipleValues, GetParsedValueLengthDelegate getParsedValueLength) |
| | 22 | 51 | | : this(supportsMultipleValues, getParsedValueLength, null) |
| | 22 | 52 | | { |
| | 22 | 53 | | } |
| | | 54 | | |
| | | 55 | | private GenericHeaderParser(bool supportsMultipleValues, GetParsedValueLengthDelegate getParsedValueLength, |
| | | 56 | | IEqualityComparer? comparer) |
| | 24 | 57 | | : base(supportsMultipleValues) |
| | 24 | 58 | | { |
| | 24 | 59 | | Debug.Assert(getParsedValueLength != null); |
| | | 60 | | |
| | 24 | 61 | | _getParsedValueLength = getParsedValueLength; |
| | 24 | 62 | | _comparer = comparer; |
| | 24 | 63 | | } |
| | | 64 | | |
| | | 65 | | protected override int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 66 | | out object? parsedValue) |
| | 613092 | 67 | | { |
| | 613092 | 68 | | return _getParsedValueLength(value, startIndex, out parsedValue); |
| | 613092 | 69 | | } |
| | | 70 | | |
| | | 71 | | #region Parse methods |
| | | 72 | | |
| | | 73 | | private static int ParseNameValue(string value, int startIndex, out object? parsedValue) |
| | 237688 | 74 | | { |
| | 237688 | 75 | | int resultLength = NameValueHeaderValue.GetNameValueLength(value, startIndex, out NameValueHeaderValue? temp |
| | | 76 | | |
| | 237688 | 77 | | parsedValue = temp; |
| | 237688 | 78 | | return resultLength; |
| | 237688 | 79 | | } |
| | | 80 | | |
| | | 81 | | private static int ParseProduct(string value, int startIndex, out object? parsedValue) |
| | 14550 | 82 | | { |
| | 14550 | 83 | | int resultLength = ProductHeaderValue.GetProductLength(value, startIndex, out ProductHeaderValue? temp); |
| | | 84 | | |
| | 14550 | 85 | | parsedValue = temp; |
| | 14550 | 86 | | return resultLength; |
| | 14550 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static int ParseSingleEntityTag(string value, int startIndex, out object? parsedValue) |
| | 52 | 90 | | { |
| | 52 | 91 | | parsedValue = null; |
| | | 92 | | |
| | 52 | 93 | | int resultLength = EntityTagHeaderValue.GetEntityTagLength(value, startIndex, out EntityTagHeaderValue? temp |
| | | 94 | | |
| | | 95 | | // If we don't allow '*' ("Any") as valid ETag value, return false (e.g. 'ETag' header) |
| | 52 | 96 | | if (temp == EntityTagHeaderValue.Any) |
| | 8 | 97 | | { |
| | 8 | 98 | | return 0; |
| | | 99 | | } |
| | | 100 | | |
| | 44 | 101 | | parsedValue = temp; |
| | 44 | 102 | | return resultLength; |
| | 52 | 103 | | } |
| | | 104 | | |
| | | 105 | | // Note that if multiple ETag values are allowed (e.g. 'If-Match', 'If-None-Match'), according to the RFC |
| | | 106 | | // the value must either be '*' or a list of ETag values. It's not allowed to have both '*' and a list of |
| | | 107 | | // ETag values. We're not that strict: We allow both '*' and ETag values in a list. If the server sends such |
| | | 108 | | // an invalid list, we want to be able to represent it using the corresponding header property. |
| | | 109 | | private static int ParseMultipleEntityTags(string value, int startIndex, out object? parsedValue) |
| | 1880 | 110 | | { |
| | 1880 | 111 | | int resultLength = EntityTagHeaderValue.GetEntityTagLength(value, startIndex, out EntityTagHeaderValue? temp |
| | | 112 | | |
| | 1880 | 113 | | parsedValue = temp; |
| | 1880 | 114 | | return resultLength; |
| | 1880 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Allows for arbitrary header values without validation (aside from newline, which is always invalid in a head |
| | | 119 | | /// </summary> |
| | | 120 | | private static int ParseWithoutValidation(string value, int startIndex, out object? parsedValue) |
| | 10 | 121 | | { |
| | 10 | 122 | | if (HttpRuleParser.ContainsNewLineOrNull(value, startIndex)) |
| | 4 | 123 | | { |
| | 4 | 124 | | parsedValue = null; |
| | 4 | 125 | | return 0; |
| | | 126 | | } |
| | | 127 | | |
| | 6 | 128 | | string result = value.Substring(startIndex); |
| | | 129 | | |
| | 6 | 130 | | parsedValue = result; |
| | 6 | 131 | | return result.Length; |
| | 10 | 132 | | } |
| | | 133 | | |
| | | 134 | | private static int ParseHost(string value, int startIndex, out object? parsedValue) |
| | 52 | 135 | | { |
| | 52 | 136 | | int hostLength = HttpRuleParser.GetHostLength(value, startIndex, false); |
| | | 137 | | |
| | 52 | 138 | | parsedValue = value.Substring(startIndex, hostLength); |
| | 52 | 139 | | return hostLength; |
| | 52 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static int ParseTokenList(string value, int startIndex, out object parsedValue) |
| | 14223 | 143 | | { |
| | 14223 | 144 | | int resultLength = HttpRuleParser.GetTokenLength(value, startIndex); |
| | | 145 | | |
| | 14223 | 146 | | parsedValue = value.Substring(startIndex, resultLength); |
| | 14223 | 147 | | return resultLength; |
| | 14223 | 148 | | } |
| | | 149 | | #endregion |
| | | 150 | | } |
| | | 151 | | } |