| | | 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.Globalization; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | internal sealed class Int64NumberHeaderParser : BaseHeaderParser |
| | | 10 | | { |
| | | 11 | | // Note that we don't need a custom comparer even though we have a value type that gets boxed (comparing two |
| | | 12 | | // equal boxed value types returns 'false' since the object instances used for boxing the two values are |
| | | 13 | | // different). The reason is that the comparer is only used by HttpHeaders when comparing values in a collection |
| | | 14 | | // Value types are never used in collections (in fact HttpHeaderValueCollection expects T to be a reference |
| | | 15 | | // type). |
| | | 16 | | |
| | 1 | 17 | | internal static readonly Int64NumberHeaderParser Parser = new Int64NumberHeaderParser(); |
| | | 18 | | |
| | | 19 | | private Int64NumberHeaderParser() |
| | 1 | 20 | | : base(false) |
| | 1 | 21 | | { |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | public override string ToString(object value) |
| | 57 | 25 | | { |
| | 57 | 26 | | Debug.Assert(value is long); |
| | | 27 | | |
| | 57 | 28 | | return ((long)value).ToString(NumberFormatInfo.InvariantInfo); |
| | 57 | 29 | | } |
| | | 30 | | |
| | | 31 | | protected override int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 32 | | out object? parsedValue) |
| | 86 | 33 | | { |
| | 86 | 34 | | parsedValue = null; |
| | | 35 | | |
| | 86 | 36 | | int numberLength = HttpRuleParser.GetNumberLength(value, startIndex, false); |
| | | 37 | | |
| | 86 | 38 | | if ((numberLength == 0) || (numberLength > HttpRuleParser.MaxInt64Digits)) |
| | 14 | 39 | | { |
| | 14 | 40 | | return 0; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | long result; |
| | 72 | 44 | | if (!HeaderUtilities.TryParseInt64(value, startIndex, numberLength, out result)) |
| | 0 | 45 | | { |
| | 0 | 46 | | return 0; |
| | | 47 | | } |
| | | 48 | | |
| | 72 | 49 | | parsedValue = result; |
| | 72 | 50 | | return numberLength; |
| | 86 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |