| | | 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.Text; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | public class ViaHeaderValue : ICloneable |
| | | 11 | | { |
| | | 12 | | private readonly string? _protocolName; |
| | | 13 | | private readonly string _protocolVersion; |
| | | 14 | | private readonly string _receivedBy; |
| | | 15 | | private readonly string? _comment; |
| | | 16 | | |
| | 0 | 17 | | public string? ProtocolName => _protocolName; |
| | | 18 | | |
| | 0 | 19 | | public string ProtocolVersion => _protocolVersion; |
| | | 20 | | |
| | 0 | 21 | | public string ReceivedBy => _receivedBy; |
| | | 22 | | |
| | 0 | 23 | | public string? Comment => _comment; |
| | | 24 | | |
| | 27248 | 25 | | private ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment, bool _) |
| | 27248 | 26 | | { |
| | | 27 | | #if DEBUG |
| | | 28 | | // This constructor should only be used with already validated values. |
| | 27248 | 29 | | new ViaHeaderValue(protocolVersion, receivedBy, protocolName, comment); |
| | | 30 | | #endif |
| | | 31 | | |
| | 27248 | 32 | | _protocolVersion = protocolVersion; |
| | 27248 | 33 | | _receivedBy = receivedBy; |
| | 27248 | 34 | | _protocolName = protocolName; |
| | 27248 | 35 | | _comment = comment; |
| | 27248 | 36 | | } |
| | | 37 | | |
| | | 38 | | public ViaHeaderValue(string protocolVersion, string receivedBy) |
| | 0 | 39 | | : this(protocolVersion, receivedBy, null, null) |
| | 0 | 40 | | { |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName) |
| | 0 | 44 | | : this(protocolVersion, receivedBy, protocolName, null) |
| | 0 | 45 | | { |
| | 0 | 46 | | } |
| | | 47 | | |
| | 27248 | 48 | | public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment) |
| | 27248 | 49 | | { |
| | 27248 | 50 | | HeaderUtilities.CheckValidToken(protocolVersion); |
| | 27248 | 51 | | CheckReceivedBy(receivedBy); |
| | | 52 | | |
| | 27248 | 53 | | if (!string.IsNullOrEmpty(protocolName)) |
| | 48 | 54 | | { |
| | 48 | 55 | | HeaderUtilities.CheckValidToken(protocolName); |
| | 48 | 56 | | _protocolName = protocolName; |
| | 48 | 57 | | } |
| | | 58 | | |
| | 27248 | 59 | | if (!string.IsNullOrEmpty(comment)) |
| | 146 | 60 | | { |
| | 146 | 61 | | HeaderUtilities.CheckValidComment(comment); |
| | 146 | 62 | | _comment = comment; |
| | 146 | 63 | | } |
| | | 64 | | |
| | 27248 | 65 | | _protocolVersion = protocolVersion; |
| | 27248 | 66 | | _receivedBy = receivedBy; |
| | 27248 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | private ViaHeaderValue(ViaHeaderValue source) |
| | 0 | 70 | | { |
| | 0 | 71 | | Debug.Assert(source != null); |
| | | 72 | | |
| | 0 | 73 | | _protocolName = source._protocolName; |
| | 0 | 74 | | _protocolVersion = source._protocolVersion; |
| | 0 | 75 | | _receivedBy = source._receivedBy; |
| | 0 | 76 | | _comment = source._comment; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public override string ToString() |
| | 44580 | 80 | | { |
| | 44580 | 81 | | var sb = new ValueStringBuilder(stackalloc char[256]); |
| | | 82 | | |
| | 44580 | 83 | | if (!string.IsNullOrEmpty(_protocolName)) |
| | 80 | 84 | | { |
| | 80 | 85 | | sb.Append(_protocolName); |
| | 80 | 86 | | sb.Append('/'); |
| | 80 | 87 | | } |
| | | 88 | | |
| | 44580 | 89 | | sb.Append(_protocolVersion); |
| | 44580 | 90 | | sb.Append(' '); |
| | 44580 | 91 | | sb.Append(_receivedBy); |
| | | 92 | | |
| | 44580 | 93 | | if (!string.IsNullOrEmpty(_comment)) |
| | 130 | 94 | | { |
| | 130 | 95 | | sb.Append(' '); |
| | 130 | 96 | | sb.Append(_comment); |
| | 130 | 97 | | } |
| | | 98 | | |
| | 44580 | 99 | | return sb.ToString(); |
| | 44580 | 100 | | } |
| | | 101 | | |
| | | 102 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 103 | | obj is ViaHeaderValue other && |
| | 0 | 104 | | // Note that for token and host case-insensitive comparison is used. Comments are compared using case- |
| | 0 | 105 | | // sensitive comparison. |
| | 0 | 106 | | string.Equals(_protocolVersion, other._protocolVersion, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 107 | | string.Equals(_receivedBy, other._receivedBy, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 108 | | string.Equals(_protocolName, other._protocolName, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 109 | | string.Equals(_comment, other._comment, StringComparison.Ordinal); |
| | | 110 | | |
| | | 111 | | public override int GetHashCode() => |
| | 0 | 112 | | HashCode.Combine( |
| | 0 | 113 | | StringComparer.OrdinalIgnoreCase.GetHashCode(_protocolVersion), |
| | 0 | 114 | | StringComparer.OrdinalIgnoreCase.GetHashCode(_receivedBy), |
| | 0 | 115 | | _protocolName is null ? 0 : StringComparer.OrdinalIgnoreCase.GetHashCode(_protocolName), |
| | 0 | 116 | | _comment); |
| | | 117 | | |
| | | 118 | | public static ViaHeaderValue Parse(string input) |
| | 0 | 119 | | { |
| | 0 | 120 | | int index = 0; |
| | 0 | 121 | | return (ViaHeaderValue)GenericHeaderParser.SingleValueViaParser.ParseValue(input, null, ref index); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ViaHeaderValue? parsedVal |
| | 0 | 125 | | { |
| | 0 | 126 | | int index = 0; |
| | 0 | 127 | | parsedValue = null; |
| | | 128 | | |
| | 0 | 129 | | if (GenericHeaderParser.SingleValueViaParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 130 | | { |
| | 0 | 131 | | parsedValue = (ViaHeaderValue)output!; |
| | 0 | 132 | | return true; |
| | | 133 | | } |
| | 0 | 134 | | return false; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | internal static int GetViaLength(string? input, int startIndex, out object? parsedValue) |
| | 29724 | 138 | | { |
| | 29724 | 139 | | Debug.Assert(startIndex >= 0); |
| | | 140 | | |
| | 29724 | 141 | | parsedValue = null; |
| | | 142 | | |
| | 29724 | 143 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 144 | | { |
| | 0 | 145 | | return 0; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // Read <protocolName> and <protocolVersion> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>] |
| | 29724 | 149 | | int current = GetProtocolEndIndex(input, startIndex, out string? protocolName, out string? protocolVersion); |
| | | 150 | | |
| | | 151 | | // If we reached the end of the string after reading protocolName/Version we return (we expect at least |
| | | 152 | | // <receivedBy> to follow). If reading protocolName/Version read 0 bytes, we return. |
| | 29724 | 153 | | if ((current == 0) || (current == input.Length)) |
| | 288 | 154 | | { |
| | 288 | 155 | | return 0; |
| | | 156 | | } |
| | 29436 | 157 | | Debug.Assert(protocolVersion != null); |
| | | 158 | | |
| | | 159 | | // Read <receivedBy> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]' |
| | 29436 | 160 | | int receivedByLength = HttpRuleParser.GetHostLength(input, current, true); |
| | 29436 | 161 | | if (receivedByLength == 0) |
| | 384 | 162 | | { |
| | 384 | 163 | | return 0; |
| | | 164 | | } |
| | | 165 | | |
| | 29052 | 166 | | string receivedBy = input.Substring(current, receivedByLength); |
| | 29052 | 167 | | current += receivedByLength; |
| | | 168 | | |
| | 29052 | 169 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 170 | | |
| | 29052 | 171 | | string? comment = null; |
| | 29052 | 172 | | if ((current < input.Length) && (input[current] == '(')) |
| | 1950 | 173 | | { |
| | | 174 | | // We have a <comment> in '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]' |
| | 1950 | 175 | | if (HttpRuleParser.GetCommentLength(input, current, out int commentLength) != HttpParseResult.Parsed) |
| | 1804 | 176 | | { |
| | 1804 | 177 | | return 0; // We found a '(' character but it wasn't a valid comment. Abort. |
| | | 178 | | } |
| | | 179 | | |
| | 146 | 180 | | comment = input.Substring(current, commentLength); |
| | | 181 | | |
| | 146 | 182 | | current += commentLength; |
| | 146 | 183 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 146 | 184 | | } |
| | | 185 | | |
| | 27248 | 186 | | parsedValue = new ViaHeaderValue(protocolVersion, receivedBy, protocolName, comment, false); |
| | 27248 | 187 | | return current - startIndex; |
| | 29724 | 188 | | } |
| | | 189 | | |
| | | 190 | | private static int GetProtocolEndIndex(string input, int startIndex, out string? protocolName, |
| | | 191 | | out string? protocolVersion) |
| | 29724 | 192 | | { |
| | | 193 | | // We have a string of the form '[<protocolName>/]<protocolVersion> <receivedBy> [<comment>]'. The first |
| | | 194 | | // token may either be the protocol name or protocol version. We'll only find out after reading the token |
| | | 195 | | // and by looking at the following character: If it is a '/' we just parsed the protocol name, otherwise |
| | | 196 | | // the protocol version. |
| | 29724 | 197 | | protocolName = null; |
| | 29724 | 198 | | protocolVersion = null; |
| | | 199 | | |
| | 29724 | 200 | | int current = startIndex; |
| | 29724 | 201 | | int protocolVersionOrNameLength = HttpRuleParser.GetTokenLength(input, current); |
| | | 202 | | |
| | 29724 | 203 | | if (protocolVersionOrNameLength == 0) |
| | 68 | 204 | | { |
| | 68 | 205 | | return 0; |
| | | 206 | | } |
| | | 207 | | |
| | 29656 | 208 | | current = startIndex + protocolVersionOrNameLength; |
| | 29656 | 209 | | int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | 29656 | 210 | | current += whitespaceLength; |
| | | 211 | | |
| | 29656 | 212 | | if (current == input.Length) |
| | 8 | 213 | | { |
| | 8 | 214 | | return 0; |
| | | 215 | | } |
| | | 216 | | |
| | 29648 | 217 | | if (input[current] == '/') |
| | 192 | 218 | | { |
| | | 219 | | // We parsed the protocol name |
| | 192 | 220 | | protocolName = input.Substring(startIndex, protocolVersionOrNameLength); |
| | | 221 | | |
| | 192 | 222 | | current++; // skip the '/' delimiter |
| | 192 | 223 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 224 | | |
| | 192 | 225 | | protocolVersionOrNameLength = HttpRuleParser.GetTokenLength(input, current); |
| | | 226 | | |
| | 192 | 227 | | if (protocolVersionOrNameLength == 0) |
| | 48 | 228 | | { |
| | 48 | 229 | | return 0; // We have a string "<token>/" followed by non-token chars. This is invalid. |
| | | 230 | | } |
| | | 231 | | |
| | 144 | 232 | | protocolVersion = input.Substring(current, protocolVersionOrNameLength); |
| | | 233 | | |
| | 144 | 234 | | current += protocolVersionOrNameLength; |
| | 144 | 235 | | whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | 144 | 236 | | current += whitespaceLength; |
| | 144 | 237 | | } |
| | | 238 | | else |
| | 29456 | 239 | | { |
| | 29456 | 240 | | protocolVersion = input.Substring(startIndex, protocolVersionOrNameLength); |
| | 29456 | 241 | | } |
| | | 242 | | |
| | 29600 | 243 | | if (whitespaceLength == 0) |
| | 164 | 244 | | { |
| | 164 | 245 | | return 0; // We were able to parse [<protocolName>/]<protocolVersion> but it wasn't followed by a WS |
| | | 246 | | } |
| | | 247 | | |
| | 29436 | 248 | | return current; |
| | 29724 | 249 | | } |
| | | 250 | | |
| | | 251 | | object ICloneable.Clone() |
| | 0 | 252 | | { |
| | 0 | 253 | | return new ViaHeaderValue(this); |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | private static void CheckReceivedBy(string receivedBy) |
| | 27248 | 257 | | { |
| | 27248 | 258 | | ArgumentException.ThrowIfNullOrEmpty(receivedBy); |
| | | 259 | | |
| | | 260 | | // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value |
| | | 261 | | // is a valid host.; |
| | 27248 | 262 | | if (HttpRuleParser.GetHostLength(receivedBy, 0, true) != receivedBy.Length) |
| | 0 | 263 | | { |
| | 0 | 264 | | throw new FormatException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_heade |
| | | 265 | | } |
| | 27248 | 266 | | } |
| | | 267 | | } |
| | | 268 | | } |