| | | 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 ProductInfoHeaderValue : ICloneable |
| | | 10 | | { |
| | | 11 | | private readonly ProductHeaderValue? _product; |
| | | 12 | | private readonly string? _comment; |
| | | 13 | | |
| | 0 | 14 | | public ProductHeaderValue? Product => _product; |
| | | 15 | | |
| | 0 | 16 | | public string? Comment => _comment; |
| | | 17 | | |
| | | 18 | | public ProductInfoHeaderValue(string productName, string? productVersion) |
| | 0 | 19 | | : this(new ProductHeaderValue(productName, productVersion)) |
| | 0 | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | 37069 | 23 | | public ProductInfoHeaderValue(ProductHeaderValue product) |
| | 37069 | 24 | | { |
| | 37069 | 25 | | ArgumentNullException.ThrowIfNull(product); |
| | | 26 | | |
| | 37069 | 27 | | _product = product; |
| | 37069 | 28 | | } |
| | | 29 | | |
| | 1080 | 30 | | public ProductInfoHeaderValue(string comment) |
| | 1080 | 31 | | { |
| | 1080 | 32 | | HeaderUtilities.CheckValidComment(comment); |
| | 1080 | 33 | | _comment = comment; |
| | 1080 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | private ProductInfoHeaderValue(ProductInfoHeaderValue source) |
| | 0 | 37 | | { |
| | 0 | 38 | | Debug.Assert(source != null); |
| | | 39 | | |
| | 0 | 40 | | _product = source._product; |
| | 0 | 41 | | _comment = source._comment; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public override string ToString() |
| | 59315 | 45 | | { |
| | 59315 | 46 | | if (_product == null) |
| | 1590 | 47 | | { |
| | 1590 | 48 | | Debug.Assert(_comment != null); |
| | 1590 | 49 | | return _comment; |
| | | 50 | | } |
| | 57725 | 51 | | return _product.ToString(); |
| | 59315 | 52 | | } |
| | | 53 | | |
| | | 54 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 55 | | { |
| | 0 | 56 | | ProductInfoHeaderValue? other = obj as ProductInfoHeaderValue; |
| | | 57 | | |
| | 0 | 58 | | if (other == null) |
| | 0 | 59 | | { |
| | 0 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | if (_product == null) |
| | 0 | 64 | | { |
| | | 65 | | // We compare comments using case-sensitive comparison. |
| | 0 | 66 | | return string.Equals(_comment, other._comment, StringComparison.Ordinal); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return _product.Equals(other._product); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public override int GetHashCode() |
| | 0 | 73 | | { |
| | 0 | 74 | | if (_product == null) |
| | 0 | 75 | | { |
| | 0 | 76 | | Debug.Assert(_comment != null); |
| | 0 | 77 | | return _comment.GetHashCode(); |
| | | 78 | | } |
| | 0 | 79 | | return _product.GetHashCode(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public static ProductInfoHeaderValue Parse(string input) |
| | 0 | 83 | | { |
| | 0 | 84 | | int index = 0; |
| | 0 | 85 | | object result = ProductInfoHeaderParser.SingleValueParser.ParseValue( |
| | 0 | 86 | | input, null, ref index); |
| | 0 | 87 | | if (index < input.Length) |
| | 0 | 88 | | { |
| | | 89 | | // There is some invalid leftover data. Normally BaseHeaderParser.TryParseValue would |
| | | 90 | | // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser. |
| | 0 | 91 | | throw new FormatException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_heade |
| | | 92 | | } |
| | 0 | 93 | | return (ProductInfoHeaderValue)result; |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | public static bool TryParse([NotNullWhen(true)] string input, [NotNullWhen(true)] out ProductInfoHeaderValue? pa |
| | 0 | 97 | | { |
| | 0 | 98 | | int index = 0; |
| | 0 | 99 | | parsedValue = null; |
| | | 100 | | |
| | 0 | 101 | | if (ProductInfoHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 102 | | { |
| | 0 | 103 | | if (index < input.Length) |
| | 0 | 104 | | { |
| | | 105 | | // There is some invalid leftover data. Normally BaseHeaderParser.TryParseValue would |
| | | 106 | | // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser. |
| | 0 | 107 | | return false; |
| | | 108 | | } |
| | 0 | 109 | | parsedValue = (ProductInfoHeaderValue)output; |
| | 0 | 110 | | return true; |
| | | 111 | | } |
| | 0 | 112 | | return false; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal static int GetProductInfoLength(string? input, int startIndex, out ProductInfoHeaderValue? parsedValue) |
| | 38757 | 116 | | { |
| | 38757 | 117 | | Debug.Assert(startIndex >= 0); |
| | | 118 | | |
| | 38757 | 119 | | parsedValue = null; |
| | | 120 | | |
| | 38757 | 121 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 122 | | { |
| | 0 | 123 | | return 0; |
| | | 124 | | } |
| | | 125 | | |
| | 38757 | 126 | | int current = startIndex; |
| | | 127 | | |
| | | 128 | | // Caller must remove leading whitespace. |
| | | 129 | | string? comment; |
| | | 130 | | ProductHeaderValue? product; |
| | 38757 | 131 | | if (input[current] == '(') |
| | 1596 | 132 | | { |
| | | 133 | | int commentLength; |
| | 1596 | 134 | | if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed) |
| | 516 | 135 | | { |
| | 516 | 136 | | return 0; |
| | | 137 | | } |
| | | 138 | | |
| | 1080 | 139 | | comment = input.Substring(current, commentLength); |
| | | 140 | | |
| | 1080 | 141 | | current += commentLength; |
| | 1080 | 142 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 143 | | |
| | 1080 | 144 | | parsedValue = new ProductInfoHeaderValue(comment); |
| | 1080 | 145 | | } |
| | | 146 | | else |
| | 37161 | 147 | | { |
| | | 148 | | // Trailing whitespace is removed by GetProductLength(). |
| | 37161 | 149 | | int productLength = ProductHeaderValue.GetProductLength(input, current, out product); |
| | | 150 | | |
| | 37161 | 151 | | if (productLength == 0) |
| | 92 | 152 | | { |
| | 92 | 153 | | return 0; |
| | | 154 | | } |
| | | 155 | | |
| | 37069 | 156 | | current += productLength; |
| | | 157 | | |
| | 37069 | 158 | | parsedValue = new ProductInfoHeaderValue(product!); |
| | 37069 | 159 | | } |
| | | 160 | | |
| | 38149 | 161 | | return current - startIndex; |
| | 38757 | 162 | | } |
| | | 163 | | |
| | | 164 | | object ICloneable.Clone() |
| | 0 | 165 | | { |
| | 0 | 166 | | return new ProductInfoHeaderValue(this); |
| | 0 | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |