root / trunk / Pithos.Network / RestClient.cs @ c945b450
History | View | Annotate | Download (21.4 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="RestClient.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System.Collections.Specialized; |
43 |
using System.Diagnostics; |
44 |
using System.Diagnostics.Contracts; |
45 |
using System.IO; |
46 |
using System.Net; |
47 |
using System.Reflection; |
48 |
using System.Runtime.Serialization; |
49 |
using System.Threading.Tasks; |
50 |
using log4net; |
51 |
|
52 |
|
53 |
namespace Pithos.Network |
54 |
{ |
55 |
using System; |
56 |
using System.Collections.Generic; |
57 |
using System.Linq; |
58 |
using System.Text; |
59 |
|
60 |
/// <summary> |
61 |
/// TODO: Update summary. |
62 |
/// </summary> |
63 |
public class RestClient:WebClient |
64 |
{ |
65 |
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
66 |
|
67 |
public int Timeout { get; set; } |
68 |
|
69 |
public bool TimedOut { get; set; } |
70 |
|
71 |
public HttpStatusCode StatusCode { get; private set; } |
72 |
|
73 |
public string StatusDescription { get; set; } |
74 |
|
75 |
public long? RangeFrom { get; set; } |
76 |
public long? RangeTo { get; set; } |
77 |
|
78 |
public int Retries { get; set; } |
79 |
|
80 |
private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>(); |
81 |
public Dictionary<string, string> Parameters |
82 |
{ |
83 |
get |
84 |
{ |
85 |
Contract.Ensures(_parameters!=null); |
86 |
return _parameters; |
87 |
} |
88 |
} |
89 |
|
90 |
|
91 |
[ContractInvariantMethod] |
92 |
private void Invariants() |
93 |
{ |
94 |
Contract.Invariant(Headers!=null); |
95 |
} |
96 |
|
97 |
public RestClient():base() |
98 |
{ |
99 |
//The maximum error response must be large because missing server hashes are return as a Conflivt (409) error response |
100 |
//Any value above 2^21-1 will result in an empty response. |
101 |
//-1 essentially ignores the maximum length |
102 |
HttpWebRequest.DefaultMaximumErrorResponseLength = -1; |
103 |
} |
104 |
|
105 |
|
106 |
public RestClient(RestClient other) |
107 |
: base() |
108 |
{ |
109 |
if (other==null) |
110 |
//Log.ErrorFormat("[ERROR] No parameters provided to the rest client. \n{0}\n", other); |
111 |
throw new ArgumentNullException("other"); |
112 |
Contract.EndContractBlock(); |
113 |
|
114 |
//The maximum error response must be large because missing server hashes are return as a Conflivt (409) error response |
115 |
//Any value above 2^21-1 will result in an empty response. |
116 |
//-1 essentially ignores the maximum length |
117 |
HttpWebRequest.DefaultMaximumErrorResponseLength = -1; |
118 |
|
119 |
CopyHeaders(other); |
120 |
Timeout = other.Timeout; |
121 |
Retries = other.Retries; |
122 |
BaseAddress = other.BaseAddress; |
123 |
|
124 |
foreach (var parameter in other.Parameters) |
125 |
{ |
126 |
Parameters.Add(parameter.Key,parameter.Value); |
127 |
} |
128 |
|
129 |
this.Proxy = other.Proxy; |
130 |
} |
131 |
|
132 |
|
133 |
protected override WebRequest GetWebRequest(Uri address) |
134 |
{ |
135 |
TimedOut = false; |
136 |
var webRequest = base.GetWebRequest(address); |
137 |
var request = (HttpWebRequest)webRequest; |
138 |
request.CookieContainer=new CookieContainer(); |
139 |
request.ServicePoint.ConnectionLimit = 50; |
140 |
if (IfModifiedSince.HasValue) |
141 |
request.IfModifiedSince = IfModifiedSince.Value; |
142 |
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; |
143 |
if(Timeout>0) |
144 |
request.Timeout = Timeout; |
145 |
|
146 |
if (RangeFrom.HasValue) |
147 |
{ |
148 |
if (RangeTo.HasValue) |
149 |
request.AddRange(RangeFrom.Value, RangeTo.Value); |
150 |
else |
151 |
request.AddRange(RangeFrom.Value); |
152 |
} |
153 |
return request; |
154 |
} |
155 |
|
156 |
public DateTime? IfModifiedSince { get; set; } |
157 |
|
158 |
//Asynchronous version |
159 |
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result) |
160 |
{ |
161 |
Log.InfoFormat("[{0}] {1}", request.Method, request.RequestUri); |
162 |
HttpWebResponse response = null; |
163 |
|
164 |
try |
165 |
{ |
166 |
response = (HttpWebResponse)base.GetWebResponse(request, result); |
167 |
} |
168 |
catch (WebException exc) |
169 |
{ |
170 |
if (!TryGetResponse(exc, request,out response)) |
171 |
throw; |
172 |
} |
173 |
|
174 |
StatusCode = response.StatusCode; |
175 |
LastModified = response.LastModified; |
176 |
StatusDescription = response.StatusDescription; |
177 |
return response; |
178 |
|
179 |
} |
180 |
|
181 |
|
182 |
//Synchronous version |
183 |
protected override WebResponse GetWebResponse(WebRequest request) |
184 |
{ |
185 |
HttpWebResponse response = null; |
186 |
try |
187 |
{ |
188 |
Log.InfoFormat("[{0}] {1}",request.Method,request.RequestUri); |
189 |
response = (HttpWebResponse)base.GetWebResponse(request); |
190 |
} |
191 |
catch (WebException exc) |
192 |
{ |
193 |
if (!TryGetResponse(exc, request,out response)) |
194 |
throw; |
195 |
} |
196 |
|
197 |
StatusCode = response.StatusCode; |
198 |
LastModified = response.LastModified; |
199 |
StatusDescription = response.StatusDescription; |
200 |
return response; |
201 |
} |
202 |
|
203 |
private bool TryGetResponse(WebException exc, WebRequest request,out HttpWebResponse response) |
204 |
{ |
205 |
response = null; |
206 |
//Fail on empty response |
207 |
if (exc.Response == null) |
208 |
{ |
209 |
Log.WarnFormat("[{0}] {1} {2}", request.Method, exc.Status, request.RequestUri); |
210 |
return false; |
211 |
} |
212 |
|
213 |
response = (exc.Response as HttpWebResponse); |
214 |
var statusCode = (int)response.StatusCode; |
215 |
//Succeed on allowed status codes |
216 |
if (AllowedStatusCodes.Contains(response.StatusCode)) |
217 |
{ |
218 |
if (Log.IsDebugEnabled) |
219 |
Log.DebugFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri); |
220 |
return true; |
221 |
} |
222 |
|
223 |
Log.WarnFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri); |
224 |
|
225 |
//Does the response have any content to log? |
226 |
if (exc.Response.ContentLength > 0) |
227 |
{ |
228 |
var content = LogContent(exc.Response); |
229 |
Log.ErrorFormat(content); |
230 |
} |
231 |
return false; |
232 |
} |
233 |
|
234 |
private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified}; |
235 |
|
236 |
public List<HttpStatusCode> AllowedStatusCodes |
237 |
{ |
238 |
get |
239 |
{ |
240 |
return _allowedStatusCodes; |
241 |
} |
242 |
} |
243 |
|
244 |
public DateTime LastModified { get; private set; } |
245 |
|
246 |
private static string LogContent(WebResponse webResponse) |
247 |
{ |
248 |
if (webResponse == null) |
249 |
throw new ArgumentNullException("webResponse"); |
250 |
Contract.EndContractBlock(); |
251 |
|
252 |
//The response stream must be copied to avoid affecting other code by disposing of the |
253 |
//original response stream. |
254 |
var stream = webResponse.GetResponseStream(); |
255 |
using(var memStream=new MemoryStream()) |
256 |
using (var reader = new StreamReader(memStream)) |
257 |
{ |
258 |
stream.CopyTo(memStream); |
259 |
string content = reader.ReadToEnd(); |
260 |
|
261 |
stream.Seek(0,SeekOrigin.Begin); |
262 |
return content; |
263 |
} |
264 |
} |
265 |
|
266 |
public string DownloadStringWithRetry(string address,int retries=0) |
267 |
{ |
268 |
|
269 |
if (address == null) |
270 |
throw new ArgumentNullException("address"); |
271 |
|
272 |
var actualAddress = GetActualAddress(address); |
273 |
|
274 |
TraceStart("GET",actualAddress); |
275 |
|
276 |
var actualRetries = (retries == 0) ? Retries : retries; |
277 |
|
278 |
var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress); |
279 |
|
280 |
var task = Retry(() => |
281 |
{ |
282 |
var content = base.DownloadString(uriString); |
283 |
|
284 |
if (StatusCode == HttpStatusCode.NoContent) |
285 |
return String.Empty; |
286 |
return content; |
287 |
|
288 |
}, actualRetries); |
289 |
|
290 |
try |
291 |
{ |
292 |
var result = task.Result; |
293 |
return result; |
294 |
|
295 |
} |
296 |
catch (AggregateException exc) |
297 |
{ |
298 |
//If the task fails, propagate the original exception |
299 |
if (exc.InnerException!=null) |
300 |
throw exc.InnerException; |
301 |
throw; |
302 |
} |
303 |
} |
304 |
|
305 |
public void Head(string address,int retries=0) |
306 |
{ |
307 |
AllowedStatusCodes.Add(HttpStatusCode.NotFound); |
308 |
RetryWithoutContent(address, retries, "HEAD"); |
309 |
} |
310 |
|
311 |
public void PutWithRetry(string address, int retries = 0, string contentType=null) |
312 |
{ |
313 |
RetryWithoutContent(address, retries, "PUT",contentType); |
314 |
} |
315 |
|
316 |
public void PostWithRetry(string address,string contentType) |
317 |
{ |
318 |
RetryWithoutContent(address, 0, "POST",contentType); |
319 |
} |
320 |
|
321 |
public void DeleteWithRetry(string address,int retries=0) |
322 |
{ |
323 |
RetryWithoutContent(address, retries, "DELETE"); |
324 |
} |
325 |
|
326 |
public string GetHeaderValue(string headerName,bool optional=false) |
327 |
{ |
328 |
if (this.ResponseHeaders==null) |
329 |
throw new InvalidOperationException("ResponseHeaders are null"); |
330 |
Contract.EndContractBlock(); |
331 |
|
332 |
var values=this.ResponseHeaders.GetValues(headerName); |
333 |
if (values != null) |
334 |
return values[0]; |
335 |
|
336 |
if (optional) |
337 |
return null; |
338 |
//A required header was not found |
339 |
throw new WebException(String.Format("The {0} header is missing", headerName)); |
340 |
} |
341 |
|
342 |
public void SetNonEmptyHeaderValue(string headerName, string value) |
343 |
{ |
344 |
if (String.IsNullOrWhiteSpace(value)) |
345 |
return; |
346 |
Headers.Add(headerName,value); |
347 |
} |
348 |
|
349 |
private void RetryWithoutContent(string address, int retries, string method,string contentType=null) |
350 |
{ |
351 |
if (address == null) |
352 |
throw new ArgumentNullException("address"); |
353 |
|
354 |
var actualAddress = GetActualAddress(address); |
355 |
var actualRetries = (retries == 0) ? Retries : retries; |
356 |
|
357 |
var task = Retry(() => |
358 |
{ |
359 |
var uriString = String.Join("/",BaseAddress ,actualAddress); |
360 |
var uri = new Uri(uriString); |
361 |
var request = GetWebRequest(uri); |
362 |
if (contentType!=null) |
363 |
{ |
364 |
request.ContentType = contentType; |
365 |
request.ContentLength = 0; |
366 |
} |
367 |
request.Method = method; |
368 |
if (ResponseHeaders!=null) |
369 |
ResponseHeaders.Clear(); |
370 |
|
371 |
TraceStart(method, uriString); |
372 |
if (method == "PUT") |
373 |
request.ContentLength = 0; |
374 |
|
375 |
//Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object |
376 |
//in order to return response headers |
377 |
var response = (HttpWebResponse)GetWebResponse(request); |
378 |
try |
379 |
{ |
380 |
LastModified = response.LastModified; |
381 |
StatusCode = response.StatusCode; |
382 |
StatusDescription = response.StatusDescription; |
383 |
} |
384 |
finally |
385 |
{ |
386 |
response.Close(); |
387 |
} |
388 |
|
389 |
|
390 |
return 0; |
391 |
}, actualRetries); |
392 |
|
393 |
try |
394 |
{ |
395 |
task.Wait(); |
396 |
} |
397 |
catch (AggregateException ex) |
398 |
{ |
399 |
var exc = ex.InnerException; |
400 |
if (exc is RetryException) |
401 |
{ |
402 |
Log.ErrorFormat("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries); |
403 |
} |
404 |
else |
405 |
{ |
406 |
Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, exc); |
407 |
} |
408 |
throw exc; |
409 |
|
410 |
} |
411 |
catch(Exception ex) |
412 |
{ |
413 |
Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, ex); |
414 |
throw; |
415 |
} |
416 |
} |
417 |
|
418 |
private static void TraceStart(string method, string actualAddress) |
419 |
{ |
420 |
Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress); |
421 |
} |
422 |
|
423 |
private string GetActualAddress(string address) |
424 |
{ |
425 |
if (Parameters.Count == 0) |
426 |
return address; |
427 |
var addressBuilder=new StringBuilder(address); |
428 |
|
429 |
bool isFirst = true; |
430 |
foreach (var parameter in Parameters) |
431 |
{ |
432 |
if(isFirst) |
433 |
addressBuilder.AppendFormat("?{0}={1}", parameter.Key, parameter.Value); |
434 |
else |
435 |
addressBuilder.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); |
436 |
isFirst = false; |
437 |
} |
438 |
return addressBuilder.ToString(); |
439 |
} |
440 |
|
441 |
public string DownloadStringWithRetry(Uri address,int retries=0) |
442 |
{ |
443 |
if (address == null) |
444 |
throw new ArgumentNullException("address"); |
445 |
|
446 |
var actualRetries = (retries == 0) ? Retries : retries; |
447 |
var task = Retry(() => |
448 |
{ |
449 |
var content = base.DownloadString(address); |
450 |
|
451 |
if (StatusCode == HttpStatusCode.NoContent) |
452 |
return String.Empty; |
453 |
return content; |
454 |
|
455 |
}, actualRetries); |
456 |
|
457 |
var result = task.Result; |
458 |
return result; |
459 |
} |
460 |
|
461 |
|
462 |
/// <summary> |
463 |
/// Copies headers from another RestClient |
464 |
/// </summary> |
465 |
/// <param name="source">The RestClient from which the headers are copied</param> |
466 |
public void CopyHeaders(RestClient source) |
467 |
{ |
468 |
if (source == null) |
469 |
throw new ArgumentNullException("source", "source can't be null"); |
470 |
Contract.EndContractBlock(); |
471 |
//The Headers getter initializes the property, it is never null |
472 |
Contract.Assume(Headers!=null); |
473 |
|
474 |
CopyHeaders(source.Headers,Headers); |
475 |
} |
476 |
|
477 |
/// <summary> |
478 |
/// Copies headers from one header collection to another |
479 |
/// </summary> |
480 |
/// <param name="source">The source collection from which the headers are copied</param> |
481 |
/// <param name="target">The target collection to which the headers are copied</param> |
482 |
public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection target) |
483 |
{ |
484 |
if (source == null) |
485 |
throw new ArgumentNullException("source", "source can't be null"); |
486 |
if (target == null) |
487 |
throw new ArgumentNullException("target", "target can't be null"); |
488 |
Contract.EndContractBlock(); |
489 |
|
490 |
for (int i = 0; i < source.Count; i++) |
491 |
{ |
492 |
target.Add(source.GetKey(i), source[i]); |
493 |
} |
494 |
} |
495 |
|
496 |
public void AssertStatusOK(string message) |
497 |
{ |
498 |
if (StatusCode >= HttpStatusCode.BadRequest) |
499 |
throw new WebException(String.Format("{0} with code {1} - {2}", message, StatusCode, StatusDescription)); |
500 |
} |
501 |
|
502 |
|
503 |
private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null) |
504 |
{ |
505 |
if (original==null) |
506 |
throw new ArgumentNullException("original"); |
507 |
Contract.EndContractBlock(); |
508 |
|
509 |
if (tcs == null) |
510 |
tcs = new TaskCompletionSource<T>(); |
511 |
Task.Factory.StartNew(original).ContinueWith(_original => |
512 |
{ |
513 |
if (!_original.IsFaulted) |
514 |
tcs.SetFromTask(_original); |
515 |
else |
516 |
{ |
517 |
var e = _original.Exception.InnerException; |
518 |
var we = (e as WebException); |
519 |
if (we==null) |
520 |
tcs.SetException(e); |
521 |
else |
522 |
{ |
523 |
var statusCode = GetStatusCode(we); |
524 |
|
525 |
//Return null for 404 |
526 |
if (statusCode == HttpStatusCode.NotFound) |
527 |
tcs.SetResult(default(T)); |
528 |
//Retry for timeouts and service unavailable |
529 |
else if (we.Status == WebExceptionStatus.Timeout || |
530 |
(we.Status == WebExceptionStatus.ProtocolError && statusCode == HttpStatusCode.ServiceUnavailable)) |
531 |
{ |
532 |
TimedOut = true; |
533 |
if (retryCount == 0) |
534 |
{ |
535 |
Log.ErrorFormat("[ERROR] Timed out too many times. \n{0}\n",e); |
536 |
tcs.SetException(new RetryException("Timed out too many times.", e)); |
537 |
} |
538 |
else |
539 |
{ |
540 |
Log.ErrorFormat( |
541 |
"[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout, |
542 |
retryCount, e); |
543 |
Retry(original, retryCount - 1, tcs); |
544 |
} |
545 |
} |
546 |
else |
547 |
tcs.SetException(e); |
548 |
} |
549 |
}; |
550 |
}); |
551 |
return tcs.Task; |
552 |
} |
553 |
|
554 |
private HttpStatusCode GetStatusCode(WebException we) |
555 |
{ |
556 |
if (we==null) |
557 |
throw new ArgumentNullException("we"); |
558 |
var statusCode = HttpStatusCode.RequestTimeout; |
559 |
if (we.Response != null) |
560 |
{ |
561 |
statusCode = ((HttpWebResponse) we.Response).StatusCode; |
562 |
this.StatusCode = statusCode; |
563 |
} |
564 |
return statusCode; |
565 |
} |
566 |
|
567 |
public UriBuilder GetAddressBuilder(string container, string objectName) |
568 |
{ |
569 |
var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName)); |
570 |
return builder; |
571 |
} |
572 |
|
573 |
public Dictionary<string, string> GetMeta(string metaPrefix) |
574 |
{ |
575 |
if (String.IsNullOrWhiteSpace(metaPrefix)) |
576 |
throw new ArgumentNullException("metaPrefix"); |
577 |
Contract.EndContractBlock(); |
578 |
|
579 |
var keys = ResponseHeaders.AllKeys.AsQueryable(); |
580 |
var dict = (from key in keys |
581 |
where key.StartsWith(metaPrefix) |
582 |
let name = key.Substring(metaPrefix.Length) |
583 |
select new { Name = name, Value = ResponseHeaders[key] }) |
584 |
.ToDictionary(t => t.Name, t => t.Value); |
585 |
return dict; |
586 |
} |
587 |
|
588 |
} |
589 |
|
590 |
public class RetryException:Exception |
591 |
{ |
592 |
public RetryException() |
593 |
:base() |
594 |
{ |
595 |
|
596 |
} |
597 |
|
598 |
public RetryException(string message) |
599 |
:base(message) |
600 |
{ |
601 |
|
602 |
} |
603 |
|
604 |
public RetryException(string message,Exception innerException) |
605 |
:base(message,innerException) |
606 |
{ |
607 |
|
608 |
} |
609 |
|
610 |
public RetryException(SerializationInfo info,StreamingContext context) |
611 |
:base(info,context) |
612 |
{ |
613 |
|
614 |
} |
615 |
} |
616 |
} |