wasSharp – Blame information for rev 33
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
18 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Linq; |
||
9 | using System.Linq.Expressions; |
||
10 | using System.Net; |
||
11 | using System.Text.RegularExpressions; |
||
12 | |||
13 | namespace wasSharp.Web.Utilities |
||
14 | { |
||
15 | public static class WebExtensions |
||
16 | { |
||
17 | private static readonly Func<string, string> directURIEscapeDataString = |
||
18 | ((Expression<Func<string, string>>) |
||
27 | office | 19 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() |
20 | .Select(o => Uri.EscapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) |
||
18 | office | 21 | .ToArray()))).Compile(); |
22 | |||
23 | private static readonly Func<string, string> directURIUnescapeDataString = |
||
24 | ((Expression<Func<string, string>>) |
||
27 | office | 25 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() |
18 | office | 26 | .Select( |
27 | office | 27 | o => Uri.UnescapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) |
18 | office | 28 | .ToArray()))).Compile(); |
29 | |||
30 | /////////////////////////////////////////////////////////////////////////// |
||
31 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
||
32 | /////////////////////////////////////////////////////////////////////////// |
||
33 | /// <summary>RFC3986 URI Escapes a string</summary> |
||
34 | /// <remarks> |
||
35 | /// data - a string to escape |
||
36 | /// </remarks> |
||
37 | /// <returns>an RFC3986 escaped string</returns> |
||
38 | public static string URIEscapeDataString(this string data) |
||
39 | { |
||
40 | return directURIEscapeDataString(data); |
||
41 | } |
||
42 | |||
43 | /////////////////////////////////////////////////////////////////////////// |
||
44 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
||
45 | /////////////////////////////////////////////////////////////////////////// |
||
46 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
||
47 | /// <remarks> |
||
48 | /// data - a string to unescape |
||
49 | /// </remarks> |
||
50 | /// <returns>the resulting string</returns> |
||
51 | public static string URIUnescapeDataString(this string data) |
||
52 | { |
||
53 | return directURIUnescapeDataString(data); |
||
54 | } |
||
55 | |||
56 | /////////////////////////////////////////////////////////////////////////// |
||
57 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
58 | /////////////////////////////////////////////////////////////////////////// |
||
59 | /// <summary>RFC1738 URL Escapes a string</summary> |
||
60 | /// <param name="data">a string to escape</param> |
||
61 | /// <returns>an RFC1738 escaped string</returns> |
||
62 | public static string URLEscapeDataString(this string data) |
||
63 | { |
||
64 | return WebUtility.UrlEncode(data); |
||
65 | } |
||
66 | |||
67 | /////////////////////////////////////////////////////////////////////////// |
||
68 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
69 | /////////////////////////////////////////////////////////////////////////// |
||
70 | /// <summary>RFC1738 URL Unescape a string</summary> |
||
71 | /// <param name="data">a string to unescape</param> |
||
72 | /// <returns>an RFC1738 unescaped string</returns> |
||
73 | public static string URLUnescapeDataString(this string data) |
||
74 | { |
||
75 | return WebUtility.UrlDecode(data); |
||
76 | } |
||
77 | |||
78 | /////////////////////////////////////////////////////////////////////////// |
||
79 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
80 | /////////////////////////////////////////////////////////////////////////// |
||
32 | office | 81 | /// <summary>Retrieves the port from an HttpListener prefix</summary> |
18 | office | 82 | /// <param name="prefix">a HttpListener prefix</param> |
83 | /// <returns>the port of the HttpListener</returns> |
||
84 | public static long GetPortFromPrefix(this string prefix) |
||
85 | { |
||
86 | var split = Regex.Replace( |
||
87 | prefix, |
||
88 | @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", |
||
89 | "$2" |
||
90 | ).Split(':'); |
||
91 | long port; |
||
92 | return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; |
||
93 | } |
||
32 | office | 94 | |
95 | /////////////////////////////////////////////////////////////////////////// |
||
96 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
97 | /////////////////////////////////////////////////////////////////////////// |
||
98 | /// <summary> |
||
99 | /// Compare Two URIs |
||
100 | /// </summary> |
||
101 | /// <param name="l">the URI to compare</param> |
||
102 | /// <param name="r">the URI to compare to</param> |
||
103 | /// <param name="components">optionally the components to compare</param> |
||
104 | /// <returns>true if the URIs match</returns> |
||
105 | public static bool UriIsEqualTo(this string l, string r, |
||
106 | UriComponents components = UriComponents.Host | UriComponents.PathAndQuery) |
||
107 | { |
||
108 | try |
||
109 | { |
||
110 | Uri a = new Uri(l); |
||
111 | Uri b = new Uri(r); |
||
112 | |||
113 | return Uri.Compare(a, b, components, |
||
114 | UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0; |
||
115 | } |
||
116 | catch |
||
117 | { |
||
118 | return false; |
||
119 | } |
||
120 | } |
||
18 | office | 121 | } |
27 | office | 122 | } |