| | | 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.Collections.Generic; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Net; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.Serialization; |
| | | 13 | | using System.Text; |
| | | 14 | | using System.Threading; |
| | | 15 | | |
| | | 16 | | namespace System |
| | | 17 | | { |
| | | 18 | | [Serializable] |
| | | 19 | | [TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] |
| | | 20 | | public partial class Uri : ISpanFormattable, IEquatable<Uri>, IParsable<Uri>, ISerializable |
| | | 21 | | { |
| | 1 | 22 | | public static readonly string UriSchemeFile = UriParser.FileUri.SchemeName; |
| | 1 | 23 | | public static readonly string UriSchemeFtp = UriParser.FtpUri.SchemeName; |
| | 1 | 24 | | public static readonly string UriSchemeSftp = "sftp"; |
| | 1 | 25 | | public static readonly string UriSchemeFtps = "ftps"; |
| | 1 | 26 | | public static readonly string UriSchemeGopher = UriParser.GopherUri.SchemeName; |
| | 1 | 27 | | public static readonly string UriSchemeHttp = UriParser.HttpUri.SchemeName; |
| | 1 | 28 | | public static readonly string UriSchemeHttps = UriParser.HttpsUri.SchemeName; |
| | 1 | 29 | | public static readonly string UriSchemeWs = UriParser.WsUri.SchemeName; |
| | 1 | 30 | | public static readonly string UriSchemeWss = UriParser.WssUri.SchemeName; |
| | 1 | 31 | | public static readonly string UriSchemeMailto = UriParser.MailToUri.SchemeName; |
| | 1 | 32 | | public static readonly string UriSchemeNews = UriParser.NewsUri.SchemeName; |
| | 1 | 33 | | public static readonly string UriSchemeNntp = UriParser.NntpUri.SchemeName; |
| | 1 | 34 | | public static readonly string UriSchemeSsh = "ssh"; |
| | 1 | 35 | | public static readonly string UriSchemeTelnet = UriParser.TelnetUri.SchemeName; |
| | 1 | 36 | | public static readonly string UriSchemeNetTcp = UriParser.NetTcpUri.SchemeName; |
| | 1 | 37 | | public static readonly string UriSchemeNetPipe = UriParser.NetPipeUri.SchemeName; |
| | | 38 | | /// <summary>Specifies that the URI is a data URI.</summary> |
| | 1 | 39 | | public static readonly string UriSchemeData = "data"; |
| | 1 | 40 | | public static readonly string SchemeDelimiter = "://"; |
| | | 41 | | |
| | | 42 | | private const int SchemeLengthLimit = 1024; |
| | | 43 | | |
| | | 44 | | internal const int StackallocThreshold = 512; |
| | | 45 | | |
| | | 46 | | // untouched user string unless string has unicode chars and iriparsing is enabled |
| | | 47 | | // or idn is on and we have unicode host or idn host |
| | | 48 | | // In that case, this string is normalized, stripped of bidi chars, and validated |
| | | 49 | | // with char limits |
| | | 50 | | private string _string; |
| | | 51 | | |
| | | 52 | | // untouched user string if string has unicode with iri on or unicode/idn host with idn on |
| | 661 | 53 | | private string _originalUnicodeString = null!; // initialized in ctor via helper |
| | | 54 | | |
| | 661 | 55 | | internal UriParser _syntax = null!; // Initialized in ctor via helper. This is a whole Uri syntax, not only th |
| | | 56 | | |
| | | 57 | | internal Flags _flags; |
| | 661 | 58 | | private UriInfo _info = null!; |
| | | 59 | | |
| | | 60 | | [Flags] |
| | | 61 | | internal enum Flags : ulong |
| | | 62 | | { |
| | | 63 | | Zero = 0, |
| | | 64 | | |
| | | 65 | | SchemeNotCanonical = 1UL << 0, |
| | | 66 | | UserNotCanonical = 1UL << 1, |
| | | 67 | | HostNotCanonical = 1UL << 2, |
| | | 68 | | PortNotCanonical = 1UL << 3, |
| | | 69 | | PathNotCanonical = 1UL << 4, |
| | | 70 | | QueryNotCanonical = 1UL << 5, |
| | | 71 | | FragmentNotCanonical = 1UL << 6, |
| | | 72 | | CannotDisplayCanonical = SchemeNotCanonical | UserNotCanonical | HostNotCanonical | PortNotCanonical | PathN |
| | | 73 | | |
| | | 74 | | E_UserNotCanonical = 1UL << 7, |
| | | 75 | | E_HostNotCanonical = 1UL << 8, |
| | | 76 | | E_PortNotCanonical = 1UL << 9, |
| | | 77 | | E_PathNotCanonical = 1UL << 10, |
| | | 78 | | E_QueryNotCanonical = 1UL << 11, |
| | | 79 | | E_FragmentNotCanonical = 1UL << 12, |
| | | 80 | | E_CannotDisplayCanonical = E_UserNotCanonical | E_HostNotCanonical | E_PortNotCanonical | E_PathNotCanonical |
| | | 81 | | |
| | | 82 | | ShouldBeCompressed = 1UL << 13, |
| | | 83 | | FirstSlashAbsent = 1UL << 14, |
| | | 84 | | BackslashInPath = 1UL << 15, |
| | | 85 | | |
| | | 86 | | IndexMask = 0xFFFFFFFF, // 32 bits |
| | | 87 | | |
| | | 88 | | HostTypeMask = 7 * (1UL << 32), |
| | | 89 | | HostNotParsed = 0, |
| | | 90 | | IPv6HostType = 1 * (1UL << 32), |
| | | 91 | | IPv4HostType = 2 * (1UL << 32), |
| | | 92 | | DnsHostType = 3 * (1UL << 32), |
| | | 93 | | UncHostType = 4 * (1UL << 32), |
| | | 94 | | BasicHostType = 5 * (1UL << 32), |
| | | 95 | | UnusedHostType = 6 * (1UL << 32), |
| | | 96 | | UnknownHostType = 7 * (1UL << 32), |
| | | 97 | | |
| | | 98 | | UserEscaped = 1UL << 35, |
| | | 99 | | AuthorityFound = 1UL << 36, |
| | | 100 | | HasUserInfo = 1UL << 37, |
| | | 101 | | LoopbackHost = 1UL << 38, |
| | | 102 | | NotDefaultPort = 1UL << 39, |
| | | 103 | | |
| | | 104 | | UserDrivenParsing = 1UL << 40, |
| | | 105 | | CanonicalDnsHost = 1UL << 41, |
| | | 106 | | ErrorOrParsingRecursion = 1UL << 42, // Used to signal a default parser error and also to confirm Port |
| | | 107 | | // and Host values in case of a custom user Parser |
| | | 108 | | DosPath = 1UL << 43, |
| | | 109 | | UncPath = 1UL << 44, |
| | | 110 | | ImplicitFile = 1UL << 45, |
| | | 111 | | MinimalUriInfoSet = 1UL << 46, |
| | | 112 | | AllUriInfoSet = 1UL << 47, |
| | | 113 | | IdnHost = 1UL << 48, |
| | | 114 | | HasUnicode = 1UL << 49, |
| | | 115 | | |
| | | 116 | | // Is this component Iri canonical |
| | | 117 | | UserIriCanonical = 1UL << 50, |
| | | 118 | | PathIriCanonical = 1UL << 51, |
| | | 119 | | QueryIriCanonical = 1UL << 52, |
| | | 120 | | FragmentIriCanonical = 1UL << 53, |
| | | 121 | | IriCanonical = UserIriCanonical | PathIriCanonical | QueryIriCanonical | FragmentIriCanonical, |
| | | 122 | | UnixPath = 1UL << 54, |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Disables any validation/normalization past the authority. Fragments will always be empty. GetComponents |
| | | 126 | | /// </summary> |
| | | 127 | | DisablePathAndQueryCanonicalization = 1UL << 55, |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Used to ensure that InitializeAndValidate is only called once per Uri instance and only from an override |
| | | 131 | | /// </summary> |
| | | 132 | | CustomParser_ParseMinimalAlreadyCalled = 1UL << 56, |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Used for asserting that certain methods are only called from the constructor to validate thread-safety a |
| | | 136 | | /// </summary> |
| | | 137 | | Debug_LeftConstructor = 1UL << 57, |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Indicates that there is an authority, but the scheme does not use "//" to delimit it (e.g. "http:\\host" |
| | | 141 | | /// </summary> |
| | | 142 | | SchemeNotCanonical_NoTrailingSlashes = 1UL << 58, |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Disables recognizing absolute file paths as file URIs during initial scheme parsing. |
| | | 146 | | /// </summary> |
| | | 147 | | DisableImplicitFilePaths = 1UL << 59, |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | [Conditional("DEBUG")] |
| | | 151 | | private void DebugSetLeftCtor() |
| | 661 | 152 | | { |
| | 661 | 153 | | DebugAssertInCtor(); |
| | | 154 | | |
| | 661 | 155 | | _flags |= Flags.Debug_LeftConstructor; |
| | | 156 | | |
| | 661 | 157 | | AssertInvariants(); |
| | 661 | 158 | | } |
| | | 159 | | |
| | | 160 | | [Conditional("DEBUG")] |
| | | 161 | | private void AssertInvariants() |
| | 1322 | 162 | | { |
| | 1322 | 163 | | Debug.Assert(InFact(Flags.MinimalUriInfoSet) == (_info is not null)); |
| | | 164 | | |
| | 1322 | 165 | | if (_info is UriInfo info) |
| | 661 | 166 | | { |
| | 661 | 167 | | Debug.Assert(IsAbsoluteUri); |
| | | 168 | | |
| | 661 | 169 | | Offset offset = info.Offset; |
| | | 170 | | |
| | 661 | 171 | | Debug.Assert(offset.Scheme >= 0); |
| | 661 | 172 | | Debug.Assert(offset.User >= 0 && offset.User >= offset.Scheme); |
| | 661 | 173 | | Debug.Assert(offset.Host >= 0 && offset.Host >= offset.User); |
| | 661 | 174 | | Debug.Assert(offset.Path >= 0); |
| | 661 | 175 | | Debug.Assert(offset.End >= 0 && offset.End >= offset.Path); |
| | | 176 | | |
| | 661 | 177 | | if (InFact(Flags.AllUriInfoSet)) |
| | 0 | 178 | | { |
| | 0 | 179 | | Debug.Assert(offset.Path >= offset.Host); |
| | 0 | 180 | | Debug.Assert(offset.Query >= 0 && offset.Query >= offset.Path); |
| | 0 | 181 | | Debug.Assert(offset.Fragment >= 0 && offset.Fragment >= offset.Query); |
| | 0 | 182 | | Debug.Assert(offset.End >= offset.Fragment && offset.End <= _string.Length); |
| | 0 | 183 | | } |
| | | 184 | | else |
| | 661 | 185 | | { |
| | | 186 | | // If we have non-ASCII, we're about to continue with ParseRemaining. |
| | | 187 | | // Between CreateUriInfo and the rest of ParseRemaining, the Path offset is pointing into the origin |
| | | 188 | | // while the Host offset is poining into the new _string. As such the Host offset may temporarily be |
| | 661 | 189 | | if (!InFact(Flags.HasUnicode)) |
| | 661 | 190 | | { |
| | 661 | 191 | | Debug.Assert(offset.Path >= offset.Host); |
| | 661 | 192 | | } |
| | | 193 | | |
| | | 194 | | // We can't check that Query and Fragment offsets are 0 here as a different thread may have called |
| | | 195 | | // ParseRemaining after we've checked InFact(Flags.AllUriInfoSet). |
| | 661 | 196 | | } |
| | 661 | 197 | | } |
| | | 198 | | else |
| | 661 | 199 | | { |
| | 661 | 200 | | if (IsAbsoluteUri && IriParsing) |
| | 661 | 201 | | { |
| | 661 | 202 | | Debug.Assert(Ascii.IsValid(_string)); |
| | 661 | 203 | | } |
| | 661 | 204 | | } |
| | 1322 | 205 | | } |
| | | 206 | | |
| | | 207 | | [Conditional("DEBUG")] |
| | | 208 | | internal void DebugAssertInCtor() |
| | 1983 | 209 | | { |
| | 1983 | 210 | | Debug.Assert((_flags & Flags.Debug_LeftConstructor) == 0); |
| | 1983 | 211 | | } |
| | | 212 | | |
| | | 213 | | private sealed class UriInfo |
| | | 214 | | { |
| | | 215 | | public Offset Offset; |
| | | 216 | | public string? String; |
| | | 217 | | public string? Host; |
| | | 218 | | public string? IdnHost; |
| | | 219 | | public string? PathAndQuery; |
| | | 220 | | |
| | | 221 | | public MoreInfo? _moreInfo; |
| | | 222 | | public MoreInfo MoreInfo |
| | | 223 | | { |
| | | 224 | | get |
| | 0 | 225 | | { |
| | 0 | 226 | | if (_moreInfo is null) |
| | 0 | 227 | | { |
| | 0 | 228 | | Interlocked.CompareExchange(ref _moreInfo, new MoreInfo(), null); |
| | 0 | 229 | | } |
| | 0 | 230 | | return _moreInfo; |
| | 0 | 231 | | } |
| | | 232 | | } |
| | | 233 | | }; |
| | | 234 | | |
| | | 235 | | private struct Offset |
| | | 236 | | { |
| | | 237 | | public int Scheme; |
| | | 238 | | public int User; |
| | | 239 | | public int Host; |
| | | 240 | | public ushort PortValue; |
| | | 241 | | public int Path; |
| | | 242 | | public int Query; |
| | | 243 | | public int Fragment; |
| | | 244 | | public int End; |
| | | 245 | | }; |
| | | 246 | | |
| | | 247 | | private sealed class MoreInfo |
| | | 248 | | { |
| | | 249 | | public string? Path; |
| | | 250 | | public string? Query; |
| | | 251 | | public string? Fragment; |
| | | 252 | | public string? AbsoluteUri; |
| | | 253 | | public string? RemoteUrl; |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Only IP v6 may need this |
| | | 257 | | /// </summary> |
| | | 258 | | public string? ScopeId; |
| | | 259 | | }; |
| | | 260 | | |
| | | 261 | | private void InterlockedSetFlags(Flags flags) |
| | 0 | 262 | | { |
| | 0 | 263 | | Debug.Assert(_syntax != null); |
| | | 264 | | |
| | 0 | 265 | | if (_syntax.IsSimple) |
| | 0 | 266 | | { |
| | | 267 | | // For built-in (simple) parsers, it is safe to do an Interlocked update here |
| | 0 | 268 | | Debug.Assert(sizeof(Flags) == sizeof(ulong)); |
| | 0 | 269 | | Interlocked.Or(ref _flags, flags); |
| | 0 | 270 | | } |
| | | 271 | | else |
| | 0 | 272 | | { |
| | | 273 | | // Custom parsers still use a lock in CreateHostString and perform non-atomic flags updates |
| | | 274 | | // We have to take the lock to ensure flags access synchronization if CreateHostString and ParseRemainin |
| | 0 | 275 | | lock (_info) |
| | 0 | 276 | | { |
| | 0 | 277 | | _flags |= flags; |
| | 0 | 278 | | } |
| | 0 | 279 | | } |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | private bool IsImplicitFile |
| | | 283 | | { |
| | 1983 | 284 | | get { return (_flags & Flags.ImplicitFile) != 0; } |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | private bool IsUncOrDosPath |
| | | 288 | | { |
| | 0 | 289 | | get { return (_flags & (Flags.UncPath | Flags.DosPath)) != 0; } |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | private bool IsDosPath |
| | | 293 | | { |
| | 0 | 294 | | get { return (_flags & Flags.DosPath) != 0; } |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | private bool IsUncPath |
| | | 298 | | { |
| | 0 | 299 | | get { return (_flags & Flags.UncPath) != 0; } |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | private bool IsUnixPath |
| | | 303 | | { |
| | 0 | 304 | | get { return (_flags & Flags.UnixPath) != 0; } |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private Flags HostType |
| | | 308 | | { |
| | 1983 | 309 | | get { return _flags & Flags.HostTypeMask; } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | private UriParser Syntax |
| | | 313 | | { |
| | | 314 | | get |
| | 0 | 315 | | { |
| | 0 | 316 | | return _syntax; |
| | 0 | 317 | | } |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | private bool IsNotAbsoluteUri |
| | | 321 | | { |
| | 1983 | 322 | | get { return _syntax is null; } |
| | | 323 | | } |
| | | 324 | | |
| | | 325 | | // |
| | | 326 | | // Checks if Iri parsing is allowed by the syntax & by config |
| | | 327 | | // |
| | 1322 | 328 | | private bool IriParsing => IriParsingStatic(_syntax); |
| | | 329 | | |
| | | 330 | | internal static bool IriParsingStatic(UriParser? syntax) |
| | 1322 | 331 | | { |
| | 1322 | 332 | | return syntax is null || syntax.InFact(UriSyntaxFlags.AllowIriParsing); |
| | 1322 | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | internal bool DisablePathAndQueryCanonicalization => (_flags & Flags.DisablePathAndQueryCanonicalization) != 0; |
| | | 336 | | |
| | | 337 | | internal bool UserDrivenParsing |
| | | 338 | | { |
| | | 339 | | get |
| | 661 | 340 | | { |
| | 661 | 341 | | return (_flags & Flags.UserDrivenParsing) != 0; |
| | 661 | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | private int SecuredPathIndex |
| | | 346 | | { |
| | | 347 | | get |
| | 0 | 348 | | { |
| | | 349 | | // This is one more trouble with a Dos Path. |
| | | 350 | | // This property gets "safe" first path slash that is not the first if path = c:\ |
| | 0 | 351 | | if (IsDosPath) |
| | 0 | 352 | | { |
| | 0 | 353 | | char ch = _string[_info.Offset.Path]; |
| | 0 | 354 | | return (ch == '/' || ch == '\\') ? 3 : 2; |
| | | 355 | | } |
| | 0 | 356 | | return 0; |
| | 0 | 357 | | } |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | private bool NotAny(Flags flags) |
| | 0 | 361 | | { |
| | 0 | 362 | | return (_flags & flags) == 0; |
| | 0 | 363 | | } |
| | | 364 | | |
| | | 365 | | private bool InFact(Flags flags) |
| | 3305 | 366 | | { |
| | 3305 | 367 | | return (_flags & flags) != 0; |
| | 3305 | 368 | | } |
| | | 369 | | |
| | | 370 | | private static bool StaticNotAny(Flags allFlags, Flags checkFlags) |
| | 123 | 371 | | { |
| | 123 | 372 | | return (allFlags & checkFlags) == 0; |
| | 123 | 373 | | } |
| | | 374 | | |
| | | 375 | | private static bool StaticInFact(Flags allFlags, Flags checkFlags) |
| | 0 | 376 | | { |
| | 0 | 377 | | return (allFlags & checkFlags) != 0; |
| | 0 | 378 | | } |
| | | 379 | | |
| | | 380 | | [MemberNotNull(nameof(_info))] |
| | | 381 | | private UriInfo EnsureUriInfo() |
| | 661 | 382 | | { |
| | 661 | 383 | | Flags cF = _flags; |
| | 661 | 384 | | if ((cF & Flags.MinimalUriInfoSet) == 0) |
| | 661 | 385 | | { |
| | 661 | 386 | | CreateUriInfo(cF); |
| | 661 | 387 | | AssertInvariants(); |
| | 661 | 388 | | } |
| | 661 | 389 | | Debug.Assert(_info != null && (_flags & Flags.MinimalUriInfoSet) != 0); |
| | 661 | 390 | | return _info; |
| | 661 | 391 | | } |
| | | 392 | | |
| | | 393 | | private void EnsureParseRemaining() |
| | 0 | 394 | | { |
| | 0 | 395 | | if ((_flags & Flags.AllUriInfoSet) == 0) |
| | 0 | 396 | | { |
| | 0 | 397 | | ParseRemaining(); |
| | 0 | 398 | | AssertInvariants(); |
| | 0 | 399 | | } |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | [MemberNotNull(nameof(_info))] |
| | | 403 | | private void EnsureHostString(bool allowDnsOptimization) |
| | 0 | 404 | | { |
| | 0 | 405 | | UriInfo info = EnsureUriInfo(); |
| | | 406 | | |
| | 0 | 407 | | if (info.Host is null) |
| | 0 | 408 | | { |
| | 0 | 409 | | if (allowDnsOptimization && InFact(Flags.CanonicalDnsHost)) |
| | 0 | 410 | | { |
| | | 411 | | /* Optimization for a canonical DNS name |
| | | 412 | | * ATTN: the host string won't be created, |
| | | 413 | | * Hence ALL _info.Host callers first call EnsureHostString(false) |
| | | 414 | | * For example IsLoopBack property is one of such callers. |
| | | 415 | | */ |
| | 0 | 416 | | return; |
| | | 417 | | } |
| | 0 | 418 | | CreateHostString(); |
| | 0 | 419 | | } |
| | 0 | 420 | | } |
| | | 421 | | |
| | | 422 | | // |
| | | 423 | | // Uri(string) |
| | | 424 | | // |
| | | 425 | | // We expect to create a Uri from a display name - e.g. that was typed by |
| | | 426 | | // a user, or that was copied & pasted from a document. That is, we do not |
| | | 427 | | // expect already encoded URI to be supplied. |
| | | 428 | | // |
| | 0 | 429 | | public Uri([StringSyntax(StringSyntaxAttribute.Uri)] string uriString) |
| | 0 | 430 | | { |
| | 0 | 431 | | ArgumentNullException.ThrowIfNull(uriString); |
| | | 432 | | |
| | 0 | 433 | | CreateThis(uriString, Flags.Zero, UriKind.Absolute); |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | // |
| | | 437 | | // Uri(string, bool) |
| | | 438 | | // |
| | | 439 | | // Uri constructor. Assumes that input string is canonically escaped |
| | | 440 | | // |
| | | 441 | | [Obsolete("This constructor has been deprecated. Use Uri(string) instead.")] |
| | 0 | 442 | | public Uri([StringSyntax(StringSyntaxAttribute.Uri)] string uriString, bool dontEscape) |
| | 0 | 443 | | { |
| | 0 | 444 | | ArgumentNullException.ThrowIfNull(uriString); |
| | | 445 | | |
| | 0 | 446 | | CreateThis(uriString, dontEscape ? Flags.UserEscaped : Flags.Zero, UriKind.Absolute); |
| | 0 | 447 | | } |
| | | 448 | | |
| | | 449 | | // |
| | | 450 | | // Uri(Uri, string, bool) |
| | | 451 | | // |
| | | 452 | | // Uri combinatorial constructor. Do not perform character escaping if |
| | | 453 | | // DontEscape is true |
| | | 454 | | // |
| | | 455 | | [Obsolete("This constructor has been deprecated. Use Uri(Uri, string) instead.")] |
| | 0 | 456 | | public Uri(Uri baseUri, string? relativeUri, bool dontEscape) |
| | 0 | 457 | | { |
| | 0 | 458 | | ArgumentNullException.ThrowIfNull(baseUri); |
| | | 459 | | |
| | 0 | 460 | | if (!baseUri.IsAbsoluteUri) |
| | 0 | 461 | | throw new ArgumentOutOfRangeException(nameof(baseUri)); |
| | | 462 | | |
| | 0 | 463 | | CreateUri(baseUri, relativeUri, dontEscape); |
| | 0 | 464 | | } |
| | | 465 | | |
| | | 466 | | // |
| | | 467 | | // Uri(string, UriKind); |
| | | 468 | | // |
| | 0 | 469 | | public Uri([StringSyntax(StringSyntaxAttribute.Uri, nameof(uriKind))] string uriString, UriKind uriKind) |
| | 0 | 470 | | { |
| | 0 | 471 | | ArgumentNullException.ThrowIfNull(uriString); |
| | | 472 | | |
| | 0 | 473 | | CreateThis(uriString, Flags.Zero, uriKind); |
| | 0 | 474 | | } |
| | | 475 | | |
| | | 476 | | /// <summary> |
| | | 477 | | /// Initializes a new instance of the <see cref="Uri"/> class with the specified URI and additional <see cref="U |
| | | 478 | | /// </summary> |
| | | 479 | | /// <param name="uriString">A string that identifies the resource to be represented by the <see cref="Uri"/> ins |
| | | 480 | | /// <param name="creationOptions">Options that control how the <seealso cref="Uri"/> is created and behaves.</pa |
| | 0 | 481 | | public Uri([StringSyntax(StringSyntaxAttribute.Uri)] string uriString, in UriCreationOptions creationOptions) |
| | 0 | 482 | | { |
| | 0 | 483 | | ArgumentNullException.ThrowIfNull(uriString); |
| | | 484 | | |
| | 0 | 485 | | CreateThis(uriString, creationOptions._flags, UriKind.Absolute); |
| | 0 | 486 | | } |
| | | 487 | | |
| | | 488 | | // |
| | | 489 | | // Uri(Uri, string) |
| | | 490 | | // |
| | | 491 | | // Construct a new Uri from a base and relative URI. The relative URI may |
| | | 492 | | // also be an absolute URI, in which case the resultant URI is constructed |
| | | 493 | | // entirely from it |
| | | 494 | | // |
| | 0 | 495 | | public Uri(Uri baseUri, string? relativeUri) |
| | 0 | 496 | | { |
| | 0 | 497 | | ArgumentNullException.ThrowIfNull(baseUri); |
| | | 498 | | |
| | 0 | 499 | | if (!baseUri.IsAbsoluteUri) |
| | 0 | 500 | | throw new ArgumentOutOfRangeException(nameof(baseUri)); |
| | | 501 | | |
| | 0 | 502 | | CreateUri(baseUri, relativeUri, false); |
| | 0 | 503 | | } |
| | | 504 | | |
| | | 505 | | // |
| | | 506 | | // Uri(SerializationInfo, StreamingContext) |
| | | 507 | | // |
| | | 508 | | // ISerializable constructor |
| | | 509 | | // |
| | | 510 | | [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma |
| | | 511 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 512 | | protected Uri(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | 0 | 513 | | { |
| | 0 | 514 | | string? uriString = serializationInfo.GetString("AbsoluteUri"); // Do not rename (binary serialization) |
| | | 515 | | |
| | 0 | 516 | | if (uriString!.Length != 0) |
| | 0 | 517 | | { |
| | 0 | 518 | | CreateThis(uriString, Flags.Zero, UriKind.Absolute); |
| | 0 | 519 | | return; |
| | | 520 | | } |
| | | 521 | | |
| | 0 | 522 | | uriString = serializationInfo.GetString("RelativeUri"); // Do not rename (binary serialization) |
| | 0 | 523 | | if (uriString is null) |
| | 0 | 524 | | throw new ArgumentException(SR.Format(SR.InvalidNullArgument, "RelativeUri"), nameof(serializationInfo)) |
| | | 525 | | |
| | 0 | 526 | | CreateThis(uriString, Flags.Zero, UriKind.Relative); |
| | 0 | 527 | | } |
| | | 528 | | |
| | | 529 | | // |
| | | 530 | | // ISerializable method |
| | | 531 | | // |
| | | 532 | | /// <internalonly/> |
| | | 533 | | void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | 0 | 534 | | { |
| | 0 | 535 | | GetObjectData(serializationInfo, streamingContext); |
| | 0 | 536 | | } |
| | | 537 | | |
| | | 538 | | // |
| | | 539 | | // FxCop: provide some way for derived classes to access GetObjectData even if the derived class |
| | | 540 | | // explicitly re-inherits ISerializable. |
| | | 541 | | // |
| | | 542 | | protected void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | 0 | 543 | | { |
| | | 544 | | |
| | 0 | 545 | | if (IsAbsoluteUri) |
| | 0 | 546 | | serializationInfo.AddValue("AbsoluteUri", GetParts(UriComponents.SerializationInfoString, UriFormat.UriE |
| | | 547 | | else |
| | 0 | 548 | | { |
| | 0 | 549 | | serializationInfo.AddValue("AbsoluteUri", string.Empty); // Do not rename (binary serialization) |
| | 0 | 550 | | serializationInfo.AddValue("RelativeUri", GetParts(UriComponents.SerializationInfoString, UriFormat.UriE |
| | 0 | 551 | | } |
| | 0 | 552 | | } |
| | | 553 | | |
| | | 554 | | [MemberNotNull(nameof(_string))] |
| | | 555 | | private void CreateUri(Uri baseUri, string? relativeUri, bool dontEscape) |
| | 0 | 556 | | { |
| | 0 | 557 | | DebugAssertInCtor(); |
| | | 558 | | |
| | | 559 | | // Parse relativeUri and populate Uri internal data. |
| | 0 | 560 | | CreateThis(relativeUri, dontEscape ? Flags.UserEscaped : Flags.Zero, UriKind.RelativeOrAbsolute); |
| | | 561 | | |
| | 0 | 562 | | if (baseUri.Syntax!.IsSimple) |
| | 0 | 563 | | { |
| | | 564 | | // Resolve Uris if possible OR get merged Uri String to re-parse below |
| | 0 | 565 | | Uri? uriResult = ResolveHelper(baseUri, this, ref relativeUri, ref dontEscape); |
| | | 566 | | |
| | | 567 | | // If resolved into a Uri then we build from that Uri |
| | 0 | 568 | | if (uriResult != null) |
| | 0 | 569 | | { |
| | 0 | 570 | | if (!ReferenceEquals(this, uriResult)) |
| | 0 | 571 | | CreateThisFromUri(uriResult); |
| | | 572 | | |
| | 0 | 573 | | return; |
| | | 574 | | } |
| | 0 | 575 | | } |
| | | 576 | | else |
| | 0 | 577 | | { |
| | 0 | 578 | | dontEscape = false; |
| | 0 | 579 | | relativeUri = baseUri.Syntax.InternalResolve(baseUri, this, out UriFormatException? e); |
| | 0 | 580 | | if (e != null) |
| | 0 | 581 | | throw e; |
| | 0 | 582 | | } |
| | | 583 | | |
| | 0 | 584 | | _flags = Flags.Zero; |
| | 0 | 585 | | _info = null!; |
| | 0 | 586 | | _syntax = null!; |
| | 0 | 587 | | _originalUnicodeString = null!; |
| | | 588 | | // If not resolved, we reparse modified Uri string and populate Uri internal data. |
| | 0 | 589 | | CreateThis(relativeUri, dontEscape ? Flags.UserEscaped : Flags.Zero, UriKind.Absolute); |
| | 0 | 590 | | } |
| | | 591 | | |
| | | 592 | | // |
| | | 593 | | // Uri(Uri , Uri ) |
| | | 594 | | // Note: a static Create() method should be used by users, not this .ctor |
| | | 595 | | // |
| | 0 | 596 | | public Uri(Uri baseUri, Uri relativeUri) |
| | 0 | 597 | | { |
| | 0 | 598 | | ArgumentNullException.ThrowIfNull(baseUri); |
| | 0 | 599 | | ArgumentNullException.ThrowIfNull(relativeUri); |
| | | 600 | | |
| | 0 | 601 | | if (!baseUri.IsAbsoluteUri) |
| | 0 | 602 | | throw new ArgumentOutOfRangeException(nameof(baseUri)); |
| | | 603 | | |
| | 0 | 604 | | CreateThisFromUri(relativeUri); |
| | | 605 | | |
| | 0 | 606 | | string? newUriString = null; |
| | | 607 | | bool dontEscape; |
| | | 608 | | |
| | 0 | 609 | | if (baseUri.Syntax!.IsSimple) |
| | 0 | 610 | | { |
| | 0 | 611 | | dontEscape = InFact(Flags.UserEscaped); |
| | 0 | 612 | | Uri? resolvedRelativeUri = ResolveHelper(baseUri, this, ref newUriString, ref dontEscape); |
| | | 613 | | |
| | 0 | 614 | | if (resolvedRelativeUri != null) |
| | 0 | 615 | | { |
| | 0 | 616 | | if (!ReferenceEquals(this, resolvedRelativeUri)) |
| | 0 | 617 | | CreateThisFromUri(resolvedRelativeUri); |
| | | 618 | | |
| | 0 | 619 | | return; |
| | | 620 | | } |
| | 0 | 621 | | } |
| | | 622 | | else |
| | 0 | 623 | | { |
| | 0 | 624 | | dontEscape = false; |
| | 0 | 625 | | newUriString = baseUri.Syntax.InternalResolve(baseUri, this, out UriFormatException? e); |
| | 0 | 626 | | if (e != null) |
| | 0 | 627 | | throw e; |
| | 0 | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | _flags = Flags.Zero; |
| | 0 | 631 | | _info = null!; |
| | 0 | 632 | | _syntax = null!; |
| | 0 | 633 | | _originalUnicodeString = null!; |
| | 0 | 634 | | CreateThis(newUriString, dontEscape ? Flags.UserEscaped : Flags.Zero, UriKind.Absolute); |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | // |
| | | 638 | | // This method is shared by base+relative Uris constructors and is only called from them. |
| | | 639 | | // The assumptions: |
| | | 640 | | // - baseUri is a valid absolute Uri |
| | | 641 | | // - relative part is not null and not empty |
| | | 642 | | private static void GetCombinedString(Uri baseUri, string relativeStr, |
| | | 643 | | bool dontEscape, ref string? result) |
| | 0 | 644 | | { |
| | | 645 | | // NB: This is not RFC2396 compliant although it is inline with w3c.org recommendations |
| | | 646 | | // This parser will allow the relativeStr to be an absolute Uri with the different scheme |
| | | 647 | | // In fact this is strict violation of RFC2396 |
| | | 648 | | // |
| | 0 | 649 | | int i = relativeStr.IndexOfAny(s_segmentSeparatorChars); |
| | | 650 | | |
| | | 651 | | // Note we don't support one-letter Uri schemes (i > 1). |
| | | 652 | | // Hence anything like x:sdsd is a relative path and be added to the baseUri Path |
| | 0 | 653 | | if ((uint)i < (uint)relativeStr.Length && relativeStr[i] == ':' && i > 1) |
| | 0 | 654 | | { |
| | 0 | 655 | | ParsingError error = ParsingError.None; |
| | 0 | 656 | | UriParser? syntax = CheckSchemeSyntax(relativeStr.AsSpan(0, i), ref error); |
| | | 657 | | |
| | 0 | 658 | | if (error == ParsingError.None) |
| | 0 | 659 | | { |
| | 0 | 660 | | if (baseUri.Syntax == syntax) |
| | 0 | 661 | | { |
| | | 662 | | //Remove the scheme for backward Uri parsers compatibility |
| | 0 | 663 | | if (i + 1 < relativeStr.Length) |
| | 0 | 664 | | { |
| | 0 | 665 | | relativeStr = relativeStr.Substring(i + 1); |
| | 0 | 666 | | } |
| | | 667 | | else |
| | 0 | 668 | | { |
| | 0 | 669 | | relativeStr = string.Empty; |
| | 0 | 670 | | } |
| | 0 | 671 | | } |
| | | 672 | | else |
| | 0 | 673 | | { |
| | | 674 | | // This is the place where we switch the scheme. |
| | | 675 | | // Return relative part as the result Uri. |
| | 0 | 676 | | result = relativeStr; |
| | 0 | 677 | | return; |
| | | 678 | | } |
| | 0 | 679 | | } |
| | 0 | 680 | | } |
| | | 681 | | |
| | 0 | 682 | | if (relativeStr.Length == 0) |
| | 0 | 683 | | { |
| | 0 | 684 | | result = baseUri.OriginalString; |
| | 0 | 685 | | } |
| | | 686 | | else |
| | 0 | 687 | | { |
| | 0 | 688 | | result = CombineUri(baseUri, relativeStr, dontEscape ? UriFormat.UriEscaped : UriFormat.SafeUnescaped); |
| | 0 | 689 | | } |
| | 0 | 690 | | } |
| | | 691 | | |
| | | 692 | | private static UriFormatException GetException(ParsingError err) |
| | 0 | 693 | | { |
| | 0 | 694 | | Debug.Assert(err != ParsingError.None); |
| | | 695 | | |
| | 0 | 696 | | string message = err switch |
| | 0 | 697 | | { |
| | 0 | 698 | | // Could be OK for Relative Uri |
| | 0 | 699 | | ParsingError.BadFormat => SR.net_uri_BadFormat, |
| | 0 | 700 | | ParsingError.BadScheme => SR.net_uri_BadScheme, |
| | 0 | 701 | | ParsingError.BadAuthority => SR.net_uri_BadAuthority, |
| | 0 | 702 | | ParsingError.EmptyUriString => SR.net_uri_EmptyUri, |
| | 0 | 703 | | |
| | 0 | 704 | | // Fatal |
| | 0 | 705 | | ParsingError.SchemeLimit => SR.net_uri_SchemeLimit, |
| | 0 | 706 | | ParsingError.MustRootedPath => SR.net_uri_MustRootedPath, |
| | 0 | 707 | | |
| | 0 | 708 | | // Derived class controllable |
| | 0 | 709 | | ParsingError.BadHostName => SR.net_uri_BadHostName, |
| | 0 | 710 | | ParsingError.BadPort => SR.net_uri_BadPort, |
| | 0 | 711 | | ParsingError.BadAuthorityTerminator => SR.net_uri_BadAuthorityTerminator, |
| | 0 | 712 | | ParsingError.CannotCreateRelative => SR.net_uri_CannotCreateRelative, |
| | 0 | 713 | | |
| | 0 | 714 | | _ => throw new UnreachableException() |
| | 0 | 715 | | }; |
| | | 716 | | |
| | 0 | 717 | | return new UriFormatException(message); |
| | 0 | 718 | | } |
| | | 719 | | |
| | | 720 | | public string AbsolutePath |
| | | 721 | | { |
| | | 722 | | get |
| | 0 | 723 | | { |
| | 0 | 724 | | if (IsNotAbsoluteUri) |
| | 0 | 725 | | { |
| | 0 | 726 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 727 | | } |
| | | 728 | | |
| | 0 | 729 | | string path = PrivateAbsolutePath; |
| | | 730 | | // |
| | | 731 | | // For Compat: |
| | | 732 | | // Remove the first slash from a Dos Path if it's present |
| | | 733 | | // |
| | 0 | 734 | | if (IsDosPath && path[0] == '/') |
| | 0 | 735 | | { |
| | 0 | 736 | | path = path.Substring(1); |
| | 0 | 737 | | } |
| | 0 | 738 | | return path; |
| | 0 | 739 | | } |
| | | 740 | | } |
| | | 741 | | |
| | | 742 | | private string PrivateAbsolutePath |
| | | 743 | | { |
| | | 744 | | get |
| | 0 | 745 | | { |
| | 0 | 746 | | Debug.Assert(IsAbsoluteUri); |
| | | 747 | | |
| | 0 | 748 | | MoreInfo info = EnsureUriInfo().MoreInfo; |
| | 0 | 749 | | return info.Path ??= GetParts(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.UriEscaped); |
| | 0 | 750 | | } |
| | | 751 | | } |
| | | 752 | | |
| | | 753 | | public string AbsoluteUri |
| | | 754 | | { |
| | | 755 | | get |
| | 0 | 756 | | { |
| | 0 | 757 | | if (_syntax == null) |
| | 0 | 758 | | { |
| | 0 | 759 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 760 | | } |
| | | 761 | | |
| | 0 | 762 | | MoreInfo info = EnsureUriInfo().MoreInfo; |
| | 0 | 763 | | return info.AbsoluteUri ??= GetParts(UriComponents.AbsoluteUri, UriFormat.UriEscaped); |
| | 0 | 764 | | } |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | // |
| | | 768 | | // LocalPath |
| | | 769 | | // |
| | | 770 | | // Returns a 'local' version of the path. This is mainly for file: URI |
| | | 771 | | // such that DOS and UNC paths are returned with '/' converted back to |
| | | 772 | | // '\', and any escape sequences converted |
| | | 773 | | // |
| | | 774 | | // The form of the returned path is in NOT Escaped |
| | | 775 | | // |
| | | 776 | | public string LocalPath |
| | | 777 | | { |
| | | 778 | | get |
| | 0 | 779 | | { |
| | 0 | 780 | | if (IsNotAbsoluteUri) |
| | 0 | 781 | | { |
| | 0 | 782 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 783 | | } |
| | 0 | 784 | | return GetLocalPath(); |
| | 0 | 785 | | } |
| | | 786 | | } |
| | | 787 | | |
| | | 788 | | // |
| | | 789 | | // The result is of the form "hostname[:port]" Port is omitted if default |
| | | 790 | | // |
| | | 791 | | public string Authority |
| | | 792 | | { |
| | | 793 | | get |
| | 0 | 794 | | { |
| | 0 | 795 | | if (IsNotAbsoluteUri) |
| | 0 | 796 | | { |
| | 0 | 797 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 798 | | } |
| | | 799 | | |
| | | 800 | | // Note: Compatibility with V1 that does not report user info |
| | 0 | 801 | | return GetParts(UriComponents.Host | UriComponents.Port, UriFormat.UriEscaped); |
| | 0 | 802 | | } |
| | | 803 | | } |
| | | 804 | | |
| | | 805 | | |
| | | 806 | | public UriHostNameType HostNameType |
| | | 807 | | { |
| | | 808 | | get |
| | 661 | 809 | | { |
| | 661 | 810 | | if (IsNotAbsoluteUri) |
| | 0 | 811 | | { |
| | 0 | 812 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 813 | | } |
| | | 814 | | |
| | 661 | 815 | | if (_syntax.IsSimple) |
| | 661 | 816 | | EnsureUriInfo(); |
| | | 817 | | else |
| | 0 | 818 | | { |
| | | 819 | | // For a custom parser we request HostString creation to confirm HostType |
| | 0 | 820 | | EnsureHostString(false); |
| | 0 | 821 | | } |
| | | 822 | | |
| | 661 | 823 | | switch (HostType) |
| | | 824 | | { |
| | 0 | 825 | | case Flags.DnsHostType: return UriHostNameType.Dns; |
| | 123 | 826 | | case Flags.IPv4HostType: return UriHostNameType.IPv4; |
| | 538 | 827 | | case Flags.IPv6HostType: return UriHostNameType.IPv6; |
| | 0 | 828 | | case Flags.BasicHostType: return UriHostNameType.Basic; |
| | 0 | 829 | | case Flags.UncHostType: return UriHostNameType.Basic; |
| | 0 | 830 | | case Flags.UnknownHostType: return UriHostNameType.Unknown; |
| | | 831 | | default: |
| | 0 | 832 | | break; |
| | | 833 | | } |
| | 0 | 834 | | return UriHostNameType.Unknown; |
| | 661 | 835 | | } |
| | | 836 | | } |
| | | 837 | | |
| | | 838 | | public bool IsDefaultPort |
| | | 839 | | { |
| | | 840 | | get |
| | 0 | 841 | | { |
| | 0 | 842 | | if (IsNotAbsoluteUri) |
| | 0 | 843 | | { |
| | 0 | 844 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 845 | | } |
| | 0 | 846 | | if (_syntax.IsSimple) |
| | 0 | 847 | | EnsureUriInfo(); |
| | | 848 | | else |
| | 0 | 849 | | { |
| | | 850 | | // For a custom parser we request HostString creation that will also set the port |
| | 0 | 851 | | EnsureHostString(false); |
| | 0 | 852 | | } |
| | | 853 | | |
| | 0 | 854 | | return NotAny(Flags.NotDefaultPort); |
| | 0 | 855 | | } |
| | | 856 | | } |
| | | 857 | | |
| | | 858 | | public bool IsFile |
| | | 859 | | { |
| | | 860 | | get |
| | 0 | 861 | | { |
| | 0 | 862 | | if (IsNotAbsoluteUri) |
| | 0 | 863 | | { |
| | 0 | 864 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 865 | | } |
| | | 866 | | |
| | 0 | 867 | | return (object)_syntax.SchemeName == (object)UriSchemeFile; |
| | 0 | 868 | | } |
| | | 869 | | } |
| | | 870 | | |
| | | 871 | | public bool IsLoopback |
| | | 872 | | { |
| | | 873 | | get |
| | 0 | 874 | | { |
| | 0 | 875 | | if (IsNotAbsoluteUri) |
| | 0 | 876 | | { |
| | 0 | 877 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 878 | | } |
| | | 879 | | |
| | 0 | 880 | | EnsureHostString(false); |
| | | 881 | | |
| | 0 | 882 | | return InFact(Flags.LoopbackHost); |
| | 0 | 883 | | } |
| | | 884 | | } |
| | | 885 | | |
| | | 886 | | // |
| | | 887 | | // Gets the escaped Uri.AbsolutePath and Uri.Query |
| | | 888 | | // properties separated by a "?" character. |
| | | 889 | | public string PathAndQuery |
| | | 890 | | { |
| | | 891 | | get |
| | 0 | 892 | | { |
| | 0 | 893 | | if (IsNotAbsoluteUri) |
| | 0 | 894 | | { |
| | 0 | 895 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 896 | | } |
| | | 897 | | |
| | 0 | 898 | | UriInfo info = EnsureUriInfo(); |
| | | 899 | | |
| | 0 | 900 | | if (info.PathAndQuery is null) |
| | 0 | 901 | | { |
| | 0 | 902 | | string result = GetParts(UriComponents.PathAndQuery, UriFormat.UriEscaped); |
| | | 903 | | |
| | | 904 | | // Compatibility: |
| | | 905 | | // Remove the first slash from a Dos Path if it's present |
| | 0 | 906 | | if (IsDosPath && result[0] == '/') |
| | 0 | 907 | | { |
| | 0 | 908 | | result = result.Substring(1); |
| | 0 | 909 | | } |
| | | 910 | | |
| | 0 | 911 | | info.PathAndQuery = result; |
| | 0 | 912 | | } |
| | | 913 | | |
| | 0 | 914 | | return info.PathAndQuery; |
| | 0 | 915 | | } |
| | | 916 | | } |
| | | 917 | | |
| | | 918 | | // |
| | | 919 | | // Gets an array of the segments that make up a URI. |
| | | 920 | | public string[] Segments |
| | | 921 | | { |
| | | 922 | | get |
| | 0 | 923 | | { |
| | 0 | 924 | | if (IsNotAbsoluteUri) |
| | 0 | 925 | | { |
| | 0 | 926 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 927 | | } |
| | | 928 | | |
| | | 929 | | string[] segments; |
| | 0 | 930 | | string path = PrivateAbsolutePath; |
| | | 931 | | |
| | 0 | 932 | | if (path.Length == 0) |
| | 0 | 933 | | { |
| | 0 | 934 | | segments = Array.Empty<string>(); |
| | 0 | 935 | | } |
| | | 936 | | else |
| | 0 | 937 | | { |
| | 0 | 938 | | var pathSegments = new ValueListBuilder<string>(4); |
| | | 939 | | |
| | 0 | 940 | | int current = 0; |
| | 0 | 941 | | while (current < path.Length) |
| | 0 | 942 | | { |
| | 0 | 943 | | int next = path.IndexOf('/', current); |
| | 0 | 944 | | if (next == -1) |
| | 0 | 945 | | { |
| | 0 | 946 | | next = path.Length - 1; |
| | 0 | 947 | | } |
| | 0 | 948 | | pathSegments.Append(path.Substring(current, (next - current) + 1)); |
| | 0 | 949 | | current = next + 1; |
| | 0 | 950 | | } |
| | | 951 | | |
| | 0 | 952 | | segments = pathSegments.AsSpan().ToArray(); |
| | 0 | 953 | | pathSegments.Dispose(); |
| | 0 | 954 | | } |
| | | 955 | | |
| | 0 | 956 | | return segments; |
| | 0 | 957 | | } |
| | | 958 | | } |
| | | 959 | | |
| | | 960 | | public bool IsUnc |
| | | 961 | | { |
| | | 962 | | get |
| | 0 | 963 | | { |
| | 0 | 964 | | if (IsNotAbsoluteUri) |
| | 0 | 965 | | { |
| | 0 | 966 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 967 | | } |
| | 0 | 968 | | return IsUncPath; |
| | 0 | 969 | | } |
| | | 970 | | } |
| | | 971 | | |
| | | 972 | | // |
| | | 973 | | // Gets a hostname part (special formatting for IPv6 form) |
| | | 974 | | public string Host |
| | | 975 | | { |
| | | 976 | | get |
| | 0 | 977 | | { |
| | 0 | 978 | | if (IsNotAbsoluteUri) |
| | 0 | 979 | | { |
| | 0 | 980 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 981 | | } |
| | | 982 | | |
| | 0 | 983 | | return GetParts(UriComponents.Host, UriFormat.UriEscaped); |
| | 0 | 984 | | } |
| | | 985 | | } |
| | | 986 | | |
| | | 987 | | private static bool StaticIsFile(UriParser syntax) |
| | 0 | 988 | | { |
| | 0 | 989 | | return syntax.InFact(UriSyntaxFlags.FileLikeUri); |
| | 0 | 990 | | } |
| | | 991 | | |
| | | 992 | | private string GetLocalPath() |
| | 0 | 993 | | { |
| | 0 | 994 | | EnsureParseRemaining(); |
| | | 995 | | |
| | | 996 | | //Other cases will get a Unix-style path |
| | 0 | 997 | | if (IsUncOrDosPath) |
| | 0 | 998 | | { |
| | 0 | 999 | | EnsureHostString(false); |
| | 0 | 1000 | | Debug.Assert(_info != null); |
| | 0 | 1001 | | Debug.Assert(_info.Host != null); |
| | | 1002 | | int start; |
| | | 1003 | | |
| | | 1004 | | // Do we have a valid local path right in _string? |
| | 0 | 1005 | | if (NotAny(Flags.HostNotCanonical | Flags.PathNotCanonical | Flags.ShouldBeCompressed)) |
| | 0 | 1006 | | { |
| | 0 | 1007 | | start = IsUncPath ? _info.Offset.Host - 2 : _info.Offset.Path; |
| | | 1008 | | |
| | 0 | 1009 | | string str = (IsImplicitFile && _info.Offset.Host == (IsDosPath ? 0 : 2) && |
| | 0 | 1010 | | _info.Offset.Query == _info.Offset.End) |
| | 0 | 1011 | | ? _string |
| | 0 | 1012 | | : (IsDosPath && _string[start] is '/' or '\\') |
| | 0 | 1013 | | ? _string.Substring(start + 1, _info.Offset.Query - start - 1) |
| | 0 | 1014 | | : _string.Substring(start, _info.Offset.Query - start); |
| | | 1015 | | |
| | | 1016 | | // Should be a rare case, convert c|\ into c:\ |
| | 0 | 1017 | | if (IsDosPath && str[1] == '|') |
| | 0 | 1018 | | { |
| | | 1019 | | // Sadly, today there is no method for replacing just one occurrence |
| | 0 | 1020 | | str = str.Remove(1, 1); |
| | 0 | 1021 | | str = str.Insert(1, ":"); |
| | 0 | 1022 | | } |
| | | 1023 | | |
| | | 1024 | | // check for all back slashes |
| | 0 | 1025 | | str = str.Replace('/', '\\'); |
| | | 1026 | | |
| | 0 | 1027 | | return str; |
| | | 1028 | | } |
| | | 1029 | | |
| | 0 | 1030 | | var result = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | | 1031 | | |
| | 0 | 1032 | | string host = _info.Host; |
| | 0 | 1033 | | start = _info.Offset.Path; |
| | | 1034 | | |
| | 0 | 1035 | | if (IsUncPath) |
| | 0 | 1036 | | { |
| | 0 | 1037 | | result.Append('\\'); |
| | 0 | 1038 | | result.Append('\\'); |
| | 0 | 1039 | | result.Append(host); |
| | 0 | 1040 | | } |
| | | 1041 | | else |
| | 0 | 1042 | | { |
| | | 1043 | | // Dos path |
| | 0 | 1044 | | if (_string[start] is '/' or '\\') |
| | 0 | 1045 | | { |
| | | 1046 | | // Skip leading slash for a DOS path |
| | 0 | 1047 | | start++; |
| | 0 | 1048 | | } |
| | 0 | 1049 | | } |
| | | 1050 | | |
| | 0 | 1051 | | int pathStart = result.Length; // save for optional Compress() call |
| | | 1052 | | |
| | 0 | 1053 | | ReadOnlySpan<char> path = _string.AsSpan(start, _info.Offset.Query - start); |
| | | 1054 | | |
| | 0 | 1055 | | if (InFact(Flags.PathNotCanonical) && !IsImplicitFile) |
| | 0 | 1056 | | { |
| | 0 | 1057 | | UriHelper.Unescape(path, ref result); |
| | 0 | 1058 | | } |
| | | 1059 | | else |
| | 0 | 1060 | | { |
| | 0 | 1061 | | result.Append(path); |
| | 0 | 1062 | | } |
| | | 1063 | | |
| | | 1064 | | // Possibly convert c|\ into c:\ |
| | 0 | 1065 | | if (result[1] == '|') |
| | 0 | 1066 | | result[1] = ':'; |
| | | 1067 | | |
| | 0 | 1068 | | if (InFact(Flags.ShouldBeCompressed)) |
| | 0 | 1069 | | { |
| | | 1070 | | // suspecting not compressed path |
| | | 1071 | | // For a dos path we won't compress the "x:" part if found /../ sequences |
| | 0 | 1072 | | Compress(ref result, IsDosPath ? pathStart + 2 : pathStart, _syntax); |
| | 0 | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | // We don't know whether all slashes were the back ones |
| | | 1076 | | // Plus going through Compress will turn them into / anyway |
| | | 1077 | | // Converting / back into \ |
| | 0 | 1078 | | Span<char> slashSpan = result.RawChars.Slice(0, result.Length); |
| | 0 | 1079 | | slashSpan.Replace('/', '\\'); |
| | | 1080 | | |
| | 0 | 1081 | | return result.ToString(); |
| | | 1082 | | } |
| | | 1083 | | else |
| | 0 | 1084 | | { |
| | | 1085 | | // Return unescaped canonical path |
| | | 1086 | | // Note we cannot call GetParts here because it has circular dependency on GelLocalPath method |
| | 0 | 1087 | | return GetUnescapedParts(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped); |
| | | 1088 | | } |
| | 0 | 1089 | | } |
| | | 1090 | | |
| | | 1091 | | public int Port |
| | | 1092 | | { |
| | | 1093 | | get |
| | 0 | 1094 | | { |
| | 0 | 1095 | | if (IsNotAbsoluteUri) |
| | 0 | 1096 | | { |
| | 0 | 1097 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1098 | | } |
| | | 1099 | | |
| | 0 | 1100 | | if (_syntax.IsSimple) |
| | 0 | 1101 | | EnsureUriInfo(); |
| | | 1102 | | else |
| | 0 | 1103 | | { |
| | | 1104 | | // For a custom parser we request HostString creation that will also set the port |
| | 0 | 1105 | | EnsureHostString(false); |
| | 0 | 1106 | | } |
| | | 1107 | | |
| | 0 | 1108 | | if (InFact(Flags.NotDefaultPort)) |
| | 0 | 1109 | | { |
| | 0 | 1110 | | return (int)_info.Offset.PortValue; |
| | | 1111 | | } |
| | 0 | 1112 | | return _syntax.DefaultPort; |
| | 0 | 1113 | | } |
| | | 1114 | | } |
| | | 1115 | | |
| | | 1116 | | // |
| | | 1117 | | // Gets the escaped query. |
| | | 1118 | | public string Query |
| | | 1119 | | { |
| | | 1120 | | get |
| | 0 | 1121 | | { |
| | 0 | 1122 | | if (IsNotAbsoluteUri) |
| | 0 | 1123 | | { |
| | 0 | 1124 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1125 | | } |
| | | 1126 | | |
| | 0 | 1127 | | MoreInfo info = EnsureUriInfo().MoreInfo; |
| | 0 | 1128 | | return info.Query ??= GetParts(UriComponents.Query | UriComponents.KeepDelimiter, UriFormat.UriEscaped); |
| | 0 | 1129 | | } |
| | | 1130 | | } |
| | | 1131 | | |
| | | 1132 | | // |
| | | 1133 | | // Gets the escaped fragment. |
| | | 1134 | | public string Fragment |
| | | 1135 | | { |
| | | 1136 | | get |
| | 0 | 1137 | | { |
| | 0 | 1138 | | if (IsNotAbsoluteUri) |
| | 0 | 1139 | | { |
| | 0 | 1140 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1141 | | } |
| | | 1142 | | |
| | 0 | 1143 | | MoreInfo info = EnsureUriInfo().MoreInfo; |
| | 0 | 1144 | | return info.Fragment ??= GetParts(UriComponents.Fragment | UriComponents.KeepDelimiter, UriFormat.UriEsc |
| | 0 | 1145 | | } |
| | | 1146 | | } |
| | | 1147 | | |
| | | 1148 | | // |
| | | 1149 | | // Gets the Scheme string of this Uri |
| | | 1150 | | // |
| | | 1151 | | public string Scheme |
| | | 1152 | | { |
| | | 1153 | | get |
| | 0 | 1154 | | { |
| | 0 | 1155 | | if (IsNotAbsoluteUri) |
| | 0 | 1156 | | { |
| | 0 | 1157 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1158 | | } |
| | | 1159 | | |
| | 0 | 1160 | | return _syntax.SchemeName; |
| | 0 | 1161 | | } |
| | | 1162 | | } |
| | | 1163 | | |
| | | 1164 | | // |
| | | 1165 | | // Gets the exact string passed by a user. |
| | | 1166 | | // The original string will switched from _string to _originalUnicodeString if |
| | | 1167 | | // iri is turned on and we have non-ascii chars |
| | | 1168 | | // |
| | 2644 | 1169 | | public string OriginalString => _originalUnicodeString ?? _string; |
| | | 1170 | | |
| | | 1171 | | // |
| | | 1172 | | // Gets the host string that is unescaped and if it's Ipv6 host, |
| | | 1173 | | // then the returned string is suitable for DNS lookup. |
| | | 1174 | | // |
| | | 1175 | | // For Ipv6 this will strip [] and add ScopeId if was found in the original string |
| | | 1176 | | public string DnsSafeHost |
| | | 1177 | | { |
| | | 1178 | | get |
| | 0 | 1179 | | { |
| | 0 | 1180 | | if (IsNotAbsoluteUri) |
| | 0 | 1181 | | { |
| | 0 | 1182 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1183 | | } |
| | | 1184 | | |
| | 0 | 1185 | | EnsureHostString(false); |
| | | 1186 | | |
| | 0 | 1187 | | Flags hostType = HostType; |
| | 0 | 1188 | | if (hostType == Flags.IPv6HostType || (hostType == Flags.BasicHostType && InFact(Flags.HostNotCanonical |
| | 0 | 1189 | | { |
| | 0 | 1190 | | return IdnHost; |
| | | 1191 | | } |
| | | 1192 | | else |
| | 0 | 1193 | | { |
| | 0 | 1194 | | return _info.Host!; |
| | | 1195 | | } |
| | 0 | 1196 | | } |
| | | 1197 | | } |
| | | 1198 | | |
| | | 1199 | | // Returns the host name represented as IDN (using punycode encoding) regardless of app.config settings |
| | | 1200 | | public string IdnHost |
| | | 1201 | | { |
| | | 1202 | | get |
| | 0 | 1203 | | { |
| | 0 | 1204 | | if (IsNotAbsoluteUri) |
| | 0 | 1205 | | { |
| | 0 | 1206 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1207 | | } |
| | | 1208 | | |
| | 0 | 1209 | | if (_info?.IdnHost is null) |
| | 0 | 1210 | | { |
| | 0 | 1211 | | EnsureHostString(false); |
| | | 1212 | | |
| | 0 | 1213 | | string host = _info.Host!; |
| | | 1214 | | |
| | 0 | 1215 | | Flags hostType = HostType; |
| | 0 | 1216 | | if (hostType == Flags.DnsHostType) |
| | 0 | 1217 | | { |
| | 0 | 1218 | | host = DomainNameHelper.IdnEquivalent(host); |
| | 0 | 1219 | | } |
| | 0 | 1220 | | else if (hostType == Flags.IPv6HostType) |
| | 0 | 1221 | | { |
| | 0 | 1222 | | host = _info._moreInfo?.ScopeId is string scopeId ? |
| | 0 | 1223 | | string.Concat(host.AsSpan(1, host.Length - 2), scopeId) : |
| | 0 | 1224 | | host.Substring(1, host.Length - 2); |
| | 0 | 1225 | | } |
| | | 1226 | | // Validate that this basic host qualifies as Dns safe, |
| | | 1227 | | // It has looser parsing rules that might allow otherwise. |
| | | 1228 | | // It might be a registry-based host from RFC 2396 Section 3.2.1 |
| | 0 | 1229 | | else if (hostType == Flags.BasicHostType && InFact(Flags.HostNotCanonical | Flags.E_HostNotCanonical |
| | 0 | 1230 | | { |
| | 0 | 1231 | | host = UnescapeDataString(host); |
| | 0 | 1232 | | } |
| | | 1233 | | |
| | 0 | 1234 | | _info.IdnHost = host; |
| | 0 | 1235 | | } |
| | | 1236 | | |
| | 0 | 1237 | | return _info.IdnHost; |
| | 0 | 1238 | | } |
| | | 1239 | | } |
| | | 1240 | | |
| | | 1241 | | // |
| | | 1242 | | // Returns false if the string passed in the constructor cannot be parsed as |
| | | 1243 | | // valid AbsoluteUri. This could be a relative Uri instead. |
| | | 1244 | | // |
| | | 1245 | | public bool IsAbsoluteUri |
| | | 1246 | | { |
| | | 1247 | | get |
| | 1322 | 1248 | | { |
| | 1322 | 1249 | | return _syntax != null; |
| | 1322 | 1250 | | } |
| | | 1251 | | } |
| | | 1252 | | |
| | | 1253 | | // |
| | | 1254 | | // Returns 'true' if the 'dontEscape' parameter was set to 'true ' when the Uri instance was created. |
| | | 1255 | | public bool UserEscaped |
| | | 1256 | | { |
| | | 1257 | | get |
| | 0 | 1258 | | { |
| | 0 | 1259 | | return InFact(Flags.UserEscaped); |
| | 0 | 1260 | | } |
| | | 1261 | | } |
| | | 1262 | | |
| | | 1263 | | // |
| | | 1264 | | // Gets the user name, password, and other user specific information associated |
| | | 1265 | | // with the Uniform Resource Identifier (URI). |
| | | 1266 | | public string UserInfo |
| | | 1267 | | { |
| | | 1268 | | get |
| | 0 | 1269 | | { |
| | 0 | 1270 | | if (IsNotAbsoluteUri) |
| | 0 | 1271 | | { |
| | 0 | 1272 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1273 | | } |
| | | 1274 | | |
| | 0 | 1275 | | return GetParts(UriComponents.UserInfo, UriFormat.UriEscaped); |
| | 0 | 1276 | | } |
| | | 1277 | | } |
| | | 1278 | | |
| | | 1279 | | // |
| | | 1280 | | // CheckHostName |
| | | 1281 | | // |
| | | 1282 | | // Determines whether a host name authority is a valid Host name according |
| | | 1283 | | // to DNS naming rules and IPv4 canonicalization rules |
| | | 1284 | | // |
| | | 1285 | | // Returns: |
| | | 1286 | | // true if <name> is valid else false |
| | | 1287 | | // |
| | | 1288 | | // Throws: |
| | | 1289 | | // Nothing |
| | | 1290 | | // |
| | | 1291 | | public static UriHostNameType CheckHostName(string? name) |
| | 661 | 1292 | | { |
| | 661 | 1293 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 1294 | | { |
| | 0 | 1295 | | return UriHostNameType.Unknown; |
| | | 1296 | | } |
| | | 1297 | | |
| | | 1298 | | int end; |
| | 661 | 1299 | | if (name.StartsWith('[') && name.EndsWith(']')) |
| | 538 | 1300 | | { |
| | | 1301 | | // we require that _entire_ name is recognized as ipv6 address |
| | 538 | 1302 | | if (IPv6AddressHelper.IsValid(name, out end) && end == name.Length) |
| | 538 | 1303 | | { |
| | 538 | 1304 | | return UriHostNameType.IPv6; |
| | | 1305 | | } |
| | 0 | 1306 | | } |
| | | 1307 | | |
| | 123 | 1308 | | if (IPv4AddressHelper.IsValid(name.AsSpan(), out end, false, false, false) && end == name.Length) |
| | 123 | 1309 | | { |
| | 123 | 1310 | | return UriHostNameType.IPv4; |
| | | 1311 | | } |
| | | 1312 | | |
| | 0 | 1313 | | if (DomainNameHelper.IsValid(name, iri: false, notImplicitFile: false, out end) && end == name.Length) |
| | 0 | 1314 | | { |
| | 0 | 1315 | | return UriHostNameType.Dns; |
| | | 1316 | | } |
| | | 1317 | | |
| | 0 | 1318 | | if (DomainNameHelper.IsValid(name, iri: true, notImplicitFile: false, out end) && end == name.Length) |
| | 0 | 1319 | | { |
| | 0 | 1320 | | return UriHostNameType.Dns; |
| | | 1321 | | } |
| | | 1322 | | |
| | | 1323 | | // This checks the form without [] |
| | | 1324 | | // we require that _entire_ name is recognized as ipv6 address |
| | 0 | 1325 | | if (IPv6AddressHelper.IsValid($"[{name}]", out end) && end - 2 == name.Length) |
| | 0 | 1326 | | { |
| | 0 | 1327 | | return UriHostNameType.IPv6; |
| | | 1328 | | } |
| | | 1329 | | |
| | 0 | 1330 | | return UriHostNameType.Unknown; |
| | 661 | 1331 | | } |
| | | 1332 | | |
| | | 1333 | | // |
| | | 1334 | | // GetLeftPart |
| | | 1335 | | // |
| | | 1336 | | // Returns part of the URI based on the parameters: |
| | | 1337 | | // |
| | | 1338 | | // Inputs: |
| | | 1339 | | // <argument> part |
| | | 1340 | | // Which part of the URI to return |
| | | 1341 | | // |
| | | 1342 | | // Returns: |
| | | 1343 | | // The requested substring |
| | | 1344 | | // |
| | | 1345 | | // Throws: |
| | | 1346 | | // UriFormatException if URI type doesn't have host-port or authority parts |
| | | 1347 | | // |
| | | 1348 | | public string GetLeftPart(UriPartial part) |
| | 0 | 1349 | | { |
| | 0 | 1350 | | if (IsNotAbsoluteUri) |
| | 0 | 1351 | | { |
| | 0 | 1352 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1353 | | } |
| | | 1354 | | |
| | 0 | 1355 | | EnsureUriInfo(); |
| | | 1356 | | const UriComponents NonPathPart = (UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriC |
| | | 1357 | | |
| | 0 | 1358 | | switch (part) |
| | | 1359 | | { |
| | | 1360 | | case UriPartial.Scheme: |
| | | 1361 | | |
| | 0 | 1362 | | return GetParts(UriComponents.Scheme | UriComponents.KeepDelimiter, UriFormat.UriEscaped); |
| | | 1363 | | |
| | | 1364 | | case UriPartial.Authority: |
| | | 1365 | | |
| | 0 | 1366 | | if (NotAny(Flags.AuthorityFound) || IsDosPath) |
| | 0 | 1367 | | { |
| | | 1368 | | // V1.0 compatibility. |
| | | 1369 | | // It not return an empty string but instead "scheme:" because it is a LEFT part. |
| | | 1370 | | // Also neither it should check for IsDosPath here |
| | | 1371 | | |
| | | 1372 | | // From V1.0 comments: |
| | | 1373 | | |
| | | 1374 | | // anything that didn't have "//" after the scheme name |
| | | 1375 | | // (mailto: and news: e.g.) doesn't have an authority |
| | | 1376 | | // |
| | | 1377 | | |
| | 0 | 1378 | | return string.Empty; |
| | | 1379 | | } |
| | 0 | 1380 | | return GetParts(NonPathPart, UriFormat.UriEscaped); |
| | | 1381 | | |
| | | 1382 | | case UriPartial.Path: |
| | 0 | 1383 | | return GetParts(NonPathPart | UriComponents.Path, UriFormat.UriEscaped); |
| | | 1384 | | |
| | | 1385 | | case UriPartial.Query: |
| | 0 | 1386 | | return GetParts(NonPathPart | UriComponents.Path | UriComponents.Query, UriFormat.UriEscaped); |
| | | 1387 | | } |
| | 0 | 1388 | | throw new ArgumentException(SR.Format(SR.Argument_InvalidUriSubcomponent, part), nameof(part)); |
| | 0 | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | // |
| | | 1392 | | // |
| | | 1393 | | /// Transforms a character into its hexadecimal representation. |
| | | 1394 | | public static string HexEscape(char character) |
| | 0 | 1395 | | { |
| | 0 | 1396 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(character, '\xff'); |
| | | 1397 | | |
| | 0 | 1398 | | return string.Create(3, (byte)character, (Span<char> chars, byte b) => |
| | 0 | 1399 | | { |
| | 0 | 1400 | | chars[0] = '%'; |
| | 0 | 1401 | | HexConverter.ToCharsBuffer(b, chars, 1, HexConverter.Casing.Upper); |
| | 0 | 1402 | | }); |
| | 0 | 1403 | | } |
| | | 1404 | | |
| | | 1405 | | // |
| | | 1406 | | // HexUnescape |
| | | 1407 | | // |
| | | 1408 | | // Converts a substring of the form "%XX" to the single character represented |
| | | 1409 | | // by the hexadecimal value XX. If the substring s[Index] does not conform to |
| | | 1410 | | // the hex encoding format then the character at s[Index] is returned |
| | | 1411 | | // |
| | | 1412 | | // Inputs: |
| | | 1413 | | // <argument> pattern |
| | | 1414 | | // String from which to read the hexadecimal encoded substring |
| | | 1415 | | // |
| | | 1416 | | // <argument> index |
| | | 1417 | | // Offset within <pattern> from which to start reading the hexadecimal |
| | | 1418 | | // encoded substring |
| | | 1419 | | // |
| | | 1420 | | // Outputs: |
| | | 1421 | | // <argument> index |
| | | 1422 | | // Incremented to the next character position within the string. This |
| | | 1423 | | // may be EOS if this was the last character/encoding within <pattern> |
| | | 1424 | | // |
| | | 1425 | | // Returns: |
| | | 1426 | | // Either the converted character if <pattern>[<index>] was hex encoded, or |
| | | 1427 | | // the character at <pattern>[<index>] |
| | | 1428 | | // |
| | | 1429 | | // Throws: |
| | | 1430 | | // ArgumentOutOfRangeException |
| | | 1431 | | // |
| | | 1432 | | |
| | | 1433 | | public static char HexUnescape(string pattern, ref int index) |
| | 0 | 1434 | | { |
| | 0 | 1435 | | ArgumentOutOfRangeException.ThrowIfNegative(index); |
| | 0 | 1436 | | ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, pattern.Length); |
| | | 1437 | | |
| | 0 | 1438 | | if ((pattern[index] == '%') |
| | 0 | 1439 | | && (pattern.Length - index >= 3)) |
| | 0 | 1440 | | { |
| | 0 | 1441 | | char ret = UriHelper.DecodeHexChars(pattern[index + 1], pattern[index + 2]); |
| | 0 | 1442 | | if (ret != c_DummyChar) |
| | 0 | 1443 | | { |
| | 0 | 1444 | | index += 3; |
| | 0 | 1445 | | return ret; |
| | | 1446 | | } |
| | 0 | 1447 | | } |
| | 0 | 1448 | | return pattern[index++]; |
| | 0 | 1449 | | } |
| | | 1450 | | |
| | | 1451 | | // |
| | | 1452 | | // IsHexEncoding |
| | | 1453 | | // |
| | | 1454 | | // Determines whether a substring has the URI hex encoding format of '%' |
| | | 1455 | | // followed by 2 hexadecimal characters |
| | | 1456 | | // |
| | | 1457 | | // Inputs: |
| | | 1458 | | // <argument> pattern |
| | | 1459 | | // String to check |
| | | 1460 | | // |
| | | 1461 | | // <argument> index |
| | | 1462 | | // Offset in <pattern> at which to check substring for hex encoding |
| | | 1463 | | // |
| | | 1464 | | // Assumes: |
| | | 1465 | | // 0 <= <index> < <pattern>.Length |
| | | 1466 | | // |
| | | 1467 | | // Returns: |
| | | 1468 | | // true if <pattern>[<index>] is hex encoded, else false |
| | | 1469 | | // |
| | | 1470 | | // Throws: |
| | | 1471 | | // Nothing |
| | | 1472 | | // |
| | | 1473 | | public static bool IsHexEncoding(string pattern, int index) |
| | 0 | 1474 | | { |
| | 0 | 1475 | | return |
| | 0 | 1476 | | (pattern.Length - index) >= 3 && |
| | 0 | 1477 | | pattern[index] == '%' && |
| | 0 | 1478 | | char.IsAsciiHexDigit(pattern[index + 1]) && |
| | 0 | 1479 | | char.IsAsciiHexDigit(pattern[index + 2]); |
| | 0 | 1480 | | } |
| | | 1481 | | |
| | 1 | 1482 | | private static readonly SearchValues<char> s_schemeChars = |
| | 1 | 1483 | | SearchValues.Create("+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); |
| | | 1484 | | |
| | | 1485 | | // CheckSchemeName |
| | | 1486 | | // |
| | | 1487 | | // Determines whether a string is a valid scheme name according to RFC 2396. |
| | | 1488 | | // Syntax is: |
| | | 1489 | | // scheme = alpha *(alpha | digit | '+' | '-' | '.') |
| | | 1490 | | public static bool CheckSchemeName([NotNullWhen(true)] string? schemeName) => |
| | 0 | 1491 | | !string.IsNullOrEmpty(schemeName) && |
| | 0 | 1492 | | char.IsAsciiLetter(schemeName[0]) && |
| | 0 | 1493 | | !schemeName.AsSpan().ContainsAnyExcept(s_schemeChars); |
| | | 1494 | | |
| | | 1495 | | // |
| | | 1496 | | // IsHexDigit |
| | | 1497 | | // |
| | | 1498 | | // Determines whether a character is a valid hexadecimal digit in the range |
| | | 1499 | | // [0..9] | [A..F] | [a..f] |
| | | 1500 | | // |
| | | 1501 | | // Inputs: |
| | | 1502 | | // <argument> character |
| | | 1503 | | // Character to test |
| | | 1504 | | // |
| | | 1505 | | // Returns: |
| | | 1506 | | // true if <character> is a hexadecimal digit character |
| | | 1507 | | // |
| | | 1508 | | // Throws: |
| | | 1509 | | // Nothing |
| | | 1510 | | // |
| | | 1511 | | public static bool IsHexDigit(char character) |
| | 0 | 1512 | | { |
| | 0 | 1513 | | return char.IsAsciiHexDigit(character); |
| | 0 | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | // |
| | | 1517 | | // Returns: |
| | | 1518 | | // Number in the range 0..15 |
| | | 1519 | | // |
| | | 1520 | | // Throws: |
| | | 1521 | | // ArgumentException |
| | | 1522 | | // |
| | | 1523 | | public static int FromHex(char digit) |
| | 0 | 1524 | | { |
| | 0 | 1525 | | int result = HexConverter.FromChar(digit); |
| | 0 | 1526 | | if (result == 0xFF) |
| | 0 | 1527 | | { |
| | 0 | 1528 | | throw new ArgumentException(null, nameof(digit)); |
| | | 1529 | | } |
| | | 1530 | | |
| | 0 | 1531 | | return result; |
| | 0 | 1532 | | } |
| | | 1533 | | |
| | | 1534 | | public override int GetHashCode() |
| | 0 | 1535 | | { |
| | 0 | 1536 | | if (IsNotAbsoluteUri) |
| | 0 | 1537 | | { |
| | 0 | 1538 | | return OriginalString.GetHashCode(); |
| | | 1539 | | } |
| | | 1540 | | else |
| | 0 | 1541 | | { |
| | 0 | 1542 | | MoreInfo info = EnsureUriInfo().MoreInfo; |
| | | 1543 | | |
| | 0 | 1544 | | UriComponents components = UriComponents.HttpRequestUrl; |
| | | 1545 | | |
| | 0 | 1546 | | if (_syntax.InFact(UriSyntaxFlags.MailToLikeUri)) |
| | 0 | 1547 | | { |
| | 0 | 1548 | | components |= UriComponents.UserInfo; |
| | 0 | 1549 | | } |
| | | 1550 | | |
| | 0 | 1551 | | string remoteUrl = info.RemoteUrl ??= GetParts(components, UriFormat.SafeUnescaped); |
| | | 1552 | | |
| | 0 | 1553 | | if (IsFile) |
| | 0 | 1554 | | { |
| | 0 | 1555 | | return StringComparer.OrdinalIgnoreCase.GetHashCode(remoteUrl); |
| | | 1556 | | } |
| | | 1557 | | else |
| | 0 | 1558 | | { |
| | 0 | 1559 | | return remoteUrl.GetHashCode(); |
| | | 1560 | | } |
| | | 1561 | | } |
| | 0 | 1562 | | } |
| | | 1563 | | |
| | | 1564 | | // |
| | | 1565 | | // ToString |
| | | 1566 | | // |
| | | 1567 | | private const UriFormat V1ToStringUnescape = (UriFormat)0x7FFF; |
| | | 1568 | | |
| | | 1569 | | public override string ToString() |
| | 0 | 1570 | | { |
| | 0 | 1571 | | if (_syntax == null) |
| | 0 | 1572 | | { |
| | 0 | 1573 | | return _string; |
| | | 1574 | | } |
| | | 1575 | | |
| | 0 | 1576 | | EnsureUriInfo(); |
| | 0 | 1577 | | return _info.String ??= |
| | 0 | 1578 | | _syntax.IsSimple ? |
| | 0 | 1579 | | GetComponentsHelper(UriComponents.AbsoluteUri, V1ToStringUnescape) : |
| | 0 | 1580 | | GetParts(UriComponents.AbsoluteUri, UriFormat.SafeUnescaped); |
| | 0 | 1581 | | } |
| | | 1582 | | |
| | | 1583 | | /// <summary> |
| | | 1584 | | /// Attempts to format a canonical string representation for the <see cref="Uri"/> instance into the specified s |
| | | 1585 | | /// </summary> |
| | | 1586 | | /// <param name="destination">The span into which to write this instance's value formatted as a span of characte |
| | | 1587 | | /// <param name="charsWritten">When this method returns, contains the number of characters that were written in |
| | | 1588 | | /// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</retur |
| | | 1589 | | public bool TryFormat(Span<char> destination, out int charsWritten) |
| | 0 | 1590 | | { |
| | | 1591 | | ReadOnlySpan<char> result; |
| | | 1592 | | |
| | 0 | 1593 | | if (_syntax is null) |
| | 0 | 1594 | | { |
| | 0 | 1595 | | result = _string; |
| | 0 | 1596 | | } |
| | | 1597 | | else |
| | 0 | 1598 | | { |
| | 0 | 1599 | | EnsureUriInfo(); |
| | 0 | 1600 | | if (_info.String is not null) |
| | 0 | 1601 | | { |
| | 0 | 1602 | | result = _info.String; |
| | 0 | 1603 | | } |
| | | 1604 | | else |
| | 0 | 1605 | | { |
| | 0 | 1606 | | UriFormat uriFormat = V1ToStringUnescape; |
| | 0 | 1607 | | if (!_syntax.IsSimple) |
| | 0 | 1608 | | { |
| | 0 | 1609 | | if (IsNotAbsoluteUri) |
| | 0 | 1610 | | { |
| | 0 | 1611 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1612 | | } |
| | | 1613 | | |
| | 0 | 1614 | | if (UserDrivenParsing) |
| | 0 | 1615 | | { |
| | 0 | 1616 | | throw new InvalidOperationException(SR.Format(SR.net_uri_UserDrivenParsing, GetType())); |
| | | 1617 | | } |
| | | 1618 | | |
| | 0 | 1619 | | if (DisablePathAndQueryCanonicalization) |
| | 0 | 1620 | | { |
| | 0 | 1621 | | throw new InvalidOperationException(SR.net_uri_GetComponentsCalledWhenCanonicalizationDisabl |
| | | 1622 | | } |
| | | 1623 | | |
| | 0 | 1624 | | uriFormat = UriFormat.SafeUnescaped; |
| | 0 | 1625 | | } |
| | | 1626 | | |
| | 0 | 1627 | | EnsureParseRemaining(); |
| | 0 | 1628 | | EnsureHostString(allowDnsOptimization: true); |
| | | 1629 | | |
| | 0 | 1630 | | ushort nonCanonical = (ushort)((ushort)_flags & (ushort)Flags.CannotDisplayCanonical); |
| | 0 | 1631 | | if (((_flags & (Flags.ShouldBeCompressed | Flags.FirstSlashAbsent | Flags.BackslashInPath)) != 0) || |
| | 0 | 1632 | | (IsDosPath && _string[_info.Offset.Path + SecuredPathIndex - 1] == '|')) // A rare case of c|\ |
| | 0 | 1633 | | { |
| | 0 | 1634 | | nonCanonical |= (ushort)Flags.PathNotCanonical; |
| | 0 | 1635 | | } |
| | | 1636 | | |
| | 0 | 1637 | | if (((ushort)UriComponents.AbsoluteUri & nonCanonical) != 0) |
| | 0 | 1638 | | { |
| | 0 | 1639 | | return TryRecreateParts(destination, out charsWritten, UriComponents.AbsoluteUri, nonCanonical, |
| | | 1640 | | } |
| | | 1641 | | |
| | 0 | 1642 | | result = _string.AsSpan(_info.Offset.Scheme, _info.Offset.End - _info.Offset.Scheme); |
| | 0 | 1643 | | } |
| | 0 | 1644 | | } |
| | | 1645 | | |
| | 0 | 1646 | | if (result.TryCopyTo(destination)) |
| | 0 | 1647 | | { |
| | 0 | 1648 | | charsWritten = result.Length; |
| | 0 | 1649 | | return true; |
| | | 1650 | | } |
| | | 1651 | | |
| | 0 | 1652 | | charsWritten = 0; |
| | 0 | 1653 | | return false; |
| | 0 | 1654 | | } |
| | | 1655 | | |
| | | 1656 | | /// <inheritdoc/> |
| | | 1657 | | bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormat |
| | 0 | 1658 | | TryFormat(destination, out charsWritten); |
| | | 1659 | | |
| | | 1660 | | /// <inheritdoc/> |
| | | 1661 | | string IFormattable.ToString(string? format, IFormatProvider? formatProvider) => |
| | 0 | 1662 | | ToString(); |
| | | 1663 | | |
| | | 1664 | | public static bool operator ==(Uri? uri1, Uri? uri2) |
| | 0 | 1665 | | { |
| | 0 | 1666 | | if (ReferenceEquals(uri1, uri2)) |
| | 0 | 1667 | | { |
| | 0 | 1668 | | return true; |
| | | 1669 | | } |
| | | 1670 | | |
| | 0 | 1671 | | if (uri1 is null || uri2 is null) |
| | 0 | 1672 | | { |
| | 0 | 1673 | | return false; |
| | | 1674 | | } |
| | | 1675 | | |
| | 0 | 1676 | | return uri1.Equals(uri2); |
| | 0 | 1677 | | } |
| | | 1678 | | |
| | | 1679 | | public static bool operator !=(Uri? uri1, Uri? uri2) |
| | 0 | 1680 | | { |
| | 0 | 1681 | | if (ReferenceEquals(uri1, uri2)) |
| | 0 | 1682 | | { |
| | 0 | 1683 | | return false; |
| | | 1684 | | } |
| | | 1685 | | |
| | 0 | 1686 | | if (uri1 is null || uri2 is null) |
| | 0 | 1687 | | { |
| | 0 | 1688 | | return true; |
| | | 1689 | | } |
| | | 1690 | | |
| | 0 | 1691 | | return !uri1.Equals(uri2); |
| | 0 | 1692 | | } |
| | | 1693 | | |
| | | 1694 | | public override bool Equals([NotNullWhen(true)] object? comparand) |
| | 0 | 1695 | | { |
| | 0 | 1696 | | if (comparand is null) |
| | 0 | 1697 | | { |
| | 0 | 1698 | | return false; |
| | | 1699 | | } |
| | | 1700 | | |
| | 0 | 1701 | | if (ReferenceEquals(this, comparand)) |
| | 0 | 1702 | | { |
| | 0 | 1703 | | return true; |
| | | 1704 | | } |
| | | 1705 | | |
| | 0 | 1706 | | Uri? other = comparand as Uri; |
| | | 1707 | | |
| | | 1708 | | // we allow comparisons of Uri and String objects only. If a string |
| | | 1709 | | // is passed, convert to Uri. This is inefficient, but allows us to |
| | | 1710 | | // canonicalize the comparand, making comparison possible |
| | 0 | 1711 | | if (other is null) |
| | 0 | 1712 | | { |
| | 0 | 1713 | | if (DisablePathAndQueryCanonicalization) |
| | 0 | 1714 | | return false; |
| | | 1715 | | |
| | 0 | 1716 | | if (!(comparand is string s)) |
| | 0 | 1717 | | return false; |
| | | 1718 | | |
| | 0 | 1719 | | if (ReferenceEquals(s, OriginalString)) |
| | 0 | 1720 | | return true; |
| | | 1721 | | |
| | 0 | 1722 | | if (!TryCreate(s, UriKind.RelativeOrAbsolute, out other)) |
| | 0 | 1723 | | return false; |
| | 0 | 1724 | | } |
| | | 1725 | | |
| | 0 | 1726 | | return Equals(other); |
| | 0 | 1727 | | } |
| | | 1728 | | |
| | | 1729 | | /// <summary> |
| | | 1730 | | /// Compares two <see cref="Uri"/> instances for equality. |
| | | 1731 | | /// </summary> |
| | | 1732 | | /// <param name="other">The <see cref="Uri"/> to compare to this instance.</param> |
| | | 1733 | | /// <returns><see langword="true"/> if the two instances represent the same URI; otherwise, <see langword="false |
| | | 1734 | | public bool Equals([NotNullWhen(true)] Uri? other) |
| | 0 | 1735 | | { |
| | 0 | 1736 | | if (other is null) |
| | 0 | 1737 | | { |
| | 0 | 1738 | | return false; |
| | | 1739 | | } |
| | | 1740 | | |
| | 0 | 1741 | | if (ReferenceEquals(this, other)) |
| | 0 | 1742 | | { |
| | 0 | 1743 | | return true; |
| | | 1744 | | } |
| | | 1745 | | |
| | 0 | 1746 | | if (DisablePathAndQueryCanonicalization != other.DisablePathAndQueryCanonicalization) |
| | 0 | 1747 | | return false; |
| | | 1748 | | |
| | 0 | 1749 | | if (ReferenceEquals(OriginalString, other.OriginalString)) |
| | 0 | 1750 | | { |
| | 0 | 1751 | | return true; |
| | | 1752 | | } |
| | | 1753 | | |
| | 0 | 1754 | | if (IsAbsoluteUri != other.IsAbsoluteUri) |
| | 0 | 1755 | | return false; |
| | | 1756 | | |
| | 0 | 1757 | | if (IsNotAbsoluteUri) |
| | 0 | 1758 | | return OriginalString.Equals(other.OriginalString); |
| | | 1759 | | |
| | 0 | 1760 | | if (NotAny(Flags.AllUriInfoSet) || other.NotAny(Flags.AllUriInfoSet)) |
| | 0 | 1761 | | { |
| | | 1762 | | // Try raw compare for _strings as the last chance to keep the working set small |
| | 0 | 1763 | | if (string.Equals(_string, other._string, IsUncOrDosPath ? StringComparison.OrdinalIgnoreCase : StringCo |
| | 0 | 1764 | | { |
| | 0 | 1765 | | return true; |
| | | 1766 | | } |
| | 0 | 1767 | | } |
| | | 1768 | | |
| | | 1769 | | // Note that equality test will bring the working set of both |
| | | 1770 | | // objects up to creation of _info.MoreInfo member |
| | 0 | 1771 | | EnsureUriInfo(); |
| | 0 | 1772 | | other.EnsureUriInfo(); |
| | | 1773 | | |
| | 0 | 1774 | | if (!UserDrivenParsing && !other.UserDrivenParsing && Syntax!.IsSimple && other.Syntax.IsSimple) |
| | 0 | 1775 | | { |
| | | 1776 | | // Optimization of canonical DNS names by avoiding host string creation. |
| | | 1777 | | // Note there could be explicit ports specified that would invalidate path offsets |
| | 0 | 1778 | | if (InFact(Flags.CanonicalDnsHost) && other.InFact(Flags.CanonicalDnsHost)) |
| | 0 | 1779 | | { |
| | 0 | 1780 | | int i1 = _info.Offset.Host; |
| | 0 | 1781 | | int end1 = _info.Offset.Path; |
| | | 1782 | | |
| | 0 | 1783 | | int i2 = other._info.Offset.Host; |
| | 0 | 1784 | | int end2 = other._info.Offset.Path; |
| | 0 | 1785 | | string str = other._string; |
| | | 1786 | | //Taking the shortest part |
| | 0 | 1787 | | if (end1 - i1 > end2 - i2) |
| | 0 | 1788 | | { |
| | 0 | 1789 | | end1 = i1 + end2 - i2; |
| | 0 | 1790 | | } |
| | | 1791 | | // compare and break on ':' if found |
| | 0 | 1792 | | while (i1 < end1) |
| | 0 | 1793 | | { |
| | 0 | 1794 | | if (_string[i1] != str[i2]) |
| | 0 | 1795 | | { |
| | 0 | 1796 | | return false; |
| | | 1797 | | } |
| | 0 | 1798 | | if (str[i2] == ':') |
| | 0 | 1799 | | { |
| | | 1800 | | // The other must have ':' too to have equal host |
| | 0 | 1801 | | break; |
| | | 1802 | | } |
| | 0 | 1803 | | ++i1; ++i2; |
| | 0 | 1804 | | } |
| | | 1805 | | |
| | | 1806 | | // The longest host must have ':' or be of the same size |
| | 0 | 1807 | | if (i1 < _info.Offset.Path && _string[i1] != ':') |
| | 0 | 1808 | | { |
| | 0 | 1809 | | return false; |
| | | 1810 | | } |
| | 0 | 1811 | | if (i2 < end2 && str[i2] != ':') |
| | 0 | 1812 | | { |
| | 0 | 1813 | | return false; |
| | | 1814 | | } |
| | | 1815 | | //hosts are equal! |
| | 0 | 1816 | | } |
| | | 1817 | | else |
| | 0 | 1818 | | { |
| | 0 | 1819 | | EnsureHostString(false); |
| | 0 | 1820 | | other.EnsureHostString(false); |
| | 0 | 1821 | | if (!_info.Host!.Equals(other._info.Host)) |
| | 0 | 1822 | | { |
| | 0 | 1823 | | return false; |
| | | 1824 | | } |
| | 0 | 1825 | | } |
| | | 1826 | | |
| | 0 | 1827 | | if (Port != other.Port) |
| | 0 | 1828 | | { |
| | 0 | 1829 | | return false; |
| | | 1830 | | } |
| | 0 | 1831 | | } |
| | | 1832 | | |
| | | 1833 | | // We want to cache RemoteUrl to improve perf for Uri as a key. |
| | | 1834 | | // We should consider reducing the overall working set by not caching some other properties mentioned in Mor |
| | | 1835 | | |
| | 0 | 1836 | | MoreInfo selfInfo = _info.MoreInfo; |
| | 0 | 1837 | | MoreInfo otherInfo = other._info.MoreInfo; |
| | | 1838 | | |
| | | 1839 | | // Fragment AND UserInfo (for non-mailto URIs) are ignored |
| | 0 | 1840 | | UriComponents components = UriComponents.HttpRequestUrl; |
| | | 1841 | | |
| | 0 | 1842 | | if (_syntax.InFact(UriSyntaxFlags.MailToLikeUri)) |
| | 0 | 1843 | | { |
| | 0 | 1844 | | if (!other._syntax.InFact(UriSyntaxFlags.MailToLikeUri)) |
| | 0 | 1845 | | return false; |
| | | 1846 | | |
| | 0 | 1847 | | components |= UriComponents.UserInfo; |
| | 0 | 1848 | | } |
| | | 1849 | | |
| | 0 | 1850 | | string selfUrl = selfInfo.RemoteUrl ??= GetParts(components, UriFormat.SafeUnescaped); |
| | 0 | 1851 | | string otherUrl = otherInfo.RemoteUrl ??= other.GetParts(components, UriFormat.SafeUnescaped); |
| | | 1852 | | |
| | | 1853 | | // if IsUncOrDosPath is true then we ignore case in the path comparison |
| | 0 | 1854 | | return string.Equals(selfUrl, otherUrl, IsUncOrDosPath ? StringComparison.OrdinalIgnoreCase : StringComparis |
| | 0 | 1855 | | } |
| | | 1856 | | |
| | | 1857 | | public Uri MakeRelativeUri(Uri uri) |
| | 0 | 1858 | | { |
| | 0 | 1859 | | ArgumentNullException.ThrowIfNull(uri); |
| | | 1860 | | |
| | 0 | 1861 | | if (IsNotAbsoluteUri || uri.IsNotAbsoluteUri) |
| | 0 | 1862 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 1863 | | |
| | | 1864 | | // Note that the UserInfo part is ignored when computing a relative Uri. |
| | 0 | 1865 | | if ((Scheme == uri.Scheme) && (Host == uri.Host) && (Port == uri.Port)) |
| | 0 | 1866 | | { |
| | 0 | 1867 | | string otherPath = uri.AbsolutePath; |
| | | 1868 | | |
| | | 1869 | | // Relative Path |
| | 0 | 1870 | | string relativeUriString = PathDifference(AbsolutePath, otherPath, !IsUncOrDosPath); |
| | | 1871 | | |
| | | 1872 | | // Relative Uri's cannot have a colon ':' in the first path segment (RFC 3986, Section 4.2) |
| | 0 | 1873 | | if (CheckForColonInFirstPathSegment(relativeUriString) |
| | 0 | 1874 | | // Except for full implicit dos file paths |
| | 0 | 1875 | | && !(uri.IsDosPath && otherPath.Equals(relativeUriString, StringComparison.Ordinal))) |
| | 0 | 1876 | | relativeUriString = "./" + relativeUriString; |
| | | 1877 | | |
| | | 1878 | | // Query & Fragment |
| | 0 | 1879 | | relativeUriString += uri.GetParts(UriComponents.Query | UriComponents.Fragment, UriFormat.UriEscaped); |
| | | 1880 | | |
| | 0 | 1881 | | return new Uri(relativeUriString, UriKind.Relative); |
| | | 1882 | | } |
| | 0 | 1883 | | return uri; |
| | 0 | 1884 | | } |
| | | 1885 | | |
| | | 1886 | | // http://www.ietf.org/rfc/rfc3986.txt |
| | | 1887 | | // |
| | | 1888 | | // 3.3. Path |
| | | 1889 | | // In addition, a URI reference (Section 4.1) may be a relative-path reference, in which case the first |
| | | 1890 | | // path segment cannot contain a colon (":") character. |
| | | 1891 | | // |
| | | 1892 | | // 4.2. Relative Reference |
| | | 1893 | | // A path segment that contains a colon character (e.g., "this:that") cannot be used as the first segment |
| | | 1894 | | // of a relative-path reference, as it would be mistaken for a scheme name. Such a segment must be |
| | | 1895 | | // preceded by a dot-segment (e.g., "./this:that") to make a relative-path reference. |
| | | 1896 | | // |
| | | 1897 | | // 5.4.2. Abnormal Examples |
| | | 1898 | | // http:(relativeUri) may be considered a valid relative Uri. |
| | | 1899 | | // |
| | | 1900 | | // Returns true if a colon is found in the first path segment, false otherwise |
| | 1 | 1901 | | private static readonly SearchValues<char> s_segmentSeparatorChars = |
| | 1 | 1902 | | SearchValues.Create(@":\/?#"); |
| | | 1903 | | |
| | | 1904 | | private static bool CheckForColonInFirstPathSegment(string uriString) |
| | 0 | 1905 | | { |
| | | 1906 | | // Check for anything that may terminate the first regular path segment |
| | | 1907 | | // or an illegal colon |
| | 0 | 1908 | | int index = uriString.AsSpan().IndexOfAny(s_segmentSeparatorChars); |
| | 0 | 1909 | | return (uint)index < (uint)uriString.Length && uriString[index] == ':'; |
| | 0 | 1910 | | } |
| | | 1911 | | |
| | | 1912 | | internal static string InternalEscapeString(string rawString) => |
| | 0 | 1913 | | rawString is null ? string.Empty : |
| | 0 | 1914 | | UriHelper.EscapeString(rawString, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMark |
| | | 1915 | | |
| | | 1916 | | // |
| | | 1917 | | // This method is called first to figure out the scheme or a simple file path |
| | | 1918 | | // Is called only at the .ctor time |
| | | 1919 | | // |
| | | 1920 | | private static ParsingError ParseScheme(string uriString, ref Flags flags, ref UriParser? syntax) |
| | 661 | 1921 | | { |
| | 661 | 1922 | | Debug.Assert((flags & Flags.Debug_LeftConstructor) == 0); |
| | | 1923 | | |
| | 661 | 1924 | | int length = uriString.Length; |
| | 661 | 1925 | | if (length == 0) |
| | 0 | 1926 | | return ParsingError.EmptyUriString; |
| | | 1927 | | |
| | | 1928 | | // Fast path for valid http(s) schemes with no leading whitespace that are expected to be very common. |
| | 661 | 1929 | | if (uriString.StartsWith("https:", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 1930 | | { |
| | 0 | 1931 | | syntax = UriParser.HttpsUri; |
| | 0 | 1932 | | flags |= (Flags)6; |
| | 0 | 1933 | | } |
| | 661 | 1934 | | else if (uriString.StartsWith("http:", StringComparison.OrdinalIgnoreCase)) |
| | 661 | 1935 | | { |
| | 661 | 1936 | | syntax = UriParser.HttpUri; |
| | 661 | 1937 | | flags |= (Flags)5; |
| | 661 | 1938 | | } |
| | | 1939 | | else |
| | 0 | 1940 | | { |
| | | 1941 | | // STEP1: parse scheme, lookup this Uri Syntax or create one using UnknownV1SyntaxFlags uri syntax templ |
| | 0 | 1942 | | ParsingError err = ParsingError.None; |
| | 0 | 1943 | | int idx = ParseSchemeCheckImplicitFile(uriString, ref err, ref flags, ref syntax); |
| | 0 | 1944 | | Debug.Assert((err is ParsingError.None) == (syntax is not null)); |
| | | 1945 | | |
| | 0 | 1946 | | if (err != ParsingError.None) |
| | 0 | 1947 | | return err; |
| | | 1948 | | |
| | 0 | 1949 | | flags |= (Flags)idx; |
| | 0 | 1950 | | } |
| | | 1951 | | |
| | 661 | 1952 | | return ParsingError.None; |
| | 661 | 1953 | | } |
| | | 1954 | | |
| | | 1955 | | // |
| | | 1956 | | // A wrapper for ParseMinimal() called from a user parser |
| | | 1957 | | // It signals back that the call has been done |
| | | 1958 | | // plus it communicates back a flag for an error if any |
| | | 1959 | | // |
| | | 1960 | | internal UriFormatException? ParseMinimal() |
| | 0 | 1961 | | { |
| | 0 | 1962 | | Debug.Assert(_syntax != null && !_syntax.IsSimple); |
| | 0 | 1963 | | Debug.Assert((_flags & Flags.CustomParser_ParseMinimalAlreadyCalled) != 0); |
| | 0 | 1964 | | DebugAssertInCtor(); |
| | | 1965 | | |
| | 0 | 1966 | | ParsingError result = PrivateParseMinimal(); |
| | 0 | 1967 | | if (result == ParsingError.None) |
| | 0 | 1968 | | return null; |
| | | 1969 | | |
| | | 1970 | | // Means the we think the Uri is invalid, bu that can be later overridden by a user parser |
| | 0 | 1971 | | _flags |= Flags.ErrorOrParsingRecursion; |
| | | 1972 | | |
| | 0 | 1973 | | return GetException(result); |
| | 0 | 1974 | | } |
| | | 1975 | | |
| | | 1976 | | // This method tries to parse the minimal information needed to certify the validity |
| | | 1977 | | // of a uri string |
| | | 1978 | | // |
| | | 1979 | | // scheme://userinfo@host:Port/Path?Query#Fragment |
| | | 1980 | | // |
| | | 1981 | | // The method must be called only at the .ctor time |
| | | 1982 | | // |
| | | 1983 | | // Returns ParsingError.None if the Uri syntax is valid, an error otherwise |
| | | 1984 | | private ParsingError PrivateParseMinimal() |
| | 661 | 1985 | | { |
| | 661 | 1986 | | Debug.Assert(_syntax != null); |
| | 661 | 1987 | | DebugAssertInCtor(); |
| | 661 | 1988 | | Debug.Assert(ReferenceEquals(_string, OriginalString)); |
| | | 1989 | | |
| | 661 | 1990 | | int idx = (int)(_flags & Flags.IndexMask); |
| | | 1991 | | |
| | | 1992 | | // Means a custom UriParser did call "base" InitializeAndValidate() |
| | 661 | 1993 | | _flags &= ~(Flags.IndexMask | Flags.UserDrivenParsing); |
| | | 1994 | | |
| | 661 | 1995 | | ReadOnlySpan<char> str = _string; |
| | | 1996 | | |
| | | 1997 | | // Trim any trailing spaces |
| | 661 | 1998 | | while (!str.IsEmpty && UriHelper.IsLWS(str[str.Length - 1])) |
| | 0 | 1999 | | { |
| | 0 | 2000 | | str = str.Slice(0, str.Length - 1); |
| | 0 | 2001 | | } |
| | | 2002 | | |
| | | 2003 | | // Unix Path |
| | 661 | 2004 | | if (!OperatingSystem.IsWindows() && InFact(Flags.UnixPath)) |
| | 0 | 2005 | | { |
| | 0 | 2006 | | _flags |= Flags.BasicHostType; |
| | 0 | 2007 | | _flags |= (Flags)idx; |
| | 0 | 2008 | | return ParsingError.None; |
| | | 2009 | | } |
| | | 2010 | | |
| | | 2011 | | // Old Uri parser tries to figure out on a DosPath in all cases. |
| | | 2012 | | // Hence http://c:/ is treated as DosPath without the host while it should be a host "c", port 80 |
| | | 2013 | | // |
| | | 2014 | | // This block is compatible with Old Uri parser in terms it will look for the DosPath if the scheme |
| | | 2015 | | // syntax allows both empty hostnames and DosPath |
| | 661 | 2016 | | if (_syntax.IsAllSet(UriSyntaxFlags.AllowEmptyHost | UriSyntaxFlags.AllowDOSPath) && |
| | 661 | 2017 | | NotAny(Flags.ImplicitFile) && |
| | 661 | 2018 | | (idx + 1 < str.Length)) |
| | 0 | 2019 | | { |
| | 0 | 2020 | | int i = idx; |
| | | 2021 | | |
| | | 2022 | | // V1 Compat: Allow _compression_ of > 3 slashes only for File scheme. |
| | | 2023 | | // This will skip all slashes and if their number is 2+ it sets the AuthorityFound flag |
| | 0 | 2024 | | for (; (uint)i < (uint)str.Length; i++) |
| | 0 | 2025 | | { |
| | 0 | 2026 | | if (str[i] is not ('\\' or '/')) |
| | 0 | 2027 | | { |
| | 0 | 2028 | | break; |
| | | 2029 | | } |
| | 0 | 2030 | | } |
| | | 2031 | | |
| | 0 | 2032 | | if (_syntax.InFact(UriSyntaxFlags.FileLikeUri) || i - idx <= 3) |
| | 0 | 2033 | | { |
| | | 2034 | | // if more than one slash after the scheme, the authority is present |
| | 0 | 2035 | | if (i - idx >= 2) |
| | 0 | 2036 | | { |
| | 0 | 2037 | | _flags |= Flags.AuthorityFound; |
| | 0 | 2038 | | } |
| | | 2039 | | |
| | | 2040 | | // DOS-like path? |
| | 0 | 2041 | | if ((uint)(i + 1) < (uint)str.Length && str[i + 1] is ':' or '|' && char.IsAsciiLetter(str[i])) |
| | 0 | 2042 | | { |
| | 0 | 2043 | | if ((uint)(i + 2) >= (uint)str.Length || str[i + 2] is not ('\\' or '/')) |
| | 0 | 2044 | | { |
| | | 2045 | | // report an error but only for a file: scheme |
| | 0 | 2046 | | if (_syntax.InFact(UriSyntaxFlags.FileLikeUri)) |
| | 0 | 2047 | | return ParsingError.MustRootedPath; |
| | 0 | 2048 | | } |
| | | 2049 | | else |
| | 0 | 2050 | | { |
| | | 2051 | | // This will set IsDosPath |
| | 0 | 2052 | | _flags |= Flags.DosPath; |
| | | 2053 | | |
| | 0 | 2054 | | if (_syntax.InFact(UriSyntaxFlags.MustHaveAuthority)) |
| | 0 | 2055 | | { |
| | | 2056 | | // when DosPath found and Authority is required, set this flag even if Authority is empt |
| | 0 | 2057 | | _flags |= Flags.AuthorityFound; |
| | 0 | 2058 | | } |
| | 0 | 2059 | | if (i != idx && i - idx != 2) |
| | 0 | 2060 | | { |
| | | 2061 | | //This will remember that DosPath is rooted |
| | 0 | 2062 | | idx = i - 1; |
| | 0 | 2063 | | } |
| | | 2064 | | else |
| | 0 | 2065 | | { |
| | 0 | 2066 | | idx = i; |
| | 0 | 2067 | | } |
| | 0 | 2068 | | } |
| | 0 | 2069 | | } |
| | | 2070 | | // UNC share? |
| | 0 | 2071 | | else if (_syntax.InFact(UriSyntaxFlags.FileLikeUri) && |
| | 0 | 2072 | | i - idx >= 2 && i - idx != 3 && |
| | 0 | 2073 | | (uint)i < (uint)str.Length && str[i] is not ('?' or '#')) |
| | 0 | 2074 | | { |
| | | 2075 | | // V1.0 did not support file:///, fixing it with minimal behavior change impact |
| | | 2076 | | // Only FILE scheme may have UNC Path flag set |
| | 0 | 2077 | | _flags |= Flags.UncPath; |
| | 0 | 2078 | | idx = i; |
| | 0 | 2079 | | } |
| | 0 | 2080 | | else if (!OperatingSystem.IsWindows() && _syntax.InFact(UriSyntaxFlags.FileLikeUri) && str[i - 1] == |
| | 0 | 2081 | | { |
| | 0 | 2082 | | _syntax = UriParser.UnixFileUri; |
| | 0 | 2083 | | _flags |= Flags.UnixPath | Flags.AuthorityFound; |
| | 0 | 2084 | | idx += 2; |
| | 0 | 2085 | | } |
| | 0 | 2086 | | } |
| | 0 | 2087 | | } |
| | | 2088 | | |
| | | 2089 | | // Decide on the Authority component |
| | 661 | 2090 | | if ((_flags & (Flags.UncPath | Flags.DosPath | Flags.UnixPath)) != 0) |
| | 0 | 2091 | | { |
| | | 2092 | | |
| | 0 | 2093 | | } |
| | 661 | 2094 | | else if ((uint)(idx + 1) < (uint)str.Length) |
| | 661 | 2095 | | { |
| | 661 | 2096 | | char first = str[idx]; |
| | 661 | 2097 | | char second = str[idx + 1]; |
| | | 2098 | | |
| | 661 | 2099 | | if (_syntax.InFact(UriSyntaxFlags.MustHaveAuthority)) |
| | 661 | 2100 | | { |
| | | 2101 | | // (V1.0 compatibility) This will allow http:\\ http:\/ http:/\ |
| | 661 | 2102 | | if ((first == '/' || first == '\\') && (second == '/' || second == '\\')) |
| | 661 | 2103 | | { |
| | 661 | 2104 | | _flags |= Flags.AuthorityFound; |
| | 661 | 2105 | | idx += 2; |
| | 661 | 2106 | | } |
| | | 2107 | | else |
| | 0 | 2108 | | { |
| | 0 | 2109 | | return ParsingError.BadAuthority; |
| | | 2110 | | } |
| | 661 | 2111 | | } |
| | 0 | 2112 | | else if (_syntax.InFact(UriSyntaxFlags.OptionalAuthority) && (InFact(Flags.AuthorityFound) || |
| | 0 | 2113 | | (first == '/' && second == '/'))) |
| | 0 | 2114 | | { |
| | 0 | 2115 | | _flags |= Flags.AuthorityFound; |
| | 0 | 2116 | | idx += 2; |
| | 0 | 2117 | | } |
| | | 2118 | | // There is no Authority component, save the Path index |
| | | 2119 | | // Ideally we would treat mailto like any other URI, but for historical reasons we have to separate out |
| | 0 | 2120 | | else if (_syntax.NotAny(UriSyntaxFlags.MailToLikeUri)) |
| | 0 | 2121 | | { |
| | | 2122 | | // By now we know the URI has no Authority, so if the URI must be normalized, initialize it without |
| | 0 | 2123 | | if (InFact(Flags.HasUnicode)) |
| | 0 | 2124 | | { |
| | 0 | 2125 | | _string = _string.Substring(0, idx); |
| | 0 | 2126 | | } |
| | | 2127 | | // Since there is no Authority, the path index is just the end of the scheme. |
| | 0 | 2128 | | _flags |= ((Flags)idx | Flags.UnknownHostType); |
| | 0 | 2129 | | return ParsingError.None; |
| | | 2130 | | } |
| | 661 | 2131 | | } |
| | 0 | 2132 | | else if (_syntax.InFact(UriSyntaxFlags.MustHaveAuthority)) |
| | 0 | 2133 | | { |
| | 0 | 2134 | | return ParsingError.BadAuthority; |
| | | 2135 | | } |
| | | 2136 | | // There is no Authority component, save the Path index |
| | | 2137 | | // Ideally we would treat mailto like any other URI, but for historical reasons we have to separate out its |
| | 0 | 2138 | | else if (_syntax.NotAny(UriSyntaxFlags.MailToLikeUri)) |
| | 0 | 2139 | | { |
| | | 2140 | | // By now we know the URI has no Authority, so if the URI must be normalized, initialize it without one. |
| | 0 | 2141 | | if (InFact(Flags.HasUnicode)) |
| | 0 | 2142 | | { |
| | 0 | 2143 | | _string = _string.Substring(0, idx); |
| | 0 | 2144 | | } |
| | | 2145 | | // Since there is no Authority, the path index is just the end of the scheme. |
| | 0 | 2146 | | _flags |= ((Flags)idx | Flags.UnknownHostType); |
| | 0 | 2147 | | return ParsingError.None; |
| | | 2148 | | } |
| | | 2149 | | |
| | | 2150 | | // vsmacros://c:\path\file |
| | | 2151 | | // Note that two slashes say there must be an Authority but instead the path goes |
| | | 2152 | | // Fro V1 compat the next block allow this case but not for schemes like http |
| | 661 | 2153 | | if (InFact(Flags.DosPath)) |
| | 0 | 2154 | | { |
| | 0 | 2155 | | _flags |= (((_flags & Flags.AuthorityFound) != 0) ? Flags.BasicHostType : Flags.UnknownHostType); |
| | 0 | 2156 | | _flags |= (Flags)idx; |
| | 0 | 2157 | | return ParsingError.None; |
| | | 2158 | | } |
| | | 2159 | | |
| | | 2160 | | //STEP 2: Check the syntax of authority expecting at least one character in it |
| | | 2161 | | // |
| | | 2162 | | // Note here we do know that there is an authority in the string OR it's a DOS path |
| | | 2163 | | |
| | | 2164 | | // We may find a userInfo and the port when parsing an authority |
| | | 2165 | | // Also we may find a registry based authority. |
| | | 2166 | | // We must ensure that known schemes do use a server-based authority |
| | 661 | 2167 | | { |
| | 661 | 2168 | | string? newHost = null; // stores newly parsed host when original strings are being switched |
| | | 2169 | | |
| | 661 | 2170 | | idx = CheckAuthorityHelper(str, idx, out ParsingError err, ref _flags, _syntax.Flags, ref newHost); |
| | 661 | 2171 | | if (err != ParsingError.None) |
| | 0 | 2172 | | return err; |
| | | 2173 | | |
| | 661 | 2174 | | if ((uint)idx < (uint)str.Length) |
| | 661 | 2175 | | { |
| | 661 | 2176 | | char hostTerminator = str[idx]; |
| | | 2177 | | |
| | | 2178 | | // This will disallow '\' as the host terminator for any scheme that is not implicitFile or cannot h |
| | 661 | 2179 | | if (hostTerminator == '\\' && NotAny(Flags.ImplicitFile) && _syntax.NotAny(UriSyntaxFlags.AllowDOSPa |
| | 0 | 2180 | | { |
| | 0 | 2181 | | return ParsingError.BadAuthorityTerminator; |
| | | 2182 | | } |
| | | 2183 | | // When the hostTerminator is '/' on Unix, use the UnixFile syntax (preserve backslashes) |
| | 661 | 2184 | | else if (!OperatingSystem.IsWindows() && hostTerminator == '/' && NotAny(Flags.ImplicitFile) && InFa |
| | 0 | 2185 | | { |
| | 0 | 2186 | | _syntax = UriParser.UnixFileUri; |
| | 0 | 2187 | | } |
| | 661 | 2188 | | } |
| | | 2189 | | |
| | 661 | 2190 | | if (newHost is not null) |
| | 0 | 2191 | | { |
| | 0 | 2192 | | _string = newHost; |
| | 0 | 2193 | | } |
| | 661 | 2194 | | } |
| | | 2195 | | |
| | | 2196 | | // The Path (or Port) parsing index is reloaded on demand in CreateUriInfo when accessing a Uri property |
| | 661 | 2197 | | _flags |= (Flags)idx; |
| | | 2198 | | |
| | | 2199 | | // The rest of the string will be parsed on demand |
| | | 2200 | | // The Host/Authority is all checked, the type is known but the host value string |
| | | 2201 | | // is not created/canonicalized at this point. |
| | | 2202 | | |
| | 661 | 2203 | | return ParsingError.None; |
| | 661 | 2204 | | } |
| | | 2205 | | |
| | | 2206 | | // |
| | | 2207 | | // |
| | | 2208 | | // The method is called when we have to access _info members. |
| | | 2209 | | // This will create the _info based on the copied parser context. |
| | | 2210 | | // If multi-threading, this method may do duplicated yet harmless work. |
| | | 2211 | | // |
| | | 2212 | | private void CreateUriInfo(Flags cF) |
| | 661 | 2213 | | { |
| | 661 | 2214 | | UriInfo info = new UriInfo(); |
| | | 2215 | | |
| | 661 | 2216 | | string str = OriginalString; |
| | | 2217 | | |
| | | 2218 | | // This will be revisited in ParseRemaining if we're rewriting the _string. |
| | 661 | 2219 | | info.Offset.End = str.Length; |
| | | 2220 | | |
| | 661 | 2221 | | if (UserDrivenParsing) |
| | 0 | 2222 | | goto Done; |
| | | 2223 | | |
| | | 2224 | | int idx; |
| | 661 | 2225 | | bool notCanonicalScheme = false; |
| | | 2226 | | |
| | | 2227 | | // Skip leading whitespace |
| | 661 | 2228 | | idx = 0; |
| | 661 | 2229 | | while (UriHelper.IsLWS(str[idx])) |
| | 0 | 2230 | | { |
| | 0 | 2231 | | idx++; |
| | 0 | 2232 | | info.Offset.Scheme++; |
| | 0 | 2233 | | } |
| | | 2234 | | |
| | 661 | 2235 | | if ((cF & Flags.ImplicitFile) != 0) |
| | 0 | 2236 | | { |
| | 0 | 2237 | | if (StaticInFact(cF, Flags.UncPath)) |
| | 0 | 2238 | | { |
| | 0 | 2239 | | Debug.Assert(str[idx] is '/' or '\\' && str[idx + 1] is '/' or '\\'); |
| | | 2240 | | |
| | | 2241 | | // For implicit file AND Unc only |
| | 0 | 2242 | | idx += 2; |
| | | 2243 | | |
| | | 2244 | | // Skip any other slashes (compatibility with V1.0 parser) |
| | 0 | 2245 | | int end = (int)(cF & Flags.IndexMask); |
| | 0 | 2246 | | while (idx < end && (str[idx] == '/' || str[idx] == '\\')) |
| | 0 | 2247 | | { |
| | 0 | 2248 | | ++idx; |
| | 0 | 2249 | | } |
| | 0 | 2250 | | } |
| | 0 | 2251 | | } |
| | | 2252 | | else |
| | 661 | 2253 | | { |
| | | 2254 | | // This is NOT an ImplicitFile uri |
| | 661 | 2255 | | idx += _syntax.SchemeName.Length + 1; |
| | 661 | 2256 | | Debug.Assert(str[idx - 1] == ':'); |
| | | 2257 | | |
| | 661 | 2258 | | if ((cF & Flags.AuthorityFound) != 0) |
| | 661 | 2259 | | { |
| | 661 | 2260 | | if (str[idx] is '/' or '\\' && str[idx + 1] is '/' or '\\') |
| | 661 | 2261 | | { |
| | 661 | 2262 | | if (str[idx] == '\\' || str[idx + 1] == '\\') |
| | 0 | 2263 | | { |
| | 0 | 2264 | | notCanonicalScheme = true; |
| | 0 | 2265 | | } |
| | | 2266 | | |
| | 661 | 2267 | | idx += 2; |
| | 661 | 2268 | | } |
| | | 2269 | | else |
| | 0 | 2270 | | { |
| | 0 | 2271 | | Debug.Assert(IsDosPath); |
| | 0 | 2272 | | Debug.Assert(char.IsAsciiLetter(str.AsSpan(idx).TrimStart(['/', '\\'])[0])); |
| | 0 | 2273 | | Debug.Assert(str.AsSpan(idx).TrimStart(['/', '\\'])[1] is ':' or '|'); |
| | 0 | 2274 | | } |
| | | 2275 | | |
| | 661 | 2276 | | if ((cF & (Flags.UncPath | Flags.DosPath)) != 0) |
| | 0 | 2277 | | { |
| | | 2278 | | // Skip slashes if it was allowed during ctor time |
| | | 2279 | | // NB: Today this is only allowed if a Unc or DosPath was found after the scheme |
| | 0 | 2280 | | int end = (int)(cF & Flags.IndexMask); |
| | 0 | 2281 | | while (idx < end && (str[idx] == '/' || str[idx] == '\\')) |
| | 0 | 2282 | | { |
| | 0 | 2283 | | notCanonicalScheme = true; |
| | 0 | 2284 | | ++idx; |
| | 0 | 2285 | | } |
| | 0 | 2286 | | } |
| | 661 | 2287 | | } |
| | 661 | 2288 | | } |
| | | 2289 | | |
| | | 2290 | | // Up until the userinfo/host, the _string and OriginalString are the same. |
| | 661 | 2291 | | Debug.Assert(_string.AsSpan(0, idx).SequenceEqual(OriginalString.AsSpan(0, idx))); |
| | | 2292 | | |
| | | 2293 | | // Some schemes (mailto) do not have Authority-based syntax, still they do have a port |
| | 661 | 2294 | | if (_syntax.DefaultPort != UriParser.NoDefaultPort) |
| | 661 | 2295 | | info.Offset.PortValue = (ushort)_syntax.DefaultPort; |
| | | 2296 | | |
| | 661 | 2297 | | info.Offset.User = idx; |
| | | 2298 | | |
| | 661 | 2299 | | if ((cF & Flags.HasUserInfo) != 0) |
| | 0 | 2300 | | { |
| | | 2301 | | // We've seen the '@' before. It should also be the first '@' in the input. |
| | 0 | 2302 | | Debug.Assert(str.AsSpan(idx).Contains('@')); |
| | 0 | 2303 | | Debug.Assert(str.IndexOf('@') == str.IndexOf('@', idx)); |
| | | 2304 | | |
| | | 2305 | | // The UserInfo in _string may have been modified if we're recreating the string. |
| | | 2306 | | // Since Offset.Host must point into _string and not OriginalString, find the offset in the new string. |
| | 0 | 2307 | | idx = _string.IndexOf('@') + 1; |
| | 0 | 2308 | | } |
| | | 2309 | | |
| | 661 | 2310 | | info.Offset.Host = idx; |
| | | 2311 | | |
| | | 2312 | | // Now reload the end of the parsed host. |
| | | 2313 | | // This index is pointing into the original string. |
| | 661 | 2314 | | idx = (int)(cF & Flags.IndexMask); |
| | 661 | 2315 | | Debug.Assert((uint)idx <= (uint)OriginalString.Length); |
| | | 2316 | | |
| | | 2317 | | // From now on we do not need IndexMask bits, and reuse the space for X_NotCanonical flags |
| | 661 | 2318 | | cF &= ~Flags.IndexMask; |
| | | 2319 | | |
| | 661 | 2320 | | if (notCanonicalScheme) |
| | 0 | 2321 | | { |
| | 0 | 2322 | | cF |= Flags.SchemeNotCanonical; |
| | 0 | 2323 | | } |
| | | 2324 | | |
| | | 2325 | | // UnknownHostType / DosPath can't have a port. |
| | 661 | 2326 | | if ((cF & Flags.HostTypeMask) != Flags.UnknownHostType && (cF & Flags.DosPath) == 0 && |
| | 661 | 2327 | | (uint)idx < (uint)str.Length && str[idx] == ':') |
| | 0 | 2328 | | { |
| | 0 | 2329 | | Debug.Assert(!IsUnc); |
| | | 2330 | | |
| | | 2331 | | // Parse the Port if any. The new spec allows a port after ':' to be empty (assuming default?) |
| | | 2332 | | // Note we already checked on general port syntax in ParseMinimal() |
| | 0 | 2333 | | bool notEmpty = false; |
| | 0 | 2334 | | int port = 0; |
| | | 2335 | | |
| | 0 | 2336 | | idx++; // Skip ':' |
| | | 2337 | | |
| | | 2338 | | // Check on some non-canonical cases http://host:0324/, http://host:03, http://host:0, etc |
| | 0 | 2339 | | if ((uint)idx < (uint)str.Length) |
| | 0 | 2340 | | { |
| | 0 | 2341 | | port = str[idx] - '0'; |
| | 0 | 2342 | | if ((uint)port <= ('9' - '0')) |
| | 0 | 2343 | | { |
| | 0 | 2344 | | notEmpty = true; |
| | 0 | 2345 | | if (port == 0) |
| | 0 | 2346 | | { |
| | 0 | 2347 | | cF |= (Flags.PortNotCanonical | Flags.E_PortNotCanonical); |
| | 0 | 2348 | | } |
| | | 2349 | | |
| | 0 | 2350 | | for (idx++; (uint)idx < (uint)str.Length; idx++) |
| | 0 | 2351 | | { |
| | 0 | 2352 | | int val = str[idx] - '0'; |
| | 0 | 2353 | | if ((uint)val > ('9' - '0')) |
| | 0 | 2354 | | { |
| | 0 | 2355 | | break; |
| | | 2356 | | } |
| | 0 | 2357 | | port = (port * 10 + val); |
| | 0 | 2358 | | } |
| | 0 | 2359 | | } |
| | 0 | 2360 | | } |
| | | 2361 | | |
| | 0 | 2362 | | if (notEmpty && _syntax.DefaultPort != port) |
| | 0 | 2363 | | { |
| | 0 | 2364 | | info.Offset.PortValue = (ushort)port; |
| | 0 | 2365 | | cF |= Flags.NotDefaultPort; |
| | 0 | 2366 | | } |
| | | 2367 | | else |
| | 0 | 2368 | | { |
| | | 2369 | | // This will tell that we do have a ':' but the port value does |
| | | 2370 | | // not follow to canonical rules |
| | 0 | 2371 | | cF |= (Flags.PortNotCanonical | Flags.E_PortNotCanonical); |
| | 0 | 2372 | | } |
| | 0 | 2373 | | } |
| | | 2374 | | |
| | | 2375 | | // While info.Offset values must point into _string instead of OriginalString when we're done with parsing, |
| | | 2376 | | // we'll temporarily point the path offset into OriginalString. |
| | | 2377 | | // ParseRemaining will update the value if we're replacing the _string. |
| | 661 | 2378 | | info.Offset.Path = idx; |
| | | 2379 | | |
| | 661 | 2380 | | Done: |
| | 661 | 2381 | | cF |= Flags.MinimalUriInfoSet; |
| | | 2382 | | |
| | 661 | 2383 | | Interlocked.CompareExchange(ref _info, info, null!); |
| | | 2384 | | |
| | 661 | 2385 | | Flags current = _flags; |
| | 661 | 2386 | | while ((current & Flags.MinimalUriInfoSet) == 0) |
| | 661 | 2387 | | { |
| | 661 | 2388 | | Flags oldValue = Interlocked.CompareExchange(ref _flags, (current & ~Flags.IndexMask) | cF, current); |
| | 661 | 2389 | | if (oldValue == current) |
| | 661 | 2390 | | { |
| | 661 | 2391 | | return; |
| | | 2392 | | } |
| | 0 | 2393 | | current = oldValue; |
| | 0 | 2394 | | } |
| | 661 | 2395 | | } |
| | | 2396 | | |
| | | 2397 | | // |
| | | 2398 | | // This will create a Host string. The validity has been already checked |
| | | 2399 | | // |
| | | 2400 | | // Assuming: UriInfo member is already set at this point |
| | | 2401 | | private void CreateHostString() |
| | 0 | 2402 | | { |
| | 0 | 2403 | | if (!_syntax.IsSimple) |
| | 0 | 2404 | | { |
| | 0 | 2405 | | lock (_info) |
| | 0 | 2406 | | { |
| | | 2407 | | // ATTN: Avoid possible recursion through |
| | | 2408 | | // CreateHostString->Syntax.GetComponents->Uri.GetComponentsHelper->CreateHostString |
| | 0 | 2409 | | if (NotAny(Flags.ErrorOrParsingRecursion)) |
| | 0 | 2410 | | { |
| | 0 | 2411 | | _flags |= Flags.ErrorOrParsingRecursion; |
| | | 2412 | | // Need to get host string through the derived type |
| | 0 | 2413 | | GetHostViaCustomSyntax(); |
| | 0 | 2414 | | _flags &= ~Flags.ErrorOrParsingRecursion; |
| | 0 | 2415 | | return; |
| | | 2416 | | } |
| | 0 | 2417 | | } |
| | 0 | 2418 | | } |
| | | 2419 | | |
| | 0 | 2420 | | Flags flags = _flags; |
| | 0 | 2421 | | string host = CreateHostStringHelper(_string, _info.Offset.Host, _info.Offset.Path, ref flags, _info); |
| | | 2422 | | |
| | | 2423 | | // now check on canonical host representation |
| | 0 | 2424 | | if (host.Length != 0) |
| | 0 | 2425 | | { |
| | | 2426 | | // An Authority may need escaping except when it's an inet server address |
| | 0 | 2427 | | if (HostType == Flags.BasicHostType) |
| | 0 | 2428 | | { |
| | 0 | 2429 | | Check result = CheckCanonical(host, c_DummyChar, out _); |
| | | 2430 | | |
| | 0 | 2431 | | if ((result & Check.DisplayCanonical) == 0) |
| | 0 | 2432 | | { |
| | | 2433 | | // For implicit file the user string must be in perfect display format, |
| | | 2434 | | // Hence, ignoring complains from CheckCanonical() |
| | 0 | 2435 | | if (NotAny(Flags.ImplicitFile) || (result & Check.ReservedFound) != 0) |
| | 0 | 2436 | | { |
| | 0 | 2437 | | flags |= Flags.HostNotCanonical; |
| | 0 | 2438 | | } |
| | 0 | 2439 | | } |
| | | 2440 | | |
| | 0 | 2441 | | if (InFact(Flags.ImplicitFile) && (result & (Check.ReservedFound | Check.EscapedCanonical)) != 0) |
| | 0 | 2442 | | { |
| | | 2443 | | // need to re-escape this host if any escaped sequence was found |
| | 0 | 2444 | | result &= ~Check.EscapedCanonical; |
| | 0 | 2445 | | } |
| | | 2446 | | |
| | 0 | 2447 | | if ((result & (Check.EscapedCanonical | Check.BackslashInPath)) != Check.EscapedCanonical) |
| | 0 | 2448 | | { |
| | | 2449 | | // we will make a canonical host in _info.Host, but mark that _string holds wrong data |
| | 0 | 2450 | | flags |= Flags.E_HostNotCanonical; |
| | 0 | 2451 | | if (NotAny(Flags.UserEscaped)) |
| | 0 | 2452 | | { |
| | 0 | 2453 | | host = UriHelper.EscapeString(host, checkExistingEscaped: !IsImplicitFile, UriHelper.Unreser |
| | 0 | 2454 | | } |
| | | 2455 | | else |
| | 0 | 2456 | | { |
| | | 2457 | | // We should throw here but currently just accept user input known as invalid |
| | 0 | 2458 | | } |
| | 0 | 2459 | | } |
| | 0 | 2460 | | } |
| | 0 | 2461 | | else if (NotAny(Flags.CanonicalDnsHost)) |
| | 0 | 2462 | | { |
| | | 2463 | | // Check to see if we can take the canonical host string out of _string |
| | 0 | 2464 | | if (_info._moreInfo?.ScopeId is not null) |
| | 0 | 2465 | | { |
| | | 2466 | | // IPv6 ScopeId is included when serializing a Uri |
| | 0 | 2467 | | flags |= (Flags.HostNotCanonical | Flags.E_HostNotCanonical); |
| | 0 | 2468 | | } |
| | | 2469 | | else |
| | 0 | 2470 | | { |
| | 0 | 2471 | | for (int i = 0; i < host.Length; ++i) |
| | 0 | 2472 | | { |
| | 0 | 2473 | | if ((_info.Offset.Host + i) >= _info.Offset.End || |
| | 0 | 2474 | | host[i] != _string[_info.Offset.Host + i]) |
| | 0 | 2475 | | { |
| | 0 | 2476 | | flags |= (Flags.HostNotCanonical | Flags.E_HostNotCanonical); |
| | 0 | 2477 | | break; |
| | | 2478 | | } |
| | 0 | 2479 | | } |
| | 0 | 2480 | | } |
| | 0 | 2481 | | } |
| | 0 | 2482 | | } |
| | | 2483 | | |
| | 0 | 2484 | | _info.Host = host; |
| | 0 | 2485 | | InterlockedSetFlags(flags); |
| | 0 | 2486 | | } |
| | | 2487 | | |
| | | 2488 | | private static string CreateHostStringHelper(string str, int idx, int end, ref Flags flags, UriInfo info) |
| | 0 | 2489 | | { |
| | 0 | 2490 | | bool loopback = false; |
| | | 2491 | | string host; |
| | 0 | 2492 | | switch (flags & Flags.HostTypeMask) |
| | | 2493 | | { |
| | | 2494 | | case Flags.DnsHostType: |
| | 0 | 2495 | | host = DomainNameHelper.ParseCanonicalName(str, idx, end, ref loopback); |
| | 0 | 2496 | | break; |
| | | 2497 | | |
| | | 2498 | | case Flags.IPv6HostType: |
| | | 2499 | | // The helper will return [...] string that is not suited for Dns.Resolve() |
| | 0 | 2500 | | host = IPv6AddressHelper.ParseCanonicalName(str.AsSpan(idx), ref loopback, out ReadOnlySpan<char> sc |
| | | 2501 | | |
| | 0 | 2502 | | if (!scopeIdSpan.IsEmpty) |
| | 0 | 2503 | | { |
| | 0 | 2504 | | info.MoreInfo.ScopeId = new string(scopeIdSpan); |
| | 0 | 2505 | | } |
| | 0 | 2506 | | break; |
| | | 2507 | | |
| | | 2508 | | case Flags.IPv4HostType: |
| | 0 | 2509 | | host = IPv4AddressHelper.ParseCanonicalName(str, idx, end, ref loopback); |
| | 0 | 2510 | | break; |
| | | 2511 | | |
| | | 2512 | | case Flags.UncHostType: |
| | 0 | 2513 | | host = UncNameHelper.ParseCanonicalName(str, idx, end, ref loopback); |
| | 0 | 2514 | | break; |
| | | 2515 | | |
| | | 2516 | | case Flags.BasicHostType: |
| | 0 | 2517 | | if (StaticInFact(flags, Flags.DosPath)) |
| | 0 | 2518 | | { |
| | 0 | 2519 | | host = string.Empty; |
| | 0 | 2520 | | } |
| | | 2521 | | else |
| | 0 | 2522 | | { |
| | | 2523 | | // This is for a registry-based authority, not relevant for known schemes |
| | 0 | 2524 | | host = str.Substring(idx, end - idx); |
| | 0 | 2525 | | } |
| | | 2526 | | // A empty host would count for a loopback |
| | 0 | 2527 | | if (host.Length == 0) |
| | 0 | 2528 | | { |
| | 0 | 2529 | | loopback = true; |
| | 0 | 2530 | | } |
| | | 2531 | | //there will be no port |
| | 0 | 2532 | | break; |
| | | 2533 | | |
| | | 2534 | | case Flags.UnknownHostType: |
| | | 2535 | | //means the host is *not expected* for this uri type |
| | 0 | 2536 | | host = string.Empty; |
| | 0 | 2537 | | break; |
| | | 2538 | | |
| | | 2539 | | default: |
| | 0 | 2540 | | throw GetException(ParsingError.BadHostName)!; |
| | | 2541 | | } |
| | | 2542 | | |
| | 0 | 2543 | | if (loopback) |
| | 0 | 2544 | | { |
| | 0 | 2545 | | flags |= Flags.LoopbackHost; |
| | 0 | 2546 | | } |
| | 0 | 2547 | | return host; |
| | 0 | 2548 | | } |
| | | 2549 | | |
| | | 2550 | | // |
| | | 2551 | | // Called under lock() |
| | | 2552 | | // |
| | | 2553 | | private void GetHostViaCustomSyntax() |
| | 0 | 2554 | | { |
| | | 2555 | | // A multithreading check |
| | 0 | 2556 | | if (_info.Host != null) |
| | 0 | 2557 | | return; |
| | | 2558 | | |
| | 0 | 2559 | | string host = _syntax.InternalGetComponents(this, UriComponents.Host, UriFormat.UriEscaped); |
| | | 2560 | | |
| | | 2561 | | // ATTN: Check on whether recursion has not happened |
| | 0 | 2562 | | if (_info.Host is null) |
| | 0 | 2563 | | { |
| | 0 | 2564 | | Flags flags = _flags & ~Flags.HostTypeMask; |
| | 0 | 2565 | | string? newHost = null; |
| | | 2566 | | |
| | 0 | 2567 | | if (CheckAuthorityHelper(host, 0, out ParsingError err, ref flags, _syntax.Flags, ref newHost) != host.L |
| | 0 | 2568 | | err != ParsingError.None) |
| | 0 | 2569 | | { |
| | | 2570 | | // Well, custom parser has returned a not known host type, take it as Basic then. |
| | 0 | 2571 | | _flags = (_flags & ~Flags.HostTypeMask) | Flags.BasicHostType; |
| | 0 | 2572 | | } |
| | | 2573 | | else |
| | 0 | 2574 | | { |
| | 0 | 2575 | | host = CreateHostStringHelper(host, 0, host.Length, ref flags, _info); |
| | | 2576 | | |
| | 0 | 2577 | | for (int i = 0; i < host.Length; ++i) |
| | 0 | 2578 | | { |
| | 0 | 2579 | | if ((_info.Offset.Host + i) >= _info.Offset.End || host[i] != _string[_info.Offset.Host + i]) |
| | 0 | 2580 | | { |
| | 0 | 2581 | | _flags |= (Flags.HostNotCanonical | Flags.E_HostNotCanonical); |
| | 0 | 2582 | | break; |
| | | 2583 | | } |
| | 0 | 2584 | | } |
| | 0 | 2585 | | _flags = (_flags & ~Flags.HostTypeMask) | (flags & Flags.HostTypeMask); |
| | 0 | 2586 | | } |
| | 0 | 2587 | | } |
| | | 2588 | | // |
| | | 2589 | | // This is a chance for a custom parser to report a different port value |
| | | 2590 | | // |
| | 0 | 2591 | | string portStr = _syntax.InternalGetComponents(this, UriComponents.StrongPort, UriFormat.UriEscaped); |
| | 0 | 2592 | | int port = 0; |
| | 0 | 2593 | | if (string.IsNullOrEmpty(portStr)) |
| | 0 | 2594 | | { |
| | | 2595 | | // It's like no port |
| | 0 | 2596 | | _flags &= ~Flags.NotDefaultPort; |
| | 0 | 2597 | | _flags |= (Flags.PortNotCanonical | Flags.E_PortNotCanonical); |
| | 0 | 2598 | | _info.Offset.PortValue = 0; |
| | 0 | 2599 | | } |
| | | 2600 | | else |
| | 0 | 2601 | | { |
| | 0 | 2602 | | for (int idx = 0; idx < portStr.Length; ++idx) |
| | 0 | 2603 | | { |
| | 0 | 2604 | | int val = portStr[idx] - '0'; |
| | 0 | 2605 | | if (val < 0 || val > 9 || (port = (port * 10 + val)) > 0xFFFF) |
| | 0 | 2606 | | throw new UriFormatException(SR.Format(SR.net_uri_PortOutOfRange, _syntax.GetType(), portStr)); |
| | 0 | 2607 | | } |
| | 0 | 2608 | | if (port != _info.Offset.PortValue) |
| | 0 | 2609 | | { |
| | 0 | 2610 | | if (port == _syntax.DefaultPort) |
| | 0 | 2611 | | _flags &= ~Flags.NotDefaultPort; |
| | | 2612 | | else |
| | 0 | 2613 | | _flags |= Flags.NotDefaultPort; |
| | | 2614 | | |
| | 0 | 2615 | | _flags |= (Flags.PortNotCanonical | Flags.E_PortNotCanonical); |
| | 0 | 2616 | | _info.Offset.PortValue = (ushort)port; |
| | 0 | 2617 | | } |
| | 0 | 2618 | | } |
| | | 2619 | | // This must be done as the last thing in this method |
| | 0 | 2620 | | _info.Host = host; |
| | 0 | 2621 | | } |
| | | 2622 | | |
| | | 2623 | | // |
| | | 2624 | | // An internal shortcut into Uri extensibility API |
| | | 2625 | | // |
| | | 2626 | | internal string GetParts(UriComponents uriParts, UriFormat formatAs) |
| | 0 | 2627 | | { |
| | 0 | 2628 | | return InternalGetComponents(uriParts, formatAs); |
| | 0 | 2629 | | } |
| | | 2630 | | |
| | | 2631 | | private string GetEscapedParts(UriComponents uriParts) |
| | 0 | 2632 | | { |
| | 0 | 2633 | | Debug.Assert(_info != null && (_flags & Flags.MinimalUriInfoSet) != 0); |
| | | 2634 | | |
| | | 2635 | | // Which Uri parts are not escaped canonically ? |
| | | 2636 | | // Notice that public UriPart and private Flags must be in Sync so below code can work |
| | | 2637 | | // |
| | 0 | 2638 | | ushort nonCanonical = unchecked((ushort)(((ushort)_flags & ((ushort)Flags.CannotDisplayCanonical << 7)) >> 6 |
| | 0 | 2639 | | if (InFact(Flags.SchemeNotCanonical)) |
| | 0 | 2640 | | { |
| | 0 | 2641 | | nonCanonical |= (ushort)Flags.SchemeNotCanonical; |
| | 0 | 2642 | | } |
| | | 2643 | | |
| | | 2644 | | // We keep separate flags for some of path canonicalization facts |
| | 0 | 2645 | | if ((uriParts & UriComponents.Path) != 0) |
| | 0 | 2646 | | { |
| | 0 | 2647 | | if (InFact(Flags.ShouldBeCompressed | Flags.FirstSlashAbsent | Flags.BackslashInPath)) |
| | 0 | 2648 | | { |
| | 0 | 2649 | | nonCanonical |= (ushort)Flags.PathNotCanonical; |
| | 0 | 2650 | | } |
| | 0 | 2651 | | else if (IsDosPath && _string[_info.Offset.Path + SecuredPathIndex - 1] == '|') |
| | 0 | 2652 | | { |
| | | 2653 | | // A rare case of c|\ |
| | 0 | 2654 | | nonCanonical |= (ushort)Flags.PathNotCanonical; |
| | 0 | 2655 | | } |
| | 0 | 2656 | | } |
| | | 2657 | | |
| | 0 | 2658 | | if ((unchecked((ushort)uriParts) & nonCanonical) == 0) |
| | 0 | 2659 | | { |
| | 0 | 2660 | | string? ret = GetUriPartsFromUserString(uriParts); |
| | 0 | 2661 | | if (ret is not null) |
| | 0 | 2662 | | { |
| | 0 | 2663 | | return ret; |
| | | 2664 | | } |
| | 0 | 2665 | | } |
| | | 2666 | | |
| | 0 | 2667 | | return RecreateParts(uriParts, nonCanonical, UriFormat.UriEscaped); |
| | 0 | 2668 | | } |
| | | 2669 | | |
| | | 2670 | | private string GetUnescapedParts(UriComponents uriParts, UriFormat formatAs) |
| | 0 | 2671 | | { |
| | 0 | 2672 | | Debug.Assert(_info != null && (_flags & Flags.MinimalUriInfoSet) != 0); |
| | | 2673 | | |
| | | 2674 | | // Which Uri parts are not escaped canonically ? |
| | | 2675 | | // Notice that public UriComponents and private Uri.Flags must be in Sync so below code can work |
| | | 2676 | | // |
| | 0 | 2677 | | ushort nonCanonical = unchecked((ushort)((ushort)_flags & (ushort)Flags.CannotDisplayCanonical)); |
| | | 2678 | | |
| | | 2679 | | // We keep separate flags for some of path canonicalization facts |
| | 0 | 2680 | | if ((uriParts & UriComponents.Path) != 0) |
| | 0 | 2681 | | { |
| | 0 | 2682 | | if ((_flags & (Flags.ShouldBeCompressed | Flags.FirstSlashAbsent | Flags.BackslashInPath)) != 0) |
| | 0 | 2683 | | { |
| | 0 | 2684 | | nonCanonical |= (ushort)Flags.PathNotCanonical; |
| | 0 | 2685 | | } |
| | 0 | 2686 | | else if (IsDosPath && _string[_info.Offset.Path + SecuredPathIndex - 1] == '|') |
| | 0 | 2687 | | { |
| | | 2688 | | // A rare case of c|\ |
| | 0 | 2689 | | nonCanonical |= (ushort)Flags.PathNotCanonical; |
| | 0 | 2690 | | } |
| | 0 | 2691 | | } |
| | | 2692 | | |
| | 0 | 2693 | | if ((unchecked((ushort)uriParts) & nonCanonical) == 0) |
| | 0 | 2694 | | { |
| | 0 | 2695 | | string? ret = GetUriPartsFromUserString(uriParts); |
| | 0 | 2696 | | if (ret is not null) |
| | 0 | 2697 | | { |
| | 0 | 2698 | | return ret; |
| | | 2699 | | } |
| | 0 | 2700 | | } |
| | | 2701 | | |
| | 0 | 2702 | | return RecreateParts(uriParts, nonCanonical, formatAs); |
| | 0 | 2703 | | } |
| | | 2704 | | |
| | | 2705 | | private string RecreateParts(UriComponents parts, ushort nonCanonical, UriFormat formatAs) |
| | 0 | 2706 | | { |
| | 0 | 2707 | | EnsureHostString(allowDnsOptimization: false); |
| | | 2708 | | |
| | 0 | 2709 | | string str = _string; |
| | | 2710 | | |
| | 0 | 2711 | | var dest = str.Length <= StackallocThreshold |
| | 0 | 2712 | | ? new ValueStringBuilder(stackalloc char[StackallocThreshold]) |
| | 0 | 2713 | | : new ValueStringBuilder(str.Length); |
| | | 2714 | | |
| | 0 | 2715 | | scoped ReadOnlySpan<char> result = RecreateParts(ref dest, str, parts, nonCanonical, formatAs); |
| | | 2716 | | |
| | 0 | 2717 | | string s = result.ToString(); |
| | 0 | 2718 | | dest.Dispose(); |
| | 0 | 2719 | | return s; |
| | 0 | 2720 | | } |
| | | 2721 | | |
| | | 2722 | | private bool TryRecreateParts(scoped Span<char> span, out int charsWritten, UriComponents parts, ushort nonCanon |
| | 0 | 2723 | | { |
| | 0 | 2724 | | EnsureHostString(allowDnsOptimization: false); |
| | | 2725 | | |
| | 0 | 2726 | | string str = _string; |
| | | 2727 | | |
| | 0 | 2728 | | var dest = str.Length <= StackallocThreshold |
| | 0 | 2729 | | ? new ValueStringBuilder(stackalloc char[StackallocThreshold]) |
| | 0 | 2730 | | : new ValueStringBuilder(str.Length); |
| | | 2731 | | |
| | 0 | 2732 | | scoped ReadOnlySpan<char> result = RecreateParts(ref dest, str, parts, nonCanonical, formatAs); |
| | | 2733 | | |
| | 0 | 2734 | | bool copied = result.TryCopyTo(span); |
| | 0 | 2735 | | charsWritten = copied ? result.Length : 0; |
| | 0 | 2736 | | dest.Dispose(); |
| | 0 | 2737 | | return copied; |
| | 0 | 2738 | | } |
| | | 2739 | | |
| | | 2740 | | private ReadOnlySpan<char> RecreateParts(scoped ref ValueStringBuilder dest, string str, UriComponents parts, us |
| | 0 | 2741 | | { |
| | 0 | 2742 | | Debug.Assert(InFact(Flags.AllUriInfoSet)); |
| | | 2743 | | |
| | | 2744 | | //Scheme and slashes |
| | 0 | 2745 | | if ((parts & UriComponents.Scheme) != 0) |
| | 0 | 2746 | | { |
| | 0 | 2747 | | dest.Append(_syntax.SchemeName); |
| | 0 | 2748 | | if (parts != UriComponents.Scheme) |
| | 0 | 2749 | | { |
| | 0 | 2750 | | dest.Append(':'); |
| | 0 | 2751 | | if (InFact(Flags.AuthorityFound)) |
| | 0 | 2752 | | { |
| | 0 | 2753 | | dest.Append('/'); |
| | 0 | 2754 | | dest.Append('/'); |
| | 0 | 2755 | | } |
| | 0 | 2756 | | } |
| | 0 | 2757 | | } |
| | | 2758 | | |
| | | 2759 | | //UserInfo |
| | 0 | 2760 | | if ((parts & UriComponents.UserInfo) != 0 && InFact(Flags.HasUserInfo)) |
| | 0 | 2761 | | { |
| | 0 | 2762 | | ReadOnlySpan<char> slice = str.AsSpan(_info.Offset.User, _info.Offset.Host - _info.Offset.User); |
| | | 2763 | | |
| | 0 | 2764 | | if ((nonCanonical & (ushort)UriComponents.UserInfo) != 0) |
| | 0 | 2765 | | { |
| | 0 | 2766 | | switch (formatAs) |
| | | 2767 | | { |
| | | 2768 | | case UriFormat.UriEscaped: |
| | 0 | 2769 | | if (NotAny(Flags.UserEscaped)) |
| | 0 | 2770 | | { |
| | 0 | 2771 | | UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, UriHelper.Unreserved |
| | 0 | 2772 | | } |
| | | 2773 | | else |
| | 0 | 2774 | | { |
| | | 2775 | | // We would ideally throw here if InFact(Flags.E_UserNotCanonical) but currently just ac |
| | 0 | 2776 | | dest.Append(slice); |
| | 0 | 2777 | | } |
| | 0 | 2778 | | break; |
| | | 2779 | | |
| | | 2780 | | case UriFormat.SafeUnescaped: |
| | 0 | 2781 | | UriHelper.UnescapeString(slice[..^1], |
| | 0 | 2782 | | ref dest, '@', '/', '\\', |
| | 0 | 2783 | | InFact(Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnescape, |
| | 0 | 2784 | | _syntax, isQuery: false); |
| | 0 | 2785 | | dest.Append('@'); |
| | 0 | 2786 | | break; |
| | | 2787 | | |
| | | 2788 | | case UriFormat.Unescaped: |
| | 0 | 2789 | | UriHelper.Unescape(slice, ref dest); |
| | 0 | 2790 | | break; |
| | | 2791 | | |
| | | 2792 | | default: //V1ToStringUnescape |
| | 0 | 2793 | | dest.Append(slice); |
| | 0 | 2794 | | break; |
| | | 2795 | | } |
| | 0 | 2796 | | } |
| | | 2797 | | else |
| | 0 | 2798 | | { |
| | 0 | 2799 | | dest.Append(slice); |
| | 0 | 2800 | | } |
| | | 2801 | | |
| | 0 | 2802 | | if (parts == UriComponents.UserInfo) |
| | 0 | 2803 | | { |
| | | 2804 | | //strip '@' delimiter |
| | 0 | 2805 | | dest.Length--; |
| | 0 | 2806 | | } |
| | 0 | 2807 | | } |
| | | 2808 | | |
| | | 2809 | | // Host |
| | 0 | 2810 | | if ((parts & UriComponents.Host) != 0) |
| | 0 | 2811 | | { |
| | 0 | 2812 | | string host = _info.Host!; |
| | | 2813 | | |
| | 0 | 2814 | | if (host.Length != 0) |
| | 0 | 2815 | | { |
| | 0 | 2816 | | var hostBuilder = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | | 2817 | | |
| | | 2818 | | // NormalizedHost |
| | 0 | 2819 | | if ((parts & UriComponents.NormalizedHost) != 0) |
| | 0 | 2820 | | { |
| | 0 | 2821 | | host = UriHelper.StripBidiControlCharacters(host, host); |
| | | 2822 | | |
| | | 2823 | | // Upconvert any punycode to unicode, xn--pck -> ? |
| | 0 | 2824 | | if (!DomainNameHelper.TryGetUnicodeEquivalent(host, ref hostBuilder)) |
| | 0 | 2825 | | { |
| | 0 | 2826 | | hostBuilder.Length = 0; |
| | 0 | 2827 | | } |
| | 0 | 2828 | | } |
| | | 2829 | | |
| | 0 | 2830 | | ReadOnlySpan<char> hostSlice = hostBuilder.Length == 0 ? host : hostBuilder.AsSpan(); |
| | | 2831 | | |
| | 0 | 2832 | | if (formatAs != UriFormat.UriEscaped && HostType == Flags.BasicHostType && (nonCanonical & (ushort)U |
| | 0 | 2833 | | { |
| | | 2834 | | // only Basic host could be in the escaped form |
| | 0 | 2835 | | if (formatAs == UriFormat.Unescaped) |
| | 0 | 2836 | | { |
| | 0 | 2837 | | UriHelper.Unescape(hostSlice, ref dest); |
| | 0 | 2838 | | } |
| | | 2839 | | else |
| | 0 | 2840 | | { |
| | 0 | 2841 | | UriHelper.UnescapeString(hostSlice, |
| | 0 | 2842 | | ref dest, '/', '?', '#', |
| | 0 | 2843 | | InFact(Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnescape, |
| | 0 | 2844 | | _syntax, isQuery: false); |
| | 0 | 2845 | | } |
| | 0 | 2846 | | } |
| | | 2847 | | else |
| | 0 | 2848 | | { |
| | 0 | 2849 | | dest.Append(hostSlice); |
| | 0 | 2850 | | } |
| | | 2851 | | |
| | 0 | 2852 | | hostBuilder.Dispose(); |
| | | 2853 | | |
| | | 2854 | | // A fix up only for SerializationInfo and IpV6 host with a scopeID |
| | 0 | 2855 | | if ((parts & UriComponents.SerializationInfoString) != 0 && HostType == Flags.IPv6HostType && _info. |
| | 0 | 2856 | | { |
| | 0 | 2857 | | dest.Length--; |
| | 0 | 2858 | | dest.Append(scopeId); |
| | 0 | 2859 | | dest.Append(']'); |
| | 0 | 2860 | | } |
| | 0 | 2861 | | } |
| | 0 | 2862 | | } |
| | | 2863 | | |
| | | 2864 | | //Port (always wants a ':' delimiter if got to this method) |
| | 0 | 2865 | | if ((parts & UriComponents.Port) != 0 && |
| | 0 | 2866 | | (InFact(Flags.NotDefaultPort) || ((parts & UriComponents.StrongPort) != 0 && _syntax.DefaultPort != UriP |
| | 0 | 2867 | | { |
| | 0 | 2868 | | dest.Append(':'); |
| | | 2869 | | |
| | | 2870 | | const int MaxUshortLength = 5; |
| | 0 | 2871 | | bool success = _info.Offset.PortValue.TryFormat(dest.AppendSpan(MaxUshortLength), out int charsWritten); |
| | 0 | 2872 | | Debug.Assert(success); |
| | 0 | 2873 | | dest.Length -= MaxUshortLength - charsWritten; |
| | 0 | 2874 | | } |
| | | 2875 | | |
| | | 2876 | | //Path |
| | 0 | 2877 | | if ((parts & UriComponents.Path) != 0) |
| | 0 | 2878 | | { |
| | 0 | 2879 | | GetCanonicalPath(ref dest, formatAs); |
| | | 2880 | | |
| | | 2881 | | // (possibly strip the leading '/' delimiter) |
| | 0 | 2882 | | if (parts == UriComponents.Path) |
| | 0 | 2883 | | { |
| | | 2884 | | int offset; |
| | 0 | 2885 | | if (InFact(Flags.AuthorityFound) && dest.Length != 0 && dest[0] == '/') |
| | 0 | 2886 | | { |
| | 0 | 2887 | | offset = 1; |
| | 0 | 2888 | | } |
| | | 2889 | | else |
| | 0 | 2890 | | { |
| | 0 | 2891 | | offset = 0; |
| | 0 | 2892 | | } |
| | | 2893 | | |
| | 0 | 2894 | | return dest.AsSpan(offset); |
| | | 2895 | | } |
| | 0 | 2896 | | } |
| | | 2897 | | |
| | | 2898 | | //Query (possibly strip the '?' delimiter) |
| | 0 | 2899 | | if ((parts & UriComponents.Query) != 0 && _info.Offset.Query < _info.Offset.Fragment) |
| | 0 | 2900 | | { |
| | 0 | 2901 | | int offset = (_info.Offset.Query + 1); |
| | 0 | 2902 | | if (parts != UriComponents.Query) |
| | 0 | 2903 | | dest.Append('?'); |
| | | 2904 | | |
| | 0 | 2905 | | FormatQueryOrFragment(str.AsSpan(offset, _info.Offset.Fragment - offset), ref dest, nonCanonical, format |
| | 0 | 2906 | | } |
| | | 2907 | | |
| | | 2908 | | //Fragment (possibly strip the '#' delimiter) |
| | 0 | 2909 | | if ((parts & UriComponents.Fragment) != 0 && _info.Offset.Fragment < _info.Offset.End) |
| | 0 | 2910 | | { |
| | 0 | 2911 | | int offset = _info.Offset.Fragment + 1; |
| | 0 | 2912 | | if (parts != UriComponents.Fragment) |
| | 0 | 2913 | | dest.Append('#'); |
| | | 2914 | | |
| | 0 | 2915 | | FormatQueryOrFragment(str.AsSpan(offset, _info.Offset.End - offset), ref dest, nonCanonical, formatAs, i |
| | 0 | 2916 | | } |
| | | 2917 | | |
| | 0 | 2918 | | return dest.AsSpan(); |
| | | 2919 | | |
| | | 2920 | | void FormatQueryOrFragment(ReadOnlySpan<char> slice, ref ValueStringBuilder dest, ushort nonCanonical, UriFo |
| | 0 | 2921 | | { |
| | 0 | 2922 | | if ((nonCanonical & (ushort)(isQuery ? UriComponents.Query : UriComponents.Fragment)) == 0) |
| | 0 | 2923 | | { |
| | 0 | 2924 | | dest.Append(slice); |
| | 0 | 2925 | | } |
| | | 2926 | | else |
| | 0 | 2927 | | { |
| | 0 | 2928 | | if (formatAs == UriFormat.UriEscaped) |
| | 0 | 2929 | | { |
| | 0 | 2930 | | if (NotAny(Flags.UserEscaped)) |
| | 0 | 2931 | | { |
| | 0 | 2932 | | UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, isQuery ? UriHelper.Unre |
| | 0 | 2933 | | } |
| | | 2934 | | else |
| | 0 | 2935 | | { |
| | 0 | 2936 | | dest.Append(slice); |
| | 0 | 2937 | | } |
| | 0 | 2938 | | } |
| | 0 | 2939 | | else if (formatAs == UriFormat.Unescaped) |
| | 0 | 2940 | | { |
| | 0 | 2941 | | UriHelper.Unescape(slice, ref dest); |
| | 0 | 2942 | | } |
| | | 2943 | | else |
| | 0 | 2944 | | { |
| | 0 | 2945 | | UnescapeMode mode = InFact(Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnesc |
| | | 2946 | | |
| | 0 | 2947 | | if (formatAs == V1ToStringUnescape) |
| | 0 | 2948 | | { |
| | 0 | 2949 | | mode |= UnescapeMode.V1ToStringFlag; |
| | 0 | 2950 | | } |
| | | 2951 | | |
| | 0 | 2952 | | UriHelper.UnescapeString(slice, |
| | 0 | 2953 | | ref dest, '#', c_DummyChar, c_DummyChar, |
| | 0 | 2954 | | mode, _syntax, isQuery); |
| | 0 | 2955 | | } |
| | 0 | 2956 | | } |
| | 0 | 2957 | | } |
| | 0 | 2958 | | } |
| | | 2959 | | |
| | | 2960 | | // |
| | | 2961 | | // This method is called only if the user string has a canonical representation |
| | | 2962 | | // of requested parts |
| | | 2963 | | // |
| | | 2964 | | private string? GetUriPartsFromUserString(UriComponents uriParts) |
| | 0 | 2965 | | { |
| | | 2966 | | int delimiterAwareIdx; |
| | | 2967 | | |
| | 0 | 2968 | | switch (uriParts & ~UriComponents.KeepDelimiter) |
| | | 2969 | | { |
| | | 2970 | | // For FindServicePoint perf |
| | | 2971 | | case UriComponents.Scheme | UriComponents.Host | UriComponents.Port: |
| | 0 | 2972 | | if (!InFact(Flags.HasUserInfo)) |
| | 0 | 2973 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.Path - _info.Offset.Scheme); |
| | | 2974 | | |
| | 0 | 2975 | | return string.Concat( |
| | 0 | 2976 | | _string.AsSpan(_info.Offset.Scheme, _info.Offset.User - _info.Offset.Scheme), |
| | 0 | 2977 | | _string.AsSpan(_info.Offset.Host, _info.Offset.Path - _info.Offset.Host)); |
| | | 2978 | | |
| | | 2979 | | // For HttpWebRequest.ConnectHostAndPort perf |
| | | 2980 | | case UriComponents.HostAndPort: //Host|StrongPort |
| | | 2981 | | |
| | 0 | 2982 | | if (!InFact(Flags.HasUserInfo)) |
| | 0 | 2983 | | goto case UriComponents.StrongAuthority; |
| | | 2984 | | |
| | 0 | 2985 | | if (InFact(Flags.NotDefaultPort) || _syntax.DefaultPort == UriParser.NoDefaultPort) |
| | 0 | 2986 | | return _string.Substring(_info.Offset.Host, _info.Offset.Path - _info.Offset.Host); |
| | | 2987 | | |
| | 0 | 2988 | | return string.Concat(_string.AsSpan(_info.Offset.Host, _info.Offset.Path - _info.Offset.Host), |
| | 0 | 2989 | | ":", _info.Offset.PortValue.ToString(CultureInfo.InvariantCulture)); |
| | | 2990 | | |
| | | 2991 | | // For an obvious common case perf |
| | | 2992 | | case UriComponents.AbsoluteUri: //Scheme|UserInfo|Host|Port|Path|Query|Fragment, |
| | 0 | 2993 | | if (_info.Offset.Scheme == 0 && _info.Offset.End == _string.Length) |
| | 0 | 2994 | | return _string; |
| | | 2995 | | |
| | 0 | 2996 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.End - _info.Offset.Scheme); |
| | | 2997 | | |
| | | 2998 | | // For Uri.Equals() and HttpWebRequest through a proxy perf |
| | | 2999 | | case UriComponents.HttpRequestUrl: //Scheme|Host|Port|Path|Query, |
| | 0 | 3000 | | if (InFact(Flags.HasUserInfo)) |
| | 0 | 3001 | | { |
| | 0 | 3002 | | return string.Concat( |
| | 0 | 3003 | | _string.AsSpan(_info.Offset.Scheme, _info.Offset.User - _info.Offset.Scheme), |
| | 0 | 3004 | | _string.AsSpan(_info.Offset.Host, _info.Offset.Fragment - _info.Offset.Host)); |
| | | 3005 | | } |
| | 0 | 3006 | | if (_info.Offset.Scheme == 0 && _info.Offset.Fragment == _string.Length) |
| | 0 | 3007 | | return _string; |
| | | 3008 | | |
| | 0 | 3009 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.Fragment - _info.Offset.Scheme); |
| | | 3010 | | |
| | | 3011 | | // For CombineUri() perf |
| | | 3012 | | case UriComponents.SchemeAndServer | UriComponents.UserInfo: |
| | 0 | 3013 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.Path - _info.Offset.Scheme); |
| | | 3014 | | |
| | | 3015 | | // For Cache perf |
| | | 3016 | | case (UriComponents.AbsoluteUri & ~UriComponents.Fragment): |
| | 0 | 3017 | | if (_info.Offset.Scheme == 0 && _info.Offset.Fragment == _string.Length) |
| | 0 | 3018 | | return _string; |
| | | 3019 | | |
| | 0 | 3020 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.Fragment - _info.Offset.Scheme); |
| | | 3021 | | |
| | | 3022 | | |
| | | 3023 | | // Strip scheme delimiter if was not requested |
| | | 3024 | | case UriComponents.Scheme: |
| | 0 | 3025 | | if (uriParts != UriComponents.Scheme) |
| | 0 | 3026 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.User - _info.Offset.Scheme); |
| | | 3027 | | |
| | 0 | 3028 | | return _syntax.SchemeName; |
| | | 3029 | | |
| | | 3030 | | // KeepDelimiter makes no sense for this component |
| | | 3031 | | case UriComponents.Host: |
| | 0 | 3032 | | int idx = _info.Offset.Path; |
| | 0 | 3033 | | if (InFact(Flags.NotDefaultPort | Flags.PortNotCanonical)) |
| | 0 | 3034 | | { |
| | | 3035 | | //Means we do have ':' after the host |
| | 0 | 3036 | | while (_string[--idx] != ':') |
| | 0 | 3037 | | ; |
| | 0 | 3038 | | } |
| | 0 | 3039 | | return (idx - _info.Offset.Host == 0) ? string.Empty : _string.Substring(_info.Offset.Host, |
| | 0 | 3040 | | idx - _info.Offset.Host); |
| | | 3041 | | |
| | | 3042 | | case UriComponents.Path: |
| | | 3043 | | |
| | | 3044 | | // Strip the leading '/' for a hierarchical URI if no delimiter was requested |
| | 0 | 3045 | | if (uriParts == UriComponents.Path && InFact(Flags.AuthorityFound) && |
| | 0 | 3046 | | _info.Offset.End > _info.Offset.Path && _string[_info.Offset.Path] == '/') |
| | 0 | 3047 | | delimiterAwareIdx = _info.Offset.Path + 1; |
| | | 3048 | | else |
| | 0 | 3049 | | delimiterAwareIdx = _info.Offset.Path; |
| | | 3050 | | |
| | 0 | 3051 | | if (delimiterAwareIdx >= _info.Offset.Query) |
| | 0 | 3052 | | return string.Empty; |
| | | 3053 | | |
| | | 3054 | | |
| | 0 | 3055 | | return _string.Substring(delimiterAwareIdx, _info.Offset.Query - delimiterAwareIdx); |
| | | 3056 | | |
| | | 3057 | | case UriComponents.Query: |
| | | 3058 | | // Strip the '?' if no delimiter was requested |
| | 0 | 3059 | | if (uriParts == UriComponents.Query) |
| | 0 | 3060 | | delimiterAwareIdx = _info.Offset.Query + 1; |
| | | 3061 | | else |
| | 0 | 3062 | | delimiterAwareIdx = _info.Offset.Query; |
| | | 3063 | | |
| | 0 | 3064 | | if (delimiterAwareIdx >= _info.Offset.Fragment) |
| | 0 | 3065 | | return string.Empty; |
| | | 3066 | | |
| | 0 | 3067 | | return _string.Substring(delimiterAwareIdx, _info.Offset.Fragment - delimiterAwareIdx); |
| | | 3068 | | |
| | | 3069 | | case UriComponents.Fragment: |
| | | 3070 | | // Strip the '#' if no delimiter was requested |
| | 0 | 3071 | | if (uriParts == UriComponents.Fragment) |
| | 0 | 3072 | | delimiterAwareIdx = _info.Offset.Fragment + 1; |
| | | 3073 | | else |
| | 0 | 3074 | | delimiterAwareIdx = _info.Offset.Fragment; |
| | | 3075 | | |
| | 0 | 3076 | | if (delimiterAwareIdx >= _info.Offset.End) |
| | 0 | 3077 | | return string.Empty; |
| | | 3078 | | |
| | 0 | 3079 | | return _string.Substring(delimiterAwareIdx, _info.Offset.End - delimiterAwareIdx); |
| | | 3080 | | |
| | | 3081 | | case UriComponents.UserInfo | UriComponents.Host | UriComponents.Port: |
| | 0 | 3082 | | return (_info.Offset.Path - _info.Offset.User == 0) ? string.Empty : |
| | 0 | 3083 | | _string.Substring(_info.Offset.User, _info.Offset.Path - _info.Offset.User); |
| | | 3084 | | |
| | | 3085 | | case UriComponents.StrongAuthority: //UserInfo|Host|StrongPort |
| | 0 | 3086 | | if (InFact(Flags.NotDefaultPort) || _syntax.DefaultPort == UriParser.NoDefaultPort) |
| | 0 | 3087 | | goto case UriComponents.UserInfo | UriComponents.Host | UriComponents.Port; |
| | | 3088 | | |
| | 0 | 3089 | | return string.Concat(_string.AsSpan(_info.Offset.User, _info.Offset.Path - _info.Offset.User), |
| | 0 | 3090 | | ":", _info.Offset.PortValue.ToString(CultureInfo.InvariantCulture)); |
| | | 3091 | | |
| | | 3092 | | case UriComponents.PathAndQuery: //Path|Query, |
| | 0 | 3093 | | return _string.Substring(_info.Offset.Path, _info.Offset.Fragment - _info.Offset.Path); |
| | | 3094 | | |
| | | 3095 | | case UriComponents.HttpRequestUrl | UriComponents.Fragment: //Scheme|Host|Port|Path|Query|Fragment, |
| | 0 | 3096 | | if (InFact(Flags.HasUserInfo)) |
| | 0 | 3097 | | { |
| | 0 | 3098 | | return string.Concat( |
| | 0 | 3099 | | _string.AsSpan(_info.Offset.Scheme, _info.Offset.User - _info.Offset.Scheme), |
| | 0 | 3100 | | _string.AsSpan(_info.Offset.Host, _info.Offset.End - _info.Offset.Host)); |
| | | 3101 | | } |
| | 0 | 3102 | | if (_info.Offset.Scheme == 0 && _info.Offset.End == _string.Length) |
| | 0 | 3103 | | return _string; |
| | | 3104 | | |
| | 0 | 3105 | | return _string.Substring(_info.Offset.Scheme, _info.Offset.End - _info.Offset.Scheme); |
| | | 3106 | | |
| | | 3107 | | case UriComponents.PathAndQuery | UriComponents.Fragment: //LocalUrl|Fragment |
| | 0 | 3108 | | return _string.Substring(_info.Offset.Path, _info.Offset.End - _info.Offset.Path); |
| | | 3109 | | |
| | | 3110 | | case UriComponents.UserInfo: |
| | | 3111 | | // Strip the '@' if no delimiter was requested |
| | | 3112 | | |
| | 0 | 3113 | | if (NotAny(Flags.HasUserInfo)) |
| | 0 | 3114 | | return string.Empty; |
| | | 3115 | | |
| | 0 | 3116 | | if (uriParts == UriComponents.UserInfo) |
| | 0 | 3117 | | delimiterAwareIdx = _info.Offset.Host - 1; |
| | | 3118 | | else |
| | 0 | 3119 | | delimiterAwareIdx = _info.Offset.Host; |
| | | 3120 | | |
| | 0 | 3121 | | if (_info.Offset.User >= delimiterAwareIdx) |
| | 0 | 3122 | | return string.Empty; |
| | | 3123 | | |
| | 0 | 3124 | | return _string.Substring(_info.Offset.User, delimiterAwareIdx - _info.Offset.User); |
| | | 3125 | | |
| | | 3126 | | default: |
| | 0 | 3127 | | return null; |
| | | 3128 | | } |
| | 0 | 3129 | | } |
| | | 3130 | | |
| | | 3131 | | // |
| | | 3132 | | //This method does: |
| | | 3133 | | // - Creates _info member |
| | | 3134 | | // - checks all components up to path on their canonical representation |
| | | 3135 | | // - continues parsing starting the path position |
| | | 3136 | | // - Sets the offsets of remaining components |
| | | 3137 | | // - Sets the Canonicalization flags if applied |
| | | 3138 | | // - Will NOT create MoreInfo members |
| | | 3139 | | // |
| | | 3140 | | private void ParseRemaining() |
| | 0 | 3141 | | { |
| | | 3142 | | // ensure we parsed up to the path |
| | 0 | 3143 | | EnsureUriInfo(); |
| | | 3144 | | |
| | 0 | 3145 | | Flags cF = _flags | Flags.AllUriInfoSet; |
| | | 3146 | | |
| | 0 | 3147 | | if ((cF & Flags.UserDrivenParsing) != 0) |
| | 0 | 3148 | | goto Done; |
| | | 3149 | | |
| | | 3150 | | // _info.Offset values may be set concurrently, but that's okay since this parsing logic is idempotent. |
| | | 3151 | | // We use a local copy of changed flags (cF) and merge any updates into _flags atomically at the end. |
| | | 3152 | | |
| | 0 | 3153 | | if ((cF & Flags.HasUnicode) != 0) |
| | 0 | 3154 | | { |
| | 0 | 3155 | | ParseRemaining_RecreateNormalizedString(); |
| | 0 | 3156 | | } |
| | | 3157 | | |
| | 0 | 3158 | | string inputStr = _string; |
| | | 3159 | | |
| | | 3160 | | // Skip trailing whitespace |
| | 0 | 3161 | | int length = inputStr.Length; |
| | | 3162 | | |
| | 0 | 3163 | | while ((uint)(length - 1) < (uint)inputStr.Length && UriHelper.IsLWS(inputStr[length - 1])) |
| | 0 | 3164 | | { |
| | 0 | 3165 | | length--; |
| | 0 | 3166 | | } |
| | | 3167 | | |
| | 0 | 3168 | | if ((cF & Flags.ImplicitFile) != 0) |
| | 0 | 3169 | | { |
| | 0 | 3170 | | cF |= Flags.SchemeNotCanonical; |
| | 0 | 3171 | | } |
| | | 3172 | | else |
| | 0 | 3173 | | { |
| | 0 | 3174 | | ReadOnlySpan<char> str = inputStr.AsSpan(_info.Offset.Scheme); |
| | | 3175 | | |
| | 0 | 3176 | | Debug.Assert(str.StartsWith(_syntax.SchemeName, StringComparison.OrdinalIgnoreCase)); |
| | 0 | 3177 | | Debug.Assert(str[_syntax.SchemeName.Length] == ':'); |
| | | 3178 | | |
| | 0 | 3179 | | int schemeLength = _syntax.SchemeName.Length; |
| | | 3180 | | |
| | | 3181 | | // Check that the scheme casing matches the canonical one (lowercase). |
| | | 3182 | | // Using a manual loop instead of ContainsAnyInRange because the expected length is small. |
| | 0 | 3183 | | foreach (char c in str.Slice(0, schemeLength)) |
| | 0 | 3184 | | { |
| | 0 | 3185 | | if (char.IsAsciiLetterUpper(c)) |
| | 0 | 3186 | | { |
| | 0 | 3187 | | cF |= Flags.SchemeNotCanonical; |
| | 0 | 3188 | | break; |
| | | 3189 | | } |
| | 0 | 3190 | | } |
| | | 3191 | | |
| | | 3192 | | // For an authority Uri only // after the scheme would be canonical |
| | | 3193 | | // (for compatibility with: http:\\host) |
| | 0 | 3194 | | if ((cF & Flags.AuthorityFound) != 0 && !str.Slice(schemeLength).StartsWith("://", StringComparison.Ordi |
| | 0 | 3195 | | { |
| | 0 | 3196 | | cF |= Flags.SchemeNotCanonical | Flags.SchemeNotCanonical_NoTrailingSlashes; |
| | 0 | 3197 | | } |
| | 0 | 3198 | | } |
| | | 3199 | | |
| | 0 | 3200 | | UriSyntaxFlags syntaxFlags = _syntax.Flags; |
| | 0 | 3201 | | bool iriParsing = (syntaxFlags & UriSyntaxFlags.AllowIriParsing) != 0; |
| | | 3202 | | |
| | | 3203 | | // Check the form of the user info |
| | 0 | 3204 | | if ((cF & Flags.HasUserInfo) != 0) |
| | 0 | 3205 | | { |
| | 0 | 3206 | | int userStart = _info.Offset.User; |
| | 0 | 3207 | | Check result = CheckCanonical(inputStr.AsSpan(userStart, _info.Offset.Host - userStart), '@', out _); |
| | | 3208 | | |
| | 0 | 3209 | | if ((result & Check.DisplayCanonical) == 0) |
| | 0 | 3210 | | { |
| | 0 | 3211 | | cF |= Flags.UserNotCanonical; |
| | 0 | 3212 | | } |
| | 0 | 3213 | | if ((result & (Check.EscapedCanonical | Check.BackslashInPath)) != Check.EscapedCanonical) |
| | 0 | 3214 | | { |
| | 0 | 3215 | | cF |= Flags.E_UserNotCanonical; |
| | 0 | 3216 | | } |
| | 0 | 3217 | | if (iriParsing && ((result & (Check.DisplayCanonical | Check.EscapedCanonical | Check.BackslashInPath |
| | 0 | 3218 | | | Check.FoundNonAscii | Check.NotIriCanonical)) |
| | 0 | 3219 | | == (Check.DisplayCanonical | Check.FoundNonAscii))) |
| | 0 | 3220 | | { |
| | 0 | 3221 | | cF |= Flags.UserIriCanonical; |
| | 0 | 3222 | | } |
| | 0 | 3223 | | } |
| | | 3224 | | |
| | | 3225 | | // Delay canonical Host checking to avoid creation of a host string. |
| | | 3226 | | // We will do that on demand. |
| | | 3227 | | // We have already checked on the port in EnsureUriInfo() that calls CreateUriInfo. |
| | | 3228 | | |
| | | 3229 | | // Parsing the Path if any. |
| | | 3230 | | // For iri parsing if we found unicode the idx has offset into _originalUnicodeString, |
| | | 3231 | | // so restart parsing from there and make _info.Offset.Path as _string.Length |
| | | 3232 | | |
| | | 3233 | | // If the user explicitly disabled canonicalization, only figure out the offsets |
| | 0 | 3234 | | if ((cF & Flags.DisablePathAndQueryCanonicalization) != 0) |
| | 0 | 3235 | | { |
| | | 3236 | | int queryStart; |
| | 0 | 3237 | | if ((cF & Flags.ImplicitFile) != 0 || (syntaxFlags & UriSyntaxFlags.MayHaveQuery) == 0) |
| | 0 | 3238 | | { |
| | 0 | 3239 | | queryStart = inputStr.Length; |
| | 0 | 3240 | | } |
| | | 3241 | | else |
| | 0 | 3242 | | { |
| | 0 | 3243 | | queryStart = inputStr.IndexOf('?'); |
| | 0 | 3244 | | if (queryStart < 0) |
| | 0 | 3245 | | { |
| | 0 | 3246 | | queryStart = inputStr.Length; |
| | 0 | 3247 | | } |
| | 0 | 3248 | | } |
| | | 3249 | | |
| | 0 | 3250 | | _info.Offset.Query = queryStart; |
| | 0 | 3251 | | _info.Offset.Fragment = inputStr.Length; // There is no fragment in DisablePathAndQueryCanonicalization |
| | 0 | 3252 | | _info.Offset.End = inputStr.Length; |
| | | 3253 | | |
| | 0 | 3254 | | goto Done; |
| | | 3255 | | } |
| | | 3256 | | |
| | 0 | 3257 | | int idx = _info.Offset.Path; |
| | | 3258 | | |
| | | 3259 | | //Some uris do not have a query |
| | | 3260 | | // When '?' is passed as delimiter, then it's special case |
| | | 3261 | | // so both '?' and '#' will work as delimiters |
| | 0 | 3262 | | { |
| | 0 | 3263 | | char delim = |
| | 0 | 3264 | | (cF & Flags.ImplicitFile) != 0 || (syntaxFlags & (UriSyntaxFlags.MayHaveQuery | UriSyntaxFlags.MayHa |
| | 0 | 3265 | | ((syntaxFlags & UriSyntaxFlags.MayHaveQuery) != 0) ? '?' : '#'; |
| | | 3266 | | |
| | 0 | 3267 | | Check result = CheckCanonical(inputStr.AsSpan(idx, length - idx), delim, out int checkedChars); |
| | 0 | 3268 | | idx += checkedChars; |
| | | 3269 | | |
| | | 3270 | | // ATTN: |
| | | 3271 | | // This may render problems for unknown schemes, but in general for an authority based Uri |
| | | 3272 | | // (that has slashes) a path should start with "/" |
| | | 3273 | | // This becomes more interesting knowing how a file uri is used in "file://c:/path" |
| | | 3274 | | // It will be converted to file:///c:/path |
| | | 3275 | | // |
| | | 3276 | | // However, even more interesting is that vsmacros://c:\path will not add the third slash in the _canoic |
| | | 3277 | | // |
| | | 3278 | | // We use special syntax flag to check if the path is rooted, i.e. has a first slash |
| | | 3279 | | // |
| | 0 | 3280 | | if (((cF & Flags.AuthorityFound) != 0) && ((syntaxFlags & UriSyntaxFlags.PathIsRooted) != 0) |
| | 0 | 3281 | | && (_info.Offset.Path == length || inputStr[_info.Offset.Path] is not ('/' or '\\'))) |
| | 0 | 3282 | | { |
| | 0 | 3283 | | cF |= Flags.FirstSlashAbsent; |
| | 0 | 3284 | | } |
| | | 3285 | | |
| | | 3286 | | // Check the need for compression or backslashes conversion |
| | | 3287 | | // we included IsDosPath since it may come with other than FILE uri, for ex. scheme://C:\path |
| | | 3288 | | // (This is very unfortunate that the original design has included that feature) |
| | 0 | 3289 | | bool nonCanonical = false; |
| | 0 | 3290 | | if ((cF & Flags.DosPath) != 0 || (((cF & Flags.AuthorityFound) != 0) && |
| | 0 | 3291 | | (((syntaxFlags & (UriSyntaxFlags.CompressPath | UriSyntaxFlags.ConvertPathSlashes)) != 0) || |
| | 0 | 3292 | | (syntaxFlags & UriSyntaxFlags.UnEscapeDotsAndSlashes) != 0))) |
| | 0 | 3293 | | { |
| | 0 | 3294 | | if (((result & Check.DotSlashEscaped) != 0) && (syntaxFlags & UriSyntaxFlags.UnEscapeDotsAndSlashes) |
| | 0 | 3295 | | { |
| | 0 | 3296 | | cF |= (Flags.E_PathNotCanonical | Flags.PathNotCanonical); |
| | 0 | 3297 | | nonCanonical = true; |
| | 0 | 3298 | | } |
| | | 3299 | | |
| | 0 | 3300 | | if (((syntaxFlags & (UriSyntaxFlags.ConvertPathSlashes)) != 0) && (result & Check.BackslashInPath) ! |
| | 0 | 3301 | | { |
| | 0 | 3302 | | cF |= (Flags.E_PathNotCanonical | Flags.PathNotCanonical); |
| | 0 | 3303 | | nonCanonical = true; |
| | 0 | 3304 | | } |
| | | 3305 | | |
| | 0 | 3306 | | if (((syntaxFlags & (UriSyntaxFlags.CompressPath)) != 0) && ((cF & Flags.E_PathNotCanonical) != 0 || |
| | 0 | 3307 | | (result & Check.DotSlashAttn) != 0)) |
| | 0 | 3308 | | { |
| | 0 | 3309 | | cF |= Flags.ShouldBeCompressed; |
| | 0 | 3310 | | } |
| | | 3311 | | |
| | 0 | 3312 | | if ((result & Check.BackslashInPath) != 0) |
| | 0 | 3313 | | cF |= Flags.BackslashInPath; |
| | 0 | 3314 | | } |
| | 0 | 3315 | | else if ((result & Check.BackslashInPath) != 0) |
| | 0 | 3316 | | { |
| | | 3317 | | // for a "generic" path '\' should be escaped |
| | 0 | 3318 | | cF |= Flags.E_PathNotCanonical; |
| | 0 | 3319 | | nonCanonical = true; |
| | 0 | 3320 | | } |
| | | 3321 | | |
| | 0 | 3322 | | if ((result & Check.DisplayCanonical) == 0) |
| | 0 | 3323 | | { |
| | | 3324 | | // For implicit file the user string is usually in perfect display format, |
| | | 3325 | | // Hence, ignoring complains from CheckCanonical() |
| | | 3326 | | // V1 compat. In fact we should simply ignore dontEscape parameter for Implicit file. |
| | | 3327 | | // Currently we don't. |
| | 0 | 3328 | | if (((cF & Flags.ImplicitFile) == 0) || ((cF & Flags.UserEscaped) != 0) || |
| | 0 | 3329 | | (result & Check.ReservedFound) != 0) |
| | 0 | 3330 | | { |
| | | 3331 | | //means it's found as escaped or has unescaped Reserved Characters |
| | 0 | 3332 | | cF |= Flags.PathNotCanonical; |
| | 0 | 3333 | | nonCanonical = true; |
| | 0 | 3334 | | } |
| | 0 | 3335 | | } |
| | | 3336 | | |
| | 0 | 3337 | | if (((cF & Flags.ImplicitFile) != 0) && (result & (Check.ReservedFound | Check.EscapedCanonical)) != 0) |
| | 0 | 3338 | | { |
| | | 3339 | | // need to escape reserved chars or re-escape '%' if an "escaped sequence" was found |
| | 0 | 3340 | | result &= ~Check.EscapedCanonical; |
| | 0 | 3341 | | } |
| | | 3342 | | |
| | 0 | 3343 | | if ((result & Check.EscapedCanonical) == 0) |
| | 0 | 3344 | | { |
| | | 3345 | | //means it's found as not completely escaped |
| | 0 | 3346 | | cF |= Flags.E_PathNotCanonical; |
| | 0 | 3347 | | } |
| | | 3348 | | |
| | 0 | 3349 | | if (iriParsing && !nonCanonical && ((result & (Check.DisplayCanonical | Check.EscapedCanonical |
| | 0 | 3350 | | | Check.FoundNonAscii | Check.NotIriCanonical)) |
| | 0 | 3351 | | == (Check.DisplayCanonical | Check.FoundNonAscii))) |
| | 0 | 3352 | | { |
| | 0 | 3353 | | cF |= Flags.PathIriCanonical; |
| | 0 | 3354 | | } |
| | 0 | 3355 | | } |
| | | 3356 | | |
| | | 3357 | | // Now we've got to parse the Query if any. Note that Query requires the presence of '?' |
| | 0 | 3358 | | _info.Offset.Query = idx; |
| | | 3359 | | |
| | 0 | 3360 | | if ((uint)idx < (uint)inputStr.Length && inputStr[idx] == '?') |
| | 0 | 3361 | | { |
| | 0 | 3362 | | idx++; // This is to exclude first '?' character from checking |
| | 0 | 3363 | | Check result = CheckCanonical(inputStr.AsSpan(idx, length - idx), (syntaxFlags & (UriSyntaxFlags.MayHave |
| | 0 | 3364 | | idx += charsChecked; |
| | | 3365 | | |
| | 0 | 3366 | | if ((result & Check.DisplayCanonical) == 0) |
| | 0 | 3367 | | { |
| | 0 | 3368 | | cF |= Flags.QueryNotCanonical; |
| | 0 | 3369 | | } |
| | | 3370 | | |
| | 0 | 3371 | | if ((result & (Check.EscapedCanonical | Check.BackslashInPath)) != Check.EscapedCanonical) |
| | 0 | 3372 | | { |
| | 0 | 3373 | | cF |= Flags.E_QueryNotCanonical; |
| | 0 | 3374 | | } |
| | | 3375 | | |
| | 0 | 3376 | | if (iriParsing && ((result & (Check.DisplayCanonical | Check.EscapedCanonical | Check.BackslashInPath |
| | 0 | 3377 | | | Check.FoundNonAscii | Check.NotIriCanonical)) |
| | 0 | 3378 | | == (Check.DisplayCanonical | Check.FoundNonAscii))) |
| | 0 | 3379 | | { |
| | 0 | 3380 | | cF |= Flags.QueryIriCanonical; |
| | 0 | 3381 | | } |
| | 0 | 3382 | | } |
| | | 3383 | | |
| | | 3384 | | // Now we've got to parse the Fragment if any. Note that Fragment requires the presence of '#' |
| | 0 | 3385 | | _info.Offset.Fragment = idx; |
| | | 3386 | | |
| | 0 | 3387 | | if ((uint)idx < (uint)inputStr.Length && inputStr[idx] == '#') |
| | 0 | 3388 | | { |
| | 0 | 3389 | | idx++; // This is to exclude first '#' character from checking |
| | | 3390 | | // We don't using c_DummyChar since want to allow '?' and '#' as unescaped |
| | 0 | 3391 | | Check result = CheckCanonical(inputStr.AsSpan(idx, length - idx), c_EOL, out int checkedChars); |
| | 0 | 3392 | | idx += checkedChars; |
| | | 3393 | | |
| | 0 | 3394 | | if ((result & Check.DisplayCanonical) == 0) |
| | 0 | 3395 | | { |
| | 0 | 3396 | | cF |= Flags.FragmentNotCanonical; |
| | 0 | 3397 | | } |
| | | 3398 | | |
| | 0 | 3399 | | if ((result & (Check.EscapedCanonical | Check.BackslashInPath)) != Check.EscapedCanonical) |
| | 0 | 3400 | | { |
| | 0 | 3401 | | cF |= Flags.E_FragmentNotCanonical; |
| | 0 | 3402 | | } |
| | | 3403 | | |
| | 0 | 3404 | | if (iriParsing && ((result & (Check.DisplayCanonical | Check.EscapedCanonical | Check.BackslashInPath |
| | 0 | 3405 | | | Check.FoundNonAscii | Check.NotIriCanonical)) |
| | 0 | 3406 | | == (Check.DisplayCanonical | Check.FoundNonAscii))) |
| | 0 | 3407 | | { |
| | 0 | 3408 | | cF |= Flags.FragmentIriCanonical; |
| | 0 | 3409 | | } |
| | 0 | 3410 | | } |
| | | 3411 | | |
| | 0 | 3412 | | _info.Offset.End = idx; |
| | | 3413 | | |
| | 0 | 3414 | | Done: |
| | 0 | 3415 | | InterlockedSetFlags(cF); |
| | 0 | 3416 | | } |
| | | 3417 | | |
| | | 3418 | | /// <summary>Recreates the <see cref="_string"/> by normalizing the Path, Query and Fragment for non-ASCII input |
| | | 3419 | | private void ParseRemaining_RecreateNormalizedString() |
| | 0 | 3420 | | { |
| | 0 | 3421 | | DebugAssertInCtor(); |
| | 0 | 3422 | | Debug.Assert(InFact(Flags.HasUnicode)); |
| | 0 | 3423 | | Debug.Assert(InFact(Flags.MinimalUriInfoSet)); |
| | 0 | 3424 | | Debug.Assert(!InFact(Flags.UserDrivenParsing)); |
| | | 3425 | | |
| | 0 | 3426 | | var vsb = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | | 3427 | | |
| | 0 | 3428 | | Flags flags = _flags; |
| | 0 | 3429 | | UriSyntaxFlags syntaxFlags = _syntax.Flags; |
| | 0 | 3430 | | ReadOnlySpan<char> original = _originalUnicodeString.AsSpan(_info.Offset.Path); |
| | | 3431 | | |
| | | 3432 | | // Dos/Unix paths have no host. Other schemes cleared/set _string with host information in PrivateParseMini |
| | 0 | 3433 | | if ((flags & (Flags.DosPath | Flags.UnixPath)) != 0) |
| | 0 | 3434 | | { |
| | 0 | 3435 | | if ((flags & Flags.ImplicitFile) == 0) |
| | 0 | 3436 | | { |
| | 0 | 3437 | | vsb.Append(_syntax.SchemeName); |
| | | 3438 | | |
| | 0 | 3439 | | if ((flags & Flags.AuthorityFound) != 0) |
| | 0 | 3440 | | { |
| | 0 | 3441 | | vsb.Append(SchemeDelimiter); |
| | 0 | 3442 | | } |
| | | 3443 | | else |
| | 0 | 3444 | | { |
| | 0 | 3445 | | vsb.Append(':'); |
| | 0 | 3446 | | } |
| | 0 | 3447 | | } |
| | | 3448 | | |
| | 0 | 3449 | | _info.Offset.Scheme = 0; |
| | 0 | 3450 | | _info.Offset.User = vsb.Length; |
| | 0 | 3451 | | _info.Offset.Host = vsb.Length; |
| | 0 | 3452 | | } |
| | | 3453 | | else |
| | 0 | 3454 | | { |
| | 0 | 3455 | | Debug.Assert(!ReferenceEquals(_string, OriginalString)); |
| | | 3456 | | |
| | 0 | 3457 | | vsb.Append(_string); |
| | 0 | 3458 | | } |
| | | 3459 | | |
| | 0 | 3460 | | _info.Offset.Path = vsb.Length; |
| | | 3461 | | |
| | 0 | 3462 | | if ((flags & Flags.DisablePathAndQueryCanonicalization) != 0) |
| | 0 | 3463 | | { |
| | 0 | 3464 | | _string = string.Concat(vsb.AsSpan(), original); |
| | 0 | 3465 | | vsb.Dispose(); |
| | 0 | 3466 | | return; |
| | | 3467 | | } |
| | | 3468 | | |
| | | 3469 | | // Trim trailing whitespace |
| | 0 | 3470 | | while (!original.IsEmpty && UriHelper.IsLWS(original[^1])) |
| | 0 | 3471 | | { |
| | 0 | 3472 | | original = original.Slice(0, original.Length - 1); |
| | 0 | 3473 | | } |
| | | 3474 | | |
| | 0 | 3475 | | int pathLength = (flags & Flags.ImplicitFile) != 0 || (syntaxFlags & UriSyntaxFlags.MayHaveQuery) == 0 |
| | 0 | 3476 | | ? -1 // We can't have a query |
| | 0 | 3477 | | : original.IndexOfAny('?', '#'); |
| | | 3478 | | |
| | 0 | 3479 | | if ((uint)pathLength >= (uint)original.Length || original[pathLength] == '#') |
| | 0 | 3480 | | { |
| | | 3481 | | // Everything is considered either the path or fragment |
| | 0 | 3482 | | IriHelper.EscapeUnescapeIri(ref vsb, original, isQuery: false); |
| | 0 | 3483 | | } |
| | | 3484 | | else |
| | 0 | 3485 | | { |
| | 0 | 3486 | | IriHelper.EscapeUnescapeIri(ref vsb, original.Slice(0, pathLength), isQuery: false); // Path |
| | 0 | 3487 | | original = original.Slice(pathLength); |
| | | 3488 | | |
| | 0 | 3489 | | Debug.Assert(original.StartsWith('?')); |
| | | 3490 | | |
| | 0 | 3491 | | int fragmentOffset = (syntaxFlags & UriSyntaxFlags.MayHaveFragment) == 0 |
| | 0 | 3492 | | ? -1 // We can't have a fragment |
| | 0 | 3493 | | : original.IndexOf('#'); |
| | | 3494 | | |
| | 0 | 3495 | | if ((uint)fragmentOffset >= (uint)original.Length) |
| | 0 | 3496 | | { |
| | 0 | 3497 | | IriHelper.EscapeUnescapeIri(ref vsb, original, isQuery: true); // Query |
| | 0 | 3498 | | } |
| | | 3499 | | else |
| | 0 | 3500 | | { |
| | 0 | 3501 | | IriHelper.EscapeUnescapeIri(ref vsb, original.Slice(0, fragmentOffset), isQuery: true); // Query |
| | 0 | 3502 | | IriHelper.EscapeUnescapeIri(ref vsb, original.Slice(fragmentOffset), isQuery: false); // Fragment |
| | 0 | 3503 | | } |
| | 0 | 3504 | | } |
| | | 3505 | | |
| | 0 | 3506 | | _string = vsb.ToString(); |
| | 0 | 3507 | | } |
| | | 3508 | | |
| | | 3509 | | private static string EscapeUnescapeIri(scoped ReadOnlySpan<char> prefix, scoped ReadOnlySpan<char> span, bool i |
| | 0 | 3510 | | { |
| | 0 | 3511 | | var vsb = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | 0 | 3512 | | IriHelper.EscapeUnescapeIri(ref vsb, span, isQuery); |
| | 0 | 3513 | | string result = string.Concat(prefix, vsb.AsSpan()); |
| | 0 | 3514 | | vsb.Dispose(); |
| | 0 | 3515 | | return result; |
| | 0 | 3516 | | } |
| | | 3517 | | |
| | | 3518 | | // verifies the syntax of the scheme part |
| | | 3519 | | // Checks on implicit File: scheme due to simple Dos/Unc path passed |
| | | 3520 | | // returns the start of the next component position |
| | | 3521 | | private static int ParseSchemeCheckImplicitFile(string uriString, ref ParsingError err, ref Flags flags, ref Uri |
| | 0 | 3522 | | { |
| | 0 | 3523 | | Debug.Assert(err == ParsingError.None); |
| | 0 | 3524 | | Debug.Assert((flags & Flags.Debug_LeftConstructor) == 0); |
| | | 3525 | | |
| | 0 | 3526 | | int i = 0; |
| | | 3527 | | |
| | | 3528 | | // skip whitespace |
| | 0 | 3529 | | while ((uint)i < (uint)uriString.Length && UriHelper.IsLWS(uriString[i])) |
| | 0 | 3530 | | { |
| | 0 | 3531 | | i++; |
| | 0 | 3532 | | } |
| | | 3533 | | |
| | | 3534 | | // Unix: Unix path? |
| | | 3535 | | // A path starting with 2 / or \ (including mixed) is treated as UNC and will be matched below |
| | 0 | 3536 | | if (!OperatingSystem.IsWindows() && |
| | 0 | 3537 | | StaticNotAny(flags, Flags.DisableImplicitFilePaths) && |
| | 0 | 3538 | | (uint)i < (uint)uriString.Length && uriString[i] == '/' && |
| | 0 | 3539 | | ((uint)(i + 1) >= (uint)uriString.Length || uriString[i + 1] is not ('/' or '\\'))) |
| | 0 | 3540 | | { |
| | 0 | 3541 | | flags |= (Flags.UnixPath | Flags.ImplicitFile | Flags.AuthorityFound); |
| | 0 | 3542 | | syntax = UriParser.UnixFileUri; |
| | 0 | 3543 | | return i; |
| | | 3544 | | } |
| | | 3545 | | |
| | | 3546 | | // Find the colon. |
| | | 3547 | | // Note that we don't support one-letter schemes that will be put into a DOS path bucket |
| | 0 | 3548 | | int colonOffset = uriString.AsSpan(i).IndexOf(':'); |
| | | 3549 | | |
| | | 3550 | | // NB: A string must have at least 3 characters and at least 1 before ':' |
| | 0 | 3551 | | if ((uint)(i + 2) >= (uint)uriString.Length || colonOffset == 0) |
| | 0 | 3552 | | { |
| | 0 | 3553 | | err = ParsingError.BadFormat; |
| | 0 | 3554 | | return 0; |
| | | 3555 | | } |
| | | 3556 | | |
| | | 3557 | | // Check for supported special cases like a DOS file path OR a UNC share path |
| | | 3558 | | // NB: A string may not have ':' if this is a UNC path |
| | 0 | 3559 | | if (uriString[i + 1] is ':' or '|') |
| | 0 | 3560 | | { |
| | | 3561 | | // DOS-like path? |
| | 0 | 3562 | | if (char.IsAsciiLetter(uriString[i]) && StaticNotAny(flags, Flags.DisableImplicitFilePaths)) |
| | 0 | 3563 | | { |
| | 0 | 3564 | | if (uriString[i + 2] is '\\' or '/') |
| | 0 | 3565 | | { |
| | 0 | 3566 | | flags |= (Flags.DosPath | Flags.ImplicitFile | Flags.AuthorityFound); |
| | 0 | 3567 | | syntax = UriParser.FileUri; |
| | 0 | 3568 | | return i; |
| | | 3569 | | } |
| | | 3570 | | |
| | 0 | 3571 | | err = ParsingError.MustRootedPath; |
| | 0 | 3572 | | return 0; |
| | | 3573 | | } |
| | | 3574 | | |
| | 0 | 3575 | | err = uriString[i + 1] == ':' ? ParsingError.BadScheme : ParsingError.BadFormat; |
| | 0 | 3576 | | return 0; |
| | | 3577 | | } |
| | 0 | 3578 | | else if (uriString[i] is '/' or '\\') |
| | 0 | 3579 | | { |
| | | 3580 | | // UNC share? |
| | 0 | 3581 | | if ((uriString[i + 1] is '\\' or '/') && StaticNotAny(flags, Flags.DisableImplicitFilePaths)) |
| | 0 | 3582 | | { |
| | 0 | 3583 | | flags |= (Flags.UncPath | Flags.ImplicitFile | Flags.AuthorityFound); |
| | 0 | 3584 | | syntax = UriParser.FileUri; |
| | 0 | 3585 | | i += 2; |
| | | 3586 | | |
| | | 3587 | | // V1.1 compat this will simply eat any slashes prepended to a UNC path |
| | 0 | 3588 | | while ((uint)i < (uint)uriString.Length && uriString[i] is '/' or '\\') |
| | 0 | 3589 | | { |
| | 0 | 3590 | | i++; |
| | 0 | 3591 | | } |
| | | 3592 | | |
| | 0 | 3593 | | return i; |
| | | 3594 | | } |
| | | 3595 | | |
| | 0 | 3596 | | err = ParsingError.BadFormat; |
| | 0 | 3597 | | return 0; |
| | | 3598 | | } |
| | | 3599 | | |
| | 0 | 3600 | | if (colonOffset < 0) |
| | 0 | 3601 | | { |
| | 0 | 3602 | | err = ParsingError.BadFormat; |
| | 0 | 3603 | | return 0; |
| | | 3604 | | } |
| | | 3605 | | |
| | | 3606 | | // This is a potentially valid scheme, but we have not identified it yet. |
| | | 3607 | | // Check for illegal characters, canonicalize, and check the length. |
| | 0 | 3608 | | syntax = CheckSchemeSyntax(uriString.AsSpan(i, colonOffset), ref err); |
| | 0 | 3609 | | if (syntax is null) |
| | 0 | 3610 | | { |
| | 0 | 3611 | | return 0; |
| | | 3612 | | } |
| | 0 | 3613 | | return i + colonOffset + 1; |
| | 0 | 3614 | | } |
| | | 3615 | | |
| | | 3616 | | // This will check whether a scheme string follows the rules |
| | | 3617 | | private static UriParser? CheckSchemeSyntax(ReadOnlySpan<char> scheme, ref ParsingError error) |
| | 0 | 3618 | | { |
| | 0 | 3619 | | Debug.Assert(error == ParsingError.None); |
| | | 3620 | | |
| | 0 | 3621 | | switch (scheme.Length) |
| | | 3622 | | { |
| | | 3623 | | case 2: |
| | 0 | 3624 | | if (scheme.Equals("ws", StringComparison.OrdinalIgnoreCase)) return UriParser.WsUri; |
| | 0 | 3625 | | break; |
| | | 3626 | | |
| | | 3627 | | case 3: |
| | 0 | 3628 | | if (scheme.Equals("wss", StringComparison.OrdinalIgnoreCase)) return UriParser.WssUri; |
| | 0 | 3629 | | if (scheme.Equals("ftp", StringComparison.OrdinalIgnoreCase)) return UriParser.FtpUri; |
| | 0 | 3630 | | break; |
| | | 3631 | | |
| | | 3632 | | case 4: |
| | 0 | 3633 | | if (scheme.Equals("http", StringComparison.OrdinalIgnoreCase)) return UriParser.HttpUri; |
| | 0 | 3634 | | if (scheme.Equals("file", StringComparison.OrdinalIgnoreCase)) return UriParser.FileUri; |
| | 0 | 3635 | | if (scheme.Equals("uuid", StringComparison.OrdinalIgnoreCase)) return UriParser.UuidUri; |
| | 0 | 3636 | | if (scheme.Equals("nntp", StringComparison.OrdinalIgnoreCase)) return UriParser.NntpUri; |
| | 0 | 3637 | | if (scheme.Equals("ldap", StringComparison.OrdinalIgnoreCase)) return UriParser.LdapUri; |
| | 0 | 3638 | | if (scheme.Equals("news", StringComparison.OrdinalIgnoreCase)) return UriParser.NewsUri; |
| | 0 | 3639 | | break; |
| | | 3640 | | |
| | | 3641 | | case 5: |
| | 0 | 3642 | | if (scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) return UriParser.HttpsUri; |
| | 0 | 3643 | | break; |
| | | 3644 | | |
| | | 3645 | | case 6: |
| | 0 | 3646 | | if (scheme.Equals("mailto", StringComparison.OrdinalIgnoreCase)) return UriParser.MailToUri; |
| | 0 | 3647 | | if (scheme.Equals("gopher", StringComparison.OrdinalIgnoreCase)) return UriParser.GopherUri; |
| | 0 | 3648 | | if (scheme.Equals("telnet", StringComparison.OrdinalIgnoreCase)) return UriParser.TelnetUri; |
| | 0 | 3649 | | break; |
| | | 3650 | | |
| | | 3651 | | case 7: |
| | 0 | 3652 | | if (scheme.Equals("net.tcp", StringComparison.OrdinalIgnoreCase)) return UriParser.NetTcpUri; |
| | 0 | 3653 | | break; |
| | | 3654 | | |
| | | 3655 | | case 8: |
| | 0 | 3656 | | if (scheme.Equals("net.pipe", StringComparison.OrdinalIgnoreCase)) return UriParser.NetPipeUri; |
| | 0 | 3657 | | break; |
| | | 3658 | | } |
| | | 3659 | | |
| | | 3660 | | // scheme = alpha *(alpha | digit | '+' | '-' | '.') |
| | 0 | 3661 | | if (scheme.Length == 0 || |
| | 0 | 3662 | | !char.IsAsciiLetter(scheme[0]) || |
| | 0 | 3663 | | scheme.ContainsAnyExcept(s_schemeChars)) |
| | 0 | 3664 | | { |
| | 0 | 3665 | | error = ParsingError.BadScheme; |
| | 0 | 3666 | | return null; |
| | | 3667 | | } |
| | | 3668 | | |
| | 0 | 3669 | | if (scheme.Length > SchemeLengthLimit) |
| | 0 | 3670 | | { |
| | 0 | 3671 | | error = ParsingError.SchemeLimit; |
| | 0 | 3672 | | return null; |
| | | 3673 | | } |
| | | 3674 | | |
| | | 3675 | | // Then look up the syntax in a string-based table. |
| | 0 | 3676 | | return UriParser.FindOrFetchAsUnknownV1Syntax(UriHelper.SpanToLowerInvariantString(scheme)); |
| | 0 | 3677 | | } |
| | | 3678 | | |
| | 1 | 3679 | | private static readonly SearchValues<char> s_userInfoEndChars = |
| | 1 | 3680 | | SearchValues.Create(@"@?#\/"); |
| | | 3681 | | |
| | | 3682 | | // Checks the syntax of an authority component. It may also get a userInfo if present |
| | | 3683 | | // Returns an error if no/mailformed authority found |
| | | 3684 | | // Does not NOT touch _info |
| | | 3685 | | // Returns position of the Path component |
| | | 3686 | | // |
| | | 3687 | | // Must be called in the ctor only |
| | | 3688 | | private int CheckAuthorityHelper(ReadOnlySpan<char> str, int startOffset, out ParsingError err, ref Flags flags, |
| | 661 | 3689 | | { |
| | 661 | 3690 | | Debug.Assert((_flags & Flags.Debug_LeftConstructor) == 0 || (!_syntax.IsSimple && Monitor.IsEntered(_info))) |
| | 661 | 3691 | | Debug.Assert((_flags & Flags.HasUserInfo) == 0 && (_flags & Flags.HostTypeMask) == 0); |
| | 661 | 3692 | | Debug.Assert((uint)startOffset <= (uint)str.Length); |
| | | 3693 | | |
| | 661 | 3694 | | err = ParsingError.None; |
| | 661 | 3695 | | newHost = null; |
| | 661 | 3696 | | bool hasUnicode = ((flags & Flags.HasUnicode) != 0); |
| | | 3697 | | |
| | | 3698 | | // need to build new Iri'zed string |
| | 661 | 3699 | | if (hasUnicode) |
| | 0 | 3700 | | { |
| | 0 | 3701 | | newHost = _originalUnicodeString.Substring(0, startOffset); |
| | 0 | 3702 | | } |
| | | 3703 | | |
| | | 3704 | | char ch; |
| | | 3705 | | |
| | | 3706 | | // Special case is an empty authority |
| | 661 | 3707 | | if ((uint)startOffset >= (uint)str.Length || ((ch = str[startOffset]) == '/' || (ch == '\\' && (syntaxFlags |
| | 0 | 3708 | | { |
| | 0 | 3709 | | if ((syntaxFlags & UriSyntaxFlags.AllowEmptyHost) != 0) |
| | 0 | 3710 | | { |
| | 0 | 3711 | | flags &= ~Flags.UncPath; //UNC cannot have an empty hostname |
| | 0 | 3712 | | if (StaticInFact(flags, Flags.ImplicitFile)) |
| | 0 | 3713 | | err = ParsingError.BadHostName; |
| | | 3714 | | else |
| | 0 | 3715 | | flags |= Flags.BasicHostType; |
| | 0 | 3716 | | } |
| | | 3717 | | else |
| | 0 | 3718 | | err = ParsingError.BadHostName; |
| | | 3719 | | |
| | 0 | 3720 | | return startOffset; |
| | | 3721 | | } |
| | | 3722 | | |
| | | 3723 | | // Attempt to parse user info first |
| | 661 | 3724 | | int i = startOffset; |
| | | 3725 | | |
| | 661 | 3726 | | if ((syntaxFlags & UriSyntaxFlags.MayHaveUserInfo) != 0) |
| | 661 | 3727 | | { |
| | 661 | 3728 | | ReadOnlySpan<char> slice = str.Slice(i); |
| | 661 | 3729 | | int userInfoLength = slice.IndexOfAny(s_userInfoEndChars); |
| | | 3730 | | |
| | | 3731 | | // Check if the first delimiter is '@' and there's at least one character after it. |
| | 661 | 3732 | | if ((uint)userInfoLength < (uint)slice.Length && slice[userInfoLength] == '@' && (uint)(++userInfoLength |
| | 0 | 3733 | | { |
| | 0 | 3734 | | ch = slice[userInfoLength]; |
| | 0 | 3735 | | i += userInfoLength; |
| | | 3736 | | |
| | 0 | 3737 | | flags |= Flags.HasUserInfo; |
| | | 3738 | | |
| | | 3739 | | // Iri'ze userinfo |
| | 0 | 3740 | | if (hasUnicode) |
| | 0 | 3741 | | { |
| | 0 | 3742 | | newHost = EscapeUnescapeIri(newHost, slice.Slice(0, userInfoLength), isQuery: false); |
| | 0 | 3743 | | } |
| | 0 | 3744 | | } |
| | 661 | 3745 | | } |
| | | 3746 | | |
| | 661 | 3747 | | int hostLength = 0; |
| | | 3748 | | |
| | 661 | 3749 | | if (ch == '[' && (syntaxFlags & UriSyntaxFlags.AllowIPv6Host) != 0 && IPv6AddressHelper.IsValid(str.Slice(i) |
| | 538 | 3750 | | { |
| | 538 | 3751 | | int delimiterIdx = i + hostLength; |
| | | 3752 | | |
| | 538 | 3753 | | if ((uint)delimiterIdx < (uint)str.Length && |
| | 538 | 3754 | | str[delimiterIdx] is not ('/' or '\\') && |
| | 538 | 3755 | | (IsImplicitFile || str[delimiterIdx] is not (':' or '?' or '#'))) |
| | 0 | 3756 | | { |
| | | 3757 | | // A valid IPv6 address wasn't followed by a valid delimiter (e.g. http://[::]extra). |
| | | 3758 | | // For implicit files we also disallow ? or #. |
| | 0 | 3759 | | err = ParsingError.BadHostName; |
| | 0 | 3760 | | return i; |
| | | 3761 | | } |
| | | 3762 | | |
| | 538 | 3763 | | flags |= Flags.IPv6HostType; |
| | | 3764 | | |
| | 538 | 3765 | | if (hasUnicode) |
| | 0 | 3766 | | { |
| | 0 | 3767 | | newHost = string.Concat(newHost, str.Slice(i, hostLength)); |
| | 0 | 3768 | | } |
| | 538 | 3769 | | } |
| | 123 | 3770 | | else if (char.IsAsciiDigit(ch) && (syntaxFlags & UriSyntaxFlags.AllowIPv4Host) != 0 && |
| | 123 | 3771 | | IPv4AddressHelper.IsValid(str.Slice(i), out hostLength, allowIPv6: false, StaticNotAny(flags, Flags.Impl |
| | 123 | 3772 | | { |
| | 123 | 3773 | | flags |= Flags.IPv4HostType; |
| | | 3774 | | |
| | 123 | 3775 | | if (hasUnicode) |
| | 0 | 3776 | | { |
| | 0 | 3777 | | newHost = string.Concat(newHost, str.Slice(i, hostLength)); |
| | 0 | 3778 | | } |
| | 123 | 3779 | | } |
| | 0 | 3780 | | else if ((syntaxFlags & (UriSyntaxFlags.AllowDnsHost | UriSyntaxFlags.AllowIriParsing)) == UriSyntaxFlags.Al |
| | 0 | 3781 | | DomainNameHelper.IsValid(str.Slice(i), iri: false, StaticNotAny(flags, Flags.ImplicitFile), out hostLeng |
| | 0 | 3782 | | { |
| | 0 | 3783 | | Debug.Assert(!hasUnicode); |
| | | 3784 | | |
| | | 3785 | | // comes here if there are only ascii chars in host with original parsing and no Iri |
| | 0 | 3786 | | flags |= Flags.DnsHostType; |
| | | 3787 | | |
| | | 3788 | | // Canonical DNS hostnames don't contain uppercase letters |
| | 0 | 3789 | | if (!str.Slice(i, hostLength).ContainsAnyInRange('A', 'Z')) |
| | 0 | 3790 | | { |
| | 0 | 3791 | | flags |= Flags.CanonicalDnsHost; |
| | 0 | 3792 | | } |
| | 0 | 3793 | | } |
| | 0 | 3794 | | else if (((syntaxFlags & UriSyntaxFlags.AllowDnsHost) != 0) && |
| | 0 | 3795 | | (hasUnicode || (syntaxFlags & UriSyntaxFlags.AllowIdn) != 0) && |
| | 0 | 3796 | | DomainNameHelper.IsValid(str.Slice(i), iri: true, StaticNotAny(flags, Flags.ImplicitFile), out hostLengt |
| | 0 | 3797 | | { |
| | 0 | 3798 | | flags |= Flags.DnsHostType; |
| | | 3799 | | |
| | 0 | 3800 | | if (hasUnicode) |
| | 0 | 3801 | | { |
| | 0 | 3802 | | ReadOnlySpan<char> host = str.Slice(i, hostLength); |
| | | 3803 | | |
| | 0 | 3804 | | if (UriHelper.StripBidiControlCharacters(host, out string? stripped)) |
| | 0 | 3805 | | { |
| | 0 | 3806 | | host = stripped; |
| | 0 | 3807 | | } |
| | | 3808 | | |
| | | 3809 | | try |
| | 0 | 3810 | | { |
| | 0 | 3811 | | newHost = UriHelper.NormalizeAndConcat(newHost, host); |
| | 0 | 3812 | | } |
| | 0 | 3813 | | catch (ArgumentException) |
| | 0 | 3814 | | { |
| | 0 | 3815 | | err = ParsingError.BadHostName; |
| | 0 | 3816 | | } |
| | 0 | 3817 | | } |
| | 0 | 3818 | | } |
| | 0 | 3819 | | else if ((syntaxFlags & UriSyntaxFlags.AllowUncHost) != 0) |
| | 0 | 3820 | | { |
| | | 3821 | | // This must remain as the last check before BasicHost type |
| | 0 | 3822 | | if (UncNameHelper.IsValid(str.Slice(i), StaticNotAny(flags, Flags.ImplicitFile), out hostLength)) |
| | 0 | 3823 | | { |
| | 0 | 3824 | | if (hostLength <= UncNameHelper.MaximumInternetNameLength) |
| | 0 | 3825 | | { |
| | 0 | 3826 | | flags |= Flags.UncHostType; |
| | 0 | 3827 | | if (hasUnicode) |
| | 0 | 3828 | | { |
| | 0 | 3829 | | newHost = string.Concat(newHost, str.Slice(i, hostLength)); |
| | 0 | 3830 | | } |
| | 0 | 3831 | | } |
| | 0 | 3832 | | } |
| | 0 | 3833 | | } |
| | | 3834 | | |
| | 661 | 3835 | | int endOfHost = i + hostLength; |
| | 661 | 3836 | | char hostDelimiter = (uint)endOfHost < (uint)str.Length ? str[endOfHost] : '\0'; |
| | | 3837 | | |
| | | 3838 | | // The deal here is that we won't allow '\' host terminator except for the File scheme |
| | | 3839 | | // If we see '\' we try to make it a part of a Basic host |
| | 661 | 3840 | | if (hostDelimiter == '\\' && (flags & Flags.HostTypeMask) != Flags.HostNotParsed && (syntaxFlags & UriSyntax |
| | 0 | 3841 | | { |
| | 0 | 3842 | | if ((syntaxFlags & UriSyntaxFlags.V1_UnknownUri) != 0) |
| | 0 | 3843 | | { |
| | 0 | 3844 | | err = ParsingError.BadHostName; |
| | 0 | 3845 | | return endOfHost; |
| | | 3846 | | } |
| | | 3847 | | |
| | 0 | 3848 | | flags &= ~Flags.HostTypeMask; |
| | 0 | 3849 | | } |
| | | 3850 | | // Here we have checked the syntax up to the end of host |
| | | 3851 | | // The only thing that can cause an exception is the port value |
| | | 3852 | | // Spend some (duplicated) cycles on that. |
| | 661 | 3853 | | else if (hostDelimiter == ':' && hostLength != 0) |
| | 0 | 3854 | | { |
| | 0 | 3855 | | if ((syntaxFlags & UriSyntaxFlags.MayHavePort) != 0) |
| | 0 | 3856 | | { |
| | 0 | 3857 | | int port = 0; |
| | 0 | 3858 | | int startPort = endOfHost; |
| | | 3859 | | |
| | 0 | 3860 | | for (i = endOfHost + 1; (uint)i < (uint)str.Length; i++) |
| | 0 | 3861 | | { |
| | 0 | 3862 | | int val = str[i] - '0'; |
| | 0 | 3863 | | if ((uint)val <= ('9' - '0')) |
| | 0 | 3864 | | { |
| | 0 | 3865 | | if ((port = (port * 10 + val)) > 0xFFFF) |
| | 0 | 3866 | | break; |
| | 0 | 3867 | | } |
| | 0 | 3868 | | else if (val == ('/' - '0') || val == ('?' - '0') || val == ('#' - '0')) |
| | 0 | 3869 | | { |
| | 0 | 3870 | | break; |
| | | 3871 | | } |
| | | 3872 | | else |
| | 0 | 3873 | | { |
| | | 3874 | | // The second check is to keep compatibility with V1 until the UriParser is registered |
| | 0 | 3875 | | if ((syntaxFlags & (UriSyntaxFlags.AllowAnyOtherHost | UriSyntaxFlags.V1_UnknownUri)) == Uri |
| | 0 | 3876 | | { |
| | 0 | 3877 | | flags &= ~Flags.HostTypeMask; |
| | 0 | 3878 | | break; |
| | | 3879 | | } |
| | | 3880 | | else |
| | 0 | 3881 | | { |
| | 0 | 3882 | | err = ParsingError.BadPort; |
| | 0 | 3883 | | return i; |
| | | 3884 | | } |
| | | 3885 | | } |
| | 0 | 3886 | | } |
| | | 3887 | | |
| | | 3888 | | // check on 0-ffff range |
| | 0 | 3889 | | if (port > 0xFFFF) |
| | 0 | 3890 | | { |
| | 0 | 3891 | | if ((syntaxFlags & UriSyntaxFlags.AllowAnyOtherHost) != 0) |
| | 0 | 3892 | | { |
| | 0 | 3893 | | flags &= ~Flags.HostTypeMask; |
| | 0 | 3894 | | } |
| | | 3895 | | else |
| | 0 | 3896 | | { |
| | 0 | 3897 | | err = ParsingError.BadPort; |
| | 0 | 3898 | | return i; |
| | | 3899 | | } |
| | 0 | 3900 | | } |
| | | 3901 | | |
| | 0 | 3902 | | if (hasUnicode) |
| | 0 | 3903 | | { |
| | 0 | 3904 | | newHost = string.Concat(newHost, str.Slice(startPort, i - startPort)); |
| | 0 | 3905 | | } |
| | 0 | 3906 | | } |
| | | 3907 | | else |
| | 0 | 3908 | | { |
| | 0 | 3909 | | flags &= ~Flags.HostTypeMask; |
| | 0 | 3910 | | } |
| | 0 | 3911 | | } |
| | | 3912 | | |
| | | 3913 | | // check on whether nothing has worked out |
| | 661 | 3914 | | if ((flags & Flags.HostTypeMask) == Flags.HostNotParsed) |
| | 0 | 3915 | | { |
| | | 3916 | | //No user info for a Basic hostname |
| | 0 | 3917 | | flags &= ~Flags.HasUserInfo; |
| | | 3918 | | |
| | | 3919 | | // Some schemes do not allow HostType = Basic (plus V1 almost never understands this issue) |
| | 0 | 3920 | | if ((syntaxFlags & UriSyntaxFlags.AllowAnyOtherHost) != 0) |
| | 0 | 3921 | | { |
| | 0 | 3922 | | flags |= Flags.BasicHostType; |
| | 0 | 3923 | | int basicHostEnd = str.Slice(i).IndexOfAny('/', '?', '#'); |
| | 0 | 3924 | | endOfHost = basicHostEnd >= 0 ? i + basicHostEnd : str.Length; |
| | | 3925 | | |
| | 0 | 3926 | | if (hasUnicode) |
| | 0 | 3927 | | { |
| | | 3928 | | // Normalize any other host or do idn |
| | | 3929 | | try |
| | 0 | 3930 | | { |
| | 0 | 3931 | | newHost = UriHelper.NormalizeAndConcat(newHost, str.Slice(i, endOfHost - i)); |
| | 0 | 3932 | | } |
| | 0 | 3933 | | catch (ArgumentException) |
| | 0 | 3934 | | { |
| | 0 | 3935 | | err = ParsingError.BadHostName; |
| | 0 | 3936 | | } |
| | 0 | 3937 | | } |
| | 0 | 3938 | | } |
| | | 3939 | | else |
| | 0 | 3940 | | { |
| | | 3941 | | // ATTN V1 compat: V1 supports hostnames like ".." and ".", and so we do but only for unknown scheme |
| | 0 | 3942 | | if ((syntaxFlags & UriSyntaxFlags.V1_UnknownUri) != 0) |
| | 0 | 3943 | | { |
| | | 3944 | | // Can assert here that the host is not empty so we will set dotFound |
| | | 3945 | | // at least once or fail before exiting the loop |
| | 0 | 3946 | | bool dotFound = false; |
| | 0 | 3947 | | for (endOfHost = i; (uint)endOfHost < (uint)str.Length; endOfHost++) |
| | 0 | 3948 | | { |
| | 0 | 3949 | | if (dotFound && (str[endOfHost] == '/' || str[endOfHost] == '?' || str[endOfHost] == '#')) |
| | 0 | 3950 | | break; |
| | | 3951 | | |
| | 0 | 3952 | | if (endOfHost < (i + 2) && str[endOfHost] == '.') |
| | 0 | 3953 | | { |
| | | 3954 | | // allow one or two dots |
| | 0 | 3955 | | dotFound = true; |
| | 0 | 3956 | | } |
| | | 3957 | | else |
| | 0 | 3958 | | { |
| | | 3959 | | //failure |
| | 0 | 3960 | | err = ParsingError.BadHostName; |
| | 0 | 3961 | | return i; |
| | | 3962 | | } |
| | 0 | 3963 | | } |
| | | 3964 | | |
| | | 3965 | | //success |
| | 0 | 3966 | | flags |= Flags.BasicHostType; |
| | | 3967 | | |
| | 0 | 3968 | | if (hasUnicode) |
| | 0 | 3969 | | { |
| | | 3970 | | // Normalize any other host |
| | | 3971 | | try |
| | 0 | 3972 | | { |
| | 0 | 3973 | | newHost = UriHelper.NormalizeAndConcat(newHost, str.Slice(i, endOfHost - i)); |
| | 0 | 3974 | | } |
| | 0 | 3975 | | catch (ArgumentException) |
| | 0 | 3976 | | { |
| | 0 | 3977 | | err = ParsingError.BadFormat; |
| | 0 | 3978 | | return i; |
| | | 3979 | | } |
| | 0 | 3980 | | } |
| | 0 | 3981 | | } |
| | 0 | 3982 | | else if ((syntaxFlags & (UriSyntaxFlags.MustHaveAuthority | UriSyntaxFlags.MailToLikeUri)) != 0) |
| | 0 | 3983 | | { |
| | 0 | 3984 | | err = ParsingError.BadHostName; |
| | 0 | 3985 | | return i; |
| | | 3986 | | } |
| | 0 | 3987 | | } |
| | 0 | 3988 | | } |
| | | 3989 | | |
| | 661 | 3990 | | return endOfHost; |
| | 661 | 3991 | | } |
| | | 3992 | | |
| | | 3993 | | // |
| | | 3994 | | // The method checks whether a string needs transformation before going to display or wire |
| | | 3995 | | // |
| | | 3996 | | // Parameters: |
| | | 3997 | | // - escaped true = treat all valid escape sequences as escaped sequences, false = escape all % |
| | | 3998 | | // - delim a character signaling the termination of the component being checked |
| | | 3999 | | // |
| | | 4000 | | // When delim=='?', then '#' character is also considered as delimiter additionally to passed '?'. |
| | | 4001 | | // |
| | | 4002 | | // The method pays attention to the dots and slashes so to signal potential Path compression action needed. |
| | | 4003 | | // Even that is not required for other components, the cycles are still spent (little inefficiency) |
| | | 4004 | | // |
| | | 4005 | | |
| | | 4006 | | internal const char c_DummyChar = (char)0xFFFF; //An Invalid Unicode character used as a dummy char passed i |
| | | 4007 | | internal const char c_EOL = (char)0xFFFE; //An Invalid Unicode character used by CheckCanonical as "no delim |
| | | 4008 | | [Flags] |
| | | 4009 | | private enum Check |
| | | 4010 | | { |
| | | 4011 | | None = 0x0, |
| | | 4012 | | EscapedCanonical = 0x1, |
| | | 4013 | | DisplayCanonical = 0x2, |
| | | 4014 | | DotSlashAttn = 0x4, |
| | | 4015 | | DotSlashEscaped = 0x80, |
| | | 4016 | | BackslashInPath = 0x10, |
| | | 4017 | | ReservedFound = 0x20, |
| | | 4018 | | NotIriCanonical = 0x40, |
| | | 4019 | | FoundNonAscii = 0x8 |
| | | 4020 | | } |
| | | 4021 | | |
| | | 4022 | | // Used by ParseRemaining as well by InternalIsWellFormedOriginalString |
| | | 4023 | | private Check CheckCanonical(ReadOnlySpan<char> span, char delim, out int end) |
| | 0 | 4024 | | { |
| | 0 | 4025 | | Debug.Assert(delim is '@' or '/' or '?' or '#' or c_DummyChar or c_EOL); |
| | | 4026 | | |
| | 0 | 4027 | | Check res = Check.None; |
| | 0 | 4028 | | bool needsEscaping = false; |
| | 0 | 4029 | | bool foundEscaping = false; |
| | 0 | 4030 | | bool iriParsing = IriParsing; |
| | | 4031 | | |
| | | 4032 | | int i; |
| | 0 | 4033 | | for (i = 0; (uint)i < (uint)span.Length; i++) |
| | 0 | 4034 | | { |
| | 0 | 4035 | | char c = span[i]; |
| | | 4036 | | |
| | 0 | 4037 | | if (char.IsAsciiLetterOrDigit(c)) |
| | 0 | 4038 | | { |
| | | 4039 | | // The most common case - unreserved chars. |
| | 0 | 4040 | | } |
| | 0 | 4041 | | else if (c <= '\x1F' || (c >= '\x7F' && c <= '\x9F')) |
| | 0 | 4042 | | { |
| | | 4043 | | // Control chars usually should be escaped in any case |
| | 0 | 4044 | | needsEscaping = true; |
| | 0 | 4045 | | foundEscaping = true; |
| | 0 | 4046 | | res |= Check.ReservedFound; |
| | 0 | 4047 | | } |
| | 0 | 4048 | | else if (c > '~') |
| | 0 | 4049 | | { |
| | 0 | 4050 | | if (iriParsing) |
| | 0 | 4051 | | { |
| | 0 | 4052 | | bool valid = false; |
| | 0 | 4053 | | res |= Check.FoundNonAscii; |
| | | 4054 | | |
| | 0 | 4055 | | if (char.IsHighSurrogate(c)) |
| | 0 | 4056 | | { |
| | 0 | 4057 | | if ((uint)(i + 1) < (uint)span.Length) |
| | 0 | 4058 | | { |
| | 0 | 4059 | | valid = IriHelper.CheckIriUnicodeRange(c, span[i + 1], out _, true); |
| | 0 | 4060 | | i++; |
| | 0 | 4061 | | } |
| | 0 | 4062 | | } |
| | | 4063 | | else |
| | 0 | 4064 | | { |
| | 0 | 4065 | | valid = IriHelper.CheckIriUnicodeRange(c, true); |
| | 0 | 4066 | | } |
| | | 4067 | | |
| | 0 | 4068 | | if (!valid) res |= Check.NotIriCanonical; |
| | 0 | 4069 | | } |
| | | 4070 | | |
| | 0 | 4071 | | needsEscaping = true; |
| | 0 | 4072 | | } |
| | 0 | 4073 | | else if (c == delim) |
| | 0 | 4074 | | { |
| | 0 | 4075 | | break; |
| | | 4076 | | } |
| | 0 | 4077 | | else if (delim == '?' && c == '#' && (_syntax != null && _syntax.InFact(UriSyntaxFlags.MayHaveFragment)) |
| | 0 | 4078 | | { |
| | | 4079 | | // this is a special case when deciding on Query/Fragment |
| | 0 | 4080 | | break; |
| | | 4081 | | } |
| | 0 | 4082 | | else if (c == '?') |
| | 0 | 4083 | | { |
| | 0 | 4084 | | if (IsImplicitFile || (_syntax != null && !_syntax.InFact(UriSyntaxFlags.MayHaveQuery) && delim != c |
| | 0 | 4085 | | { |
| | | 4086 | | // If found as reserved this char is not suitable for safe unescaped display |
| | | 4087 | | // Will need to escape it when both escaping and unescaping the string |
| | 0 | 4088 | | res |= Check.ReservedFound; |
| | 0 | 4089 | | foundEscaping = true; |
| | 0 | 4090 | | needsEscaping = true; |
| | 0 | 4091 | | } |
| | 0 | 4092 | | } |
| | 0 | 4093 | | else if (c == '#') |
| | 0 | 4094 | | { |
| | 0 | 4095 | | needsEscaping = true; |
| | | 4096 | | |
| | 0 | 4097 | | if (IsImplicitFile || (_syntax != null && !_syntax.InFact(UriSyntaxFlags.MayHaveFragment))) |
| | 0 | 4098 | | { |
| | | 4099 | | // If found as reserved this char is not suitable for safe unescaped display |
| | | 4100 | | // Will need to escape it when both escaping and unescaping the string |
| | 0 | 4101 | | res |= Check.ReservedFound; |
| | 0 | 4102 | | foundEscaping = true; |
| | 0 | 4103 | | } |
| | 0 | 4104 | | } |
| | 0 | 4105 | | else if (c == '/' || c == '\\') |
| | 0 | 4106 | | { |
| | 0 | 4107 | | if (c == '\\') |
| | 0 | 4108 | | { |
| | 0 | 4109 | | res |= Check.BackslashInPath; |
| | 0 | 4110 | | } |
| | | 4111 | | |
| | 0 | 4112 | | if ((uint)(i + 1) < (uint)span.Length && (span[i + 1] == '/' || span[i + 1] == '\\')) |
| | 0 | 4113 | | { |
| | 0 | 4114 | | res |= Check.DotSlashAttn; |
| | 0 | 4115 | | } |
| | 0 | 4116 | | } |
| | 0 | 4117 | | else if (c == '.') |
| | 0 | 4118 | | { |
| | 0 | 4119 | | if (((uint)(i + 1) >= (uint)span.Length || span[i + 1] == '.' || span[i + 1] == '/' || span[i + 1] = |
| | 0 | 4120 | | { |
| | 0 | 4121 | | res |= Check.DotSlashAttn; |
| | 0 | 4122 | | } |
| | 0 | 4123 | | } |
| | 0 | 4124 | | else if (((c <= '"' && c != '!') || (c >= '[' && c <= '^') || c == '>' |
| | 0 | 4125 | | || c == '<' || c == '`')) |
| | 0 | 4126 | | { |
| | 0 | 4127 | | needsEscaping = true; |
| | | 4128 | | |
| | | 4129 | | // The check above validates only that we have valid IRI characters, which is not enough to |
| | | 4130 | | // conclude that we have a valid canonical IRI. |
| | | 4131 | | // If we have an IRI with Flags.HasUnicode, we need to set Check.NotIriCanonical so that the |
| | | 4132 | | // path, query, and fragment will be validated. |
| | 0 | 4133 | | if ((_flags & Flags.HasUnicode) != 0) |
| | 0 | 4134 | | { |
| | 0 | 4135 | | res |= Check.NotIriCanonical; |
| | 0 | 4136 | | } |
| | 0 | 4137 | | } |
| | 0 | 4138 | | else if (c >= '{' && c <= '}') // includes '{', '|', '}' |
| | 0 | 4139 | | { |
| | 0 | 4140 | | needsEscaping = true; |
| | 0 | 4141 | | } |
| | 0 | 4142 | | else if (c == '%') |
| | 0 | 4143 | | { |
| | 0 | 4144 | | foundEscaping = true; |
| | | 4145 | | |
| | | 4146 | | // try unescape a byte hex escaping |
| | 0 | 4147 | | if ((uint)(i + 2) < (uint)span.Length && (c = UriHelper.DecodeHexChars(span[i + 1], span[i + 2])) != |
| | 0 | 4148 | | { |
| | 0 | 4149 | | if (c == '.' || c == '/' || c == '\\') |
| | 0 | 4150 | | { |
| | 0 | 4151 | | res |= Check.DotSlashEscaped; |
| | 0 | 4152 | | } |
| | | 4153 | | |
| | 0 | 4154 | | i += 2; |
| | 0 | 4155 | | continue; |
| | | 4156 | | } |
| | | 4157 | | |
| | | 4158 | | // otherwise we follow to non escaped case |
| | 0 | 4159 | | needsEscaping = true; |
| | 0 | 4160 | | } |
| | 0 | 4161 | | } |
| | | 4162 | | |
| | 0 | 4163 | | if (!needsEscaping) |
| | 0 | 4164 | | { |
| | 0 | 4165 | | res |= Check.EscapedCanonical; |
| | 0 | 4166 | | } |
| | | 4167 | | |
| | 0 | 4168 | | if (!foundEscaping) |
| | 0 | 4169 | | { |
| | 0 | 4170 | | res |= Check.DisplayCanonical; |
| | 0 | 4171 | | } |
| | | 4172 | | |
| | 0 | 4173 | | end = i; |
| | 0 | 4174 | | return res; |
| | 0 | 4175 | | } |
| | | 4176 | | |
| | | 4177 | | // |
| | | 4178 | | // Returns the escaped and canonicalized path string |
| | | 4179 | | // the passed array must be long enough to hold at least |
| | | 4180 | | // canonical unescaped path representation (allocated by the caller) |
| | | 4181 | | // |
| | | 4182 | | private void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat formatAs) |
| | 0 | 4183 | | { |
| | 0 | 4184 | | if (InFact(Flags.FirstSlashAbsent)) |
| | 0 | 4185 | | dest.Append('/'); |
| | | 4186 | | |
| | 0 | 4187 | | if (_info.Offset.Path == _info.Offset.Query) |
| | 0 | 4188 | | return; |
| | | 4189 | | |
| | 0 | 4190 | | int start = dest.Length; |
| | | 4191 | | |
| | 0 | 4192 | | int dosPathIdx = SecuredPathIndex; |
| | | 4193 | | |
| | | 4194 | | // Note that unescaping and then escaping back is not transitive hence not safe. |
| | | 4195 | | // We are vulnerable due to the way the UserEscaped flag is processed. |
| | | 4196 | | // Try to unescape only needed chars. |
| | 0 | 4197 | | if (formatAs == UriFormat.UriEscaped) |
| | 0 | 4198 | | { |
| | 0 | 4199 | | if (InFact(Flags.ShouldBeCompressed)) |
| | 0 | 4200 | | { |
| | 0 | 4201 | | dest.Append(_string.AsSpan(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path)); |
| | | 4202 | | |
| | | 4203 | | // If the path was found as needed compression and contains escaped characters, unescape only |
| | | 4204 | | // interesting characters (safe) |
| | | 4205 | | |
| | 0 | 4206 | | if (_syntax.InFact(UriSyntaxFlags.UnEscapeDotsAndSlashes) && InFact(Flags.PathNotCanonical) |
| | 0 | 4207 | | && !IsImplicitFile) |
| | 0 | 4208 | | { |
| | 0 | 4209 | | UnescapePathSlashesAndDots(ref dest, start, _syntax.InFact(UriSyntaxFlags.ConvertPathSlashes)); |
| | 0 | 4210 | | } |
| | 0 | 4211 | | } |
| | | 4212 | | else |
| | 0 | 4213 | | { |
| | | 4214 | | //Note: we may produce non escaped Uri characters on the wire |
| | 0 | 4215 | | if (InFact(Flags.E_PathNotCanonical) && NotAny(Flags.UserEscaped)) |
| | 0 | 4216 | | { |
| | 0 | 4217 | | ReadOnlySpan<char> str = _string; |
| | | 4218 | | |
| | | 4219 | | // Check on not canonical disk designation like C|\, should be rare, rare case |
| | 0 | 4220 | | if (dosPathIdx != 0 && str[dosPathIdx + _info.Offset.Path - 1] == '|') |
| | 0 | 4221 | | { |
| | 0 | 4222 | | char[] chars = str.ToArray(); |
| | 0 | 4223 | | chars[dosPathIdx + _info.Offset.Path - 1] = ':'; |
| | 0 | 4224 | | str = chars; |
| | 0 | 4225 | | } |
| | | 4226 | | |
| | 0 | 4227 | | UriHelper.EscapeString( |
| | 0 | 4228 | | str.Slice(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path), |
| | 0 | 4229 | | ref dest, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedExceptQuestionM |
| | 0 | 4230 | | } |
| | | 4231 | | else |
| | 0 | 4232 | | { |
| | 0 | 4233 | | dest.Append(_string.AsSpan(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path)); |
| | 0 | 4234 | | } |
| | 0 | 4235 | | } |
| | | 4236 | | |
| | | 4237 | | // On Unix, escape '\\' in path of file uris to '%5C' canonical form. |
| | 0 | 4238 | | if (!OperatingSystem.IsWindows() && InFact(Flags.BackslashInPath) && _syntax.NotAny(UriSyntaxFlags.Conve |
| | 0 | 4239 | | { |
| | | 4240 | | // We can't do an in-place escape, create a copy |
| | 0 | 4241 | | var copy = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | 0 | 4242 | | copy.Append(dest.AsSpan(start, dest.Length - start)); |
| | | 4243 | | |
| | 0 | 4244 | | dest.Length = start; |
| | | 4245 | | |
| | 0 | 4246 | | UriHelper.EscapeString(copy.AsSpan(), ref dest, checkExistingEscaped: true, UriHelper.UnreservedRese |
| | 0 | 4247 | | start = dest.Length; |
| | | 4248 | | |
| | 0 | 4249 | | copy.Dispose(); |
| | 0 | 4250 | | } |
| | 0 | 4251 | | } |
| | | 4252 | | else |
| | 0 | 4253 | | { |
| | 0 | 4254 | | dest.Append(_string.AsSpan(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path)); |
| | | 4255 | | |
| | 0 | 4256 | | if (InFact(Flags.ShouldBeCompressed)) |
| | 0 | 4257 | | { |
| | | 4258 | | // If the path was found as needed compression and contains escaped characters, |
| | | 4259 | | // unescape only interesting characters (safe) |
| | | 4260 | | |
| | 0 | 4261 | | if (_syntax.InFact(UriSyntaxFlags.UnEscapeDotsAndSlashes) && InFact(Flags.PathNotCanonical) |
| | 0 | 4262 | | && !IsImplicitFile) |
| | 0 | 4263 | | { |
| | 0 | 4264 | | UnescapePathSlashesAndDots(ref dest, start, _syntax.InFact(UriSyntaxFlags.ConvertPathSlashes)); |
| | 0 | 4265 | | } |
| | 0 | 4266 | | } |
| | 0 | 4267 | | } |
| | | 4268 | | |
| | | 4269 | | // Here we already got output data as copied into dest array |
| | | 4270 | | // We just may need more processing of that data |
| | | 4271 | | |
| | | 4272 | | // |
| | | 4273 | | // if this URI is using 'non-proprietary' disk drive designation, convert to MS-style |
| | | 4274 | | // |
| | | 4275 | | // (path is already >= 3 chars if recognized as a DOS-like) |
| | | 4276 | | // |
| | 0 | 4277 | | int offset = start + dosPathIdx; |
| | 0 | 4278 | | if (dosPathIdx != 0 && dest[offset - 1] == '|') |
| | 0 | 4279 | | dest[offset - 1] = ':'; |
| | | 4280 | | |
| | 0 | 4281 | | if (InFact(Flags.ShouldBeCompressed) && dest.Length - offset > 0) |
| | 0 | 4282 | | { |
| | | 4283 | | // It will also convert back slashes if needed |
| | 0 | 4284 | | Compress(ref dest, offset, _syntax); |
| | | 4285 | | |
| | 0 | 4286 | | if (dest.Length > start && dest[start] == '\\') |
| | 0 | 4287 | | { |
| | 0 | 4288 | | dest[start] = '/'; |
| | 0 | 4289 | | } |
| | | 4290 | | |
| | | 4291 | | // Escape path if requested and found as not fully escaped |
| | 0 | 4292 | | if (formatAs == UriFormat.UriEscaped && NotAny(Flags.UserEscaped) && InFact(Flags.E_PathNotCanonical)) |
| | 0 | 4293 | | { |
| | | 4294 | | //Note: Flags.UserEscaped check is solely based on trusting the user |
| | | 4295 | | |
| | | 4296 | | // We can't do an in-place escape, create a copy |
| | 0 | 4297 | | var copy = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | 0 | 4298 | | copy.Append(dest.AsSpan(start, dest.Length - start)); |
| | | 4299 | | |
| | 0 | 4300 | | dest.Length = start; |
| | | 4301 | | |
| | 0 | 4302 | | UriHelper.EscapeString(copy.AsSpan(), ref dest, checkExistingEscaped: !IsImplicitFile, UriHelper.Unr |
| | 0 | 4303 | | start = dest.Length; |
| | | 4304 | | |
| | 0 | 4305 | | copy.Dispose(); |
| | 0 | 4306 | | } |
| | 0 | 4307 | | } |
| | | 4308 | | |
| | 0 | 4309 | | if (formatAs != UriFormat.UriEscaped && InFact(Flags.PathNotCanonical)) |
| | 0 | 4310 | | { |
| | 0 | 4311 | | ReadOnlySpan<char> slice = dest.AsSpan(start, dest.Length - start); |
| | | 4312 | | |
| | 0 | 4313 | | if (formatAs == UriFormat.Unescaped) |
| | 0 | 4314 | | { |
| | 0 | 4315 | | if (!IsImplicitFile) |
| | 0 | 4316 | | { |
| | | 4317 | | // Unescape in-place |
| | 0 | 4318 | | dest.Length = start; |
| | 0 | 4319 | | UriHelper.Unescape(slice, ref dest); |
| | 0 | 4320 | | Debug.Assert(slice.IsEmpty || slice.Overlaps(dest.RawChars)); |
| | 0 | 4321 | | } |
| | 0 | 4322 | | } |
| | | 4323 | | else |
| | 0 | 4324 | | { |
| | | 4325 | | // UriFormat.SafeUnescaped / V1ToStringUnescape |
| | 0 | 4326 | | UnescapeMode mode = InFact(Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnescape; |
| | | 4327 | | |
| | 0 | 4328 | | if (IsImplicitFile) |
| | 0 | 4329 | | { |
| | 0 | 4330 | | mode &= ~UnescapeMode.Unescape; |
| | 0 | 4331 | | } |
| | | 4332 | | |
| | 0 | 4333 | | if (formatAs == V1ToStringUnescape) |
| | 0 | 4334 | | { |
| | 0 | 4335 | | mode |= UnescapeMode.V1ToStringFlag; |
| | 0 | 4336 | | } |
| | | 4337 | | |
| | 0 | 4338 | | if (mode != UnescapeMode.None) |
| | 0 | 4339 | | { |
| | | 4340 | | // We can't do an in-place escape/unescape, create a copy |
| | 0 | 4341 | | var copy = new ValueStringBuilder(stackalloc char[StackallocThreshold]); |
| | 0 | 4342 | | copy.Append(slice); |
| | | 4343 | | |
| | 0 | 4344 | | dest.Length = start; |
| | | 4345 | | |
| | 0 | 4346 | | UriHelper.UnescapeString(copy.AsSpan(), |
| | 0 | 4347 | | ref dest, '?', '#', c_DummyChar, |
| | 0 | 4348 | | mode, |
| | 0 | 4349 | | _syntax, isQuery: false); |
| | | 4350 | | |
| | 0 | 4351 | | copy.Dispose(); |
| | 0 | 4352 | | } |
| | 0 | 4353 | | } |
| | 0 | 4354 | | } |
| | 0 | 4355 | | } |
| | | 4356 | | |
| | | 4357 | | /// <summary>Partially unescape the path ('/', '.' and optionally '\\') before compressing.</summary> |
| | | 4358 | | private static void UnescapePathSlashesAndDots(ref ValueStringBuilder vsb, int i, bool unescapeBackslashes) |
| | 0 | 4359 | | { |
| | 0 | 4360 | | Span<char> chars = vsb.RawChars.Slice(0, vsb.Length); |
| | | 4361 | | |
| | 0 | 4362 | | int writeOffset = i; |
| | | 4363 | | |
| | 0 | 4364 | | while ((uint)i < (uint)chars.Length) |
| | 0 | 4365 | | { |
| | 0 | 4366 | | if (chars[i] == '%' && (uint)(i + 2) < (uint)chars.Length) |
| | 0 | 4367 | | { |
| | 0 | 4368 | | char decoded = UriHelper.DecodeHexChars(chars[i + 1], chars[i + 2]); |
| | | 4369 | | |
| | 0 | 4370 | | if (decoded == '/' || decoded == '.' || (unescapeBackslashes && decoded == '\\')) |
| | 0 | 4371 | | { |
| | 0 | 4372 | | chars[writeOffset++] = decoded; |
| | 0 | 4373 | | i += 3; |
| | 0 | 4374 | | continue; |
| | | 4375 | | } |
| | 0 | 4376 | | } |
| | | 4377 | | |
| | 0 | 4378 | | chars[writeOffset++] = chars[i]; |
| | 0 | 4379 | | i++; |
| | 0 | 4380 | | } |
| | | 4381 | | |
| | 0 | 4382 | | vsb.Length = writeOffset; |
| | 0 | 4383 | | } |
| | | 4384 | | |
| | | 4385 | | private static void Compress(ref ValueStringBuilder dest, int start, UriParser syntax) |
| | 0 | 4386 | | { |
| | 0 | 4387 | | Debug.Assert(start <= dest.Length); |
| | | 4388 | | |
| | 0 | 4389 | | dest.Length = start + UriHelper.Compress( |
| | 0 | 4390 | | dest.RawChars.Slice(start, dest.Length - start), |
| | 0 | 4391 | | syntax.InFact(UriSyntaxFlags.ConvertPathSlashes), |
| | 0 | 4392 | | syntax.InFact(UriSyntaxFlags.CanonicalizeAsFilePath)); |
| | 0 | 4393 | | } |
| | | 4394 | | |
| | | 4395 | | private static void Compress(char[] dest, int start, ref int destLength, UriParser syntax) |
| | 0 | 4396 | | { |
| | 0 | 4397 | | destLength = start + UriHelper.Compress( |
| | 0 | 4398 | | dest.AsSpan(start, destLength - start), |
| | 0 | 4399 | | syntax.InFact(UriSyntaxFlags.ConvertPathSlashes), |
| | 0 | 4400 | | syntax.InFact(UriSyntaxFlags.CanonicalizeAsFilePath)); |
| | 0 | 4401 | | } |
| | | 4402 | | |
| | | 4403 | | // |
| | | 4404 | | // CombineUri |
| | | 4405 | | // |
| | | 4406 | | // Given 2 URI strings, combine them into a single resultant URI string |
| | | 4407 | | // |
| | | 4408 | | // Inputs: |
| | | 4409 | | // <argument> basePart |
| | | 4410 | | // Base URI to combine with |
| | | 4411 | | // |
| | | 4412 | | // <argument> relativePart |
| | | 4413 | | // String expected to be relative URI |
| | | 4414 | | // |
| | | 4415 | | // Assumes: |
| | | 4416 | | // <basePart> is in canonic form |
| | | 4417 | | // |
| | | 4418 | | // Returns: |
| | | 4419 | | // Resulting combined URI string |
| | | 4420 | | // |
| | | 4421 | | private static string CombineUri(Uri basePart, string relativePart, UriFormat uriFormat) |
| | 0 | 4422 | | { |
| | | 4423 | | //NB: relativePart is ensured as not empty by the caller |
| | | 4424 | | // Another assumption is that basePart is an AbsoluteUri |
| | | 4425 | | |
| | | 4426 | | // This method was not optimized for efficiency |
| | | 4427 | | // Means a relative Uri ctor may be relatively slow plus it increases the footprint of the baseUri |
| | | 4428 | | |
| | 0 | 4429 | | char c1 = relativePart[0]; |
| | | 4430 | | |
| | | 4431 | | //check a special case for the base as DOS path and a rooted relative string |
| | 0 | 4432 | | if (basePart.IsDosPath && |
| | 0 | 4433 | | (c1 == '/' || c1 == '\\') && |
| | 0 | 4434 | | (relativePart.Length == 1 || (relativePart[1] != '/' && relativePart[1] != '\\'))) |
| | 0 | 4435 | | { |
| | | 4436 | | // take relative part appended to the base string after the drive letter |
| | 0 | 4437 | | int idx = basePart.OriginalString.IndexOf(':'); |
| | 0 | 4438 | | if (basePart.IsImplicitFile) |
| | 0 | 4439 | | { |
| | 0 | 4440 | | return string.Concat(basePart.OriginalString.AsSpan(0, idx + 1), relativePart); |
| | | 4441 | | } |
| | | 4442 | | |
| | | 4443 | | // The basePart has explicit scheme (could be not file:), take the DOS drive ':' position |
| | 0 | 4444 | | idx = basePart.OriginalString.IndexOf(':', idx + 1); |
| | 0 | 4445 | | return string.Concat(basePart.OriginalString.AsSpan(0, idx + 1), relativePart); |
| | | 4446 | | } |
| | | 4447 | | |
| | | 4448 | | // Check special case for Unc or absolute path in relativePart when base is FILE |
| | 0 | 4449 | | if (StaticIsFile(basePart.Syntax)) |
| | 0 | 4450 | | { |
| | 0 | 4451 | | if (c1 == '\\' || c1 == '/') |
| | 0 | 4452 | | { |
| | 0 | 4453 | | if (relativePart.Length >= 2 && (relativePart[1] == '\\' || relativePart[1] == '/')) |
| | 0 | 4454 | | { |
| | | 4455 | | //Assuming relative is a Unc path and base is a file uri. |
| | 0 | 4456 | | return basePart.IsImplicitFile ? relativePart : "file:" + relativePart; |
| | | 4457 | | } |
| | | 4458 | | |
| | | 4459 | | // here we got an absolute path in relativePart, |
| | | 4460 | | // For compatibility with V1.0 parser we restrict the compression scope to Unc Share, i.e. \\host\sh |
| | 0 | 4461 | | if (basePart.IsUnc) |
| | 0 | 4462 | | { |
| | 0 | 4463 | | ReadOnlySpan<char> share = basePart.GetParts(UriComponents.Path | UriComponents.KeepDelimiter, U |
| | 0 | 4464 | | int i = share.Slice(1).IndexOf('/'); |
| | 0 | 4465 | | if (i >= 0) |
| | 0 | 4466 | | { |
| | 0 | 4467 | | share = share.Slice(0, i + 1); |
| | 0 | 4468 | | } |
| | | 4469 | | |
| | 0 | 4470 | | if (basePart.IsImplicitFile) |
| | 0 | 4471 | | { |
| | 0 | 4472 | | return string.Concat(@"\\", basePart.GetParts(UriComponents.Host, UriFormat.Unescaped), shar |
| | | 4473 | | } |
| | | 4474 | | |
| | 0 | 4475 | | return string.Concat("file://", basePart.GetParts(UriComponents.Host, uriFormat), share, relativ |
| | | 4476 | | } |
| | | 4477 | | // It's not obvious but we've checked (for this relativePart format) that baseUti is nor UNC nor DOS |
| | | 4478 | | // |
| | | 4479 | | // Means base is a Unix style path and, btw, IsImplicitFile cannot be the case either |
| | 0 | 4480 | | return "file://" + relativePart; |
| | | 4481 | | } |
| | 0 | 4482 | | } |
| | | 4483 | | |
| | | 4484 | | // If we are here we did not recognize absolute DOS/UNC path for a file: base uri |
| | | 4485 | | // Note that DOS path may still happen in the relativePart and if so it may override the base uri scheme. |
| | | 4486 | | |
| | 0 | 4487 | | bool convBackSlashes = basePart.Syntax.InFact(UriSyntaxFlags.ConvertPathSlashes); |
| | | 4488 | | |
| | | 4489 | | string? left; |
| | | 4490 | | |
| | | 4491 | | // check for network or local absolute path |
| | 0 | 4492 | | if (c1 == '/' || (c1 == '\\' && convBackSlashes)) |
| | 0 | 4493 | | { |
| | 0 | 4494 | | if (relativePart.Length >= 2 && relativePart[1] == '/') |
| | 0 | 4495 | | { |
| | | 4496 | | // got an authority in relative path and the base scheme is not file (checked) |
| | 0 | 4497 | | return basePart.Scheme + ':' + relativePart; |
| | | 4498 | | } |
| | | 4499 | | |
| | | 4500 | | // Got absolute relative path, and the base is not FILE nor a DOS path (checked at the method start) |
| | 0 | 4501 | | if (basePart.HostType == Flags.IPv6HostType) |
| | 0 | 4502 | | { |
| | 0 | 4503 | | left = $"{basePart.GetParts(UriComponents.Scheme | UriComponents.UserInfo, uriFormat)}[{basePart.Dns |
| | 0 | 4504 | | } |
| | | 4505 | | else |
| | 0 | 4506 | | { |
| | 0 | 4507 | | left = basePart.GetParts(UriComponents.SchemeAndServer | UriComponents.UserInfo, uriFormat); |
| | 0 | 4508 | | } |
| | | 4509 | | |
| | 0 | 4510 | | return convBackSlashes && c1 == '\\' ? |
| | 0 | 4511 | | string.Concat(left, "/", relativePart.AsSpan(1)) : |
| | 0 | 4512 | | left + relativePart; |
| | | 4513 | | } |
| | | 4514 | | |
| | | 4515 | | // Here we got a relative path |
| | | 4516 | | // Need to run path Compression because this is how relative Uri combining works |
| | | 4517 | | |
| | | 4518 | | // Take the base part path up to and including the last slash |
| | 0 | 4519 | | left = basePart.GetParts(UriComponents.Path | UriComponents.KeepDelimiter, |
| | 0 | 4520 | | basePart.IsImplicitFile ? UriFormat.Unescaped : uriFormat); |
| | 0 | 4521 | | int length = left.Length; |
| | 0 | 4522 | | char[] path = new char[length + relativePart.Length]; |
| | | 4523 | | |
| | 0 | 4524 | | if (length > 0) |
| | 0 | 4525 | | { |
| | 0 | 4526 | | left.CopyTo(0, path, 0, length); |
| | 0 | 4527 | | while (length > 0) |
| | 0 | 4528 | | { |
| | 0 | 4529 | | if (path[--length] == '/') |
| | 0 | 4530 | | { |
| | 0 | 4531 | | ++length; |
| | 0 | 4532 | | break; |
| | | 4533 | | } |
| | 0 | 4534 | | } |
| | 0 | 4535 | | } |
| | | 4536 | | |
| | | 4537 | | //Append relative path to the result |
| | 0 | 4538 | | relativePart.CopyTo(0, path, length, relativePart.Length); |
| | | 4539 | | |
| | | 4540 | | // Split relative on path and extra (for compression) |
| | 0 | 4541 | | c1 = basePart.Syntax.InFact(UriSyntaxFlags.MayHaveQuery) ? '?' : c_DummyChar; |
| | | 4542 | | |
| | | 4543 | | // The implicit file check is to avoid a fragment in the implicit file combined uri. |
| | 0 | 4544 | | char c2 = (!basePart.IsImplicitFile && basePart.Syntax.InFact(UriSyntaxFlags.MayHaveFragment)) ? '#' : |
| | 0 | 4545 | | c_DummyChar; |
| | 0 | 4546 | | ReadOnlySpan<char> extra = string.Empty; |
| | | 4547 | | |
| | | 4548 | | // assuming c_DummyChar may not happen in an unicode uri string |
| | 0 | 4549 | | if (!(c1 == c_DummyChar && c2 == c_DummyChar)) |
| | 0 | 4550 | | { |
| | 0 | 4551 | | int i = 0; |
| | 0 | 4552 | | for (; i < relativePart.Length; ++i) |
| | 0 | 4553 | | { |
| | 0 | 4554 | | if (path[length + i] == c1 || path[length + i] == c2) |
| | 0 | 4555 | | { |
| | 0 | 4556 | | break; |
| | | 4557 | | } |
| | 0 | 4558 | | } |
| | 0 | 4559 | | if (i == 0) |
| | 0 | 4560 | | { |
| | 0 | 4561 | | extra = relativePart; |
| | 0 | 4562 | | } |
| | 0 | 4563 | | else if (i < relativePart.Length) |
| | 0 | 4564 | | { |
| | 0 | 4565 | | extra = relativePart.AsSpan(i); |
| | 0 | 4566 | | } |
| | 0 | 4567 | | length += i; |
| | 0 | 4568 | | } |
| | | 4569 | | else |
| | 0 | 4570 | | { |
| | 0 | 4571 | | length += relativePart.Length; |
| | 0 | 4572 | | } |
| | | 4573 | | |
| | | 4574 | | // Take the base part up to the path |
| | 0 | 4575 | | if (basePart.HostType == Flags.IPv6HostType) |
| | 0 | 4576 | | { |
| | 0 | 4577 | | if (basePart.IsImplicitFile) |
| | 0 | 4578 | | { |
| | 0 | 4579 | | left = @"\\[" + basePart.DnsSafeHost + ']'; |
| | 0 | 4580 | | } |
| | | 4581 | | else |
| | 0 | 4582 | | { |
| | 0 | 4583 | | left = basePart.GetParts(UriComponents.Scheme | UriComponents.UserInfo, uriFormat) |
| | 0 | 4584 | | + '[' + basePart.DnsSafeHost + ']' |
| | 0 | 4585 | | + basePart.GetParts(UriComponents.KeepDelimiter | UriComponents.Port, uriFormat); |
| | 0 | 4586 | | } |
| | 0 | 4587 | | } |
| | | 4588 | | else |
| | 0 | 4589 | | { |
| | 0 | 4590 | | if (basePart.IsImplicitFile) |
| | 0 | 4591 | | { |
| | 0 | 4592 | | if (basePart.IsDosPath) |
| | 0 | 4593 | | { |
| | | 4594 | | // The FILE DOS path comes as /c:/path, we have to exclude first 3 chars from compression |
| | 0 | 4595 | | Compress(path, 3, ref length, basePart.Syntax); |
| | 0 | 4596 | | return string.Concat(path.AsSpan(1, length - 1), extra); |
| | | 4597 | | } |
| | 0 | 4598 | | else if (!OperatingSystem.IsWindows() && basePart.IsUnixPath) |
| | 0 | 4599 | | { |
| | 0 | 4600 | | left = basePart.GetParts(UriComponents.Host, UriFormat.Unescaped); |
| | 0 | 4601 | | } |
| | | 4602 | | else |
| | 0 | 4603 | | { |
| | 0 | 4604 | | left = @"\\" + basePart.GetParts(UriComponents.Host, UriFormat.Unescaped); |
| | 0 | 4605 | | } |
| | 0 | 4606 | | } |
| | | 4607 | | else |
| | 0 | 4608 | | { |
| | 0 | 4609 | | left = basePart.GetParts(UriComponents.SchemeAndServer | UriComponents.UserInfo, uriFormat); |
| | 0 | 4610 | | } |
| | 0 | 4611 | | } |
| | | 4612 | | //compress the path |
| | 0 | 4613 | | Compress(path, basePart.SecuredPathIndex, ref length, basePart.Syntax); |
| | 0 | 4614 | | return string.Concat(left, path.AsSpan(0, length), extra); |
| | 0 | 4615 | | } |
| | | 4616 | | |
| | | 4617 | | // |
| | | 4618 | | // PathDifference |
| | | 4619 | | // |
| | | 4620 | | // Performs the relative path calculation for MakeRelative() |
| | | 4621 | | // |
| | | 4622 | | // Inputs: |
| | | 4623 | | // <argument> path1 |
| | | 4624 | | // <argument> path2 |
| | | 4625 | | // Paths for which we calculate the difference |
| | | 4626 | | // |
| | | 4627 | | // <argument> compareCase |
| | | 4628 | | // False if we consider characters that differ only in case to be |
| | | 4629 | | // equal |
| | | 4630 | | // |
| | | 4631 | | // Returns: |
| | | 4632 | | // A string which is the relative path difference between <path1> and |
| | | 4633 | | // <path2> such that if <path1> and the calculated difference are used |
| | | 4634 | | // as arguments to Combine(), <path2> is returned |
| | | 4635 | | // |
| | | 4636 | | // Throws: |
| | | 4637 | | // Nothing |
| | | 4638 | | // |
| | | 4639 | | private static string PathDifference(string path1, string path2, bool compareCase) |
| | 0 | 4640 | | { |
| | | 4641 | | int i; |
| | 0 | 4642 | | int si = -1; |
| | | 4643 | | |
| | 0 | 4644 | | for (i = 0; (i < path1.Length) && (i < path2.Length); ++i) |
| | 0 | 4645 | | { |
| | 0 | 4646 | | if ((path1[i] != path2[i]) |
| | 0 | 4647 | | && (compareCase |
| | 0 | 4648 | | || (char.ToLowerInvariant(path1[i]) |
| | 0 | 4649 | | != char.ToLowerInvariant(path2[i])))) |
| | 0 | 4650 | | { |
| | 0 | 4651 | | break; |
| | | 4652 | | } |
| | 0 | 4653 | | else if (path1[i] == '/') |
| | 0 | 4654 | | { |
| | 0 | 4655 | | si = i; |
| | 0 | 4656 | | } |
| | 0 | 4657 | | } |
| | | 4658 | | |
| | 0 | 4659 | | if (i == 0) |
| | 0 | 4660 | | { |
| | 0 | 4661 | | return path2; |
| | | 4662 | | } |
| | 0 | 4663 | | if ((i == path1.Length) && (i == path2.Length)) |
| | 0 | 4664 | | { |
| | 0 | 4665 | | return string.Empty; |
| | | 4666 | | } |
| | | 4667 | | |
| | 0 | 4668 | | StringBuilder relPath = new StringBuilder(); |
| | | 4669 | | // Walk down several dirs |
| | 0 | 4670 | | for (; i < path1.Length; ++i) |
| | 0 | 4671 | | { |
| | 0 | 4672 | | if (path1[i] == '/') |
| | 0 | 4673 | | { |
| | 0 | 4674 | | relPath.Append("../"); |
| | 0 | 4675 | | } |
| | 0 | 4676 | | } |
| | | 4677 | | // Same path except that path1 ended with a file name and path2 didn't |
| | 0 | 4678 | | if (relPath.Length == 0 && path2.Length - 1 == si) |
| | 0 | 4679 | | return "./"; // Truncate the file name |
| | 0 | 4680 | | return relPath.Append(path2.AsSpan(si + 1)).ToString(); |
| | 0 | 4681 | | } |
| | | 4682 | | |
| | | 4683 | | // |
| | | 4684 | | // MakeRelative (toUri) |
| | | 4685 | | // |
| | | 4686 | | // Return a relative path which when applied to this Uri would create the |
| | | 4687 | | // resulting Uri <toUri> |
| | | 4688 | | // |
| | | 4689 | | // Inputs: |
| | | 4690 | | // <argument> toUri |
| | | 4691 | | // Uri to which we calculate the transformation from this Uri |
| | | 4692 | | // |
| | | 4693 | | // Returns: |
| | | 4694 | | // If the 2 Uri are common except for a relative path difference, then that |
| | | 4695 | | // difference, else the display name of this Uri |
| | | 4696 | | // |
| | | 4697 | | // Throws: |
| | | 4698 | | // ArgumentNullException, InvalidOperationException |
| | | 4699 | | // |
| | | 4700 | | [Obsolete("Uri.MakeRelative has been deprecated. Use MakeRelativeUri(Uri uri) instead.")] |
| | | 4701 | | public string MakeRelative(Uri toUri) |
| | 0 | 4702 | | { |
| | 0 | 4703 | | ArgumentNullException.ThrowIfNull(toUri); |
| | | 4704 | | |
| | 0 | 4705 | | if (IsNotAbsoluteUri || toUri.IsNotAbsoluteUri) |
| | 0 | 4706 | | throw new InvalidOperationException(SR.net_uri_NotAbsolute); |
| | | 4707 | | |
| | 0 | 4708 | | if ((Scheme == toUri.Scheme) && (Host == toUri.Host) && (Port == toUri.Port)) |
| | 0 | 4709 | | return PathDifference(AbsolutePath, toUri.AbsolutePath, !IsUncOrDosPath); |
| | | 4710 | | |
| | 0 | 4711 | | return toUri.ToString(); |
| | 0 | 4712 | | } |
| | | 4713 | | |
| | | 4714 | | /// <internalonly/> |
| | | 4715 | | [Obsolete("Uri.Canonicalize has been deprecated and is not supported.")] |
| | | 4716 | | protected virtual void Canonicalize() |
| | 0 | 4717 | | { |
| | | 4718 | | // this method if suppressed by the derived class |
| | | 4719 | | // would lead to suppressing of a path compression |
| | | 4720 | | // It does not make much sense and violates Fxcop on calling a virtual method in the ctor. |
| | | 4721 | | // Should be deprecated and removed asap. |
| | 0 | 4722 | | } |
| | | 4723 | | |
| | | 4724 | | /// <internalonly/> |
| | | 4725 | | [Obsolete("Uri.Parse has been deprecated and is not supported.")] |
| | | 4726 | | protected virtual void Parse() |
| | 0 | 4727 | | { |
| | | 4728 | | // this method if suppressed by the derived class |
| | | 4729 | | // would lead to an unconstructed Uri instance. |
| | | 4730 | | // It does not make any sense and violates Fxcop on calling a virtual method in the ctor. |
| | | 4731 | | // Should be deprecated and removed asap. |
| | 0 | 4732 | | } |
| | | 4733 | | |
| | | 4734 | | /// <internalonly/> |
| | | 4735 | | [Obsolete("Uri.Escape has been deprecated and is not supported.")] |
| | | 4736 | | protected virtual void Escape() |
| | 0 | 4737 | | { |
| | | 4738 | | // this method if suppressed by the derived class |
| | | 4739 | | // would lead to the same effect as dontEscape=true. |
| | | 4740 | | // It does not make much sense and violates Fxcop on calling a virtual method in the ctor. |
| | | 4741 | | // Should be deprecated and removed asap. |
| | 0 | 4742 | | } |
| | | 4743 | | |
| | | 4744 | | // |
| | | 4745 | | // Unescape |
| | | 4746 | | // |
| | | 4747 | | // Convert any escape sequences in <path>. Escape sequences can be |
| | | 4748 | | // hex encoded reserved characters (e.g. %40 == '@') or hex encoded |
| | | 4749 | | // UTF-8 sequences (e.g. %C4%D2 == 'Latin capital Ligature Ij') |
| | | 4750 | | // |
| | | 4751 | | /// <internalonly/> |
| | | 4752 | | [Obsolete("Uri.Unescape has been deprecated. Use GetComponents() or Uri.UnescapeDataString() to unescape a Uri c |
| | | 4753 | | protected virtual string Unescape(string path) |
| | 0 | 4754 | | { |
| | | 4755 | | // This method is dangerous since it gives path unescaping control |
| | | 4756 | | // to the derived class without any permission demand. |
| | | 4757 | | // Should be deprecated and removed asap. |
| | | 4758 | | |
| | 0 | 4759 | | return UnescapeDataString(path); |
| | 0 | 4760 | | } |
| | | 4761 | | |
| | | 4762 | | [Obsolete("Uri.EscapeString has been deprecated. Use GetComponents() or Uri.EscapeDataString to escape a Uri com |
| | | 4763 | | protected static string EscapeString(string? str) => |
| | 0 | 4764 | | str is null ? string.Empty : |
| | 0 | 4765 | | UriHelper.EscapeString(str, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMarkHash); |
| | | 4766 | | |
| | | 4767 | | // |
| | | 4768 | | // CheckSecurity |
| | | 4769 | | // |
| | | 4770 | | // Check for any invalid or problematic character sequences |
| | | 4771 | | // |
| | | 4772 | | /// <internalonly/> |
| | | 4773 | | [Obsolete("Uri.CheckSecurity has been deprecated and is not supported.")] |
| | | 4774 | | protected virtual void CheckSecurity() |
| | 0 | 4775 | | { |
| | | 4776 | | // This method just does not make sense |
| | | 4777 | | // Should be deprecated and removed asap. |
| | 0 | 4778 | | } |
| | | 4779 | | |
| | | 4780 | | // |
| | | 4781 | | // IsReservedCharacter |
| | | 4782 | | // |
| | | 4783 | | // Determine whether a character is part of the reserved set |
| | | 4784 | | // |
| | | 4785 | | // Returns: |
| | | 4786 | | // true if <character> is reserved else false |
| | | 4787 | | // |
| | | 4788 | | /// <internalonly/> |
| | | 4789 | | [Obsolete("Uri.IsReservedCharacter has been deprecated and is not supported.")] |
| | | 4790 | | protected virtual bool IsReservedCharacter(char character) |
| | 0 | 4791 | | { |
| | | 4792 | | // This method just does not make sense as protected virtual |
| | | 4793 | | // It should go public static asap |
| | | 4794 | | |
| | 0 | 4795 | | return (character == ';') |
| | 0 | 4796 | | || (character == '/') |
| | 0 | 4797 | | || (character == ':') |
| | 0 | 4798 | | || (character == '@') // OK FS char |
| | 0 | 4799 | | || (character == '&') |
| | 0 | 4800 | | || (character == '=') |
| | 0 | 4801 | | || (character == '+') // OK FS char |
| | 0 | 4802 | | || (character == '$') // OK FS char |
| | 0 | 4803 | | || (character == ',') |
| | 0 | 4804 | | ; |
| | 0 | 4805 | | } |
| | | 4806 | | |
| | | 4807 | | // |
| | | 4808 | | // IsExcludedCharacter |
| | | 4809 | | // |
| | | 4810 | | // Determine if a character should be excluded from a URI and therefore be |
| | | 4811 | | // escaped |
| | | 4812 | | // |
| | | 4813 | | // Returns: |
| | | 4814 | | // true if <character> should be escaped else false |
| | | 4815 | | // |
| | | 4816 | | /// <internalonly/> |
| | | 4817 | | [Obsolete("Uri.IsExcludedCharacter has been deprecated and is not supported.")] |
| | | 4818 | | protected static bool IsExcludedCharacter(char character) |
| | 0 | 4819 | | { |
| | | 4820 | | // This method just does not make sense as protected |
| | | 4821 | | // It should go public static asap |
| | | 4822 | | |
| | | 4823 | | // |
| | | 4824 | | // the excluded characters... |
| | | 4825 | | // |
| | | 4826 | | |
| | 0 | 4827 | | return (character <= 0x20) |
| | 0 | 4828 | | || (character >= 0x7f) |
| | 0 | 4829 | | || (character == '<') |
| | 0 | 4830 | | || (character == '>') |
| | 0 | 4831 | | || (character == '#') |
| | 0 | 4832 | | || (character == '%') |
| | 0 | 4833 | | || (character == '"') |
| | 0 | 4834 | | |
| | 0 | 4835 | | // |
| | 0 | 4836 | | // the 'unwise' characters... |
| | 0 | 4837 | | // |
| | 0 | 4838 | | |
| | 0 | 4839 | | || (character == '{') |
| | 0 | 4840 | | || (character == '}') |
| | 0 | 4841 | | || (character == '|') |
| | 0 | 4842 | | || (character == '\\') |
| | 0 | 4843 | | || (character == '^') |
| | 0 | 4844 | | || (character == '[') |
| | 0 | 4845 | | || (character == ']') |
| | 0 | 4846 | | || (character == '`') |
| | 0 | 4847 | | ; |
| | 0 | 4848 | | } |
| | | 4849 | | |
| | | 4850 | | // |
| | | 4851 | | // IsBadFileSystemCharacter |
| | | 4852 | | // |
| | | 4853 | | // Determine whether a character would be an invalid character if used in |
| | | 4854 | | // a file system name. Note, this is really based on NTFS rules |
| | | 4855 | | // |
| | | 4856 | | // Returns: |
| | | 4857 | | // true if <character> would be a treated as a bad file system character |
| | | 4858 | | // else false |
| | | 4859 | | // |
| | | 4860 | | [Obsolete("Uri.IsBadFileSystemCharacter has been deprecated and is not supported.")] |
| | | 4861 | | protected virtual bool IsBadFileSystemCharacter(char character) |
| | 0 | 4862 | | { |
| | | 4863 | | // This method just does not make sense as protected virtual |
| | | 4864 | | // It should go public static asap |
| | | 4865 | | |
| | 0 | 4866 | | return (character < 0x20) |
| | 0 | 4867 | | || (character == ';') |
| | 0 | 4868 | | || (character == '/') |
| | 0 | 4869 | | || (character == '?') |
| | 0 | 4870 | | || (character == ':') |
| | 0 | 4871 | | || (character == '&') |
| | 0 | 4872 | | || (character == '=') |
| | 0 | 4873 | | || (character == ',') |
| | 0 | 4874 | | || (character == '*') |
| | 0 | 4875 | | || (character == '<') |
| | 0 | 4876 | | || (character == '>') |
| | 0 | 4877 | | || (character == '"') |
| | 0 | 4878 | | || (character == '|') |
| | 0 | 4879 | | || (character == '\\') |
| | 0 | 4880 | | || (character == '^') |
| | 0 | 4881 | | ; |
| | 0 | 4882 | | } |
| | | 4883 | | |
| | | 4884 | | //Used by UriBuilder |
| | | 4885 | | internal bool HasAuthority |
| | | 4886 | | { |
| | | 4887 | | get |
| | | 4888 | | { |
| | | 4889 | | return InFact(Flags.AuthorityFound); |
| | | 4890 | | } |
| | | 4891 | | } |
| | | 4892 | | } // class Uri |
| | | 4893 | | } // namespace System |
| | | 4894 | | |