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