// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace Microsoft.AspNet.Mvc.Internal { public static class UrlUtility { /// /// Returns a value that indicates whether the URL is local. An URL with an absolute path is considered local /// if it does not have a host/authority part. URLs using the virtual paths ('~/') are also local. /// /// The URL. /// true if the URL is local; otherwise, false. /// /// /// For example, the following URLs are considered local: /// /Views/Default/Index.html /// ~/Index.html /// /// /// The following URLs are non-local: /// ../Index.html /// http://www.contoso.com/ /// http://localhost/Index.html /// /// public static bool IsLocalUrl(string url) { return !string.IsNullOrEmpty(url) && // Allows "/" or "/foo" but not "//" or "/\". ((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) || // Allows "~/" or "~/foo". (url.Length > 1 && url[0] == '~' && url[1] == '/')); } } }