| | | 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 EntityTagHeaderValue : ICloneable |
| | | 10 | | { |
| | 6115 | 11 | | public string Tag { get; private init; } |
| | | 12 | | |
| | 6115 | 13 | | public bool IsWeak { get; private init; } |
| | | 14 | | |
| | 67 | 15 | | public static EntityTagHeaderValue Any { get; } = new EntityTagHeaderValue("*", isWeak: false, false); |
| | | 16 | | |
| | 1787 | 17 | | private EntityTagHeaderValue(string tag, bool isWeak, bool _) |
| | 1787 | 18 | | { |
| | | 19 | | #if DEBUG |
| | | 20 | | // This constructor should only be used with already validated values. |
| | | 21 | | // "*" is a special case that can only be created via the static Any property. |
| | 1787 | 22 | | if (tag != "*") |
| | 1786 | 23 | | { |
| | 1786 | 24 | | new EntityTagHeaderValue(tag, isWeak); |
| | 1786 | 25 | | } |
| | | 26 | | #endif |
| | | 27 | | |
| | 1787 | 28 | | Tag = tag; |
| | 1787 | 29 | | IsWeak = isWeak; |
| | 1787 | 30 | | } |
| | | 31 | | |
| | | 32 | | public EntityTagHeaderValue(string tag) |
| | 0 | 33 | | : this(tag, false) |
| | 0 | 34 | | { |
| | 0 | 35 | | } |
| | | 36 | | |
| | 1786 | 37 | | public EntityTagHeaderValue(string tag, bool isWeak) |
| | 1786 | 38 | | { |
| | 1786 | 39 | | HeaderUtilities.CheckValidQuotedString(tag); |
| | | 40 | | |
| | 1786 | 41 | | Tag = tag; |
| | 1786 | 42 | | IsWeak = isWeak; |
| | 1786 | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | private EntityTagHeaderValue(EntityTagHeaderValue source) |
| | 0 | 46 | | { |
| | 0 | 47 | | Debug.Assert(source != null); |
| | | 48 | | |
| | 0 | 49 | | Tag = source.Tag; |
| | 0 | 50 | | IsWeak = source.IsWeak; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public override string ToString() => |
| | 2542 | 54 | | IsWeak ? $"W/{Tag}" : Tag; |
| | | 55 | | |
| | | 56 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 57 | | obj is EntityTagHeaderValue other && |
| | 0 | 58 | | IsWeak == other.IsWeak && |
| | 0 | 59 | | // Since the tag is a quoted-string we treat it case-sensitive. |
| | 0 | 60 | | string.Equals(Tag, other.Tag, StringComparison.Ordinal); |
| | | 61 | | |
| | | 62 | | public override int GetHashCode() => |
| | 0 | 63 | | HashCode.Combine(Tag, IsWeak); |
| | | 64 | | |
| | | 65 | | public static EntityTagHeaderValue Parse(string input) |
| | 0 | 66 | | { |
| | 0 | 67 | | int index = 0; |
| | 0 | 68 | | return (EntityTagHeaderValue)GenericHeaderParser.SingleValueEntityTagParser.ParseValue( |
| | 0 | 69 | | input, null, ref index); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out EntityTagHeaderValue? par |
| | 0 | 73 | | { |
| | 0 | 74 | | int index = 0; |
| | 0 | 75 | | parsedValue = null; |
| | | 76 | | |
| | 0 | 77 | | if (GenericHeaderParser.SingleValueEntityTagParser.TryParseValue(input, null, ref index, out object? output) |
| | 0 | 78 | | { |
| | 0 | 79 | | parsedValue = (EntityTagHeaderValue)output!; |
| | 0 | 80 | | return true; |
| | | 81 | | } |
| | 0 | 82 | | return false; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | internal static int GetEntityTagLength(string? input, int startIndex, out EntityTagHeaderValue? parsedValue) |
| | 3238 | 86 | | { |
| | 3238 | 87 | | Debug.Assert(startIndex >= 0); |
| | | 88 | | |
| | 3238 | 89 | | parsedValue = null; |
| | | 90 | | |
| | 3238 | 91 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 92 | | { |
| | 0 | 93 | | return 0; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // Caller must remove leading whitespace. If not, we'll return 0. |
| | 3238 | 97 | | bool isWeak = false; |
| | 3238 | 98 | | int current = startIndex; |
| | | 99 | | |
| | 3238 | 100 | | char firstChar = input[startIndex]; |
| | 3238 | 101 | | if (firstChar == '*') |
| | 14 | 102 | | { |
| | | 103 | | // We have '*' value, indicating "any" ETag. |
| | 14 | 104 | | parsedValue = Any; |
| | 14 | 105 | | current++; |
| | 14 | 106 | | } |
| | | 107 | | else |
| | 3224 | 108 | | { |
| | | 109 | | // The RFC defines 'W/' as prefix, but we'll be flexible and also accept lower-case 'w'. |
| | 3224 | 110 | | if ((firstChar == 'W') || (firstChar == 'w')) |
| | 26 | 111 | | { |
| | 26 | 112 | | current++; |
| | | 113 | | // We need at least 3 more chars: the '/' character followed by two quotes. |
| | 26 | 114 | | if ((current + 2 >= input.Length) || (input[current] != '/')) |
| | 14 | 115 | | { |
| | 14 | 116 | | return 0; |
| | | 117 | | } |
| | 12 | 118 | | isWeak = true; |
| | 12 | 119 | | current++; // we have a weak-entity tag. |
| | 12 | 120 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 12 | 121 | | } |
| | | 122 | | |
| | 3210 | 123 | | if (current == input.Length || HttpRuleParser.GetQuotedStringLength(input, current, out int tagLength) ! |
| | 1424 | 124 | | { |
| | 1424 | 125 | | return 0; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | // Most of the time we'll have strong ETags without leading/trailing whitespace. |
| | 1786 | 129 | | Debug.Assert(tagLength != input.Length || (startIndex == 0 && !isWeak)); |
| | | 130 | | |
| | 1786 | 131 | | parsedValue = new EntityTagHeaderValue(input.Substring(current, tagLength), isWeak, false); |
| | | 132 | | |
| | 1786 | 133 | | current += tagLength; |
| | 1786 | 134 | | } |
| | 1800 | 135 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 136 | | |
| | 1800 | 137 | | return current - startIndex; |
| | 3238 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | object ICloneable.Clone() => ReferenceEquals(this, Any) ? Any : new EntityTagHeaderValue(this); |
| | | 141 | | } |
| | | 142 | | } |