| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System |
| | | 10 | | { |
| | | 11 | | public class UriBuilder |
| | | 12 | | { |
| | 0 | 13 | | private string _scheme = "http"; |
| | 0 | 14 | | private string _username = string.Empty; |
| | 0 | 15 | | private string _password = string.Empty; |
| | 0 | 16 | | private string _host = "localhost"; |
| | 0 | 17 | | private int _port = -1; |
| | 0 | 18 | | private string _path = "/"; |
| | 0 | 19 | | private string _query = string.Empty; |
| | 0 | 20 | | private string _fragment = string.Empty; |
| | | 21 | | |
| | 0 | 22 | | private bool _changed = true; |
| | | 23 | | private Uri? _uri; |
| | | 24 | | |
| | 0 | 25 | | public UriBuilder() |
| | 0 | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public UriBuilder([StringSyntax(StringSyntaxAttribute.Uri)] string uri) |
| | 0 | 30 | | { |
| | | 31 | | // setting allowRelative=true for a string like www.acme.org |
| | 0 | 32 | | _uri = new Uri(uri, UriKind.RelativeOrAbsolute); |
| | | 33 | | |
| | 0 | 34 | | if (!_uri.IsAbsoluteUri) |
| | 0 | 35 | | { |
| | 0 | 36 | | _uri = new Uri(Uri.UriSchemeHttp + Uri.SchemeDelimiter + uri); |
| | 0 | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | SetFieldsFromUri(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | public UriBuilder(Uri uri) |
| | 0 | 43 | | { |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(uri); |
| | | 45 | | |
| | 0 | 46 | | _uri = uri; |
| | 0 | 47 | | SetFieldsFromUri(); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public UriBuilder(string? schemeName, string? hostName) |
| | 0 | 51 | | { |
| | 0 | 52 | | Scheme = schemeName; |
| | 0 | 53 | | Host = hostName; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public UriBuilder(string? scheme, string? host, int portNumber) |
| | 0 | 57 | | : this(scheme, host) |
| | 0 | 58 | | { |
| | 0 | 59 | | Port = portNumber; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public UriBuilder(string? scheme, string? host, int port, string? pathValue) |
| | 0 | 63 | | : this(scheme, host, port) |
| | 0 | 64 | | { |
| | 0 | 65 | | Path = pathValue; |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public UriBuilder(string? scheme, string? host, int port, string? path, string? extraValue) |
| | 0 | 69 | | : this(scheme, host, port, path) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (!string.IsNullOrEmpty(extraValue)) |
| | 0 | 72 | | { |
| | 0 | 73 | | if (extraValue[0] == '#') |
| | 0 | 74 | | { |
| | 0 | 75 | | _fragment = extraValue; |
| | 0 | 76 | | } |
| | 0 | 77 | | else if (extraValue[0] == '?') |
| | 0 | 78 | | { |
| | 0 | 79 | | int fragmentIndex = extraValue.IndexOf('#'); |
| | 0 | 80 | | if (fragmentIndex == -1) |
| | 0 | 81 | | { |
| | 0 | 82 | | _query = extraValue; |
| | 0 | 83 | | } |
| | | 84 | | else |
| | 0 | 85 | | { |
| | 0 | 86 | | _query = extraValue.Substring(0, fragmentIndex); |
| | 0 | 87 | | _fragment = extraValue.Substring(fragmentIndex); |
| | 0 | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | else |
| | 0 | 91 | | { |
| | 0 | 92 | | throw new ArgumentException(SR.Argument_ExtraNotValid, nameof(extraValue)); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | if (_query.Length == 1) |
| | 0 | 96 | | { |
| | 0 | 97 | | _query = string.Empty; |
| | 0 | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | if (_fragment.Length == 1) |
| | 0 | 101 | | { |
| | 0 | 102 | | _fragment = string.Empty; |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | [AllowNull] |
| | | 108 | | public string Scheme |
| | | 109 | | { |
| | 0 | 110 | | get => _scheme; |
| | | 111 | | set |
| | 0 | 112 | | { |
| | 0 | 113 | | value ??= string.Empty; |
| | | 114 | | |
| | 0 | 115 | | if (value.Length != 0) |
| | 0 | 116 | | { |
| | 0 | 117 | | if (!Uri.CheckSchemeName(value)) |
| | 0 | 118 | | { |
| | 0 | 119 | | int index = value.IndexOf(':'); |
| | 0 | 120 | | if (index != -1) |
| | 0 | 121 | | { |
| | 0 | 122 | | value = value.Substring(0, index); |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (!Uri.CheckSchemeName(value)) |
| | 0 | 126 | | { |
| | 0 | 127 | | throw new ArgumentException(SR.net_uri_BadScheme, nameof(value)); |
| | | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | value = value.ToLowerInvariant(); |
| | 0 | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | _scheme = value; |
| | 0 | 135 | | _changed = true; |
| | 0 | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | [AllowNull] |
| | | 140 | | public string UserName |
| | | 141 | | { |
| | 0 | 142 | | get => _username; |
| | | 143 | | set |
| | 0 | 144 | | { |
| | 0 | 145 | | _username = value ?? string.Empty; |
| | 0 | 146 | | _changed = true; |
| | 0 | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | [AllowNull] |
| | | 151 | | public string Password |
| | | 152 | | { |
| | 0 | 153 | | get => _password; |
| | | 154 | | set |
| | 0 | 155 | | { |
| | 0 | 156 | | _password = value ?? string.Empty; |
| | 0 | 157 | | _changed = true; |
| | 0 | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Problematic characters that could result in the Host component escaping into other components like the Path. |
| | | 163 | | /// </summary> |
| | 0 | 164 | | private static readonly SearchValues<char> s_hostReservedChars = SearchValues.Create(@":/\?#@[]"); |
| | 0 | 165 | | private static readonly SearchValues<char> s_hostReservedCharsExceptColon = SearchValues.Create(@"/\?#@[]"); |
| | | 166 | | |
| | | 167 | | [AllowNull] |
| | | 168 | | public string Host |
| | | 169 | | { |
| | 0 | 170 | | get => _host; |
| | | 171 | | set |
| | 0 | 172 | | { |
| | 0 | 173 | | if (!string.IsNullOrEmpty(value) && value.AsSpan().ContainsAny(s_hostReservedChars)) |
| | 0 | 174 | | { |
| | 0 | 175 | | if (value.Contains(':')) |
| | 0 | 176 | | { |
| | 0 | 177 | | if (!value.StartsWith('[') || !value.EndsWith(']')) |
| | 0 | 178 | | { |
| | | 179 | | // probable ipv6 address - Note: this is only supported for cases where the authority is ine |
| | 0 | 180 | | value = $"[{value}]"; |
| | 0 | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | if (value.AsSpan(1, value.Length - 2).ContainsAny(s_hostReservedCharsExceptColon)) |
| | 0 | 184 | | { |
| | | 185 | | // Reject inputs like "[::]/path" or "::]/path". |
| | 0 | 186 | | throw new ArgumentException(SR.net_uri_BadHostName, nameof(value)); |
| | | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | else |
| | 0 | 190 | | { |
| | | 191 | | // Reject inputs like "contoso.com/path" or "user@contoso.com". |
| | | 192 | | // Nonsensical inputs will only be allowed by a custom parser with GenericUriParserOptions.Gener |
| | 0 | 193 | | throw new ArgumentException(SR.net_uri_BadHostName, nameof(value)); |
| | | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | _host = value ?? string.Empty; |
| | 0 | 198 | | _changed = true; |
| | 0 | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public int Port |
| | | 203 | | { |
| | 0 | 204 | | get => _port; |
| | | 205 | | set |
| | 0 | 206 | | { |
| | 0 | 207 | | ArgumentOutOfRangeException.ThrowIfLessThan(value, -1); |
| | 0 | 208 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(value, 0xFFFF); |
| | 0 | 209 | | _port = value; |
| | 0 | 210 | | _changed = true; |
| | 0 | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | [AllowNull] |
| | | 215 | | public string Path |
| | | 216 | | { |
| | 0 | 217 | | get => _path; |
| | | 218 | | set |
| | 0 | 219 | | { |
| | 0 | 220 | | _path = string.IsNullOrEmpty(value) |
| | 0 | 221 | | ? "/" |
| | 0 | 222 | | : Uri.InternalEscapeString(value.Replace('\\', '/')); |
| | 0 | 223 | | _changed = true; |
| | 0 | 224 | | } |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | [AllowNull] |
| | | 228 | | public string Query |
| | | 229 | | { |
| | 0 | 230 | | get => _query; |
| | | 231 | | set |
| | 0 | 232 | | { |
| | 0 | 233 | | if (!string.IsNullOrEmpty(value) && value[0] != '?') |
| | 0 | 234 | | { |
| | 0 | 235 | | value = '?' + value; |
| | 0 | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | _query = value ?? string.Empty; |
| | 0 | 239 | | _changed = true; |
| | 0 | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | [AllowNull] |
| | | 244 | | public string Fragment |
| | | 245 | | { |
| | 0 | 246 | | get => _fragment; |
| | | 247 | | set |
| | 0 | 248 | | { |
| | 0 | 249 | | if (!string.IsNullOrEmpty(value) && value[0] != '#') |
| | 0 | 250 | | { |
| | 0 | 251 | | value = '#' + value; |
| | 0 | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | _fragment = value ?? string.Empty; |
| | 0 | 255 | | _changed = true; |
| | 0 | 256 | | } |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | public Uri Uri |
| | | 260 | | { |
| | | 261 | | get |
| | 0 | 262 | | { |
| | 0 | 263 | | if (_changed) |
| | 0 | 264 | | { |
| | 0 | 265 | | _uri = new Uri(ToString()); |
| | 0 | 266 | | SetFieldsFromUri(); |
| | 0 | 267 | | _changed = false; |
| | 0 | 268 | | } |
| | | 269 | | else |
| | 0 | 270 | | { |
| | 0 | 271 | | Debug.Assert(_uri is not null); |
| | 0 | 272 | | } |
| | 0 | 273 | | return _uri; |
| | 0 | 274 | | } |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | public override bool Equals([NotNullWhen(true)] object? rparam) => rparam is not null && Uri.Equals(rparam.ToStr |
| | | 278 | | |
| | 0 | 279 | | public override int GetHashCode() => Uri.GetHashCode(); |
| | | 280 | | |
| | | 281 | | // The following characters ("/" / "\" / "?" / "#" / "@") are from the gen-delims group. |
| | | 282 | | // We have to escape them to avoid corrupting the rest of the Uri string. |
| | | 283 | | // Other characters like spaces or non-ASCII will be escaped by Uri, we can ignore them here. |
| | 0 | 284 | | private static readonly SearchValues<char> s_userInfoReservedChars = |
| | 0 | 285 | | SearchValues.Create(@"/\?#@"); |
| | | 286 | | |
| | | 287 | | private static string EncodeUserInfo(string input) |
| | 0 | 288 | | { |
| | 0 | 289 | | if (!input.AsSpan().ContainsAny(s_userInfoReservedChars)) |
| | 0 | 290 | | { |
| | 0 | 291 | | return input; |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | return input |
| | 0 | 295 | | .Replace("/", "%2F", StringComparison.Ordinal) |
| | 0 | 296 | | .Replace(@"\", "%5C", StringComparison.Ordinal) |
| | 0 | 297 | | .Replace("?", "%3F", StringComparison.Ordinal) |
| | 0 | 298 | | .Replace("#", "%23", StringComparison.Ordinal) |
| | 0 | 299 | | .Replace("@", "%40", StringComparison.Ordinal); |
| | 0 | 300 | | } |
| | | 301 | | |
| | | 302 | | private void SetFieldsFromUri() |
| | 0 | 303 | | { |
| | 0 | 304 | | Debug.Assert(_uri is not null); |
| | 0 | 305 | | _scheme = _uri.Scheme; |
| | 0 | 306 | | _host = _uri.Host; |
| | 0 | 307 | | _port = _uri.Port; |
| | 0 | 308 | | _path = _uri.AbsolutePath; |
| | 0 | 309 | | _query = _uri.Query; |
| | 0 | 310 | | _fragment = _uri.Fragment; |
| | | 311 | | |
| | 0 | 312 | | string userInfo = _uri.UserInfo; |
| | | 313 | | |
| | 0 | 314 | | if (userInfo.Length > 0) |
| | 0 | 315 | | { |
| | 0 | 316 | | int index = userInfo.IndexOf(':'); |
| | | 317 | | |
| | 0 | 318 | | if (index != -1) |
| | 0 | 319 | | { |
| | 0 | 320 | | _password = userInfo.Substring(index + 1); |
| | 0 | 321 | | _username = userInfo.Substring(0, index); |
| | 0 | 322 | | } |
| | | 323 | | else |
| | 0 | 324 | | { |
| | 0 | 325 | | _username = userInfo; |
| | 0 | 326 | | } |
| | 0 | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | public override string ToString() |
| | 0 | 331 | | { |
| | 0 | 332 | | if (UserName.Length == 0 && Password.Length != 0) |
| | 0 | 333 | | { |
| | 0 | 334 | | throw new UriFormatException(SR.net_uri_BadUserPassword); |
| | | 335 | | } |
| | | 336 | | |
| | 0 | 337 | | var vsb = new ValueStringBuilder(stackalloc char[Uri.StackallocThreshold]); |
| | | 338 | | |
| | 0 | 339 | | string scheme = Scheme; |
| | 0 | 340 | | string host = Host; |
| | | 341 | | |
| | 0 | 342 | | if (scheme.Length != 0) |
| | 0 | 343 | | { |
| | 0 | 344 | | UriParser? syntax = UriParser.GetSyntax(scheme); |
| | | 345 | | string schemeDelimiter; |
| | 0 | 346 | | if (syntax is null) |
| | 0 | 347 | | { |
| | 0 | 348 | | schemeDelimiter = host.Length == 0 ? ":" : Uri.SchemeDelimiter; |
| | 0 | 349 | | } |
| | | 350 | | else |
| | 0 | 351 | | { |
| | 0 | 352 | | schemeDelimiter = syntax.InFact(UriSyntaxFlags.MustHaveAuthority) |
| | 0 | 353 | | || (host.Length != 0 && syntax.NotAny(UriSyntaxFlags.MailToLikeUri) && syntax.InFact(UriSyntaxFl |
| | 0 | 354 | | ? Uri.SchemeDelimiter |
| | 0 | 355 | | : ":"; |
| | 0 | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | vsb.Append(scheme); |
| | 0 | 359 | | vsb.Append(schemeDelimiter); |
| | 0 | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | string username = EncodeUserInfo(UserName); |
| | 0 | 363 | | if (username.Length != 0) |
| | 0 | 364 | | { |
| | 0 | 365 | | vsb.Append(username); |
| | | 366 | | |
| | 0 | 367 | | string password = EncodeUserInfo(Password); |
| | 0 | 368 | | if (password.Length != 0) |
| | 0 | 369 | | { |
| | 0 | 370 | | vsb.Append(':'); |
| | 0 | 371 | | vsb.Append(password); |
| | 0 | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | vsb.Append('@'); |
| | 0 | 375 | | } |
| | | 376 | | |
| | 0 | 377 | | if (host.Length != 0) |
| | 0 | 378 | | { |
| | 0 | 379 | | vsb.Append(host); |
| | | 380 | | |
| | 0 | 381 | | if (_port != -1) |
| | 0 | 382 | | { |
| | 0 | 383 | | vsb.Append(':'); |
| | | 384 | | |
| | | 385 | | const int MaxUshortLength = 5; |
| | 0 | 386 | | bool success = _port.TryFormat(vsb.AppendSpan(MaxUshortLength), out int charsWritten); |
| | 0 | 387 | | Debug.Assert(success); |
| | 0 | 388 | | vsb.Length -= MaxUshortLength - charsWritten; |
| | 0 | 389 | | } |
| | 0 | 390 | | } |
| | | 391 | | |
| | 0 | 392 | | string path = Path; |
| | 0 | 393 | | if (path.Length != 0) |
| | 0 | 394 | | { |
| | 0 | 395 | | if (!path.StartsWith('/') && host.Length != 0) |
| | 0 | 396 | | { |
| | 0 | 397 | | vsb.Append('/'); |
| | 0 | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | vsb.Append(path); |
| | 0 | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | vsb.Append(Query); |
| | | 404 | | |
| | 0 | 405 | | vsb.Append(Fragment); |
| | | 406 | | |
| | 0 | 407 | | return vsb.ToString(); |
| | 0 | 408 | | } |
| | | 409 | | } |
| | | 410 | | } |
| | | 411 | | |