wasSharp – Blame information for rev 8

Subversion Repositories:
Rev:
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  
8 office 108 public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout) : this(userAgent, new CookieContainer(), mediaType, null, null, timeout)
109 {
110 }
111  
7 office 112 public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType,
113 AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout)
114 {
115 var HTTPClientHandler = new HttpClientHandler
116 {
117 CookieContainer = cookieContainer,
118 UseCookies = true
119 };
120 if (HTTPClientHandler.SupportsRedirectConfiguration)
121 {
122 HTTPClientHandler.AllowAutoRedirect = true;
123 }
124 if (HTTPClientHandler.SupportsProxy)
125 {
126 HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy;
127 HTTPClientHandler.UseProxy = true;
128 }
129 if (HTTPClientHandler.SupportsAutomaticDecompression)
130 {
131 HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
132 }
133 HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
134  
135 HTTPClient = new HttpClient(HTTPClientHandler, false);
136 HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent);
137 if (authentication != null)
138 {
139 HTTPClient.DefaultRequestHeaders.Authorization = authentication;
140 }
141 if (headers != null)
142 {
143 foreach (var header in headers)
144 {
145 HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value);
146 }
147 }
148 HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout);
149 MediaType = mediaType;
150 }
151  
152 ///////////////////////////////////////////////////////////////////////////
153 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
154 ///////////////////////////////////////////////////////////////////////////
155 /// <summary>
156 /// Sends a PUT request to an URL with binary data.
157 /// </summary>
158 /// <param name="URL">the url to send the message to</param>
159 /// <param name="data">key-value pairs to send</param>
160 /// <param name="headers">headers to send with the request</param>
161 public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers)
162 {
163 try
164 {
165 using (var content = new ByteArrayContent(data))
166 {
167 using (
168 var request = new HttpRequestMessage
169 {
170 RequestUri = new Uri(URL),
171 Method = HttpMethod.Put,
172 Content = content
173 })
174 {
175 foreach (var header in headers)
176 {
177 request.Headers.Add(header.Key, header.Value);
178 }
179 using (var response = await HTTPClient.SendAsync(request))
180 {
181 return response.IsSuccessStatusCode
182 ? await response.Content.ReadAsByteArrayAsync()
183 : null;
184 }
185 }
186 }
187 }
188 catch (Exception)
189 {
190 return null;
191 }
192 }
193  
194 ///////////////////////////////////////////////////////////////////////////
195 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
196 ///////////////////////////////////////////////////////////////////////////
197 /// <summary>
198 /// Sends a PUT request to an URL with text.
199 /// </summary>
200 /// <param name="URL">the url to send the message to</param>
201 /// <param name="data">key-value pairs to send</param>
202 /// <param name="headers">headers to send with the request</param>
203 public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers)
204 {
205 try
206 {
207 using (var content =
208 new StringContent(data, Encoding.UTF8, MediaType))
209 {
210 using (
211 var request = new HttpRequestMessage
212 {
213 RequestUri = new Uri(URL),
214 Method = HttpMethod.Put,
215 Content = content
216 })
217 {
218 foreach (var header in headers)
219 {
220 request.Headers.Add(header.Key, header.Value);
221 }
222 using (var response = await HTTPClient.SendAsync(request))
223 {
224 return response.IsSuccessStatusCode
225 ? await response.Content.ReadAsByteArrayAsync()
226 : null;
227 }
228 }
229 }
230 }
231 catch (Exception)
232 {
233 return null;
234 }
235 }
236  
237 ///////////////////////////////////////////////////////////////////////////
238 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
239 ///////////////////////////////////////////////////////////////////////////
240 /// <summary>
241 /// Sends a PUT request to an URL with binary data.
242 /// </summary>
243 /// <param name="URL">the url to send the message to</param>
244 /// <param name="data">key-value pairs to send</param>
245 public async Task<byte[]> PUT(string URL, byte[] data)
246 {
247 try
248 {
249 using (var content = new ByteArrayContent(data))
250 {
251 using (var response = await HTTPClient.PutAsync(URL, content))
252 {
253 return response.IsSuccessStatusCode
254 ? await response.Content.ReadAsByteArrayAsync()
255 : null;
256 }
257 }
258 }
259 catch (Exception)
260 {
261 return null;
262 }
263 }
264  
265 ///////////////////////////////////////////////////////////////////////////
266 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
267 ///////////////////////////////////////////////////////////////////////////
268 /// <summary>
269 /// Sends a PUT request to an URL with text.
270 /// </summary>
271 /// <param name="URL">the url to send the message to</param>
272 /// <param name="data">key-value pairs to send</param>
273 public async Task<byte[]> PUT(string URL, string data)
274 {
275 try
276 {
277 using (var content =
278 new StringContent(data, Encoding.UTF8, MediaType))
279 {
280 using (var response = await HTTPClient.PutAsync(URL, content))
281 {
282 return response.IsSuccessStatusCode
283 ? await response.Content.ReadAsByteArrayAsync()
284 : null;
285 }
286 }
287 }
288 catch (Exception)
289 {
290 return null;
291 }
292 }
293  
294 ///////////////////////////////////////////////////////////////////////////
295 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
296 ///////////////////////////////////////////////////////////////////////////
297 /// <summary>
298 /// Sends a POST request to an URL with set key-value pairs.
299 /// </summary>
300 /// <param name="URL">the url to send the message to</param>
301 /// <param name="message">key-value pairs to send</param>
302 public async Task<byte[]> POST(string URL, Dictionary<string, string> message)
303 {
304 try
305 {
306 using (var content =
307 new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType))
308 {
309 using (var response = await HTTPClient.PostAsync(URL, content))
310 {
311 return response.IsSuccessStatusCode
312 ? await response.Content.ReadAsByteArrayAsync()
313 : null;
314 }
315 }
316 }
317 catch (Exception)
318 {
319 return null;
320 }
321 }
322  
323 ///////////////////////////////////////////////////////////////////////////
324 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
325 ///////////////////////////////////////////////////////////////////////////
326 /// <summary>
327 /// Sends a GET request to an URL with set key-value pairs.
328 /// </summary>
329 /// <param name="URL">the url to send the message to</param>
330 /// <param name="message">key-value pairs to send</param>
331 public async Task<byte[]> GET(string URL, Dictionary<string, string> message)
332 {
333 try
334 {
335 using (var response =
336 await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message)))
337 {
338 return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null;
339 }
340 }
341 catch (Exception)
342 {
343 return null;
344 }
345 }
346 }
1 zed 347 }
348 }