/// <summary> /// Determines whether the specified URL is reserved or is inside a reserved path. /// </summary> /// <param name="url">The URL to check.</param> /// <returns> /// <c>true</c> if the specified URL is reserved; otherwise, <c>false</c>. /// </returns> public static bool IsReservedPathOrUrl(string url) { if (_reservedUrlsCache == null) { lock (Locker) { if (_reservedUrlsCache == null) { // store references to strings to determine changes _reservedPathsCache = GlobalSettings.ReservedPaths; _reservedUrlsCache = GlobalSettings.ReservedUrls; string _root = SystemDirectories.Root.Trim().ToLower(); // add URLs and paths to a new list StartsWithContainer _newReservedList = new StartsWithContainer(); foreach (string reservedUrl in _reservedUrlsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { //resolves the url to support tilde chars string reservedUrlTrimmed = IOHelper.ResolveUrl(reservedUrl).Trim().ToLower(); if (reservedUrlTrimmed.Length > 0) { _newReservedList.Add(reservedUrlTrimmed); } } foreach (string reservedPath in _reservedPathsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { bool trimEnd = !reservedPath.EndsWith("/"); //resolves the url to support tilde chars string reservedPathTrimmed = IOHelper.ResolveUrl(reservedPath).Trim().ToLower(); if (reservedPathTrimmed.Length > 0) { _newReservedList.Add(reservedPathTrimmed + (reservedPathTrimmed.EndsWith("/") ? "" : "/")); } } // use the new list from now on _reservedList = _newReservedList; } } } //The url should be cleaned up before checking: // * If it doesn't contain an '.' in the path then we assume it is a path based URL, if that is the case we should add an trailing '/' because all of our reservedPaths use a trailing '/' // * We shouldn't be comparing the query at all var pathPart = url.Split('?')[0]; if (!pathPart.Contains(".") && !pathPart.EndsWith("/")) { pathPart += "/"; } // return true if url starts with an element of the reserved list return(_reservedList.StartsWith(pathPart.ToLowerInvariant())); }
#pragma warning restore /// <summary> /// Determines whether the specified URL is reserved or is inside a reserved path. /// </summary> /// <param name="url">The URL to check.</param> /// <returns> /// <c>true</c> if the specified URL is reserved; otherwise, <c>false</c>. /// </returns> internal static bool IsReservedPathOrUrl(string url) { if (_reservedUrlsCache == null) { lock (_locker) { if (_reservedUrlsCache == null) { // store references to strings to determine changes _reservedPathsCache = GlobalSettings.ReservedPaths; _reservedUrlsCache = GlobalSettings.ReservedUrls; // add URLs and paths to a new list #pragma warning disable 0618 StartsWithContainer _newReservedList = new StartsWithContainer(); #pragma warning restore foreach (string reservedUrl in _reservedUrlsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { //resolves the url to support tilde chars string reservedUrlTrimmed = IOHelper.ResolveUrl(reservedUrl).Trim().ToLower(); if (reservedUrlTrimmed.Length > 0) _newReservedList.Add(reservedUrlTrimmed); } foreach (string reservedPath in _reservedPathsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { bool trimEnd = !reservedPath.EndsWith("/"); //resolves the url to support tilde chars string reservedPathTrimmed = IOHelper.ResolveUrl(reservedPath).Trim().ToLower(); if (reservedPathTrimmed.Length > 0) _newReservedList.Add(reservedPathTrimmed + (reservedPathTrimmed.EndsWith("/") ? "" : "/")); } // use the new list from now on _reservedList = _newReservedList; } } } //The url should be cleaned up before checking: // * If it doesn't contain an '.' in the path then we assume it is a path based URL, if that is the case we should add an trailing '/' because all of our reservedPaths use a trailing '/' // * We shouldn't be comparing the query at all var pathPart = url.Split('?')[0]; if (!pathPart.Contains(".") && !pathPart.EndsWith("/")) pathPart += "/"; // check if path is longer than one character, then if it does not start with / then add a / if (pathPart.Length > 1 && pathPart[0] != '/') { pathPart = '/' + pathPart; // fix because sometimes there is no leading /... depends on browser... } // return true if url starts with an element of the reserved list return _reservedList.StartsWith(pathPart.ToLowerInvariant()); }
public void TryFindLongestPrefix_DontFindIt(string word) { var target = new StartsWithContainer( "abc", "xyz", "abcd"); var actual = target.TryFindLongestPrefix(word, out var actualPrefix); Assert.IsFalse(actual); }
public void TryFindLongestPrefix_FindIt(string word, string expectedPrefix) { var target = new StartsWithContainer( "abc", "xyz", "abcd"); var actual = target.TryFindLongestPrefix(word, out var actualPrefix); Assert.IsTrue(actual); Assert.AreEqual(expectedPrefix, actualPrefix); }
#pragma warning restore /// <summary> /// Determines whether the specified URL is reserved or is inside a reserved path. /// </summary> /// <param name="url">The URL to check.</param> /// <returns> /// <c>true</c> if the specified URL is reserved; otherwise, <c>false</c>. /// </returns> internal static bool IsReservedPathOrUrl(string url) { if (_reservedUrlsCache == null) { lock (_locker) { if (_reservedUrlsCache == null) { // store references to strings to determine changes _reservedPathsCache = GlobalSettings.ReservedPaths; _reservedUrlsCache = GlobalSettings.ReservedUrls; // add URLs and paths to a new list #pragma warning disable 0618 StartsWithContainer _newReservedList = new StartsWithContainer(); #pragma warning restore foreach (string reservedUrl in _reservedUrlsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { //resolves the url to support tilde chars string reservedUrlTrimmed = IOHelper.ResolveUrl(reservedUrl).Trim().ToLower(); if (reservedUrlTrimmed.Length > 0) { _newReservedList.Add(reservedUrlTrimmed); } } foreach (string reservedPath in _reservedPathsCache.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { bool trimEnd = !reservedPath.EndsWith("/"); //resolves the url to support tilde chars string reservedPathTrimmed = IOHelper.ResolveUrl(reservedPath).Trim().ToLower(); if (reservedPathTrimmed.Length > 0) { _newReservedList.Add(reservedPathTrimmed + (reservedPathTrimmed.EndsWith("/") ? "" : "/")); } } // use the new list from now on _reservedList = _newReservedList; } } } //The url should be cleaned up before checking: // * If it doesn't contain an '.' in the path then we assume it is a path based URL, if that is the case we should add an trailing '/' because all of our reservedPaths use a trailing '/' // * We shouldn't be comparing the query at all var pathPart = url.Split('?')[0]; if (!pathPart.Contains(".") && !pathPart.EndsWith("/")) { pathPart += "/"; } // check if path is longer than one character, then if it does not start with / then add a / if (pathPart.Length > 1 && pathPart[0] != '/') { pathPart = '/' + pathPart; // fix because sometimes there is no leading /... depends on browser... } // return true if url starts with an element of the reserved list return(_reservedList.StartsWith(pathPart.ToLowerInvariant())); }