| | | 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 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Globalization; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http.Headers |
| | | 10 | | { |
| | | 11 | | public class RangeItemHeaderValue : ICloneable |
| | | 12 | | { |
| | | 13 | | // Set to -1 if not set. |
| | | 14 | | private readonly long _from; |
| | | 15 | | private readonly long _to; |
| | | 16 | | |
| | 102072 | 17 | | public long? From => _from >= 0 ? _from : null; |
| | | 18 | | |
| | 70614 | 19 | | public long? To => _to >= 0 ? _to : null; |
| | | 20 | | |
| | 44708 | 21 | | public RangeItemHeaderValue(long? from, long? to) |
| | 44708 | 22 | | { |
| | 44708 | 23 | | if (!from.HasValue && !to.HasValue) |
| | 0 | 24 | | { |
| | 0 | 25 | | throw new ArgumentException(SR.net_http_headers_invalid_range); |
| | | 26 | | } |
| | 44708 | 27 | | if (from.HasValue) |
| | 39506 | 28 | | { |
| | 39506 | 29 | | ArgumentOutOfRangeException.ThrowIfNegative(from.GetValueOrDefault(), nameof(from)); |
| | 39506 | 30 | | } |
| | 44708 | 31 | | if (to.HasValue) |
| | 15656 | 32 | | { |
| | 15656 | 33 | | ArgumentOutOfRangeException.ThrowIfNegative(to.GetValueOrDefault(), nameof(to)); |
| | 15656 | 34 | | } |
| | 44708 | 35 | | if (from.HasValue && to.HasValue) |
| | 10454 | 36 | | { |
| | 10454 | 37 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(from.GetValueOrDefault(), to.GetValueOrDefault(), nameof( |
| | 10454 | 38 | | } |
| | | 39 | | |
| | 44708 | 40 | | _from = from ?? -1; |
| | 44708 | 41 | | _to = to ?? -1; |
| | 44708 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | internal RangeItemHeaderValue(RangeItemHeaderValue source) |
| | 0 | 45 | | { |
| | 0 | 46 | | Debug.Assert(source != null); |
| | | 47 | | |
| | 0 | 48 | | _from = source._from; |
| | 0 | 49 | | _to = source._to; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public override string ToString() |
| | 0 | 53 | | { |
| | 0 | 54 | | Span<char> stackBuffer = stackalloc char[128]; |
| | | 55 | | |
| | 0 | 56 | | if (_from < 0) |
| | 0 | 57 | | { |
| | 0 | 58 | | return string.Create(CultureInfo.InvariantCulture, stackBuffer, $"-{_to}"); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | if (_to < 0) |
| | 0 | 62 | | { |
| | 0 | 63 | | return string.Create(CultureInfo.InvariantCulture, stackBuffer, $"{_from}-"); ; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return string.Create(CultureInfo.InvariantCulture, stackBuffer, $"{_from}-{_to}"); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 70 | | obj is RangeItemHeaderValue other && |
| | 0 | 71 | | _from == other._from && |
| | 0 | 72 | | _to == other._to; |
| | | 73 | | |
| | | 74 | | public override int GetHashCode() => |
| | 0 | 75 | | HashCode.Combine(_from, _to); |
| | | 76 | | |
| | | 77 | | // Returns the length of a range list. E.g. "1-2, 3-4, 5-6" adds 3 ranges to 'rangeCollection'. Note that empty |
| | | 78 | | // list segments are allowed, e.g. ",1-2, , 3-4,,". |
| | | 79 | | internal static int GetRangeItemListLength(string? input, int startIndex, |
| | | 80 | | ICollection<RangeItemHeaderValue> rangeCollection) |
| | 1064 | 81 | | { |
| | 1064 | 82 | | Debug.Assert(rangeCollection != null); |
| | 1064 | 83 | | Debug.Assert(startIndex >= 0); |
| | | 84 | | |
| | 1064 | 85 | | if ((string.IsNullOrEmpty(input)) || (startIndex >= input.Length)) |
| | 14 | 86 | | { |
| | 14 | 87 | | return 0; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // Empty segments are allowed, so skip all delimiter-only segments (e.g. ", ,"). |
| | 1050 | 91 | | int current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(input, startIndex, true, out _); |
| | | 92 | | // It's OK if we didn't find leading separator characters. Ignore 'separatorFound'. |
| | | 93 | | |
| | 1050 | 94 | | if (current == input.Length) |
| | 4 | 95 | | { |
| | 4 | 96 | | return 0; |
| | | 97 | | } |
| | | 98 | | |
| | 45108 | 99 | | while (true) |
| | 45108 | 100 | | { |
| | 45108 | 101 | | int rangeLength = GetRangeItemLength(input, current, out RangeItemHeaderValue? range); |
| | | 102 | | |
| | 45108 | 103 | | if (rangeLength == 0) |
| | 400 | 104 | | { |
| | 400 | 105 | | return 0; |
| | | 106 | | } |
| | | 107 | | |
| | 44708 | 108 | | rangeCollection.Add(range!); |
| | | 109 | | |
| | 44708 | 110 | | current += rangeLength; |
| | 44708 | 111 | | current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(input, current, true, out bool separatorFound |
| | | 112 | | |
| | | 113 | | // If the string is not consumed, we must have a delimiter, otherwise the string is not a valid |
| | | 114 | | // range list. |
| | 44708 | 115 | | if ((current < input.Length) && !separatorFound) |
| | 160 | 116 | | { |
| | 160 | 117 | | return 0; |
| | | 118 | | } |
| | | 119 | | |
| | 44548 | 120 | | if (current == input.Length) |
| | 486 | 121 | | { |
| | 486 | 122 | | return current - startIndex; |
| | | 123 | | } |
| | 44062 | 124 | | } |
| | 1064 | 125 | | } |
| | | 126 | | |
| | | 127 | | internal static int GetRangeItemLength(string? input, int startIndex, out RangeItemHeaderValue? parsedValue) |
| | 45108 | 128 | | { |
| | 45108 | 129 | | Debug.Assert(startIndex >= 0); |
| | | 130 | | |
| | | 131 | | // This parser parses number ranges: e.g. '1-2', '1-', '-2'. |
| | | 132 | | |
| | 45108 | 133 | | parsedValue = null; |
| | | 134 | | |
| | 45108 | 135 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 136 | | { |
| | 0 | 137 | | return 0; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // Caller must remove leading whitespace. If not, we'll return 0. |
| | 45108 | 141 | | int current = startIndex; |
| | | 142 | | |
| | | 143 | | // Try parse the first value of a value pair. |
| | 45108 | 144 | | int fromStartIndex = current; |
| | 45108 | 145 | | int fromLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 146 | | |
| | 45108 | 147 | | if (fromLength > HttpRuleParser.MaxInt64Digits) |
| | 0 | 148 | | { |
| | 0 | 149 | | return 0; |
| | | 150 | | } |
| | | 151 | | |
| | 45108 | 152 | | current += fromLength; |
| | 45108 | 153 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 154 | | |
| | | 155 | | // After the first value, the '-' character must follow. |
| | 45108 | 156 | | if ((current == input.Length) || (input[current] != '-')) |
| | 254 | 157 | | { |
| | | 158 | | // We need a '-' character otherwise this can't be a valid range. |
| | 254 | 159 | | return 0; |
| | | 160 | | } |
| | | 161 | | |
| | 44854 | 162 | | current++; // skip the '-' character |
| | 44854 | 163 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 164 | | |
| | 44854 | 165 | | int toStartIndex = current; |
| | 44854 | 166 | | int toLength = 0; |
| | | 167 | | |
| | | 168 | | // If we didn't reach the end of the string, try parse the second value of the range. |
| | 44854 | 169 | | if (current < input.Length) |
| | 44772 | 170 | | { |
| | 44772 | 171 | | toLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 172 | | |
| | 44772 | 173 | | if (toLength > HttpRuleParser.MaxInt64Digits) |
| | 0 | 174 | | { |
| | 0 | 175 | | return 0; |
| | | 176 | | } |
| | | 177 | | |
| | 44772 | 178 | | current += toLength; |
| | 44772 | 179 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 44772 | 180 | | } |
| | | 181 | | |
| | 44854 | 182 | | if ((fromLength == 0) && (toLength == 0)) |
| | 144 | 183 | | { |
| | 144 | 184 | | return 0; // At least one value must be provided in order to be a valid range. |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | // Try convert first value to int64 |
| | 44710 | 188 | | long from = 0; |
| | 44710 | 189 | | if ((fromLength > 0) && !HeaderUtilities.TryParseInt64(input, fromStartIndex, fromLength, out from)) |
| | 0 | 190 | | { |
| | 0 | 191 | | return 0; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | // Try convert second value to int64 |
| | 44710 | 195 | | long to = 0; |
| | 44710 | 196 | | if ((toLength > 0) && !HeaderUtilities.TryParseInt64(input, toStartIndex, toLength, out to)) |
| | 0 | 197 | | { |
| | 0 | 198 | | return 0; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | // 'from' must not be greater than 'to' |
| | 44710 | 202 | | if ((fromLength > 0) && (toLength > 0) && (from > to)) |
| | 2 | 203 | | { |
| | 2 | 204 | | return 0; |
| | | 205 | | } |
| | | 206 | | |
| | 44708 | 207 | | parsedValue = new RangeItemHeaderValue((fromLength == 0 ? null : from), (toLength == 0 ? null : to)); |
| | 44708 | 208 | | return current - startIndex; |
| | 45108 | 209 | | } |
| | | 210 | | |
| | | 211 | | object ICloneable.Clone() |
| | 0 | 212 | | { |
| | 0 | 213 | | return new RangeItemHeaderValue(this); |
| | 0 | 214 | | } |
| | | 215 | | } |
| | | 216 | | } |