-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
UrlPrefix.cs
208 lines (181 loc) · 7.39 KB
/
UrlPrefix.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using Microsoft.AspNetCore.HttpSys.Internal;
namespace Microsoft.AspNetCore.Server.HttpSys;
/// <summary>
/// A set of URL parameters used to listen for incoming requests.
/// </summary>
public class UrlPrefix
{
private UrlPrefix(bool isHttps, string scheme, string host, string port, int portValue, string path)
{
IsHttps = isHttps;
Scheme = scheme;
Host = host;
Port = port;
HostAndPort = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", Host, Port);
PortValue = portValue;
Path = path;
PathWithoutTrailingSlash = Path.Length > 1 ? Path[0..^1] : string.Empty;
FullPrefix = string.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}{3}", Scheme, Host, Port, Path);
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364698(v=vs.85).aspx
/// </summary>
/// <param name="scheme">http or https. Will be normalized to lower case.</param>
/// <param name="host">+, *, IPv4, [IPv6], or a dns name. Http.Sys does not permit punycode (xn--), use Unicode instead.</param>
/// <param name="port">If empty, the default port for the given scheme will be used (80 or 443).</param>
/// <param name="path">Should start and end with a '/', though a missing trailing slash will be added. This value must be un-escaped.</param>
public static UrlPrefix Create(string scheme, string host, string port, string path)
{
int? portValue = null;
if (!string.IsNullOrWhiteSpace(port))
{
portValue = int.Parse(port, NumberStyles.None, CultureInfo.InvariantCulture);
}
return UrlPrefix.Create(scheme, host, portValue, path);
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364698(v=vs.85).aspx
/// </summary>
/// <param name="scheme">http or https. Will be normalized to lower case.</param>
/// <param name="host">+, *, IPv4, [IPv6], or a dns name. Http.Sys does not permit punycode (xn--), use Unicode instead.</param>
/// <param name="portValue">If empty, the default port for the given scheme will be used (80 or 443).</param>
/// <param name="path">Should start and end with a '/', though a missing trailing slash will be added. This value must be un-escaped.</param>
public static UrlPrefix Create(string scheme, string host, int? portValue, string path)
{
bool isHttps;
if (string.Equals(Constants.HttpScheme, scheme, StringComparison.OrdinalIgnoreCase))
{
scheme = Constants.HttpScheme; // Always use a lower case scheme
isHttps = false;
}
else if (string.Equals(Constants.HttpsScheme, scheme, StringComparison.OrdinalIgnoreCase))
{
scheme = Constants.HttpsScheme; // Always use a lower case scheme
isHttps = true;
}
else
{
throw new ArgumentOutOfRangeException(nameof(scheme), scheme, Resources.Exception_UnsupportedScheme);
}
ArgumentException.ThrowIfNullOrEmpty(host);
string port;
if (!portValue.HasValue)
{
port = isHttps ? "443" : "80";
portValue = isHttps ? 443 : 80;
}
else
{
port = portValue.Value.ToString(CultureInfo.InvariantCulture);
}
// Http.Sys requires the path end with a slash.
if (string.IsNullOrWhiteSpace(path))
{
path = "/";
}
else if (!path.EndsWith('/'))
{
path += "/";
}
return new UrlPrefix(isHttps, scheme, host, port, portValue.Value, path);
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364698(v=vs.85).aspx
/// </summary>
/// <param name="prefix">The string that the <see cref="UrlPrefix"/> will be created from.</param>
public static UrlPrefix Create(string prefix)
{
string scheme;
string host;
int? port = null;
string path;
var whole = prefix ?? string.Empty;
var schemeDelimiterEnd = whole.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal);
if (schemeDelimiterEnd < 0)
{
throw new FormatException("Invalid prefix, missing scheme separator: " + prefix);
}
var hostDelimiterStart = schemeDelimiterEnd + Uri.SchemeDelimiter.Length;
var pathDelimiterStart = whole.IndexOf("/", hostDelimiterStart, StringComparison.Ordinal);
if (pathDelimiterStart < 0)
{
pathDelimiterStart = whole.Length;
}
var hostDelimiterEnd = whole.LastIndexOf(":", pathDelimiterStart - 1, pathDelimiterStart - hostDelimiterStart, StringComparison.Ordinal);
if (hostDelimiterEnd < 0)
{
hostDelimiterEnd = pathDelimiterStart;
}
scheme = whole.Substring(0, schemeDelimiterEnd);
var portString = whole.AsSpan(hostDelimiterEnd, pathDelimiterStart - hostDelimiterEnd); // The leading ":" is included
int portValue;
if (!portString.IsEmpty)
{
var portValueString = portString.Slice(1); // Trim the leading ":"
if (int.TryParse(portValueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out portValue))
{
host = whole.Substring(hostDelimiterStart, hostDelimiterEnd - hostDelimiterStart);
port = portValue;
}
else
{
// This means a port was specified but was invalid or empty.
throw new FormatException("Invalid prefix, invalid port specified: " + prefix);
}
}
else
{
host = whole.Substring(hostDelimiterStart, pathDelimiterStart - hostDelimiterStart);
}
path = whole.Substring(pathDelimiterStart);
return Create(scheme, host, port, path);
}
/// <summary>
/// Gets a value that determines if the prefix's scheme is HTTPS.
/// </summary>
public bool IsHttps { get; }
/// <summary>
/// Gets the scheme used by the prefix.
/// </summary>
public string Scheme { get; }
/// <summary>
/// Gets the host domain name used by the prefix.
/// </summary>
public string Host { get; }
/// <summary>
/// Gets a string representation of the port used by the prefix.
/// </summary>
public string Port { get; }
internal string HostAndPort { get; }
/// <summary>
/// Gets an integer representation of the port used by the prefix.
/// </summary>
public int PortValue { get; }
/// <summary>
/// Gets the path component of the prefix.
/// </summary>
public string Path { get; }
internal string PathWithoutTrailingSlash { get; }
/// <summary>
/// Gets a string representation of the prefix
/// </summary>
public string FullPrefix { get; }
/// <inheritdoc />
public override bool Equals(object? obj)
{
return string.Equals(FullPrefix, Convert.ToString(obj, CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase);
}
/// <inheritdoc />
public override int GetHashCode()
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(FullPrefix);
}
/// <inheritdoc />
public override string ToString()
{
return FullPrefix;
}
}