public static bool TryParse(string uriString, out UriComponents uriComponents) { uriString = uriString.Replace('\\', '/'); if (RootedPathRegex.IsMatch(uriString)) { uriString = $"file:///{uriString}"; } Match absoluteUriMatch = AbsoluteUriRegex.Match(uriString); if (!absoluteUriMatch.Success) { uriComponents = null; return(false); } string scheme = absoluteUriMatch.Groups["scheme"].Value; string userInfo = absoluteUriMatch.Groups["userInfo"].Value; string path = absoluteUriMatch.Groups["path"].Value; string query = absoluteUriMatch.Groups["query"].Value; string fragment = absoluteUriMatch.Groups["fragment"].Value; string host; int port; string hostGroupValue = absoluteUriMatch.Groups["host"].Value; string portGroupValue = absoluteUriMatch.Groups["port"].Value; if (portGroupValue.IsNullOrEmpty()) { host = hostGroupValue; port = GetDefaultPort(scheme); } else if (!Int32.TryParse(portGroupValue.Substring(1), out port)) { host = hostGroupValue + portGroupValue; port = -1; } else { host = hostGroupValue; } uriComponents = new UriComponents(scheme, userInfo, host, port, path, query, fragment); return(true); }
public static bool TryParse(string uriString, out UriComponents uriComponents) { uriString = uriString.Replace('\\', '/'); if (RootedPathRegex.Match(uriString) != null) { uriString = "file:///" + uriString; } string[] absoluteUriMatch = AbsoluteUriRegex.Match(uriString); if (absoluteUriMatch == null) { uriComponents = null; return(false); } string scheme = absoluteUriMatch[1].DefaultIfNullOrEmpty(); string userInfo = absoluteUriMatch[4].DefaultIfNullOrEmpty(); string path = absoluteUriMatch[7].DefaultIfNullOrEmpty(); string query = absoluteUriMatch[8].DefaultIfNullOrEmpty(); string fragment = absoluteUriMatch[9].DefaultIfNullOrEmpty(); string host; int port; string hostGroupValue = absoluteUriMatch[5].DefaultIfNullOrEmpty(); string portGroupValue = absoluteUriMatch[6].DefaultIfNullOrEmpty(); if (portGroupValue.IsNullOrEmpty()) { host = hostGroupValue; port = GetDefaultPort(scheme); } else if (!Int32.TryParse(portGroupValue.Substring(1), out port)) { host = hostGroupValue + portGroupValue; port = -1; } else { host = hostGroupValue; } uriComponents = new UriComponents(scheme, userInfo, host, port, path, query, fragment); return(true); }
public static bool TryCreateAbsoluteUri(System.Uri baseUri, string relativeUriString, out System.Uri uri) { UriComponents baseUriComponents; if (!TryGetUriComponents(baseUri, out baseUriComponents)) { uri = null; return(false); } UriComponents uriComponents = baseUriComponents.Combine(relativeUriString); uri = new System.Uri(uriComponents.AbsoluteUri); uri["UriComponents"] = uriComponents; return(true); }
public static bool TryGetUriComponents(System.Uri uri, out UriComponents uriComponents) { if (uri.HasOwnProperty("UriComponents")) { uriComponents = (UriComponents)uri["UriComponents"]; } else { if (!UriComponents.TryParse(uri.AbsoluteUri, out uriComponents)) { uriComponents = null; } uri["UriComponents"] = uriComponents; } return(uriComponents != null); }