| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | public class ContentRangeHeaderValue : ICloneable |
| | | 11 | | { |
| | 48 | 12 | | private string _unit = null!; |
| | | 13 | | private long _from; |
| | | 14 | | private long _to; |
| | | 15 | | private long _length; |
| | | 16 | | |
| | | 17 | | public string Unit |
| | | 18 | | { |
| | 0 | 19 | | get { return _unit; } |
| | | 20 | | set |
| | 0 | 21 | | { |
| | 0 | 22 | | HeaderUtilities.CheckValidToken(value); |
| | 0 | 23 | | _unit = value; |
| | 0 | 24 | | } |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | public long? From => HasRange ? _from : null; |
| | | 28 | | |
| | 0 | 29 | | public long? To => HasRange ? _to : null; |
| | | 30 | | |
| | 0 | 31 | | public long? Length => HasLength ? _length : null; |
| | | 32 | | |
| | 39 | 33 | | public bool HasLength => _length >= 0; // e.g. "Content-Range: bytes 12-34/*" |
| | | 34 | | |
| | 39 | 35 | | public bool HasRange => _from >= 0; // e.g. "Content-Range: bytes */1234" |
| | | 36 | | |
| | 0 | 37 | | public ContentRangeHeaderValue(long from, long to, long length) |
| | 0 | 38 | | { |
| | | 39 | | // Scenario: "Content-Range: bytes 12-34/5678" |
| | | 40 | | |
| | 0 | 41 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | 0 | 42 | | ArgumentOutOfRangeException.ThrowIfNegative(to); |
| | 0 | 43 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(to, length); |
| | 0 | 44 | | ArgumentOutOfRangeException.ThrowIfNegative(from); |
| | 0 | 45 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(from, to); |
| | | 46 | | |
| | 0 | 47 | | _from = from; |
| | 0 | 48 | | _to = to; |
| | 0 | 49 | | _length = length; |
| | 0 | 50 | | _unit = HeaderUtilities.BytesUnit; |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | public ContentRangeHeaderValue(long length) |
| | 0 | 54 | | { |
| | | 55 | | // Scenario: "Content-Range: bytes */1234" |
| | | 56 | | |
| | 0 | 57 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | | 58 | | |
| | 0 | 59 | | _length = length; |
| | 0 | 60 | | _unit = HeaderUtilities.BytesUnit; |
| | 0 | 61 | | _from = -1; |
| | 0 | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | public ContentRangeHeaderValue(long from, long to) |
| | 0 | 65 | | { |
| | | 66 | | // Scenario: "Content-Range: bytes 12-34/*" |
| | | 67 | | |
| | 0 | 68 | | ArgumentOutOfRangeException.ThrowIfNegative(to); |
| | 0 | 69 | | ArgumentOutOfRangeException.ThrowIfNegative(from); |
| | 0 | 70 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(from, to); |
| | | 71 | | |
| | 0 | 72 | | _from = from; |
| | 0 | 73 | | _to = to; |
| | 0 | 74 | | _unit = HeaderUtilities.BytesUnit; |
| | 0 | 75 | | _length = -1; |
| | 0 | 76 | | } |
| | | 77 | | |
| | 48 | 78 | | private ContentRangeHeaderValue() |
| | 48 | 79 | | { |
| | 48 | 80 | | _from = -1; |
| | 48 | 81 | | _length = -1; |
| | 48 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private ContentRangeHeaderValue(ContentRangeHeaderValue source) |
| | 0 | 85 | | { |
| | 0 | 86 | | Debug.Assert(source != null); |
| | | 87 | | |
| | 0 | 88 | | _from = source._from; |
| | 0 | 89 | | _to = source._to; |
| | 0 | 90 | | _length = source._length; |
| | 0 | 91 | | _unit = source._unit; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 95 | | obj is ContentRangeHeaderValue other && |
| | 0 | 96 | | _from == other._from && |
| | 0 | 97 | | _to == other._to && |
| | 0 | 98 | | _length == other._length && |
| | 0 | 99 | | string.Equals(_unit, other._unit, StringComparison.OrdinalIgnoreCase); |
| | | 100 | | |
| | | 101 | | public override int GetHashCode() => |
| | 0 | 102 | | HashCode.Combine( |
| | 0 | 103 | | StringComparer.OrdinalIgnoreCase.GetHashCode(_unit), |
| | 0 | 104 | | _from, |
| | 0 | 105 | | _to, |
| | 0 | 106 | | _length); |
| | | 107 | | |
| | | 108 | | public override string ToString() |
| | 39 | 109 | | { |
| | 39 | 110 | | var sb = new ValueStringBuilder(stackalloc char[256]); |
| | 39 | 111 | | sb.Append(_unit); |
| | 39 | 112 | | sb.Append(' '); |
| | | 113 | | |
| | 39 | 114 | | if (HasRange) |
| | 0 | 115 | | { |
| | 0 | 116 | | sb.AppendSpanFormattable(_from); |
| | 0 | 117 | | sb.Append('-'); |
| | 0 | 118 | | sb.AppendSpanFormattable(_to); |
| | 0 | 119 | | } |
| | | 120 | | else |
| | 39 | 121 | | { |
| | 39 | 122 | | sb.Append('*'); |
| | 39 | 123 | | } |
| | | 124 | | |
| | 39 | 125 | | sb.Append('/'); |
| | 39 | 126 | | if (HasLength) |
| | 6 | 127 | | { |
| | 6 | 128 | | sb.AppendSpanFormattable(_length); |
| | 6 | 129 | | } |
| | | 130 | | else |
| | 33 | 131 | | { |
| | 33 | 132 | | sb.Append('*'); |
| | 33 | 133 | | } |
| | | 134 | | |
| | 39 | 135 | | return sb.ToString(); |
| | 39 | 136 | | } |
| | | 137 | | |
| | | 138 | | public static ContentRangeHeaderValue Parse(string input) |
| | 0 | 139 | | { |
| | 0 | 140 | | int index = 0; |
| | 0 | 141 | | return (ContentRangeHeaderValue)GenericHeaderParser.ContentRangeParser.ParseValue(input, null, ref index); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ContentRangeHeaderValue? |
| | 0 | 145 | | { |
| | 0 | 146 | | int index = 0; |
| | 0 | 147 | | parsedValue = null; |
| | | 148 | | |
| | 0 | 149 | | if (GenericHeaderParser.ContentRangeParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 150 | | { |
| | 0 | 151 | | parsedValue = (ContentRangeHeaderValue)output!; |
| | 0 | 152 | | return true; |
| | | 153 | | } |
| | 0 | 154 | | return false; |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | internal static int GetContentRangeLength(string? input, int startIndex, out object? parsedValue) |
| | 174 | 158 | | { |
| | 174 | 159 | | Debug.Assert(startIndex >= 0); |
| | | 160 | | |
| | 174 | 161 | | parsedValue = null; |
| | | 162 | | |
| | 174 | 163 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 164 | | { |
| | 0 | 165 | | return 0; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | // Parse the unit string: <unit> in '<unit> <from>-<to>/<length>' |
| | 174 | 169 | | int unitLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 170 | | |
| | 174 | 171 | | if (unitLength == 0) |
| | 4 | 172 | | { |
| | 4 | 173 | | return 0; |
| | | 174 | | } |
| | | 175 | | |
| | 170 | 176 | | string unit = input.Substring(startIndex, unitLength); |
| | 170 | 177 | | int current = startIndex + unitLength; |
| | 170 | 178 | | int separatorLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 179 | | |
| | 170 | 180 | | if (separatorLength == 0) |
| | 4 | 181 | | { |
| | 4 | 182 | | return 0; |
| | | 183 | | } |
| | | 184 | | |
| | 166 | 185 | | current += separatorLength; |
| | | 186 | | |
| | 166 | 187 | | if (current == input.Length) |
| | 4 | 188 | | { |
| | 4 | 189 | | return 0; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | // Read range values <from> and <to> in '<unit> <from>-<to>/<length>' |
| | 162 | 193 | | int fromStartIndex = current; |
| | | 194 | | int fromLength; |
| | | 195 | | int toStartIndex; |
| | | 196 | | int toLength; |
| | 162 | 197 | | if (!TryGetRangeLength(input, ref current, out fromLength, out toStartIndex, out toLength)) |
| | 58 | 198 | | { |
| | 58 | 199 | | return 0; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | // After the range is read we expect the length separator '/' |
| | 104 | 203 | | if ((current == input.Length) || (input[current] != '/')) |
| | 40 | 204 | | { |
| | 40 | 205 | | return 0; |
| | | 206 | | } |
| | | 207 | | |
| | 64 | 208 | | current++; // Skip '/' separator |
| | 64 | 209 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 210 | | |
| | 64 | 211 | | if (current == input.Length) |
| | 2 | 212 | | { |
| | 2 | 213 | | return 0; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | // We may not have a length (e.g. 'bytes 1-2/*'). But if we do, parse the length now. |
| | 62 | 217 | | int lengthStartIndex = current; |
| | | 218 | | int lengthLength; |
| | 62 | 219 | | if (!TryGetLengthLength(input, ref current, out lengthLength)) |
| | 14 | 220 | | { |
| | 14 | 221 | | return 0; |
| | | 222 | | } |
| | | 223 | | |
| | 48 | 224 | | if (!TryCreateContentRange(input, unit, fromStartIndex, fromLength, toStartIndex, toLength, |
| | 48 | 225 | | lengthStartIndex, lengthLength, out parsedValue)) |
| | 0 | 226 | | { |
| | 0 | 227 | | return 0; |
| | | 228 | | } |
| | | 229 | | |
| | 48 | 230 | | return current - startIndex; |
| | 174 | 231 | | } |
| | | 232 | | |
| | | 233 | | private static bool TryGetLengthLength(string input, ref int current, out int lengthLength) |
| | 62 | 234 | | { |
| | 62 | 235 | | lengthLength = 0; |
| | | 236 | | |
| | 62 | 237 | | if (input[current] == '*') |
| | 36 | 238 | | { |
| | 36 | 239 | | current++; |
| | 36 | 240 | | } |
| | | 241 | | else |
| | 26 | 242 | | { |
| | | 243 | | // Parse length value: <length> in '<unit> <from>-<to>/<length>' |
| | 26 | 244 | | lengthLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 245 | | |
| | 26 | 246 | | if ((lengthLength == 0) || (lengthLength > HttpRuleParser.MaxInt64Digits)) |
| | 14 | 247 | | { |
| | 14 | 248 | | return false; |
| | | 249 | | } |
| | | 250 | | |
| | 12 | 251 | | current += lengthLength; |
| | 12 | 252 | | } |
| | | 253 | | |
| | 48 | 254 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 48 | 255 | | return true; |
| | 62 | 256 | | } |
| | | 257 | | |
| | | 258 | | private static bool TryGetRangeLength(string input, ref int current, out int fromLength, out int toStartIndex, |
| | | 259 | | out int toLength) |
| | 162 | 260 | | { |
| | 162 | 261 | | fromLength = 0; |
| | 162 | 262 | | toStartIndex = 0; |
| | 162 | 263 | | toLength = 0; |
| | | 264 | | |
| | | 265 | | // Check if we have a value like 'bytes */133'. If yes, skip the range part and continue parsing the |
| | | 266 | | // length separator '/'. |
| | 162 | 267 | | if (input[current] == '*') |
| | 102 | 268 | | { |
| | 102 | 269 | | current++; |
| | 102 | 270 | | } |
| | | 271 | | else |
| | 60 | 272 | | { |
| | | 273 | | // Parse first range value: <from> in '<unit> <from>-<to>/<length>' |
| | 60 | 274 | | fromLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 275 | | |
| | 60 | 276 | | if ((fromLength == 0) || (fromLength > HttpRuleParser.MaxInt64Digits)) |
| | 16 | 277 | | { |
| | 16 | 278 | | return false; |
| | | 279 | | } |
| | | 280 | | |
| | 44 | 281 | | current += fromLength; |
| | 44 | 282 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 283 | | |
| | | 284 | | // After the first value, the '-' character must follow. |
| | 44 | 285 | | if ((current == input.Length) || (input[current] != '-')) |
| | 22 | 286 | | { |
| | | 287 | | // We need a '-' character otherwise this can't be a valid range. |
| | 22 | 288 | | return false; |
| | | 289 | | } |
| | | 290 | | |
| | 22 | 291 | | current++; // skip the '-' character |
| | 22 | 292 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 293 | | |
| | 22 | 294 | | if (current == input.Length) |
| | 2 | 295 | | { |
| | 2 | 296 | | return false; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | // Parse second range value: <to> in '<unit> <from>-<to>/<length>' |
| | 20 | 300 | | toStartIndex = current; |
| | 20 | 301 | | toLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 302 | | |
| | 20 | 303 | | if ((toLength == 0) || (toLength > HttpRuleParser.MaxInt64Digits)) |
| | 18 | 304 | | { |
| | 18 | 305 | | return false; |
| | | 306 | | } |
| | | 307 | | |
| | 2 | 308 | | current += toLength; |
| | 2 | 309 | | } |
| | | 310 | | |
| | 104 | 311 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 104 | 312 | | return true; |
| | 162 | 313 | | } |
| | | 314 | | |
| | | 315 | | private static bool TryCreateContentRange(string input, string unit, int fromStartIndex, int fromLength, |
| | | 316 | | int toStartIndex, int toLength, int lengthStartIndex, int lengthLength, [NotNullWhen(true)] out object? pars |
| | 48 | 317 | | { |
| | 48 | 318 | | parsedValue = null; |
| | | 319 | | |
| | 48 | 320 | | long from = 0; |
| | 48 | 321 | | if ((fromLength > 0) && !HeaderUtilities.TryParseInt64(input, fromStartIndex, fromLength, out from)) |
| | 0 | 322 | | { |
| | 0 | 323 | | return false; |
| | | 324 | | } |
| | | 325 | | |
| | 48 | 326 | | long to = 0; |
| | 48 | 327 | | if ((toLength > 0) && !HeaderUtilities.TryParseInt64(input, toStartIndex, toLength, out to)) |
| | 0 | 328 | | { |
| | 0 | 329 | | return false; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | // 'from' must not be greater than 'to' |
| | 48 | 333 | | if ((fromLength > 0) && (toLength > 0) && (from > to)) |
| | 0 | 334 | | { |
| | 0 | 335 | | return false; |
| | | 336 | | } |
| | | 337 | | |
| | 48 | 338 | | long length = 0; |
| | 48 | 339 | | if ((lengthLength > 0) && !HeaderUtilities.TryParseInt64(input, lengthStartIndex, lengthLength, out length)) |
| | 0 | 340 | | { |
| | 0 | 341 | | return false; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | // 'from' and 'to' must be less than 'length' |
| | 48 | 345 | | if ((toLength > 0) && (lengthLength > 0) && (to >= length)) |
| | 0 | 346 | | { |
| | 0 | 347 | | return false; |
| | | 348 | | } |
| | | 349 | | |
| | 48 | 350 | | ContentRangeHeaderValue result = new ContentRangeHeaderValue(); |
| | 48 | 351 | | result._unit = unit; |
| | | 352 | | |
| | 48 | 353 | | if (fromLength > 0) |
| | 0 | 354 | | { |
| | 0 | 355 | | result._from = from; |
| | 0 | 356 | | result._to = to; |
| | 0 | 357 | | } |
| | | 358 | | |
| | 48 | 359 | | if (lengthLength > 0) |
| | 12 | 360 | | { |
| | 12 | 361 | | result._length = length; |
| | 12 | 362 | | } |
| | | 363 | | |
| | 48 | 364 | | parsedValue = result; |
| | 48 | 365 | | return true; |
| | 48 | 366 | | } |
| | | 367 | | |
| | | 368 | | object ICloneable.Clone() |
| | 0 | 369 | | { |
| | 0 | 370 | | return new ContentRangeHeaderValue(this); |
| | 0 | 371 | | } |
| | | 372 | | } |
| | | 373 | | } |