| | | 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 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | public class RangeConditionHeaderValue : ICloneable |
| | | 10 | | { |
| | | 11 | | // Exactly one of date and entityTag will be set. |
| | | 12 | | private readonly DateTimeOffset _date; |
| | | 13 | | private readonly EntityTagHeaderValue? _entityTag; |
| | | 14 | | |
| | 0 | 15 | | public DateTimeOffset? Date => _entityTag is null ? _date : null; |
| | | 16 | | |
| | 0 | 17 | | public EntityTagHeaderValue? EntityTag => _entityTag; |
| | | 18 | | |
| | 0 | 19 | | public RangeConditionHeaderValue(DateTimeOffset date) |
| | 0 | 20 | | { |
| | 0 | 21 | | _date = date; |
| | 0 | 22 | | } |
| | | 23 | | |
| | 86 | 24 | | public RangeConditionHeaderValue(EntityTagHeaderValue entityTag) |
| | 86 | 25 | | { |
| | 86 | 26 | | ArgumentNullException.ThrowIfNull(entityTag); |
| | | 27 | | |
| | 86 | 28 | | _entityTag = entityTag; |
| | 86 | 29 | | } |
| | | 30 | | |
| | | 31 | | public RangeConditionHeaderValue(string entityTag) |
| | 0 | 32 | | : this(new EntityTagHeaderValue(entityTag)) |
| | 0 | 33 | | { |
| | 0 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | private RangeConditionHeaderValue(RangeConditionHeaderValue source) |
| | 0 | 37 | | { |
| | 0 | 38 | | Debug.Assert(source != null); |
| | | 39 | | |
| | 0 | 40 | | _entityTag = source._entityTag; |
| | 0 | 41 | | _date = source._date; |
| | 0 | 42 | | } |
| | | 43 | | |
| | 129 | 44 | | public override string ToString() => _entityTag?.ToString() ?? _date.ToString("r"); |
| | | 45 | | |
| | | 46 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 47 | | obj is RangeConditionHeaderValue other && |
| | 0 | 48 | | (_entityTag is null ? other._entityTag is null : _entityTag.Equals(other._entityTag)) && |
| | 0 | 49 | | _date == other._date; |
| | | 50 | | |
| | 0 | 51 | | public override int GetHashCode() => _entityTag?.GetHashCode() ?? _date.GetHashCode(); |
| | | 52 | | |
| | | 53 | | public static RangeConditionHeaderValue Parse(string input) |
| | 0 | 54 | | { |
| | 0 | 55 | | int index = 0; |
| | 0 | 56 | | return (RangeConditionHeaderValue)GenericHeaderParser.RangeConditionParser.ParseValue( |
| | 0 | 57 | | input, null, ref index); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out RangeConditionHeaderValue |
| | 0 | 61 | | { |
| | 0 | 62 | | int index = 0; |
| | 0 | 63 | | parsedValue = null; |
| | | 64 | | |
| | 0 | 65 | | if (GenericHeaderParser.RangeConditionParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 66 | | { |
| | 0 | 67 | | parsedValue = (RangeConditionHeaderValue)output!; |
| | 0 | 68 | | return true; |
| | | 69 | | } |
| | 0 | 70 | | return false; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | internal static int GetRangeConditionLength(string? input, int startIndex, out object? parsedValue) |
| | 1328 | 74 | | { |
| | 1328 | 75 | | Debug.Assert(startIndex >= 0); |
| | | 76 | | |
| | 1328 | 77 | | parsedValue = null; |
| | | 78 | | |
| | | 79 | | // Make sure we have at least 2 characters |
| | 1328 | 80 | | if (string.IsNullOrEmpty(input) || (startIndex + 1 >= input.Length)) |
| | 4 | 81 | | { |
| | 4 | 82 | | return 0; |
| | | 83 | | } |
| | | 84 | | |
| | 1324 | 85 | | int current = startIndex; |
| | | 86 | | |
| | | 87 | | // Caller must remove leading whitespace. |
| | 1324 | 88 | | DateTimeOffset date = DateTimeOffset.MinValue; |
| | 1324 | 89 | | EntityTagHeaderValue? entityTag = null; |
| | | 90 | | |
| | | 91 | | // Entity tags are quoted strings optionally preceded by "W/". By looking at the first two character we |
| | | 92 | | // can determine whether the string is en entity tag or a date. |
| | 1324 | 93 | | char firstChar = input[current]; |
| | 1324 | 94 | | char secondChar = input[current + 1]; |
| | | 95 | | |
| | 1324 | 96 | | if ((firstChar == '\"') || (((firstChar == 'w') || (firstChar == 'W')) && (secondChar == '/'))) |
| | 1306 | 97 | | { |
| | | 98 | | // trailing whitespace is removed by GetEntityTagLength() |
| | 1306 | 99 | | int entityTagLength = EntityTagHeaderValue.GetEntityTagLength(input, current, out entityTag); |
| | | 100 | | |
| | 1306 | 101 | | if (entityTagLength == 0) |
| | 1194 | 102 | | { |
| | 1194 | 103 | | return 0; |
| | | 104 | | } |
| | | 105 | | |
| | 112 | 106 | | current += entityTagLength; |
| | | 107 | | |
| | | 108 | | // RangeConditionHeaderValue only allows 1 value. There must be no delimiter/other chars after an |
| | | 109 | | // entity tag. |
| | 112 | 110 | | if (current != input.Length) |
| | 26 | 111 | | { |
| | 26 | 112 | | return 0; |
| | | 113 | | } |
| | 86 | 114 | | } |
| | | 115 | | else |
| | 18 | 116 | | { |
| | 18 | 117 | | if (!HttpDateParser.TryParse(input.AsSpan(current), out date)) |
| | 18 | 118 | | { |
| | 18 | 119 | | return 0; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // If we got a valid date, then the parser consumed the whole string (incl. trailing whitespace). |
| | 0 | 123 | | current = input.Length; |
| | 0 | 124 | | } |
| | | 125 | | |
| | 86 | 126 | | if (entityTag == null) |
| | 0 | 127 | | { |
| | 0 | 128 | | parsedValue = new RangeConditionHeaderValue(date); |
| | 0 | 129 | | } |
| | | 130 | | else |
| | 86 | 131 | | { |
| | 86 | 132 | | parsedValue = new RangeConditionHeaderValue(entityTag); |
| | 86 | 133 | | } |
| | | 134 | | |
| | 86 | 135 | | return current - startIndex; |
| | 1328 | 136 | | } |
| | | 137 | | |
| | | 138 | | object ICloneable.Clone() |
| | 0 | 139 | | { |
| | 0 | 140 | | return new RangeConditionHeaderValue(this); |
| | 0 | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |