| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Net; |
| | 8 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace Azure.Storage |
| | 11 | | { |
| | 12 | | internal sealed class UriQueryParamsCollection : Dictionary<string, string> |
| | 13 | | { |
| 0 | 14 | | public UriQueryParamsCollection() : base(StringComparer.OrdinalIgnoreCase) { } |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Takes encoded query params string, output decoded params map |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="encodedQueryParamString"></param> |
| 944 | 20 | | public UriQueryParamsCollection(string encodedQueryParamString) |
| | 21 | | { |
| 944 | 22 | | encodedQueryParamString = encodedQueryParamString ?? throw Errors.ArgumentNull(nameof(encodedQueryParamStrin |
| | 23 | |
|
| 944 | 24 | | if (encodedQueryParamString.StartsWith("?", true, CultureInfo.InvariantCulture)) |
| | 25 | | { |
| 452 | 26 | | encodedQueryParamString = encodedQueryParamString.Substring(1); |
| | 27 | | } |
| | 28 | |
|
| 944 | 29 | | var keysAndValues = encodedQueryParamString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries); |
| 4416 | 30 | | foreach (var qp in keysAndValues) |
| | 31 | | { |
| 1264 | 32 | | var keyAndValue = qp.Split(new[] { '=' }, 2); |
| 1264 | 33 | | if (keyAndValue.Length == 1) |
| | 34 | | { |
| 0 | 35 | | Add(WebUtility.UrlDecode(keyAndValue[0]), default); // The map's keys/values are url-decoded |
| | 36 | | } |
| | 37 | | else |
| | 38 | | { |
| 1264 | 39 | | Add(WebUtility.UrlDecode(keyAndValue[0]), WebUtility.UrlDecode(keyAndValue[1])); // The map's keys/v |
| | 40 | | } |
| | 41 | | } |
| 944 | 42 | | } |
| | 43 | |
|
| | 44 | | // Returns the url-encoded query parameter string |
| | 45 | | public override string ToString() |
| | 46 | | { |
| 0 | 47 | | var sb = new StringBuilder(); |
| 0 | 48 | | foreach (KeyValuePair<string, string> kv in this) |
| | 49 | | { |
| 0 | 50 | | if (sb.Length > 0) |
| | 51 | | { |
| 0 | 52 | | sb.Append('&'); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | sb.Append(WebUtility.UrlEncode(kv.Key)).Append('=').Append(WebUtility.UrlEncode(kv.Value)); // Query p |
| | 56 | | } |
| 0 | 57 | | return sb.ToString(); |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |