Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UriBuilder.cs
Go to the documentation of this file.
2using System.Text;
3
4namespace System;
5
6public class UriBuilder
7{
8 private string _scheme = "http";
9
10 private string _username = string.Empty;
11
12 private string _password = string.Empty;
13
14 private string _host = "localhost";
15
16 private int _port = -1;
17
18 private string _path = "/";
19
20 private string _query = string.Empty;
21
22 private string _fragment = string.Empty;
23
24 private bool _changed = true;
25
26 private Uri _uri;
27
28 public string Scheme
29 {
30 get
31 {
32 return _scheme;
33 }
34 [param: AllowNull]
35 set
36 {
37 if (value == null)
38 {
39 value = string.Empty;
40 }
41 if (value.Length != 0)
42 {
44 {
45 int num = value.IndexOf(':');
46 if (num != -1)
47 {
48 value = value.Substring(0, num);
49 }
51 {
52 throw new ArgumentException(System.SR.net_uri_BadScheme, "value");
53 }
54 }
55 value = value.ToLowerInvariant();
56 }
57 _scheme = value;
58 _changed = true;
59 }
60 }
61
62 public string UserName
63 {
64 get
65 {
66 return _username;
67 }
68 [param: AllowNull]
69 set
70 {
71 _username = value ?? string.Empty;
72 _changed = true;
73 }
74 }
75
76 public string Password
77 {
78 get
79 {
80 return _password;
81 }
82 [param: AllowNull]
83 set
84 {
85 _password = value ?? string.Empty;
86 _changed = true;
87 }
88 }
89
90 public string Host
91 {
92 get
93 {
94 return _host;
95 }
96 [param: AllowNull]
97 set
98 {
99 if (!string.IsNullOrEmpty(value) && value.Contains(':') && value[0] != '[')
100 {
101 value = "[" + value + "]";
102 }
103 _host = value ?? string.Empty;
104 _changed = true;
105 }
106 }
107
108 public int Port
109 {
110 get
111 {
112 return _port;
113 }
114 set
115 {
116 if (value < -1 || value > 65535)
117 {
118 throw new ArgumentOutOfRangeException("value");
119 }
120 _port = value;
121 _changed = true;
122 }
123 }
124
125 public string Path
126 {
127 get
128 {
129 return _path;
130 }
131 [param: AllowNull]
132 set
133 {
134 _path = (string.IsNullOrEmpty(value) ? "/" : System.Uri.InternalEscapeString(value.Replace('\\', '/')));
135 _changed = true;
136 }
137 }
138
139 public string Query
140 {
141 get
142 {
143 return _query;
144 }
145 [param: AllowNull]
146 set
147 {
148 if (!string.IsNullOrEmpty(value) && value[0] != '?')
149 {
150 value = "?" + value;
151 }
152 _query = value ?? string.Empty;
153 _changed = true;
154 }
155 }
156
157 public string Fragment
158 {
159 get
160 {
161 return _fragment;
162 }
163 [param: AllowNull]
164 set
165 {
166 if (!string.IsNullOrEmpty(value) && value[0] != '#')
167 {
168 value = "#" + value;
169 }
170 _fragment = value ?? string.Empty;
171 _changed = true;
172 }
173 }
174
175 public Uri Uri
176 {
177 get
178 {
179 if (_changed)
180 {
181 _uri = new Uri(ToString());
183 _changed = false;
184 }
185 return _uri;
186 }
187 }
188
189 public UriBuilder()
190 {
191 }
192
193 public UriBuilder(string uri)
194 {
195 _uri = new Uri(uri, UriKind.RelativeOrAbsolute);
196 if (!_uri.IsAbsoluteUri)
197 {
199 }
201 }
202
203 public UriBuilder(Uri uri)
204 {
205 _uri = uri ?? throw new ArgumentNullException("uri");
207 }
208
209 public UriBuilder(string? schemeName, string? hostName)
210 {
211 Scheme = schemeName;
212 Host = hostName;
213 }
214
215 public UriBuilder(string? scheme, string? host, int portNumber)
216 : this(scheme, host)
217 {
218 Port = portNumber;
219 }
220
221 public UriBuilder(string? scheme, string? host, int port, string? pathValue)
222 : this(scheme, host, port)
223 {
224 Path = pathValue;
225 }
226
227 public UriBuilder(string? scheme, string? host, int port, string? path, string? extraValue)
228 : this(scheme, host, port, path)
229 {
230 if (string.IsNullOrEmpty(extraValue))
231 {
232 return;
233 }
234 if (extraValue[0] == '#')
235 {
236 _fragment = extraValue;
237 }
238 else
239 {
240 if (extraValue[0] != '?')
241 {
242 throw new ArgumentException(System.SR.Argument_ExtraNotValid, "extraValue");
243 }
244 int num = extraValue.IndexOf('#');
245 if (num == -1)
246 {
247 _query = extraValue;
248 }
249 else
250 {
251 _query = extraValue.Substring(0, num);
252 _fragment = extraValue.Substring(num);
253 }
254 }
255 if (_query.Length == 1)
256 {
257 _query = string.Empty;
258 }
259 if (_fragment.Length == 1)
260 {
261 _fragment = string.Empty;
262 }
263 }
264
265 public override bool Equals([NotNullWhen(true)] object? rparam)
266 {
267 if (rparam != null)
268 {
269 return Uri.Equals(rparam.ToString());
270 }
271 return false;
272 }
273
274 public override int GetHashCode()
275 {
276 return Uri.GetHashCode();
277 }
278
279 private void SetFieldsFromUri()
280 {
282 _host = _uri.Host;
283 _port = _uri.Port;
285 _query = _uri.Query;
287 string userInfo = _uri.UserInfo;
288 if (userInfo.Length > 0)
289 {
290 int num = userInfo.IndexOf(':');
291 if (num != -1)
292 {
293 _password = userInfo.Substring(num + 1);
294 _username = userInfo.Substring(0, num);
295 }
296 else
297 {
298 _username = userInfo;
299 }
300 }
301 }
302
303 public override string ToString()
304 {
305 if (UserName.Length == 0 && Password.Length != 0)
306 {
308 }
309 Span<char> initialBuffer = stackalloc char[512];
310 System.Text.ValueStringBuilder valueStringBuilder = new System.Text.ValueStringBuilder(initialBuffer);
311 string scheme = Scheme;
312 string host = Host;
313 if (scheme.Length != 0)
314 {
315 UriParser syntax = UriParser.GetSyntax(scheme);
316 string s = ((syntax != null) ? ((syntax.InFact(UriSyntaxFlags.MustHaveAuthority) || (host.Length != 0 && syntax.NotAny(UriSyntaxFlags.MailToLikeUri) && syntax.InFact(UriSyntaxFlags.OptionalAuthority))) ? System.Uri.SchemeDelimiter : ":") : ((host.Length == 0) ? ":" : System.Uri.SchemeDelimiter));
317 valueStringBuilder.Append(scheme);
318 valueStringBuilder.Append(s);
319 }
320 string userName = UserName;
321 if (userName.Length != 0)
322 {
323 valueStringBuilder.Append(userName);
324 string password = Password;
325 if (password.Length != 0)
326 {
327 valueStringBuilder.Append(':');
328 valueStringBuilder.Append(password);
329 }
330 valueStringBuilder.Append('@');
331 }
332 if (host.Length != 0)
333 {
334 valueStringBuilder.Append(host);
335 if (_port != -1)
336 {
337 valueStringBuilder.Append(':');
338 int charsWritten;
339 bool flag = _port.TryFormat(valueStringBuilder.AppendSpan(5), out charsWritten);
340 valueStringBuilder.Length -= 5 - charsWritten;
341 }
342 }
343 string path = Path;
344 if (path.Length != 0)
345 {
346 if (!path.StartsWith('/') && host.Length != 0)
347 {
348 valueStringBuilder.Append('/');
349 }
350 valueStringBuilder.Append(path);
351 }
352 valueStringBuilder.Append(Query);
353 valueStringBuilder.Append(Fragment);
354 return valueStringBuilder.ToString();
355 }
356}
static string Argument_ExtraNotValid
Definition SR.cs:56
static string net_uri_BadScheme
Definition SR.cs:28
static string net_uri_BadUserPassword
Definition SR.cs:32
Definition SR.cs:7
UriBuilder(string? scheme, string? host, int port, string? path, string? extraValue)
UriBuilder(string? scheme, string? host, int port, string? pathValue)
override int GetHashCode()
UriBuilder(string? schemeName, string? hostName)
UriBuilder(string? scheme, string? host, int portNumber)
override bool Equals([NotNullWhen(true)] object? rparam)
override string ToString()
void SetFieldsFromUri()
UriBuilder(Uri uri)
UriBuilder(string uri)
bool NotAny(UriSyntaxFlags flags)
Definition UriParser.cs:216
bool InFact(UriSyntaxFlags flags)
Definition UriParser.cs:221
static UriParser GetSyntax(string lwrCaseScheme)
Definition UriParser.cs:294
string UserInfo
Definition Uri.cs:577
string Host
Definition Uri.cs:441
string AbsolutePath
Definition Uri.cs:239
string Query
Definition Uri.cs:477
bool IsAbsoluteUri
Definition Uri.cs:572
static bool CheckSchemeName([NotNullWhen(true)] string? schemeName)
Definition Uri.cs:1072
static readonly string SchemeDelimiter
Definition Uri.cs:187
static string InternalEscapeString(string rawString)
Definition Uri.cs:1310
override bool Equals([NotNullWhen(true)] object? comparand)
Definition Uri.cs:1166
static readonly string UriSchemeHttp
Definition Uri.cs:165
string Scheme
Definition Uri.cs:505
string Fragment
Definition Uri.cs:491
int Port
Definition Uri.cs:453
override int GetHashCode()
Definition Uri.cs:1103
UriKind
Definition UriKind.cs:4
Span< char > AppendSpan(int length)