| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Collections.Immutable; |
| | 4 | |
|
| | 5 | | namespace IceRpc.Internal; |
| | 6 | |
|
| | 7 | | /// <summary>Extension methods for <see cref="Uri" />.</summary> |
| | 8 | | internal static class UriExtensions |
| | 9 | | { |
| | 10 | | /// <summary>Parses the query portion of a URI into a dictionary of name/value. The value of the alt-server |
| | 11 | | /// and transport parameters, if set, are returned separately.</summary> |
| | 12 | | internal static (ImmutableDictionary<string, string> QueryParams, string? AltServerValue, string? TransportValue) Pa |
| | 13 | | this Uri uri) |
| 2507 | 14 | | { |
| 2507 | 15 | | if (uri.Query.Length < 2) |
| 2027 | 16 | | { |
| | 17 | | // no query or empty query |
| 2027 | 18 | | return (ImmutableDictionary<string, string>.Empty, null, null); |
| | 19 | | } |
| | 20 | | else |
| 480 | 21 | | { |
| 480 | 22 | | ImmutableDictionary<string, string> queryParams = ImmutableDictionary<string, string>.Empty; |
| 480 | 23 | | string? altServer = null; |
| 480 | 24 | | string? transport = null; |
| | 25 | |
|
| 2876 | 26 | | foreach (string p in uri.Query.TrimStart('?').Split('&')) |
| 719 | 27 | | { |
| 719 | 28 | | int equalPos = p.IndexOf('=', StringComparison.Ordinal); |
| 719 | 29 | | string name = equalPos == -1 ? p : p[..equalPos]; |
| 719 | 30 | | string value = equalPos == -1 ? "" : p[(equalPos + 1)..]; |
| | 31 | |
|
| 719 | 32 | | if (name == "alt-server") |
| 119 | 33 | | { |
| 119 | 34 | | altServer = altServer is null ? value : $"{altServer},{value}"; |
| 119 | 35 | | } |
| 600 | 36 | | else if (name == "transport") |
| 219 | 37 | | { |
| | 38 | | // This is the regular parsing for query parameters, even though it's not meaningful for transport. |
| 219 | 39 | | transport = transport is null ? value : $"{transport},{value}"; |
| 219 | 40 | | } |
| | 41 | | else |
| 381 | 42 | | { |
| 381 | 43 | | if (name.Length == 0) |
| 2 | 44 | | { |
| 2 | 45 | | throw new FormatException($"Invalid empty query parameter name in URI '{uri.OriginalString}'."); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | // we assume the C# URI parser validates the name and value sufficiently |
| | 49 | |
|
| 379 | 50 | | if (queryParams.TryGetValue(name, out string? existingValue)) |
| 28 | 51 | | { |
| 28 | 52 | | queryParams = queryParams.SetItem(name, $"{existingValue},{value}"); |
| 28 | 53 | | } |
| | 54 | | else |
| 351 | 55 | | { |
| 351 | 56 | | queryParams = queryParams.Add(name, value); |
| 351 | 57 | | } |
| 379 | 58 | | } |
| 717 | 59 | | } |
| 478 | 60 | | return (queryParams, altServer, transport); |
| | 61 | | } |
| 2505 | 62 | | } |
| | 63 | | } |