private Dictionary <string, string[]> GetQualifiedSitesInsideLock(Uri current) { // we do our best, but can't do the impossible if (Sites == null) { return(null); } // cached? if (_qualifiedSites != null && _qualifiedSites.ContainsKey(current.Scheme)) { return(_qualifiedSites[current.Scheme]); } _qualifiedSites = _qualifiedSites ?? new Dictionary <string, Dictionary <string, string[]> >(); // convert sites into authority sites based upon current scheme // because some domains in the sites might not have a scheme -- and cache return(_qualifiedSites[current.Scheme] = Sites .ToDictionary( kvp => kvp.Key, kvp => kvp.Value.Select(d => new Uri(UriUtilityCore.StartWithScheme(d, current.Scheme)).GetLeftPart(UriPartial.Authority)).ToArray() )); // .ToDictionary will evaluate and create the dictionary immediately // the new value is .ToArray so it will also be evaluated immediately // therefore it is safe to return and exit the configuration lock }
/// <summary> /// Parses a domain name into a URI. /// </summary> /// <param name="domainName">The domain name to parse</param> /// <param name="currentUri">The currently requested URI. If the domain name is relative, the authority of URI will be used.</param> /// <returns>The domain name as a URI</returns> public static Uri ParseUriFromDomainName(string domainName, Uri currentUri) { // turn "/en" into "http://whatever.com/en" so it becomes a parseable uri var name = domainName.StartsWith("/") && currentUri != null ? currentUri.GetLeftPart(UriPartial.Authority) + domainName : domainName; var scheme = currentUri?.Scheme ?? Uri.UriSchemeHttp; return(new Uri(UriUtilityCore.TrimPathEndSlash(UriUtilityCore.StartWithScheme(name, scheme)))); }