wasSharp – Blame information for rev 7
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | zed | 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; |
||
7 | office | 8 | using System.Collections.Generic; |
1 | zed | 9 | using System.Linq; |
6 | eva | 10 | using System.Linq.Expressions; |
3 | eva | 11 | using System.Net; |
7 | office | 12 | using System.Net.Http; |
13 | using System.Net.Http.Headers; |
||
14 | using System.Text; |
||
15 | using System.Text.RegularExpressions; |
||
16 | using System.Threading.Tasks; |
||
1 | zed | 17 | |
18 | namespace wasSharp |
||
19 | { |
||
7 | office | 20 | public static class Web |
1 | zed | 21 | { |
7 | office | 22 | private static readonly Func<string, string> directURIEscapeDataString = |
23 | ((Expression<Func<string, string>>) |
||
24 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
||
25 | .Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
||
26 | .ToArray()))).Compile(); |
||
27 | |||
28 | private static readonly Func<string, string> directURIUnescapeDataString = |
||
29 | ((Expression<Func<string, string>>) |
||
30 | (data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
||
31 | .Select( |
||
32 | o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
||
33 | .ToArray()))).Compile(); |
||
34 | |||
1 | zed | 35 | /////////////////////////////////////////////////////////////////////////// |
36 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
||
37 | /////////////////////////////////////////////////////////////////////////// |
||
38 | /// <summary>RFC3986 URI Escapes a string</summary> |
||
6 | eva | 39 | /// <remarks> |
40 | /// data - a string to escape |
||
41 | /// </remarks> |
||
1 | zed | 42 | /// <returns>an RFC3986 escaped string</returns> |
7 | office | 43 | public static string URIEscapeDataString(string data) |
44 | { |
||
45 | return directURIEscapeDataString(data); |
||
46 | } |
||
1 | zed | 47 | |
48 | /////////////////////////////////////////////////////////////////////////// |
||
49 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
||
50 | /////////////////////////////////////////////////////////////////////////// |
||
51 | /// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
||
6 | eva | 52 | /// <remarks> |
53 | /// data - a string to unescape |
||
54 | /// </remarks> |
||
1 | zed | 55 | /// <returns>the resulting string</returns> |
7 | office | 56 | public static string URIUnescapeDataString(string data) |
57 | { |
||
58 | return directURIUnescapeDataString(data); |
||
59 | } |
||
1 | zed | 60 | |
61 | /////////////////////////////////////////////////////////////////////////// |
||
62 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
63 | /////////////////////////////////////////////////////////////////////////// |
||
64 | /// <summary>RFC1738 URL Escapes a string</summary> |
||
65 | /// <param name="data">a string to escape</param> |
||
66 | /// <returns>an RFC1738 escaped string</returns> |
||
5 | eva | 67 | public static string URLEscapeDataString(string data) |
1 | zed | 68 | { |
3 | eva | 69 | return WebUtility.UrlEncode(data); |
1 | zed | 70 | } |
71 | |||
72 | /////////////////////////////////////////////////////////////////////////// |
||
73 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
74 | /////////////////////////////////////////////////////////////////////////// |
||
75 | /// <summary>RFC1738 URL Unescape a string</summary> |
||
76 | /// <param name="data">a string to unescape</param> |
||
77 | /// <returns>an RFC1738 unescaped string</returns> |
||
5 | eva | 78 | public static string URLUnescapeDataString(string data) |
1 | zed | 79 | { |
3 | eva | 80 | return WebUtility.UrlDecode(data); |
1 | zed | 81 | } |
7 | office | 82 | |
83 | /////////////////////////////////////////////////////////////////////////// |
||
84 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
85 | /////////////////////////////////////////////////////////////////////////// |
||
86 | /// <param name="prefix">a HttpListener prefix</param> |
||
87 | /// <returns>the port of the HttpListener</returns> |
||
88 | public static long GetPortFromPrefix(string prefix) |
||
89 | { |
||
90 | var split = Regex.Replace( |
||
91 | prefix, |
||
92 | @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", |
||
93 | "$2" |
||
94 | ).Split(':'); |
||
95 | long port; |
||
96 | return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; |
||
97 | } |
||
98 | |||
99 | /////////////////////////////////////////////////////////////////////////// |
||
100 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
101 | /////////////////////////////////////////////////////////////////////////// |
||
102 | // <summary>A portable HTTP client.</summar> |
||
103 | public class wasHTTPClient |
||
104 | { |
||
105 | private readonly HttpClient HTTPClient; |
||
106 | private readonly string MediaType; |
||
107 | |||
108 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
||
109 | AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout) |
||
110 | { |
||
111 | var HTTPClientHandler = new HttpClientHandler |
||
112 | { |
||
113 | CookieContainer = cookieContainer, |
||
114 | UseCookies = true |
||
115 | }; |
||
116 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
||
117 | { |
||
118 | HTTPClientHandler.AllowAutoRedirect = true; |
||
119 | } |
||
120 | if (HTTPClientHandler.SupportsProxy) |
||
121 | { |
||
122 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
||
123 | HTTPClientHandler.UseProxy = true; |
||
124 | } |
||
125 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
||
126 | { |
||
127 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
||
128 | } |
||
129 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
||
130 | |||
131 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
||
132 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
||
133 | if (authentication != null) |
||
134 | { |
||
135 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
||
136 | } |
||
137 | if (headers != null) |
||
138 | { |
||
139 | foreach (var header in headers) |
||
140 | { |
||
141 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
||
142 | } |
||
143 | } |
||
144 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
||
145 | MediaType = mediaType; |
||
146 | } |
||
147 | |||
148 | /////////////////////////////////////////////////////////////////////////// |
||
149 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
150 | /////////////////////////////////////////////////////////////////////////// |
||
151 | /// <summary> |
||
152 | /// Sends a PUT request to an URL with binary data. |
||
153 | /// </summary> |
||
154 | /// <param name="URL">the url to send the message to</param> |
||
155 | /// <param name="data">key-value pairs to send</param> |
||
156 | /// <param name="headers">headers to send with the request</param> |
||
157 | public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers) |
||
158 | { |
||
159 | try |
||
160 | { |
||
161 | using (var content = new ByteArrayContent(data)) |
||
162 | { |
||
163 | using ( |
||
164 | var request = new HttpRequestMessage |
||
165 | { |
||
166 | RequestUri = new Uri(URL), |
||
167 | Method = HttpMethod.Put, |
||
168 | Content = content |
||
169 | }) |
||
170 | { |
||
171 | foreach (var header in headers) |
||
172 | { |
||
173 | request.Headers.Add(header.Key, header.Value); |
||
174 | } |
||
175 | using (var response = await HTTPClient.SendAsync(request)) |
||
176 | { |
||
177 | return response.IsSuccessStatusCode |
||
178 | ? await response.Content.ReadAsByteArrayAsync() |
||
179 | : null; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | catch (Exception) |
||
185 | { |
||
186 | return null; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | /////////////////////////////////////////////////////////////////////////// |
||
191 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
192 | /////////////////////////////////////////////////////////////////////////// |
||
193 | /// <summary> |
||
194 | /// Sends a PUT request to an URL with text. |
||
195 | /// </summary> |
||
196 | /// <param name="URL">the url to send the message to</param> |
||
197 | /// <param name="data">key-value pairs to send</param> |
||
198 | /// <param name="headers">headers to send with the request</param> |
||
199 | public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers) |
||
200 | { |
||
201 | try |
||
202 | { |
||
203 | using (var content = |
||
204 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
205 | { |
||
206 | using ( |
||
207 | var request = new HttpRequestMessage |
||
208 | { |
||
209 | RequestUri = new Uri(URL), |
||
210 | Method = HttpMethod.Put, |
||
211 | Content = content |
||
212 | }) |
||
213 | { |
||
214 | foreach (var header in headers) |
||
215 | { |
||
216 | request.Headers.Add(header.Key, header.Value); |
||
217 | } |
||
218 | using (var response = await HTTPClient.SendAsync(request)) |
||
219 | { |
||
220 | return response.IsSuccessStatusCode |
||
221 | ? await response.Content.ReadAsByteArrayAsync() |
||
222 | : null; |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | catch (Exception) |
||
228 | { |
||
229 | return null; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /////////////////////////////////////////////////////////////////////////// |
||
234 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
235 | /////////////////////////////////////////////////////////////////////////// |
||
236 | /// <summary> |
||
237 | /// Sends a PUT request to an URL with binary data. |
||
238 | /// </summary> |
||
239 | /// <param name="URL">the url to send the message to</param> |
||
240 | /// <param name="data">key-value pairs to send</param> |
||
241 | public async Task<byte[]> PUT(string URL, byte[] data) |
||
242 | { |
||
243 | try |
||
244 | { |
||
245 | using (var content = new ByteArrayContent(data)) |
||
246 | { |
||
247 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
248 | { |
||
249 | return response.IsSuccessStatusCode |
||
250 | ? await response.Content.ReadAsByteArrayAsync() |
||
251 | : null; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | catch (Exception) |
||
256 | { |
||
257 | return null; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /////////////////////////////////////////////////////////////////////////// |
||
262 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
263 | /////////////////////////////////////////////////////////////////////////// |
||
264 | /// <summary> |
||
265 | /// Sends a PUT request to an URL with text. |
||
266 | /// </summary> |
||
267 | /// <param name="URL">the url to send the message to</param> |
||
268 | /// <param name="data">key-value pairs to send</param> |
||
269 | public async Task<byte[]> PUT(string URL, string data) |
||
270 | { |
||
271 | try |
||
272 | { |
||
273 | using (var content = |
||
274 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
275 | { |
||
276 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
277 | { |
||
278 | return response.IsSuccessStatusCode |
||
279 | ? await response.Content.ReadAsByteArrayAsync() |
||
280 | : null; |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | catch (Exception) |
||
285 | { |
||
286 | return null; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /////////////////////////////////////////////////////////////////////////// |
||
291 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
292 | /////////////////////////////////////////////////////////////////////////// |
||
293 | /// <summary> |
||
294 | /// Sends a POST request to an URL with set key-value pairs. |
||
295 | /// </summary> |
||
296 | /// <param name="URL">the url to send the message to</param> |
||
297 | /// <param name="message">key-value pairs to send</param> |
||
298 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
||
299 | { |
||
300 | try |
||
301 | { |
||
302 | using (var content = |
||
303 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
304 | { |
||
305 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
306 | { |
||
307 | return response.IsSuccessStatusCode |
||
308 | ? await response.Content.ReadAsByteArrayAsync() |
||
309 | : null; |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | catch (Exception) |
||
314 | { |
||
315 | return null; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /////////////////////////////////////////////////////////////////////////// |
||
320 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
321 | /////////////////////////////////////////////////////////////////////////// |
||
322 | /// <summary> |
||
323 | /// Sends a GET request to an URL with set key-value pairs. |
||
324 | /// </summary> |
||
325 | /// <param name="URL">the url to send the message to</param> |
||
326 | /// <param name="message">key-value pairs to send</param> |
||
327 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
||
328 | { |
||
329 | try |
||
330 | { |
||
331 | using (var response = |
||
332 | await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message))) |
||
333 | { |
||
334 | return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null; |
||
335 | } |
||
336 | } |
||
337 | catch (Exception) |
||
338 | { |
||
339 | return null; |
||
340 | } |
||
341 | } |
||
342 | } |
||
1 | zed | 343 | } |
344 | } |