This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathUriHelper.cs
217 lines (193 loc) · 9.14 KB
/
UriHelper.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text;
namespace Microsoft.AspNetCore.Http.Extensions
{
/// <summary>
/// A helper class for constructing encoded Uris for use in headers and other Uris.
/// </summary>
public static class UriHelper
{
private const string ForwardSlash = "/";
private const string Pound = "#";
private const string QuestionMark = "?";
private const string SchemeDelimiter = "://";
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// </summary>
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query">The query, if any.</param>
/// <param name="fragment">The fragment, if any.</param>
/// <returns></returns>
public static string BuildRelative(
PathString pathBase = new PathString(),
PathString path = new PathString(),
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
return combinePath + query.ToString() + fragment.ToString();
}
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// Note that unicode in the HostString will be encoded as punycode.
/// </summary>
/// <param name="scheme">http, https, etc.</param>
/// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query">The query, if any.</param>
/// <param name="fragment">The fragment, if any.</param>
/// <returns></returns>
public static string BuildAbsolute(
string scheme,
HostString host,
PathString pathBase = new PathString(),
PathString path = new PathString(),
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
var combinedPath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
var encodedHost = host.ToString();
var encodedQuery = query.ToString();
var encodedFragment = fragment.ToString();
// PERF: Calculate string length to allocate correct buffer size for StringBuilder.
var length = scheme.Length + SchemeDelimiter.Length + encodedHost.Length
+ combinedPath.Length + encodedQuery.Length + encodedFragment.Length;
return new StringBuilder(length)
.Append(scheme)
.Append(SchemeDelimiter)
.Append(encodedHost)
.Append(combinedPath)
.Append(encodedQuery)
.Append(encodedFragment)
.ToString();
}
/// <summary>
/// Separates the given absolute URI string into components. Assumes no PathBase.
/// </summary>
/// <param name="uri">A string representation of the uri.</param>
/// <param name="scheme">http, https, etc.</param>
/// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query">The query, if any.</param>
/// <param name="fragment">The fragment, if any.</param>
public static void FromAbsolute(
string uri,
out string scheme,
out HostString host,
out PathString path,
out QueryString query,
out FragmentString fragment)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
path = new PathString();
query = new QueryString();
fragment = new FragmentString();
var startIndex = uri.IndexOf(SchemeDelimiter);
if (startIndex < 0)
{
throw new FormatException("No scheme delimiter in uri.");
}
scheme = uri.Substring(0, startIndex);
// PERF: Calculate the end of the scheme for next IndexOf
startIndex += SchemeDelimiter.Length;
var searchIndex = -1;
var limit = uri.Length;
if ((searchIndex = uri.IndexOf(Pound, startIndex)) >= 0 && searchIndex < limit)
{
fragment = FragmentString.FromUriComponent(uri.Substring(searchIndex));
limit = searchIndex;
}
if ((searchIndex = uri.IndexOf(QuestionMark, startIndex)) >= 0 && searchIndex < limit)
{
query = QueryString.FromUriComponent(uri.Substring(searchIndex, limit - searchIndex));
limit = searchIndex;
}
if ((searchIndex = uri.IndexOf(ForwardSlash, startIndex)) >= 0 && searchIndex < limit)
{
path = PathString.FromUriComponent(uri.Substring(searchIndex, limit - searchIndex));
limit = searchIndex;
}
host = HostString.FromUriComponent(uri.Substring(startIndex, limit - startIndex));
}
/// <summary>
/// Generates a string from the given absolute or relative Uri that is appropriately encoded for use in
/// HTTP headers. Note that a unicode host name will be encoded as punycode.
/// </summary>
/// <param name="uri">The Uri to encode.</param>
/// <returns></returns>
public static string Encode(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (uri.IsAbsoluteUri)
{
return BuildAbsolute(
scheme: uri.Scheme,
host: HostString.FromUriComponent(uri),
pathBase: PathString.FromUriComponent(uri),
query: QueryString.FromUriComponent(uri),
fragment: FragmentString.FromUriComponent(uri));
}
else
{
return uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
}
}
/// <summary>
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
/// and other HTTP operations.
/// </summary>
/// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns>
public static string GetEncodedUrl(this HttpRequest request)
{
return BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
}
/// <summary>
/// Returns the relative url
/// </summary>
/// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns>
public static string GetEncodedPathAndQuery(this HttpRequest request)
{
return BuildRelative(request.PathBase, request.Path, request.QueryString);
}
/// <summary>
/// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)
/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
/// </summary>
/// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns>
public static string GetDisplayUrl(this HttpRequest request)
{
var host = request.Host.Value;
var pathBase = request.PathBase.Value;
var path = request.Path.Value;
var queryString = request.QueryString.Value;
// PERF: Calculate string length to allocate correct buffer size for StringBuilder.
var length = request.Scheme.Length + SchemeDelimiter.Length + host.Length
+ pathBase.Length + path.Length + queryString.Length;
return new StringBuilder(length)
.Append(request.Scheme)
.Append(SchemeDelimiter)
.Append(host)
.Append(pathBase)
.Append(path)
.Append(queryString)
.ToString();
}
}
}