| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace Azure.Storage.Blobs.Models |
| | 11 | | { |
| | 12 | | internal struct ContentRange |
| | 13 | | { |
| | 14 | | private const string WildcardMarker = "*"; |
| | 15 | |
|
| | 16 | | public struct RangeUnit |
| | 17 | | { |
| | 18 | | internal const string BytesValue = "bytes"; |
| | 19 | |
|
| | 20 | | private readonly string _value; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="RangeUnit"/> structure. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="value">The string value of the instance.</param> |
| | 26 | | public RangeUnit(string value) |
| | 27 | | { |
| 0 | 28 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Label for bytes as the measurement of content range. |
| | 33 | | /// </summary> |
| 0 | 34 | | public static RangeUnit Bytes { get; } = new RangeUnit(BytesValue); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Determines if two <see cref="Unit"/> values are the same. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="left">The first <see cref="Unit"/> to compare.</param> |
| | 40 | | /// <param name="right">The second <see cref="Unit"/> to compare.</param> |
| | 41 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</r |
| 0 | 42 | | public static bool operator ==(RangeUnit left, RangeUnit right) => left.Equals(right); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Determines if two <see cref="RangeUnit"/> values are different. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="left">The first <see cref="RangeUnit"/> to compare.</param> |
| | 48 | | /// <param name="right">The second <see cref="RangeUnit"/> to compare.</param> |
| | 49 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</ |
| 0 | 50 | | public static bool operator !=(RangeUnit left, RangeUnit right) => !left.Equals(right); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Converts a string to a <see cref="RangeUnit"/>. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="value">The string value to convert.</param> |
| 0 | 56 | | public static implicit operator RangeUnit(string value) => new RangeUnit(value); |
| | 57 | |
|
| | 58 | | /// <inheritdoc/> |
| | 59 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 60 | | public override bool Equals(object obj) => obj is RangeUnit other && Equals(other); |
| | 61 | |
|
| | 62 | | /// <inheritdoc/> |
| 0 | 63 | | public bool Equals(RangeUnit other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | 64 | |
|
| | 65 | | /// <inheritdoc/> |
| | 66 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 67 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | 68 | |
|
| | 69 | | /// <inheritdoc/> |
| 0 | 70 | | public override string ToString() => _value; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Inclusive index where the range starts, measured in this instance's <see cref="Unit"/>. |
| | 75 | | /// </summary> |
| 0 | 76 | | public long? Start { get; } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Inclusive index where the range ends, measured in this instance's <see cref="Unit"/>. |
| | 80 | | /// </summary> |
| 0 | 81 | | public long? End { get; } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Size of this range, measured in this instance's <see cref="Unit"/>. |
| | 85 | | /// </summary> |
| 0 | 86 | | public long? Size { get; } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Unit this range is measured in. Generally "bytes". |
| | 90 | | /// </summary> |
| 0 | 91 | | public RangeUnit Unit { get; } |
| | 92 | |
|
| | 93 | | public ContentRange(RangeUnit unit, long? start, long? end, long? size) |
| | 94 | | { |
| 0 | 95 | | Start = start; |
| 0 | 96 | | End = end; |
| 0 | 97 | | Size = size; |
| 0 | 98 | | Unit = unit; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public static ContentRange Parse(string headerValue) |
| | 102 | | { |
| | 103 | | /* Parse header value (e.g. "<unit> <start>-<end>/<blobSize>") |
| | 104 | | * Either side of the "/" can be an asterisk, so possible results include: |
| | 105 | | * [<unit>, <start>, <end>, <blobSize>] |
| | 106 | | * [<unit>, "*", <blobSize>] |
| | 107 | | * [<unit>, <start>, <end>, "*"] |
| | 108 | | * [<unit>, "*", "*"] (unsure if possible but not hard to support) |
| | 109 | | * "End" is the inclusive last byte; e.g. header "bytes 0-7/8" is the entire 8-byte blob |
| | 110 | | */ |
| 0 | 111 | | var tokens = headerValue.Split(new char[] { ' ', '-', '/' }); // ["bytes", "<start>", "<end>", "<blobSize>"] |
| | 112 | | string unit = default; |
| 0 | 113 | | long? start = default; |
| 0 | 114 | | long? end = default; |
| 0 | 115 | | long? size = default; |
| | 116 | |
|
| | 117 | | try |
| | 118 | | { |
| | 119 | | // unit always first and always present |
| 0 | 120 | | unit = tokens[0]; |
| | 121 | |
|
| | 122 | | int blobSizeIndex; |
| 0 | 123 | | if (tokens[1] == WildcardMarker) // [<unit>, "*", (<blobSize> | "*")] |
| | 124 | | { |
| 0 | 125 | | blobSizeIndex = 2; |
| | 126 | | } |
| | 127 | | else // [<unit>, <start>, <end>, (<blobSize> | "*")] |
| | 128 | | { |
| 0 | 129 | | blobSizeIndex = 3; |
| | 130 | |
|
| 0 | 131 | | start = int.Parse(tokens[1], CultureInfo.InvariantCulture); |
| 0 | 132 | | end = int.Parse(tokens[2], CultureInfo.InvariantCulture); |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | var rawSize = tokens[blobSizeIndex]; |
| 0 | 136 | | if (rawSize != WildcardMarker) |
| | 137 | | { |
| 0 | 138 | | size = int.Parse(rawSize, CultureInfo.InvariantCulture); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | return new ContentRange(unit, start, end, size); |
| | 142 | | } |
| 0 | 143 | | catch (IndexOutOfRangeException) |
| | 144 | | { |
| 0 | 145 | | throw Errors.ParsingHttpRangeFailed(); |
| | 146 | | } |
| 0 | 147 | | } |
| | 148 | | } |
| | 149 | | } |