corrade-vassal – Diff between revs 14 and 16
?pathlinks?
Rev 14 | Rev 16 | |||
---|---|---|---|---|
Line 3... | Line 3... | |||
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 | /////////////////////////////////////////////////////////////////////////// |
|
Line 6... | Line 6... | |||
6 | |
6 | |
|
- | 7 | using System; |
||
7 | using System; |
8 | using System.Collections.Generic; |
|
8 | using System.Linq; |
9 | using System.Linq; |
|
9 | using System.Linq.Expressions; |
10 | using System.Linq.Expressions; |
|
- | 11 | using System.Net; |
||
- | 12 | using System.Net.Http; |
||
- | 13 | using System.Net.Http.Headers; |
||
- | 14 | using System.Text; |
||
Line 10... | Line 15... | |||
10 | using System.Net; |
15 | using System.Threading.Tasks; |
|
11 | |
16 | |
|
12 | namespace wasSharp |
17 | namespace wasSharp |
|
13 | { |
18 | { |
|
- | 19 | public static class Web |
||
- | 20 | { |
||
- | 21 | private static readonly Func<string, string> directURIEscapeDataString = |
||
- | 22 | ((Expression<Func<string, string>>) |
||
- | 23 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
||
- | 24 | .Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
||
- | 25 | .ToArray()))).Compile(); |
||
- | 26 | |
||
- | 27 | private static readonly Func<string, string> directURIUnescapeDataString = |
||
- | 28 | ((Expression<Func<string, string>>) |
||
- | 29 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
||
- | 30 | .Select( |
||
- | 31 | o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
||
14 | public class Web |
32 | .ToArray()))).Compile(); |
|
15 | { |
33 | |
|
16 | /////////////////////////////////////////////////////////////////////////// |
34 | /////////////////////////////////////////////////////////////////////////// |
|
17 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
35 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
18 | /////////////////////////////////////////////////////////////////////////// |
36 | /////////////////////////////////////////////////////////////////////////// |
|
19 | /// <summary>RFC3986 URI Escapes a string</summary> |
37 | /// <summary>RFC3986 URI Escapes a string</summary> |
|
20 | /// <remarks> |
38 | /// <remarks> |
|
21 | /// data - a string to escape |
39 | /// data - a string to escape |
|
22 | /// </remarks> |
40 | /// </remarks> |
|
23 | /// <returns>an RFC3986 escaped string</returns> |
41 | /// <returns>an RFC3986 escaped string</returns> |
|
24 | public static Func<string, string> URIEscapeDataString = |
42 | public static string URIEscapeDataString(string data) |
|
25 | ((Expression<Func<string, string>>) |
- | ||
26 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766) |
43 | { |
|
Line 27... | Line 44... | |||
27 | .Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766))))) |
44 | return directURIEscapeDataString(data); |
|
28 | .ToArray()))).Compile(); |
45 | } |
|
29 | |
46 | |
|
30 | /////////////////////////////////////////////////////////////////////////// |
47 | /////////////////////////////////////////////////////////////////////////// |
|
31 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
48 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
32 | /////////////////////////////////////////////////////////////////////////// |
49 | /////////////////////////////////////////////////////////////////////////// |
|
33 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
50 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
|
34 | /// <remarks> |
51 | /// <remarks> |
|
35 | /// data - a string to unescape |
52 | /// data - a string to unescape |
|
36 | /// </remarks> |
53 | /// </remarks> |
|
37 | /// <returns>the resulting string</returns> |
54 | /// <returns>the resulting string</returns> |
|
38 | public static Func<string, string> URIUnescapeDataString = |
55 | public static string URIUnescapeDataString(string data) |
|
39 | ((Expression<Func<string, string>>) |
- | ||
40 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766) |
- | ||
Line 41... | Line 56... | |||
41 | .Select( |
56 | { |
|
42 | o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766))))) |
57 | return directURIUnescapeDataString(data); |
|
43 | .ToArray()))).Compile(); |
58 | } |
|
44 | |
59 | |
|
Line 61... | Line 76... | |||
61 | /// <returns>an RFC1738 unescaped string</returns> |
76 | /// <returns>an RFC1738 unescaped string</returns> |
|
62 | public static string URLUnescapeDataString(string data) |
77 | public static string URLUnescapeDataString(string data) |
|
63 | { |
78 | { |
|
64 | return WebUtility.UrlDecode(data); |
79 | return WebUtility.UrlDecode(data); |
|
65 | } |
80 | } |
|
- | 81 | |
||
- | 82 | /////////////////////////////////////////////////////////////////////////// |
||
- | 83 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 84 | /////////////////////////////////////////////////////////////////////////// |
||
- | 85 | // <summary>A portable HTTP client.</summar> |
||
- | 86 | public class wasHTTPClient |
||
- | 87 | { |
||
- | 88 | private readonly HttpClient HTTPClient; |
||
- | 89 | private readonly string MediaType; |
||
- | 90 | |
||
- | 91 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
||
- | 92 | uint timeout) |
||
- | 93 | { |
||
- | 94 | var HTTPClientHandler = new HttpClientHandler |
||
- | 95 | { |
||
- | 96 | CookieContainer = cookieContainer, |
||
- | 97 | UseCookies = true |
||
- | 98 | }; |
||
- | 99 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
||
- | 100 | { |
||
- | 101 | HTTPClientHandler.AllowAutoRedirect = true; |
||
- | 102 | } |
||
- | 103 | if (HTTPClientHandler.SupportsProxy) |
||
- | 104 | { |
||
- | 105 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
||
- | 106 | HTTPClientHandler.UseProxy = true; |
||
- | 107 | } |
||
- | 108 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
||
- | 109 | { |
||
- | 110 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
||
- | 111 | } |
||
- | 112 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
||
- | 113 | |
||
- | 114 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
||
- | 115 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
||
- | 116 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
||
- | 117 | MediaType = mediaType; |
||
- | 118 | } |
||
- | 119 | |
||
- | 120 | /////////////////////////////////////////////////////////////////////////// |
||
- | 121 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 122 | /////////////////////////////////////////////////////////////////////////// |
||
- | 123 | /// <summary> |
||
- | 124 | /// Sends a POST request to an URL with set key-value pairs. |
||
- | 125 | /// </summary> |
||
- | 126 | /// <param name="URL">the url to send the message to</param> |
||
- | 127 | /// <param name="message">key-value pairs to send</param> |
||
- | 128 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
||
- | 129 | { |
||
- | 130 | try |
||
- | 131 | { |
||
- | 132 | using (var content = |
||
- | 133 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
- | 134 | { |
||
- | 135 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
- | 136 | { |
||
- | 137 | return response.IsSuccessStatusCode |
||
- | 138 | ? await response.Content.ReadAsByteArrayAsync() |
||
- | 139 | : null; |
||
- | 140 | } |
||
- | 141 | } |
||
- | 142 | } |
||
- | 143 | catch (Exception) |
||
- | 144 | { |
||
- | 145 | return null; |
||
- | 146 | } |
||
- | 147 | } |
||
- | 148 | |
||
- | 149 | /////////////////////////////////////////////////////////////////////////// |
||
- | 150 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 151 | /////////////////////////////////////////////////////////////////////////// |
||
- | 152 | /// <summary> |
||
- | 153 | /// Sends a GET request to an URL with set key-value pairs. |
||
- | 154 | /// </summary> |
||
- | 155 | /// <param name="URL">the url to send the message to</param> |
||
- | 156 | /// <param name="message">key-value pairs to send</param> |
||
- | 157 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
||
- | 158 | { |
||
- | 159 | try |
||
- | 160 | { |
||
- | 161 | using (var response = |
||
- | 162 | await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message))) |
||
- | 163 | { |
||
- | 164 | return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null; |
||
- | 165 | } |
||
- | 166 | } |
||
- | 167 | catch (Exception) |
||
- | 168 | { |
||
- | 169 | return null; |
||
- | 170 | } |
||
- | 171 | } |
||
- | 172 | } |
||
66 | } |
173 | } |
|
67 | } |
174 | } |
|
68 | |
175 | |