forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryHelpers.cs
201 lines (175 loc) · 6.88 KB
/
QueryHelpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0.
/*
This is derived from the ASP.NET Core source at HttpAbstractions/src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs
from https://github.com/aspnet/HttpAbstractions/blob/release/1.0/src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs
Altered from the original as follows:
Commented out methods "ParseNullableQuery" and "ParseQuery" to remove dependency on Microsoft.Extensions.Primitives
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
// using Microsoft.Extensions.Primitives;
namespace NetCoreForce.Client
{
public static class QueryHelpers
{
/// <summary>
/// Append the given query key and value to the URI.
/// </summary>
/// <param name="uri">The base URI.</param>
/// <param name="name">The name of the query key.</param>
/// <param name="value">The query value.</param>
/// <returns>The combined result.</returns>
public static string AddQueryString(string uri, string name, string value)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return AddQueryString(
uri, new[] { new KeyValuePair<string, string>(name, value) });
}
/// <summary>
/// Append the given query keys and values to the uri.
/// </summary>
/// <param name="uri">The base uri.</param>
/// <param name="queryString">A collection of name value query pairs to append.</param>
/// <returns>The combined result.</returns>
public static string AddQueryString(string uri, IDictionary<string, string> queryString)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (queryString == null)
{
throw new ArgumentNullException(nameof(queryString));
}
return AddQueryString(uri, (IEnumerable<KeyValuePair<string, string>>)queryString);
}
private static string AddQueryString(
string uri,
IEnumerable<KeyValuePair<string, string>> queryString)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (queryString == null)
{
throw new ArgumentNullException(nameof(queryString));
}
var anchorIndex = uri.IndexOf('#');
var uriToBeAppended = uri;
var anchorText = "";
// If there is an anchor, then the query string must be inserted before its first occurance.
if (anchorIndex != -1)
{
anchorText = uri.Substring(anchorIndex);
uriToBeAppended = uri.Substring(0, anchorIndex);
}
var queryIndex = uriToBeAppended.IndexOf('?');
var hasQuery = queryIndex != -1;
var sb = new StringBuilder();
sb.Append(uriToBeAppended);
foreach (var parameter in queryString)
{
sb.Append(hasQuery ? '&' : '?');
sb.Append(UrlEncoder.Default.Encode(parameter.Key));
sb.Append('=');
sb.Append(UrlEncoder.Default.Encode(parameter.Value));
hasQuery = true;
}
sb.Append(anchorText);
return sb.ToString();
}
/*
/// <summary>
/// Parse a query string into its component key and value parts.
/// </summary>
/// <param name="queryString">The raw query string value, with or without the leading '?'.</param>
/// <returns>A collection of parsed keys and values.</returns>
public static Dictionary<string, StringValues> ParseQuery(string queryString)
{
var result = ParseNullableQuery(queryString);
if (result == null)
{
return new Dictionary<string, StringValues>();
}
return result;
}
/// <summary>
/// Parse a query string into its component key and value parts.
/// </summary>
/// <param name="queryString">The raw query string value, with or without the leading '?'.</param>
/// <returns>A collection of parsed keys and values, null if there are no entries.</returns>
public static Dictionary<string, StringValues> ParseNullableQuery(string queryString)
{
var accumulator = new KeyValueAccumulator();
if (string.IsNullOrEmpty(queryString) || queryString == "?")
{
return null;
}
int scanIndex = 0;
if (queryString[0] == '?')
{
scanIndex = 1;
}
int textLength = queryString.Length;
int equalIndex = queryString.IndexOf('=');
if (equalIndex == -1)
{
equalIndex = textLength;
}
while (scanIndex < textLength)
{
int delimiterIndex = queryString.IndexOf('&', scanIndex);
if (delimiterIndex == -1)
{
delimiterIndex = textLength;
}
if (equalIndex < delimiterIndex)
{
while (scanIndex != equalIndex && char.IsWhiteSpace(queryString[scanIndex]))
{
++scanIndex;
}
string name = queryString.Substring(scanIndex, equalIndex - scanIndex);
string value = queryString.Substring(equalIndex + 1, delimiterIndex - equalIndex - 1);
accumulator.Append(
Uri.UnescapeDataString(name.Replace('+', ' ')),
Uri.UnescapeDataString(value.Replace('+', ' ')));
equalIndex = queryString.IndexOf('=', delimiterIndex);
if (equalIndex == -1)
{
equalIndex = textLength;
}
}
else
{
if (delimiterIndex > scanIndex)
{
accumulator.Append(queryString.Substring(scanIndex, delimiterIndex - scanIndex), string.Empty);
}
}
scanIndex = delimiterIndex + 1;
}
if (!accumulator.HasValues)
{
return null;
}
return accumulator.GetResults();
}
*/
}
}