/
HttpUtils.cs
463 lines (413 loc) · 14.1 KB
/
HttpUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* ********************************************************************************
*
* Copyright 2010-2013 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *******************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web.Mvc;
namespace ElmcityUtils
{
// turn what would otherwise be exceptions into normal responses
// see http://blog.jonudell.net/2009/01/22/unifying-http-success-and-failure-in-net
public static class HttpWebRequestExtensions
{
public static HttpWebResponse GetResponse2(this WebRequest request)
{
HttpWebResponse response;
try
{
response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
catch (Exception e)
{
var msg = "GetResponse2" + " " + e.Message;
GenUtils.PriorityLogMsg("exception", msg, e.StackTrace);
throw new Exception(msg);
}
return response;
}
}
// simple, high-level encapsulation of basic stuff needed in an http response
public class HttpResponse
{
public HttpStatusCode status;
public string message;
public byte[] bytes;
public Dictionary<string, string> headers;
public HttpResponse(HttpStatusCode status, string message, byte[] bytes, Dictionary<string, string> headers)
{
this.status = status;
this.message = message;
this.bytes = bytes;
this.headers = headers;
}
public string DataAsString()
{
return Encoding.UTF8.GetString(this.bytes);
}
}
// basic http-related operations
public static class HttpUtils
{
public static string elmcity_user_agent = "elmcity";
public const int wait_secs = 3;
public const int retries = 3;
public const int timeout = 10;
// equivalent of curl --head
public static HttpResponse HeadFetchUrl(Uri url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
byte[] UTf8ByteArray = new byte[0];
return DoHttpWebRequest(request, UTf8ByteArray);
}
public static HttpResponse PostUrl(Uri uri, string query)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = query.Length;
var bytes = Encoding.UTF8.GetBytes(query);
return DoHttpWebRequest(request, bytes);
}
// return header if it appears in the request context
public static String MaybeExtractHeaderFromRequestContext(string header_name, ControllerContext context)
{
var request_headers = context.HttpContext.Request.Headers;
if (request_headers.AllKeys.Contains(header_name))
return request_headers[header_name];
else
return null;
}
// return header if it appears in the response to an HTTP HEAD
public static String MaybeGetHeaderFromUriHead(string header, Uri uri)
{
var head_response = HttpUtils.HeadFetchUrl(uri);
if (head_response.headers.ContainsKey(header))
return head_response.headers[header];
else
return null;
}
public static void FetchResponseBodyAndETagFromUri(Uri uri, Dictionary<string, object> dict)
{
try
{
var response = FetchUrl(uri);
dict["response_body"] = (byte[])response.bytes;
dict["ETag"] = response.headers["ETag"];
}
catch (Exception e)
{
GenUtils.PriorityLogMsg("exception", "FetchResponseBodyAndETagFromUri: " + uri.ToString(), e.Message);
}
}
public static HttpResponse FetchUrl(Uri url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] UTf8ByteArray = new byte[0];
return DoHttpWebRequest(request, UTf8ByteArray);
}
public static HttpResponse FetchUrl(string url)
{
var uri = new Uri(url);
return FetchUrl(uri);
}
public static HttpResponse SlowFetchUrl(Uri url, int delay_secs)
{
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
return FetchUrl(url);
}
public static HttpResponse FetchUrlNoRedirect(Uri url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
byte[] UTf8ByteArray = new byte[0];
return DoHttpWebRequest(request, UTf8ByteArray);
}
public static HttpResponse FetchUrlAsUserAgent(Uri url, string user_agent)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = user_agent;
byte[] UTf8ByteArray = new byte[0];
return DoHttpWebRequest(request, UTf8ByteArray);
}
public static HttpResponse FetchUrlNoCache(Uri url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Cache-Control", "no-cache");
byte[] UTf8ByteArray = new byte[0];
return DoHttpWebRequest(request, UTf8ByteArray);
}
public static HttpResponse DoAuthorizedHttpRequest(HttpWebRequest request, string user, string pass, byte[] data)
{
byte[] auth = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", user, pass));
string authstr = Convert.ToBase64String(auth);
request.Headers.Add("Authorization", "Basic " + authstr);
return DoHttpWebRequest(request, data);
}
public static HttpResponse DoCertifiedServiceManagementRequest(byte[] cert, HttpWebRequest request, byte[] data)
{
X509Certificate2 x509 = new X509Certificate2(cert);
request.ClientCertificates.Add(x509);
request.Headers.Add("x-ms-version", "2010-10-28");
request.ContentType = "application/xml";
return DoHttpWebRequest(request, data);
}
public static HttpResponse DoHttpWebRequest(HttpWebRequest request, byte[] data)
{
if (request.UserAgent == null)
request.UserAgent = elmcity_user_agent;
try
{
if (data != null && data.Length > 0)
{
request.ContentLength = data.Length;
var bw = new BinaryWriter(request.GetRequestStream());
bw.Write(data);
bw.Flush();
bw.Close();
}
}
catch (Exception ex_write)
{
GenUtils.PriorityLogMsg("exception", "DoHttpWebRequest: writing data", ex_write.Message + ex_write.InnerException.Message + ex_write.StackTrace);
return new HttpResponse(HttpStatusCode.ServiceUnavailable, "DoHttpWebRequest cannot write", null, new Dictionary<string, string>());
}
try
{
var response = (HttpWebResponse)request.GetResponse2();
var status = response.StatusCode;
var message = response.StatusDescription;
var headers = new Dictionary<string, string>();
foreach (string key in response.Headers.Keys)
headers[key] = response.Headers[key];
byte[] return_data = GetResponseData(response);
response.Close();
return new HttpResponse(status, message, return_data, headers);
}
catch (Exception ex_read)
{
GenUtils.PriorityLogMsg("exception", "DoHttpWebRequest: reading data", ex_read.Message + ex_read.InnerException.Message + ex_read.StackTrace);
return new HttpResponse(HttpStatusCode.ServiceUnavailable, "DoHttpWebRequest cannot read", null, new Dictionary<string, string>());
}
}
private static byte[] GetResponseData(HttpWebResponse response)
{
try
{
//NUnit.Framework.Assert.IsNotNull(response);
Stream response_stream = response.GetResponseStream();
if (response_stream.CanRead == false)
return new byte[0];
//NUnit.Framework.Assert.IsNotNull(response_stream);
Encoding encoding;
var charset = response.CharacterSet ?? "";
switch (charset.ToLower())
{
case "utf-8":
encoding = Encoding.UTF8;
break;
default:
encoding = Encoding.Default;
break;
}
if (response.ContentLength > 0)
{
var reader = new BinaryReader(response_stream);
return reader.ReadBytes((int)response.ContentLength);
}
else // empty or unspecified, read zero or more bytes
{
var reader = new StreamReader(response_stream);
return encoding.GetBytes(reader.ReadToEnd());
}
}
catch (Exception e)
{
GenUtils.PriorityLogMsg("exception", "HttpUtils.GetResponseData", e.Message + e.StackTrace);
throw (e);
}
}
// see http://www.cookcomputing.com/blog/archives/000556.html
public static bool SetAllowUnsafeHeaderParsing()
{
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(
typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for
// the internal class
Type aSettingsType = aNetAssembly.GetType(
"System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance
// of the internal settings class. If the static instance
// isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty
| BindingFlags.NonPublic, null, null, new object[] { });
if (anInstance != null)
{
//Locate the private bool field that tells the
// framework is unsafe header parsing should be
// allowed or not
FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField(
"useUnsafeHeaderParsing",
BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
}
}
}
return false;
}
// used to create a response etag from a response body
public static string GetMd5Hash(byte[] bytes)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
sb.Append(result[i].ToString("X2"));
return sb.ToString();
}
public static void Wait(int seconds)
{
System.Threading.Thread.Sleep(seconds * 1000);
}
public static LogMsg MakeHttpLogMsg(System.Web.Mvc.ControllerContext c)
{
string requestor_ip_addr;
string url;
try
{
requestor_ip_addr = c.HttpContext.Request.UserHostAddress;
url = c.HttpContext.Request.Url.ToString();
}
catch
{
requestor_ip_addr = "ip_unavailable";
url = "url_unavailable";
}
var msg = new LogMsg("request", requestor_ip_addr, url);
return msg;
}
public static bool CompletedIfStatusEqualsExpected(HttpResponse r, Object o)
{
return r.status == (HttpStatusCode) o;
}
public static bool CompletedIfStatusNotServiceUnavailable(HttpResponse r, Object o)
{
return r.status != HttpStatusCode.ServiceUnavailable;
}
public static HttpResponse RetryHttpRequestExpectingStatus(HttpWebRequest request, HttpStatusCode expected_status, byte[] data, int wait_secs, int max_tries, TimeSpan timeout_secs)
{
try
{
return GenUtils.Actions.Retry<HttpResponse>(
delegate()
{
try
{
return DoHttpWebRequest(request, data);
}
catch (Exception e)
{
string msg;
if (e.GetType() != typeof(NullReferenceException))
{
msg = "RetryHttpRequest " + e.ToString() + ": " + request.RequestUri;
GenUtils.LogMsg("warning", msg, null);
}
else
msg = request.RequestUri.ToString();
return new HttpResponse(HttpStatusCode.NotFound, msg, null, null);
}
},
CompletedIfStatusEqualsExpected,
completed_delegate_object: expected_status,
wait_secs: wait_secs,
max_tries: max_tries,
timeout_secs: timeout_secs);
}
catch (Exception e)
{
var msg = "RetryHttpRequest " + e.ToString() + ": " + request.RequestUri;
GenUtils.LogMsg("exception", msg, null);
return new HttpResponse(HttpStatusCode.NotFound, msg, null, null);
}
}
public static HttpResponse RetryHttpRequestExpectingStatus(string url, HttpStatusCode expected_status)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri(url));
return HttpUtils.RetryHttpRequestExpectingStatus(request, HttpStatusCode.OK, null, 1, 3, TimeSpan.FromSeconds(10));
}
public static HttpResponse RetryStorageRequestExpectingServiceAvailable(string method, Hashtable headers, byte[] data, string content_type, Uri uri, int wait_secs, int max_tries, TimeSpan timeout_secs)
{
try
{
return GenUtils.Actions.Retry<HttpResponse>(
delegate()
{
try
{
return StorageUtils.DoStorageRequest(method, headers, data, content_type, uri);
}
catch // (Exception e)
{
//GenUtils.PriorityLogMsg("exception", "RetryStorageRequest: " + uri, e.Message + e.StackTrace);
throw new Exception("RetryStorageRequestException");
}
},
CompletedIfStatusNotServiceUnavailable,
completed_delegate_object: null,
wait_secs: wait_secs,
max_tries: max_tries,
timeout_secs: timeout_secs);
}
catch (Exception e)
{
GenUtils.LogMsg("exception", "RetryStorageRequest: " + uri, e.Message + e.StackTrace);
return new HttpResponse(HttpStatusCode.ServiceUnavailable, uri.ToString(), null, new Dictionary<string, string>());
}
}
public static HttpResponse RetryStorageRequestExpectingServiceAvailable(string method, Hashtable headers, byte[] data, string content_type, Uri uri)
{
try
{
return RetryStorageRequestExpectingServiceAvailable(method, headers, data, content_type, uri, 60, 3, TimeSpan.FromSeconds(500));
}
catch (Exception e)
{
GenUtils.LogMsg("exception", "RetryStorageRequest: " + uri, e.Message + e.StackTrace);
return new HttpResponse(HttpStatusCode.ServiceUnavailable, uri.ToString(), null, new Dictionary<string, string>());
}
}
}
}