/// <summary> /// Gets the prefix <see cref="Uri" /> that matches the specified <see cref="Uri" />. /// </summary> /// <param name="uri">The <see cref="Uri" /> to find the most specific prefix <see cref="Uri" /> for.</param> /// <param name="server"> /// The /// <see cref="WebDavServer" /> that hosts the WebDAV server and holds the collection /// of known prefixes. /// </param> /// <returns> /// The most specific <see cref="Uri" /> for the given <paramref name="uri" />. /// </returns> /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavInternalServerException">Unable to find correct server root</exception> /// <exception cref="WebDavInternalServerException"> /// <paramref name="uri" /> specifies a <see cref="Uri" /> that is not /// known to the <paramref name="server" />. /// </exception> public static Uri GetPrefixUri(this Uri uri, WebDavServer server) { string url = uri.ToString(); string exactPrefix = server.Listener.Prefixes .FirstOrDefault(item => url.StartsWith(item, StringComparison.OrdinalIgnoreCase)); if (!IsNullOrEmpty(exactPrefix)) { return new Uri(exactPrefix); } string wildcardUrl = new UriBuilder(uri) { Host = "WebDAVSharpSpecialHostTag" } .ToString().Replace("WebDAVSharpSpecialHostTag", "*"); string wildcardPrefix = server.Listener.Prefixes .FirstOrDefault(item => wildcardUrl.StartsWith(item, StringComparison.OrdinalIgnoreCase)); if (!IsNullOrEmpty(wildcardPrefix)) { return new Uri(wildcardPrefix.Replace("://*", $"://{uri.Host}")); } throw new WebDavInternalServerException("Unable to find correct server root"); }
private static Uri GetPrefixWithWildCard(Uri uri, WebDavServer server) { foreach (var wc in AllIpWildChards) { string wildcardUrl = new UriBuilder(uri) { Host = "WebDAVSharpSpecialHostTag" } .ToString().Replace("WebDAVSharpSpecialHostTag", wc); string wildcardPrefix = server.Listener.Prefixes .FirstOrDefault(item => wildcardUrl.StartsWith(item, StringComparison.OrdinalIgnoreCase)); if (!String.IsNullOrEmpty(wildcardPrefix)) { return new Uri(wildcardPrefix.Replace("://" + wc, string.Format("://{0}", uri.Host))); } } return null; }