public string GetLeftPart(UriPartial part) { switch (part) { case UriPartial.Scheme: return(this.scheme + this.GetOpaqueWiseSchemeDelimiter()); case UriPartial.Authority: { if (this.host == string.Empty || this.scheme == Uri.UriSchemeMailto || this.scheme == Uri.UriSchemeNews) { return(string.Empty); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(this.scheme); stringBuilder.Append(this.GetOpaqueWiseSchemeDelimiter()); if (this.path.Length > 1 && this.path[1] == ':' && Uri.UriSchemeFile == this.scheme) { stringBuilder.Append('/'); } if (this.userinfo.Length > 0) { stringBuilder.Append(this.userinfo).Append('@'); } stringBuilder.Append(this.host); int defaultPort = Uri.GetDefaultPort(this.scheme); if (this.port != -1 && this.port != defaultPort) { stringBuilder.Append(':').Append(this.port); } return(stringBuilder.ToString()); } case UriPartial.Path: { StringBuilder stringBuilder2 = new StringBuilder(); stringBuilder2.Append(this.scheme); stringBuilder2.Append(this.GetOpaqueWiseSchemeDelimiter()); if (this.path.Length > 1 && this.path[1] == ':' && Uri.UriSchemeFile == this.scheme) { stringBuilder2.Append('/'); } if (this.userinfo.Length > 0) { stringBuilder2.Append(this.userinfo).Append('@'); } stringBuilder2.Append(this.host); int defaultPort = Uri.GetDefaultPort(this.scheme); if (this.port != -1 && this.port != defaultPort) { stringBuilder2.Append(':').Append(this.port); } stringBuilder2.Append(this.path); return(stringBuilder2.ToString()); } default: return(null); } }
private void Parse(string uriString) { if (uriString == null) { throw new ArgumentNullException("uriString"); } int length = uriString.Length; if (length <= 1) { throw new FormatException(); } int num = uriString.IndexOf(':'); if (num < 0) { if (uriString[0] == '/') { this.ParseAsUnixAbsoluteFilePath(uriString); } else { if (!uriString.StartsWith("\\\\")) { throw new FormatException("URI scheme was not recognized, nor input string is not recognized as an absolute file path."); } this.ParseAsWindowsUNC(uriString); } return; } if (num == 1) { if (!char.IsLetter(uriString[0])) { throw new FormatException("URI scheme must start with alphabet character."); } this.ParseAsWindowsAbsoluteFilePath(uriString); return; } else { this.scheme = uriString.Substring(0, num).ToLower(CultureInfo.InvariantCulture); if (!char.IsLetter(this.scheme[0])) { throw new FormatException("URI scheme must start with alphabet character."); } for (int i = 1; i < this.scheme.Length; i++) { if (!char.IsLetterOrDigit(this.scheme, i)) { switch (this.scheme[i]) { case '+': case '-': case '.': goto IL_132; } throw new FormatException("URI scheme must consist of one of alphabet, digits, '+', '-' or '.' character."); } IL_132 :; } uriString = uriString.Substring(num + 1); num = uriString.IndexOf('#'); if (!this.IsUnc && num != -1) { this.fragment = uriString.Substring(num); uriString = uriString.Substring(0, num); } num = uriString.IndexOf('?'); if (num != -1) { this.query = uriString.Substring(num); uriString = uriString.Substring(0, num); if (!this.userEscaped) { this.query = Uri.EscapeString(this.query); } } bool flag = this.scheme == Uri.UriSchemeFile && uriString.StartsWith("///"); if (uriString.StartsWith("//")) { if (uriString.StartsWith("////")) { flag = false; } uriString = uriString.TrimStart(new char[] { '/' }); if (uriString.Length > 1 && uriString[1] == ':') { flag = false; } } else if (!Uri.IsPredefinedScheme(this.scheme)) { this.path = uriString; this.isOpaquePart = true; return; } num = uriString.IndexOfAny(new char[] { '/' }); if (flag) { num = -1; } if (num == -1) { if (this.scheme != Uri.UriSchemeMailto && this.scheme != Uri.UriSchemeNews && this.scheme != Uri.UriSchemeFile) { this.path = "/"; } } else { this.path = uriString.Substring(num); uriString = uriString.Substring(0, num); } num = uriString.IndexOf("@"); if (num != -1) { this.userinfo = uriString.Substring(0, num); uriString = uriString.Remove(0, num + 1); } this.port = -1; num = uriString.LastIndexOf(":"); if (flag) { num = -1; } if (num != -1 && num != uriString.Length - 1) { string text = uriString.Remove(0, num + 1); if (text.Length > 1 && text[text.Length - 1] != ']') { try { this.port = (int)uint.Parse(text, CultureInfo.InvariantCulture); uriString = uriString.Substring(0, num); } catch (Exception) { throw new FormatException("Invalid URI: invalid port number"); } } } if (this.port == -1) { this.port = Uri.GetDefaultPort(this.scheme); } this.host = uriString; if (flag) { this.path = '/' + uriString; this.host = string.Empty; } else if (this.host.Length == 2 && this.host[1] == ':') { this.path = this.host + this.path; this.host = string.Empty; } else if (this.isUnixFilePath) { uriString = "//" + uriString; this.host = string.Empty; } else { if (this.host.Length == 0) { throw new FormatException("Invalid URI: The hostname could not be parsed"); } if (this.scheme == Uri.UriSchemeFile) { this.isUnc = true; } } if (this.scheme != Uri.UriSchemeMailto && this.scheme != Uri.UriSchemeNews && this.scheme != Uri.UriSchemeFile && this.reduce) { this.path = Uri.Reduce(this.path); } return; } }