| | | 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.IO; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http.Headers |
| | | 11 | | { |
| | | 12 | | public class RangeHeaderValue : ICloneable |
| | | 13 | | { |
| | | 14 | | private string _unit; |
| | | 15 | | private UnvalidatedObjectCollection<RangeItemHeaderValue>? _ranges; |
| | | 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 | | |
| | 1064 | 27 | | public ICollection<RangeItemHeaderValue> Ranges => _ranges ??= new UnvalidatedObjectCollection<RangeItemHeaderVa |
| | | 28 | | |
| | 1074 | 29 | | public RangeHeaderValue() |
| | 1074 | 30 | | { |
| | 1074 | 31 | | _unit = HeaderUtilities.BytesUnit; |
| | 1074 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | public RangeHeaderValue(long? from, long? to) |
| | 0 | 35 | | { |
| | | 36 | | // convenience ctor: "Range: bytes=from-to" |
| | 0 | 37 | | _unit = HeaderUtilities.BytesUnit; |
| | 0 | 38 | | Ranges.Add(new RangeItemHeaderValue(from, to)); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | private RangeHeaderValue(RangeHeaderValue source) |
| | 0 | 42 | | { |
| | 0 | 43 | | Debug.Assert(source != null); |
| | | 44 | | |
| | 0 | 45 | | _unit = source._unit; |
| | 0 | 46 | | if (source._ranges != null) |
| | 0 | 47 | | { |
| | 0 | 48 | | foreach (RangeItemHeaderValue range in source._ranges) |
| | 0 | 49 | | { |
| | 0 | 50 | | this.Ranges.Add(new RangeItemHeaderValue(range)); |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public override string ToString() |
| | 729 | 56 | | { |
| | 729 | 57 | | var sb = new ValueStringBuilder(stackalloc char[256]); |
| | 729 | 58 | | sb.Append(_unit); |
| | 729 | 59 | | sb.Append('='); |
| | | 60 | | |
| | 729 | 61 | | if (_ranges != null) |
| | 729 | 62 | | { |
| | 729 | 63 | | bool first = true; |
| | 110001 | 64 | | foreach (RangeItemHeaderValue range in _ranges) |
| | 53907 | 65 | | { |
| | 53907 | 66 | | if (first) |
| | 729 | 67 | | { |
| | 729 | 68 | | first = false; |
| | 729 | 69 | | } |
| | | 70 | | else |
| | 53178 | 71 | | { |
| | 53178 | 72 | | sb.Append(", "); |
| | 53178 | 73 | | } |
| | | 74 | | |
| | 102072 | 75 | | if (range.From.HasValue) sb.AppendSpanFormattable(range.From.GetValueOrDefault()); |
| | 53907 | 76 | | sb.Append('-'); |
| | 70614 | 77 | | if (range.To.HasValue) sb.AppendSpanFormattable(range.To.GetValueOrDefault()); |
| | 53907 | 78 | | } |
| | 729 | 79 | | } |
| | | 80 | | |
| | 729 | 81 | | return sb.ToString(); |
| | 729 | 82 | | } |
| | | 83 | | |
| | | 84 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 85 | | { |
| | 0 | 86 | | RangeHeaderValue? other = obj as RangeHeaderValue; |
| | | 87 | | |
| | 0 | 88 | | if (other == null) |
| | 0 | 89 | | { |
| | 0 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | return string.Equals(_unit, other._unit, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 94 | | HeaderUtilities.AreEqualCollections(_ranges, other._ranges); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public override int GetHashCode() |
| | 0 | 98 | | { |
| | 0 | 99 | | int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_unit); |
| | | 100 | | |
| | 0 | 101 | | if (_ranges != null) |
| | 0 | 102 | | { |
| | 0 | 103 | | foreach (RangeItemHeaderValue range in _ranges) |
| | 0 | 104 | | { |
| | 0 | 105 | | result ^= range.GetHashCode(); |
| | 0 | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | return result; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | public static RangeHeaderValue Parse(string input) |
| | 0 | 113 | | { |
| | 0 | 114 | | int index = 0; |
| | 0 | 115 | | return (RangeHeaderValue)GenericHeaderParser.RangeParser.ParseValue(input, null, ref index); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out RangeHeaderValue? parsedV |
| | 0 | 119 | | { |
| | 0 | 120 | | int index = 0; |
| | 0 | 121 | | parsedValue = null; |
| | | 122 | | |
| | 0 | 123 | | if (GenericHeaderParser.RangeParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 124 | | { |
| | 0 | 125 | | parsedValue = (RangeHeaderValue)output!; |
| | 0 | 126 | | return true; |
| | | 127 | | } |
| | 0 | 128 | | return false; |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal static int GetRangeLength(string? input, int startIndex, out object? parsedValue) |
| | 1080 | 132 | | { |
| | 1080 | 133 | | Debug.Assert(startIndex >= 0); |
| | | 134 | | |
| | 1080 | 135 | | parsedValue = null; |
| | | 136 | | |
| | 1080 | 137 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 138 | | { |
| | 0 | 139 | | return 0; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // Parse the unit string: <unit> in '<unit>=<from1>-<to1>, <from2>-<to2>' |
| | 1080 | 143 | | int unitLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 144 | | |
| | 1080 | 145 | | if (unitLength == 0) |
| | 6 | 146 | | { |
| | 6 | 147 | | return 0; |
| | | 148 | | } |
| | | 149 | | |
| | 1074 | 150 | | RangeHeaderValue result = new RangeHeaderValue(); |
| | 1074 | 151 | | result._unit = input.Substring(startIndex, unitLength); |
| | 1074 | 152 | | int current = startIndex + unitLength; |
| | 1074 | 153 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 154 | | |
| | 1074 | 155 | | if ((current == input.Length) || (input[current] != '=')) |
| | 10 | 156 | | { |
| | 10 | 157 | | return 0; |
| | | 158 | | } |
| | | 159 | | |
| | 1064 | 160 | | current++; // skip '=' separator |
| | 1064 | 161 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 162 | | |
| | 1064 | 163 | | int rangesLength = RangeItemHeaderValue.GetRangeItemListLength(input, current, result.Ranges); |
| | | 164 | | |
| | 1064 | 165 | | if (rangesLength == 0) |
| | 578 | 166 | | { |
| | 578 | 167 | | return 0; |
| | | 168 | | } |
| | | 169 | | |
| | 486 | 170 | | current += rangesLength; |
| | 486 | 171 | | Debug.Assert(current == input.Length, "GetRangeItemListLength() should consume the whole string or fail."); |
| | | 172 | | |
| | 486 | 173 | | parsedValue = result; |
| | 486 | 174 | | return current - startIndex; |
| | 1080 | 175 | | } |
| | | 176 | | |
| | | 177 | | object ICloneable.Clone() |
| | 0 | 178 | | { |
| | 0 | 179 | | return new RangeHeaderValue(this); |
| | 0 | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |