| | | 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 | | /// <summary> |
| | | 11 | | /// Parses Alt-Svc header values, per RFC 7838 Section 3. |
| | | 12 | | /// </summary> |
| | | 13 | | internal sealed class AltSvcHeaderParser : BaseHeaderParser |
| | | 14 | | { |
| | | 15 | | internal const long DefaultMaxAgeTicks = 24 * TimeSpan.TicksPerHour; |
| | | 16 | | |
| | 2 | 17 | | public static AltSvcHeaderParser Parser { get; } = new AltSvcHeaderParser(); |
| | | 18 | | |
| | | 19 | | private AltSvcHeaderParser() |
| | 1 | 20 | | : base(supportsMultipleValues: true) |
| | 1 | 21 | | { |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected override int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 25 | | out object? parsedValue) |
| | 672 | 26 | | { |
| | 672 | 27 | | Debug.Assert(startIndex >= 0); |
| | 672 | 28 | | Debug.Assert(startIndex < value.Length); |
| | | 29 | | |
| | 672 | 30 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 31 | | { |
| | 0 | 32 | | parsedValue = null; |
| | 0 | 33 | | return 0; |
| | | 34 | | } |
| | | 35 | | |
| | 672 | 36 | | int idx = startIndex; |
| | | 37 | | |
| | 672 | 38 | | if (!TryReadPercentEncodedAlpnProtocolName(value, idx, out string? alpnProtocolName, out int alpnProtocolNam |
| | 330 | 39 | | { |
| | 330 | 40 | | parsedValue = null; |
| | 330 | 41 | | return 0; |
| | | 42 | | } |
| | | 43 | | |
| | 342 | 44 | | idx += alpnProtocolNameLength; |
| | | 45 | | |
| | 342 | 46 | | if (alpnProtocolName == AltSvcHeaderValue.ClearString) |
| | 0 | 47 | | { |
| | 0 | 48 | | if (idx != value.Length) |
| | 0 | 49 | | { |
| | | 50 | | // Clear has no parameters and should be the only Alt-Svc value present, so there should be nothing |
| | 0 | 51 | | parsedValue = null; |
| | 0 | 52 | | return 0; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | parsedValue = AltSvcHeaderValue.Clear; |
| | 0 | 56 | | return idx - startIndex; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Make sure we have at least 2 characters and first one being an '='. |
| | 342 | 60 | | if (idx + 1 >= value.Length || value[idx++] != '=') |
| | 306 | 61 | | { |
| | 306 | 62 | | parsedValue = null; |
| | 306 | 63 | | return 0; |
| | | 64 | | } |
| | | 65 | | |
| | 36 | 66 | | if (!TryReadQuotedAltAuthority(value, idx, out string? altAuthorityHost, out int altAuthorityPort, out int a |
| | 36 | 67 | | { |
| | 36 | 68 | | parsedValue = null; |
| | 36 | 69 | | return 0; |
| | | 70 | | } |
| | 0 | 71 | | idx += altAuthorityLength; |
| | | 72 | | |
| | | 73 | | // Parse parameters: *( OWS ";" OWS parameter ) |
| | 0 | 74 | | int? maxAge = null; |
| | 0 | 75 | | bool persist = false; |
| | | 76 | | |
| | 0 | 77 | | while (idx < value.Length) |
| | 0 | 78 | | { |
| | | 79 | | // Skip OWS before semicolon. |
| | 0 | 80 | | while (idx < value.Length && IsOptionalWhiteSpace(value[idx])) ++idx; |
| | | 81 | | |
| | 0 | 82 | | if (idx == value.Length) |
| | 0 | 83 | | { |
| | 0 | 84 | | parsedValue = null; |
| | 0 | 85 | | return 0; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | char ch = value[idx]; |
| | | 89 | | |
| | 0 | 90 | | if (ch == ',') |
| | 0 | 91 | | { |
| | | 92 | | // Multi-value header: return this value; will get called again to parse the next. |
| | 0 | 93 | | break; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | if (ch != ';') |
| | 0 | 97 | | { |
| | | 98 | | // Expecting parameters starting with semicolon; fail out. |
| | 0 | 99 | | parsedValue = null; |
| | 0 | 100 | | return 0; |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | ++idx; |
| | | 104 | | |
| | | 105 | | // Skip OWS after semicolon / before value. |
| | 0 | 106 | | while (idx < value.Length && IsOptionalWhiteSpace(value[idx])) ++idx; |
| | | 107 | | |
| | | 108 | | // Get the parameter key length. |
| | 0 | 109 | | int tokenLength = HttpRuleParser.GetTokenLength(value, idx); |
| | 0 | 110 | | if (tokenLength == 0) |
| | 0 | 111 | | { |
| | 0 | 112 | | parsedValue = null; |
| | 0 | 113 | | return 0; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if ((idx + tokenLength) >= value.Length || value[idx + tokenLength] != '=') |
| | 0 | 117 | | { |
| | 0 | 118 | | parsedValue = null; |
| | 0 | 119 | | return 0; |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | if (tokenLength == 2 && value[idx] == 'm' && value[idx + 1] == 'a') |
| | 0 | 123 | | { |
| | | 124 | | // Parse "ma" (Max Age). |
| | | 125 | | |
| | 0 | 126 | | idx += 3; // Skip "ma=" |
| | 0 | 127 | | if (!TryReadTokenOrQuotedInt32(value, idx, out int maxAgeTmp, out int parameterLength)) |
| | 0 | 128 | | { |
| | 0 | 129 | | parsedValue = null; |
| | 0 | 130 | | return 0; |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | if (maxAge == null) |
| | 0 | 134 | | { |
| | 0 | 135 | | maxAge = maxAgeTmp; |
| | 0 | 136 | | } |
| | | 137 | | else |
| | 0 | 138 | | { |
| | | 139 | | // RFC makes it unclear what to do if a duplicate parameter is found. For now, take the minimum. |
| | 0 | 140 | | maxAge = Math.Min(maxAge.GetValueOrDefault(), maxAgeTmp); |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | idx += parameterLength; |
| | 0 | 144 | | } |
| | 0 | 145 | | else if (value.AsSpan(idx).StartsWith("persist=")) |
| | 0 | 146 | | { |
| | 0 | 147 | | idx += 8; // Skip "persist=" |
| | 0 | 148 | | if (TryReadTokenOrQuotedInt32(value, idx, out int persistInt, out int parameterLength)) |
| | 0 | 149 | | { |
| | 0 | 150 | | persist = persistInt == 1; |
| | 0 | 151 | | } |
| | 0 | 152 | | else if (!TrySkipTokenOrQuoted(value, idx, out parameterLength)) |
| | 0 | 153 | | { |
| | | 154 | | // Cold path: unsupported value, just skip the parameter. |
| | 0 | 155 | | parsedValue = null; |
| | 0 | 156 | | return 0; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | idx += parameterLength; |
| | 0 | 160 | | } |
| | | 161 | | else |
| | 0 | 162 | | { |
| | | 163 | | // Some unknown parameter. Skip it. |
| | | 164 | | |
| | 0 | 165 | | idx += tokenLength + 1; |
| | 0 | 166 | | if (!TrySkipTokenOrQuoted(value, idx, out int parameterLength)) |
| | 0 | 167 | | { |
| | 0 | 168 | | parsedValue = null; |
| | 0 | 169 | | return 0; |
| | | 170 | | } |
| | 0 | 171 | | idx += parameterLength; |
| | 0 | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | // If no "ma" parameter present, use the default. |
| | 0 | 176 | | TimeSpan maxAgeTimeSpan = TimeSpan.FromTicks(maxAge * TimeSpan.TicksPerSecond ?? DefaultMaxAgeTicks); |
| | | 177 | | |
| | 0 | 178 | | parsedValue = new AltSvcHeaderValue(alpnProtocolName, altAuthorityHost, altAuthorityPort, maxAgeTimeSpan, pe |
| | 0 | 179 | | return idx - startIndex; |
| | 672 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static bool IsOptionalWhiteSpace(char ch) |
| | 0 | 183 | | { |
| | 0 | 184 | | return ch == ' ' || ch == '\t'; |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | private static bool TryReadPercentEncodedAlpnProtocolName(string value, int startIndex, [NotNullWhen(true)] out |
| | 672 | 188 | | { |
| | 672 | 189 | | int tokenLength = HttpRuleParser.GetTokenLength(value, startIndex); |
| | | 190 | | |
| | 672 | 191 | | if (tokenLength == 0) |
| | 12 | 192 | | { |
| | 12 | 193 | | result = ""; |
| | 12 | 194 | | readLength = 0; |
| | 12 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | |
| | 660 | 198 | | ReadOnlySpan<char> span = value.AsSpan(startIndex, tokenLength); |
| | | 199 | | |
| | 660 | 200 | | readLength = tokenLength; |
| | | 201 | | |
| | | 202 | | // Special-case expected values to avoid allocating one-off strings. |
| | 660 | 203 | | switch (span.Length) |
| | | 204 | | { |
| | 84 | 205 | | case 2 when span is "h3": |
| | 6 | 206 | | result = "h3"; |
| | 6 | 207 | | return true; |
| | | 208 | | |
| | 78 | 209 | | case 2 when span is "h2": |
| | 6 | 210 | | result = "h2"; |
| | 6 | 211 | | return true; |
| | | 212 | | |
| | 100 | 213 | | case 3 when span is "h2c": |
| | 0 | 214 | | result = "h2c"; |
| | 0 | 215 | | readLength = 3; |
| | 0 | 216 | | return true; |
| | | 217 | | |
| | 12 | 218 | | case 5 when span is "clear": |
| | 0 | 219 | | result = "clear"; |
| | 0 | 220 | | return true; |
| | | 221 | | |
| | 14 | 222 | | case 10 when span.StartsWith("http%2F1."): |
| | 0 | 223 | | char ch = span[9]; |
| | 0 | 224 | | if (ch == '1') |
| | 0 | 225 | | { |
| | 0 | 226 | | result = "http/1.1"; |
| | 0 | 227 | | return true; |
| | | 228 | | } |
| | 0 | 229 | | if (ch == '0') |
| | 0 | 230 | | { |
| | 0 | 231 | | result = "http/1.0"; |
| | 0 | 232 | | return true; |
| | | 233 | | } |
| | 0 | 234 | | break; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | // Unrecognized ALPN protocol name. Percent-decode. |
| | 648 | 238 | | return TryReadUnknownPercentEncodedAlpnProtocolName(span, out result); |
| | 672 | 239 | | } |
| | | 240 | | |
| | | 241 | | private static bool TryReadUnknownPercentEncodedAlpnProtocolName(ReadOnlySpan<char> value, [NotNullWhen(true)] o |
| | 648 | 242 | | { |
| | 648 | 243 | | int idx = value.IndexOf('%'); |
| | | 244 | | |
| | 648 | 245 | | if (idx < 0) |
| | 188 | 246 | | { |
| | 188 | 247 | | result = new string(value); |
| | 188 | 248 | | return true; |
| | | 249 | | } |
| | | 250 | | |
| | 460 | 251 | | var builder = value.Length <= 128 ? |
| | 460 | 252 | | new ValueStringBuilder(stackalloc char[128]) : |
| | 460 | 253 | | new ValueStringBuilder(value.Length); |
| | | 254 | | |
| | | 255 | | do |
| | 2412 | 256 | | { |
| | 2412 | 257 | | if (idx != 0) |
| | 1058 | 258 | | { |
| | 1058 | 259 | | builder.Append(value.Slice(0, idx)); |
| | 1058 | 260 | | } |
| | | 261 | | |
| | 2412 | 262 | | if ((value.Length - idx) < 3 || !TryReadAlpnHexDigit(value[idx + 1], out int hi) || !TryReadAlpnHexDigit |
| | 330 | 263 | | { |
| | 330 | 264 | | builder.Dispose(); |
| | 330 | 265 | | result = null; |
| | 330 | 266 | | return false; |
| | | 267 | | } |
| | | 268 | | |
| | 2082 | 269 | | builder.Append((char)((hi << 4) | lo)); |
| | | 270 | | |
| | 2082 | 271 | | value = value.Slice(idx + 3); |
| | 2082 | 272 | | idx = value.IndexOf('%'); |
| | 2082 | 273 | | } |
| | 2082 | 274 | | while (idx != -1); |
| | | 275 | | |
| | 130 | 276 | | if (value.Length != 0) |
| | 84 | 277 | | { |
| | 84 | 278 | | builder.Append(value); |
| | 84 | 279 | | } |
| | | 280 | | |
| | 130 | 281 | | result = builder.ToString(); |
| | 130 | 282 | | return !HttpRuleParser.ContainsNewLineOrNull(result); |
| | 648 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Reads a hex nibble. Specialized for ALPN protocol names as they explicitly can not contain lower-case hex. |
| | | 287 | | /// </summary> |
| | | 288 | | private static bool TryReadAlpnHexDigit(char ch, out int nibble) |
| | 4354 | 289 | | { |
| | 4354 | 290 | | int result = HexConverter.FromUpperChar(ch); |
| | 4354 | 291 | | if (result == 0xFF) |
| | 112 | 292 | | { |
| | 112 | 293 | | nibble = 0; |
| | 112 | 294 | | return false; |
| | | 295 | | } |
| | | 296 | | |
| | 4242 | 297 | | nibble = result; |
| | 4242 | 298 | | return true; |
| | 4354 | 299 | | } |
| | | 300 | | |
| | | 301 | | private static bool TryReadQuotedAltAuthority(string value, int startIndex, out string? host, out int port, out |
| | 36 | 302 | | { |
| | 36 | 303 | | if (HttpRuleParser.GetQuotedStringLength(value, startIndex, out int quotedLength) != HttpParseResult.Parsed) |
| | 26 | 304 | | { |
| | 26 | 305 | | goto parseError; |
| | | 306 | | } |
| | | 307 | | |
| | 10 | 308 | | Debug.Assert(value[startIndex] == '"' && value[startIndex + quotedLength - 1] == '"', $"{nameof(HttpRulePars |
| | 10 | 309 | | ReadOnlySpan<char> quoted = value.AsSpan(startIndex + 1, quotedLength - 2); |
| | | 310 | | |
| | 10 | 311 | | int idx = quoted.IndexOf(':'); |
| | 10 | 312 | | if (idx == -1) |
| | 10 | 313 | | { |
| | 10 | 314 | | goto parseError; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | // Parse the port. Port comes at the end of the string, but do this first so we don't allocate a host string |
| | 0 | 318 | | if (!TryReadQuotedInt32Value(quoted.Slice(idx + 1), out port)) |
| | 0 | 319 | | { |
| | 0 | 320 | | goto parseError; |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | // Parse the optional host. |
| | 0 | 324 | | if (idx == 0) |
| | 0 | 325 | | { |
| | 0 | 326 | | host = null; |
| | 0 | 327 | | } |
| | 0 | 328 | | else if (!TryReadQuotedValue(quoted.Slice(0, idx), out host)) |
| | 0 | 329 | | { |
| | 0 | 330 | | goto parseError; |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | readLength = quotedLength; |
| | 0 | 334 | | return true; |
| | | 335 | | |
| | 36 | 336 | | parseError: |
| | 36 | 337 | | host = null; |
| | 36 | 338 | | port = 0; |
| | 36 | 339 | | readLength = 0; |
| | 36 | 340 | | return false; |
| | 36 | 341 | | } |
| | | 342 | | |
| | | 343 | | private static bool TryReadQuotedValue(ReadOnlySpan<char> value, out string? result) |
| | 0 | 344 | | { |
| | 0 | 345 | | int idx = value.IndexOf('\\'); |
| | | 346 | | |
| | 0 | 347 | | if (idx == -1) |
| | 0 | 348 | | { |
| | | 349 | | // Hostnames shouldn't require quoted pairs, so this should be the hot path. |
| | 0 | 350 | | result = value.Length != 0 ? new string(value) : null; |
| | 0 | 351 | | return true; |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | var builder = new ValueStringBuilder(stackalloc char[128]); |
| | | 355 | | |
| | | 356 | | do |
| | 0 | 357 | | { |
| | 0 | 358 | | if (idx + 1 == value.Length) |
| | 0 | 359 | | { |
| | | 360 | | // quoted-pair requires two characters: the quote, and the quoted character. |
| | 0 | 361 | | builder.Dispose(); |
| | 0 | 362 | | result = null; |
| | 0 | 363 | | return false; |
| | | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | if (idx != 0) |
| | 0 | 367 | | { |
| | 0 | 368 | | builder.Append(value.Slice(0, idx)); |
| | 0 | 369 | | } |
| | | 370 | | |
| | 0 | 371 | | builder.Append(value[idx + 1]); |
| | | 372 | | |
| | 0 | 373 | | value = value.Slice(idx + 2); |
| | 0 | 374 | | idx = value.IndexOf('\\'); |
| | 0 | 375 | | } |
| | 0 | 376 | | while (idx != -1); |
| | | 377 | | |
| | 0 | 378 | | if (value.Length != 0) |
| | 0 | 379 | | { |
| | 0 | 380 | | builder.Append(value); |
| | 0 | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | result = builder.ToString(); |
| | 0 | 384 | | return true; |
| | 0 | 385 | | } |
| | | 386 | | |
| | | 387 | | private static bool TryReadTokenOrQuotedInt32(string value, int startIndex, out int result, out int readLength) |
| | 0 | 388 | | { |
| | 0 | 389 | | if (startIndex >= value.Length) |
| | 0 | 390 | | { |
| | 0 | 391 | | result = 0; |
| | 0 | 392 | | readLength = 0; |
| | 0 | 393 | | return false; |
| | | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | int tokenLength = HttpRuleParser.GetTokenLength(value, startIndex); |
| | 0 | 397 | | if (tokenLength > 0) |
| | 0 | 398 | | { |
| | | 399 | | // No reason for integers to be quoted, so this should be the hot path. |
| | 0 | 400 | | readLength = tokenLength; |
| | 0 | 401 | | return HeaderUtilities.TryParseInt32(value, startIndex, tokenLength, out result); |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (HttpRuleParser.GetQuotedStringLength(value, startIndex, out int quotedLength) == HttpParseResult.Parsed) |
| | 0 | 405 | | { |
| | 0 | 406 | | readLength = quotedLength; |
| | 0 | 407 | | return TryReadQuotedInt32Value(value.AsSpan(1, quotedLength - 2), out result); |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | result = 0; |
| | 0 | 411 | | readLength = 0; |
| | 0 | 412 | | return false; |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | private static bool TryReadQuotedInt32Value(ReadOnlySpan<char> value, out int result) |
| | 0 | 416 | | { |
| | 0 | 417 | | if (value.Length == 0) |
| | 0 | 418 | | { |
| | 0 | 419 | | result = 0; |
| | 0 | 420 | | return false; |
| | | 421 | | } |
| | | 422 | | |
| | 0 | 423 | | int port = 0; |
| | | 424 | | |
| | 0 | 425 | | foreach (char ch in value) |
| | 0 | 426 | | { |
| | | 427 | | // The port shouldn't ever need a quoted-pair, but they're still valid... skip if found. |
| | 0 | 428 | | if (ch == '\\') continue; |
| | | 429 | | |
| | 0 | 430 | | if (!char.IsAsciiDigit(ch)) |
| | 0 | 431 | | { |
| | 0 | 432 | | result = 0; |
| | 0 | 433 | | return false; |
| | | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | long portTmp = port * 10L + (ch - '0'); |
| | | 437 | | |
| | 0 | 438 | | if (portTmp > int.MaxValue) |
| | 0 | 439 | | { |
| | 0 | 440 | | result = 0; |
| | 0 | 441 | | return false; |
| | | 442 | | } |
| | | 443 | | |
| | 0 | 444 | | port = (int)portTmp; |
| | 0 | 445 | | } |
| | | 446 | | |
| | 0 | 447 | | result = port; |
| | 0 | 448 | | return true; |
| | 0 | 449 | | } |
| | | 450 | | |
| | | 451 | | private static bool TrySkipTokenOrQuoted(string value, int startIndex, out int readLength) |
| | 0 | 452 | | { |
| | 0 | 453 | | if (startIndex >= value.Length) |
| | 0 | 454 | | { |
| | 0 | 455 | | readLength = 0; |
| | 0 | 456 | | return false; |
| | | 457 | | } |
| | | 458 | | |
| | 0 | 459 | | int tokenLength = HttpRuleParser.GetTokenLength(value, startIndex); |
| | 0 | 460 | | if (tokenLength > 0) |
| | 0 | 461 | | { |
| | 0 | 462 | | readLength = tokenLength; |
| | 0 | 463 | | return true; |
| | | 464 | | } |
| | | 465 | | |
| | 0 | 466 | | if (HttpRuleParser.GetQuotedStringLength(value, startIndex, out int quotedLength) == HttpParseResult.Parsed) |
| | 0 | 467 | | { |
| | 0 | 468 | | readLength = quotedLength; |
| | 0 | 469 | | return true; |
| | | 470 | | } |
| | | 471 | | |
| | 0 | 472 | | readLength = 0; |
| | 0 | 473 | | return false; |
| | 0 | 474 | | } |
| | | 475 | | } |
| | | 476 | | } |