wasSharp – Diff between revs 27 and 32
?pathlinks?
Rev 27 | Rev 32 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Linq; |
8 | using System.Linq; |
|
9 | using System.Linq.Expressions; |
9 | using System.Linq.Expressions; |
|
10 | using System.Net; |
10 | using System.Net; |
|
11 | using System.Text.RegularExpressions; |
11 | using System.Text.RegularExpressions; |
|
12 | |
12 | |
|
13 | namespace wasSharp.Web.Utilities |
13 | namespace wasSharp.Web.Utilities |
|
14 | { |
14 | { |
|
15 | public static class WebExtensions |
15 | public static class WebExtensions |
|
16 | { |
16 | { |
|
17 | private static readonly Func<string, string> directURIEscapeDataString = |
17 | private static readonly Func<string, string> directURIEscapeDataString = |
|
18 | ((Expression<Func<string, string>>) |
18 | ((Expression<Func<string, string>>) |
|
19 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() |
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)))) |
20 | .Select(o => Uri.EscapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) |
|
21 | .ToArray()))).Compile(); |
21 | .ToArray()))).Compile(); |
|
22 | |
22 | |
|
23 | private static readonly Func<string, string> directURIUnescapeDataString = |
23 | private static readonly Func<string, string> directURIUnescapeDataString = |
|
24 | ((Expression<Func<string, string>>) |
24 | ((Expression<Func<string, string>>) |
|
25 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() |
25 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765) / 32766).AsParallel() |
|
26 | .Select( |
26 | .Select( |
|
27 | o => Uri.UnescapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) |
27 | o => Uri.UnescapeDataString(data.Substring(o * 32766, Math.Min(32766, data.Length - o * 32766)))) |
|
28 | .ToArray()))).Compile(); |
28 | .ToArray()))).Compile(); |
|
29 | |
29 | |
|
30 | /////////////////////////////////////////////////////////////////////////// |
30 | /////////////////////////////////////////////////////////////////////////// |
|
31 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
31 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
32 | /////////////////////////////////////////////////////////////////////////// |
32 | /////////////////////////////////////////////////////////////////////////// |
|
33 | /// <summary>RFC3986 URI Escapes a string</summary> |
33 | /// <summary>RFC3986 URI Escapes a string</summary> |
|
34 | /// <remarks> |
34 | /// <remarks> |
|
35 | /// data - a string to escape |
35 | /// data - a string to escape |
|
36 | /// </remarks> |
36 | /// </remarks> |
|
37 | /// <returns>an RFC3986 escaped string</returns> |
37 | /// <returns>an RFC3986 escaped string</returns> |
|
38 | public static string URIEscapeDataString(this string data) |
38 | public static string URIEscapeDataString(this string data) |
|
39 | { |
39 | { |
|
40 | return directURIEscapeDataString(data); |
40 | return directURIEscapeDataString(data); |
|
41 | } |
41 | } |
|
42 | |
42 | |
|
43 | /////////////////////////////////////////////////////////////////////////// |
43 | /////////////////////////////////////////////////////////////////////////// |
|
44 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
44 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
45 | /////////////////////////////////////////////////////////////////////////// |
45 | /////////////////////////////////////////////////////////////////////////// |
|
46 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
46 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
|
47 | /// <remarks> |
47 | /// <remarks> |
|
48 | /// data - a string to unescape |
48 | /// data - a string to unescape |
|
49 | /// </remarks> |
49 | /// </remarks> |
|
50 | /// <returns>the resulting string</returns> |
50 | /// <returns>the resulting string</returns> |
|
51 | public static string URIUnescapeDataString(this string data) |
51 | public static string URIUnescapeDataString(this string data) |
|
52 | { |
52 | { |
|
53 | return directURIUnescapeDataString(data); |
53 | return directURIUnescapeDataString(data); |
|
54 | } |
54 | } |
|
55 | |
55 | |
|
56 | /////////////////////////////////////////////////////////////////////////// |
56 | /////////////////////////////////////////////////////////////////////////// |
|
57 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
57 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
|
58 | /////////////////////////////////////////////////////////////////////////// |
58 | /////////////////////////////////////////////////////////////////////////// |
|
59 | /// <summary>RFC1738 URL Escapes a string</summary> |
59 | /// <summary>RFC1738 URL Escapes a string</summary> |
|
60 | /// <param name="data">a string to escape</param> |
60 | /// <param name="data">a string to escape</param> |
|
61 | /// <returns>an RFC1738 escaped string</returns> |
61 | /// <returns>an RFC1738 escaped string</returns> |
|
62 | public static string URLEscapeDataString(this string data) |
62 | public static string URLEscapeDataString(this string data) |
|
63 | { |
63 | { |
|
64 | return WebUtility.UrlEncode(data); |
64 | return WebUtility.UrlEncode(data); |
|
65 | } |
65 | } |
|
66 | |
66 | |
|
67 | /////////////////////////////////////////////////////////////////////////// |
67 | /////////////////////////////////////////////////////////////////////////// |
|
68 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
68 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
|
69 | /////////////////////////////////////////////////////////////////////////// |
69 | /////////////////////////////////////////////////////////////////////////// |
|
70 | /// <summary>RFC1738 URL Unescape a string</summary> |
70 | /// <summary>RFC1738 URL Unescape a string</summary> |
|
71 | /// <param name="data">a string to unescape</param> |
71 | /// <param name="data">a string to unescape</param> |
|
72 | /// <returns>an RFC1738 unescaped string</returns> |
72 | /// <returns>an RFC1738 unescaped string</returns> |
|
73 | public static string URLUnescapeDataString(this string data) |
73 | public static string URLUnescapeDataString(this string data) |
|
74 | { |
74 | { |
|
75 | return WebUtility.UrlDecode(data); |
75 | return WebUtility.UrlDecode(data); |
|
76 | } |
76 | } |
|
77 | |
77 | |
|
78 | /////////////////////////////////////////////////////////////////////////// |
78 | /////////////////////////////////////////////////////////////////////////// |
|
79 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
79 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
|
80 | /////////////////////////////////////////////////////////////////////////// |
80 | /////////////////////////////////////////////////////////////////////////// |
|
- | 81 | /// <summary>Retrieves the port from an HttpListener prefix</summary> |
||
81 | /// <param name="prefix">a HttpListener prefix</param> |
82 | /// <param name="prefix">a HttpListener prefix</param> |
|
82 | /// <returns>the port of the HttpListener</returns> |
83 | /// <returns>the port of the HttpListener</returns> |
|
83 | public static long GetPortFromPrefix(this string prefix) |
84 | public static long GetPortFromPrefix(this string prefix) |
|
84 | { |
85 | { |
|
85 | var split = Regex.Replace( |
86 | var split = Regex.Replace( |
|
86 | prefix, |
87 | prefix, |
|
87 | @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", |
88 | @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", |
|
88 | "$2" |
89 | "$2" |
|
89 | ).Split(':'); |
90 | ).Split(':'); |
|
90 | long port; |
91 | long port; |
|
91 | return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; |
92 | return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; |
|
92 | } |
93 | } |
|
- | 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 | } |
||
93 | } |
121 | } |
|
94 | } |
122 | } |
|
95 | |
123 | |