| | | 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.Globalization; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | public class RetryConditionHeaderValue : ICloneable |
| | | 11 | | { |
| | | 12 | | private const long DeltaNotSetTicksSentinel = long.MaxValue; |
| | | 13 | | |
| | | 14 | | // Only one of date and delta may be set. |
| | | 15 | | private readonly DateTimeOffset _date; |
| | | 16 | | private readonly TimeSpan _delta; |
| | | 17 | | |
| | 0 | 18 | | public DateTimeOffset? Date => _delta.Ticks == DeltaNotSetTicksSentinel ? _date : null; |
| | | 19 | | |
| | 0 | 20 | | public TimeSpan? Delta => _delta.Ticks == DeltaNotSetTicksSentinel ? null : _delta; |
| | | 21 | | |
| | 0 | 22 | | public RetryConditionHeaderValue(DateTimeOffset date) |
| | 0 | 23 | | { |
| | 0 | 24 | | _date = date; |
| | 0 | 25 | | _delta = new TimeSpan(DeltaNotSetTicksSentinel); |
| | 0 | 26 | | } |
| | | 27 | | |
| | 2 | 28 | | public RetryConditionHeaderValue(TimeSpan delta) |
| | 2 | 29 | | { |
| | | 30 | | // The amount of seconds for 'delta' must be in the range 0..2^31 |
| | 2 | 31 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(delta.TotalSeconds, int.MaxValue); |
| | | 32 | | |
| | 2 | 33 | | _delta = delta; |
| | 2 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | private RetryConditionHeaderValue(RetryConditionHeaderValue source) |
| | 0 | 37 | | { |
| | 0 | 38 | | Debug.Assert(source != null); |
| | | 39 | | |
| | 0 | 40 | | _delta = source._delta; |
| | 0 | 41 | | _date = source._date; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public override string ToString() => |
| | 3 | 45 | | _delta.Ticks != DeltaNotSetTicksSentinel |
| | 3 | 46 | | ? ((int)_delta.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo) |
| | 3 | 47 | | : _date.ToString("r"); |
| | | 48 | | |
| | | 49 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 50 | | obj is RetryConditionHeaderValue other && |
| | 0 | 51 | | _delta == other._delta && |
| | 0 | 52 | | _date == other._date; |
| | | 53 | | |
| | | 54 | | public override int GetHashCode() => |
| | 0 | 55 | | HashCode.Combine(_delta, _date); |
| | | 56 | | |
| | | 57 | | public static RetryConditionHeaderValue Parse(string input) |
| | 0 | 58 | | { |
| | 0 | 59 | | int index = 0; |
| | 0 | 60 | | return (RetryConditionHeaderValue)GenericHeaderParser.RetryConditionParser.ParseValue( |
| | 0 | 61 | | input, null, ref index); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out RetryConditionHeaderValue |
| | 0 | 65 | | { |
| | 0 | 66 | | int index = 0; |
| | 0 | 67 | | parsedValue = null; |
| | | 68 | | |
| | 0 | 69 | | if (GenericHeaderParser.RetryConditionParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 70 | | { |
| | 0 | 71 | | parsedValue = (RetryConditionHeaderValue)output!; |
| | 0 | 72 | | return true; |
| | | 73 | | } |
| | 0 | 74 | | return false; |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal static int GetRetryConditionLength(string? input, int startIndex, out object? parsedValue) |
| | 32 | 78 | | { |
| | 32 | 79 | | Debug.Assert(startIndex >= 0); |
| | | 80 | | |
| | 32 | 81 | | parsedValue = null; |
| | | 82 | | |
| | 32 | 83 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 84 | | { |
| | 0 | 85 | | return 0; |
| | | 86 | | } |
| | | 87 | | |
| | 32 | 88 | | int current = startIndex; |
| | | 89 | | |
| | | 90 | | // Caller must remove leading whitespace. |
| | 32 | 91 | | DateTimeOffset date = DateTimeOffset.MinValue; |
| | 32 | 92 | | int deltaSeconds = -1; // use -1 to indicate that the value was not set. 'delta' values are always >=0 |
| | | 93 | | |
| | | 94 | | // We either have a timespan or a date/time value. Determine which one we have by looking at the first char. |
| | | 95 | | // If it is a number, we have a timespan, otherwise we assume we have a date. |
| | 32 | 96 | | char firstChar = input[current]; |
| | | 97 | | |
| | 32 | 98 | | if (char.IsAsciiDigit(firstChar)) |
| | 30 | 99 | | { |
| | 30 | 100 | | int deltaStartIndex = current; |
| | 30 | 101 | | int deltaLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 102 | | |
| | | 103 | | // The value must be in the range 0..2^31 |
| | 30 | 104 | | if ((deltaLength == 0) || (deltaLength > HttpRuleParser.MaxInt32Digits)) |
| | 18 | 105 | | { |
| | 18 | 106 | | return 0; |
| | | 107 | | } |
| | | 108 | | |
| | 12 | 109 | | current += deltaLength; |
| | 12 | 110 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 111 | | |
| | | 112 | | // RetryConditionHeaderValue only allows 1 value. There must be no delimiter/other chars after 'delta' |
| | 12 | 113 | | if (current != input.Length) |
| | 6 | 114 | | { |
| | 6 | 115 | | return 0; |
| | | 116 | | } |
| | | 117 | | |
| | 6 | 118 | | if (!HeaderUtilities.TryParseInt32(input, deltaStartIndex, deltaLength, out deltaSeconds)) |
| | 4 | 119 | | { |
| | 4 | 120 | | return 0; // int.TryParse() may return 'false' if the value has 10 digits and is > Int32.MaxValue. |
| | | 121 | | } |
| | 2 | 122 | | } |
| | | 123 | | else |
| | 2 | 124 | | { |
| | 2 | 125 | | if (!HttpDateParser.TryParse(input.AsSpan(current), out date)) |
| | 2 | 126 | | { |
| | 2 | 127 | | return 0; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | // If we got a valid date, then the parser consumed the whole string (incl. trailing whitespace). |
| | 0 | 131 | | current = input.Length; |
| | 0 | 132 | | } |
| | | 133 | | |
| | 2 | 134 | | if (deltaSeconds == -1) // we didn't change delta, so we must have found a date. |
| | 0 | 135 | | { |
| | 0 | 136 | | parsedValue = new RetryConditionHeaderValue(date); |
| | 0 | 137 | | } |
| | | 138 | | else |
| | 2 | 139 | | { |
| | 2 | 140 | | parsedValue = new RetryConditionHeaderValue(new TimeSpan(0, 0, deltaSeconds)); |
| | 2 | 141 | | } |
| | | 142 | | |
| | 2 | 143 | | return current - startIndex; |
| | 32 | 144 | | } |
| | | 145 | | |
| | | 146 | | object ICloneable.Clone() |
| | 0 | 147 | | { |
| | 0 | 148 | | return new RetryConditionHeaderValue(this); |
| | 0 | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |