/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System; using System.Linq; using System.Linq.Expressions; using System.Net; using System.Text.RegularExpressions; namespace wasSharp.Web.Utilities { public static class WebExtensions { private static readonly Func directURIEscapeDataString = ((Expression>) (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() .Select(o => Uri.EscapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) .ToArray()))).Compile(); private static readonly Func directURIUnescapeDataString = ((Expression>) (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() .Select( o => Uri.UnescapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) .ToArray()))).Compile(); /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// RFC3986 URI Escapes a string /// /// data - a string to escape /// /// an RFC3986 escaped string public static string URIEscapeDataString(this string data) { return directURIEscapeDataString(data); } /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// URI unescapes an RFC3986 URI escaped string /// /// data - a string to unescape /// /// the resulting string public static string URIUnescapeDataString(this string data) { return directURIUnescapeDataString(data); } /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// RFC1738 URL Escapes a string /// a string to escape /// an RFC1738 escaped string public static string URLEscapeDataString(this string data) { return WebUtility.UrlEncode(data); } /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// RFC1738 URL Unescape a string /// a string to unescape /// an RFC1738 unescaped string public static string URLUnescapeDataString(this string data) { return WebUtility.UrlDecode(data); } /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// Retrieves the port from an HttpListener prefix /// a HttpListener prefix /// the port of the HttpListener public static long GetPortFromPrefix(this string prefix) { var split = Regex.Replace( prefix, @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", "$2" ).Split(':'); long port; return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; } /////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Compare Two URIs /// /// the URI to compare /// the URI to compare to /// optionally the components to compare /// true if the URIs match public static bool UriIsEqualTo(this string l, string r, UriComponents components = UriComponents.Host | UriComponents.PathAndQuery) { try { Uri a = new Uri(l); Uri b = new Uri(r); return Uri.Compare(a, b, components, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0; } catch { return false; } } } }