| | | 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> |
| | 4555 | 20 | | public UriQueryParamsCollection(string encodedQueryParamString) |
| | | 21 | | { |
| | 4555 | 22 | | encodedQueryParamString = encodedQueryParamString ?? throw Errors.ArgumentNull(nameof(encodedQueryParamStrin |
| | | 23 | | |
| | 4555 | 24 | | if (encodedQueryParamString.StartsWith("?", true, CultureInfo.InvariantCulture)) |
| | | 25 | | { |
| | 3056 | 26 | | encodedQueryParamString = encodedQueryParamString.Substring(1); |
| | | 27 | | } |
| | | 28 | | |
| | 4555 | 29 | | var keysAndValues = encodedQueryParamString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries); |
| | 17116 | 30 | | foreach (var qp in keysAndValues) |
| | | 31 | | { |
| | 4003 | 32 | | var keyAndValue = qp.Split(new[] { '=' }, 2); |
| | 4003 | 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 | | { |
| | 4003 | 39 | | Add(WebUtility.UrlDecode(keyAndValue[0]), WebUtility.UrlDecode(keyAndValue[1])); // The map's keys/v |
| | | 40 | | } |
| | | 41 | | } |
| | 4555 | 42 | | } |
| | | 43 | | |
| | | 44 | | // Returns the url-encoded query parameter string |
| | | 45 | | public override string ToString() |
| | | 46 | | { |
| | 1231 | 47 | | var sb = new StringBuilder(); |
| | 3370 | 48 | | foreach (KeyValuePair<string, string> kv in this) |
| | | 49 | | { |
| | 454 | 50 | | if (sb.Length > 0) |
| | | 51 | | { |
| | 380 | 52 | | sb.Append('&'); |
| | | 53 | | } |
| | | 54 | | |
| | 454 | 55 | | sb.Append(WebUtility.UrlEncode(kv.Key)).Append('=').Append(WebUtility.UrlEncode(kv.Value)); // Query p |
| | | 56 | | } |
| | 1231 | 57 | | return sb.ToString(); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | } |