wasSharp – Blame information for rev 9

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