2 /* -----------------------------------------------------------------------
3 * <copyright file="RestClient.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
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.
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.
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.
39 * -----------------------------------------------------------------------
42 using System.Collections.Specialized;
43 using System.Diagnostics;
44 using System.Diagnostics.Contracts;
47 using System.Reflection;
48 using System.Runtime.Serialization;
49 using System.Threading.Tasks;
53 namespace Pithos.Network
56 using System.Collections.Generic;
61 /// TODO: Update summary.
63 public class RestClient:WebClient
65 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
67 public int Timeout { get; set; }
69 public bool TimedOut { get; set; }
71 public HttpStatusCode StatusCode { get; private set; }
73 public string StatusDescription { get; set; }
75 public long? RangeFrom { get; set; }
76 public long? RangeTo { get; set; }
78 public int Retries { get; set; }
80 private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>();
81 public Dictionary<string, string> Parameters
85 Contract.Ensures(_parameters!=null);
91 [ContractInvariantMethod]
92 private void Invariants()
94 Contract.Invariant(Headers!=null);
97 public RestClient():base()
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;
106 public RestClient(RestClient other)
110 //Log.ErrorFormat("[ERROR] No parameters provided to the rest client. \n{0}\n", other);
111 throw new ArgumentNullException("other");
112 Contract.EndContractBlock();
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;
121 Timeout = other.Timeout;
122 Retries = other.Retries;
123 BaseAddress = other.BaseAddress;
125 foreach (var parameter in other.Parameters)
127 Parameters.Add(parameter.Key,parameter.Value);
130 this.Proxy = other.Proxy;
134 protected override WebRequest GetWebRequest(Uri address)
137 var webRequest = base.GetWebRequest(address);
138 var request = (HttpWebRequest)webRequest;
139 request.CookieContainer=new CookieContainer();
140 request.ServicePoint.ConnectionLimit = 50;
141 if (IfModifiedSince.HasValue)
142 request.IfModifiedSince = IfModifiedSince.Value;
143 request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
145 request.Timeout = Timeout;
147 if (RangeFrom.HasValue)
149 if (RangeTo.HasValue)
150 request.AddRange(RangeFrom.Value, RangeTo.Value);
152 request.AddRange(RangeFrom.Value);
157 public DateTime? IfModifiedSince { get; set; }
159 //Asynchronous version
160 protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
162 Log.InfoFormat("[{0}] {1}", request.Method, request.RequestUri);
163 HttpWebResponse response = null;
167 response = (HttpWebResponse)base.GetWebResponse(request, result);
169 catch (WebException exc)
171 if (!TryGetResponse(exc, request,out response))
175 StatusCode = response.StatusCode;
176 LastModified = response.LastModified;
177 StatusDescription = response.StatusDescription;
183 //Synchronous version
184 protected override WebResponse GetWebResponse(WebRequest request)
186 HttpWebResponse response = null;
189 Log.InfoFormat("[{0}] {1}",request.Method,request.RequestUri);
190 response = (HttpWebResponse)base.GetWebResponse(request);
192 catch (WebException exc)
194 if (!TryGetResponse(exc, request,out response))
198 StatusCode = response.StatusCode;
199 LastModified = response.LastModified;
200 StatusDescription = response.StatusDescription;
204 private bool TryGetResponse(WebException exc, WebRequest request,out HttpWebResponse response)
207 //Fail on empty response
208 if (exc.Response == null)
210 Log.WarnFormat("[{0}] {1} {2}", request.Method, exc.Status, request.RequestUri);
214 response = (exc.Response as HttpWebResponse);
215 var statusCode = (int)response.StatusCode;
216 //Succeed on allowed status codes
217 if (AllowedStatusCodes.Contains(response.StatusCode))
219 if (Log.IsDebugEnabled)
220 Log.DebugFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri);
224 Log.WarnFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri);
226 //Does the response have any content to log?
227 if (exc.Response.ContentLength > 0)
229 var content = LogContent(exc.Response);
230 Log.ErrorFormat(content);
235 private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};
237 public List<HttpStatusCode> AllowedStatusCodes
241 return _allowedStatusCodes;
245 public DateTime LastModified { get; private set; }
247 private static string LogContent(WebResponse webResponse)
249 if (webResponse == null)
250 throw new ArgumentNullException("webResponse");
251 Contract.EndContractBlock();
253 //The response stream must be copied to avoid affecting other code by disposing of the
254 //original response stream.
255 var stream = webResponse.GetResponseStream();
256 using(var memStream=new MemoryStream())
257 using (var reader = new StreamReader(memStream))
259 stream.CopyTo(memStream);
260 string content = reader.ReadToEnd();
262 stream.Seek(0,SeekOrigin.Begin);
267 public string DownloadStringWithRetry(string address,int retries=0)
271 throw new ArgumentNullException("address");
273 var actualAddress = GetActualAddress(address);
275 TraceStart("GET",actualAddress);
277 var actualRetries = (retries == 0) ? Retries : retries;
279 var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
281 var task = Retry(() =>
283 var content = DownloadString(uriString);
285 if (StatusCode == HttpStatusCode.NoContent)
293 var result = task.Result;
297 catch (AggregateException exc)
299 //If the task fails, propagate the original exception
300 if (exc.InnerException!=null)
301 throw exc.InnerException;
306 public void Head(string address,int retries=0)
308 AllowedStatusCodes.Add(HttpStatusCode.NotFound);
309 RetryWithoutContent(address, retries, "HEAD");
312 public void PutWithRetry(string address, int retries = 0, string contentType=null)
314 RetryWithoutContent(address, retries, "PUT",contentType);
317 public void PostWithRetry(string address,string contentType)
319 RetryWithoutContent(address, 0, "POST",contentType);
322 public void DeleteWithRetry(string address,int retries=0)
324 RetryWithoutContent(address, retries, "DELETE");
327 public string GetHeaderValue(string headerName,bool optional=false)
329 if (this.ResponseHeaders==null)
330 throw new InvalidOperationException("ResponseHeaders are null");
331 Contract.EndContractBlock();
333 var values=this.ResponseHeaders.GetValues(headerName);
339 //A required header was not found
340 throw new WebException(String.Format("The {0} header is missing", headerName));
343 public void SetNonEmptyHeaderValue(string headerName, string value)
345 if (String.IsNullOrWhiteSpace(value))
347 Headers.Add(headerName,value);
350 private void RetryWithoutContent(string address, int retries, string method,string contentType=null)
353 throw new ArgumentNullException("address");
355 var actualAddress = GetActualAddress(address);
356 var actualRetries = (retries == 0) ? Retries : retries;
358 var task = Retry(() =>
360 var uriString = String.Join("/",BaseAddress ,actualAddress);
361 var uri = new Uri(uriString);
362 var request = GetWebRequest(uri);
363 if (contentType!=null)
365 request.ContentType = contentType;
366 request.ContentLength = 0;
368 request.Method = method;
369 if (ResponseHeaders!=null)
370 ResponseHeaders.Clear();
372 TraceStart(method, uriString);
374 request.ContentLength = 0;
376 //Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object
377 //in order to return response headers
378 var response = (HttpWebResponse)GetWebResponse(request);
381 LastModified = response.LastModified;
382 StatusCode = response.StatusCode;
383 StatusDescription = response.StatusDescription;
398 catch (AggregateException ex)
400 var exc = ex.InnerException;
401 if (exc is RetryException)
403 Log.ErrorFormat("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
407 Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, exc);
414 Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, ex);
419 private static void TraceStart(string method, string actualAddress)
421 Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
424 private string GetActualAddress(string address)
426 if (Parameters.Count == 0)
428 var addressBuilder=new StringBuilder(address);
431 foreach (var parameter in Parameters)
434 addressBuilder.AppendFormat("?{0}={1}", parameter.Key, parameter.Value);
436 addressBuilder.AppendFormat("&{0}={1}", parameter.Key, parameter.Value);
439 return addressBuilder.ToString();
442 public string DownloadStringWithRetry(Uri address,int retries=0)
445 throw new ArgumentNullException("address");
447 var actualRetries = (retries == 0) ? Retries : retries;
448 var task = Retry(() =>
450 var content = base.DownloadString(address);
452 if (StatusCode == HttpStatusCode.NoContent)
458 var result = task.Result;
464 /// Copies headers from another RestClient
466 /// <param name="source">The RestClient from which the headers are copied</param>
467 public void CopyHeaders(RestClient source)
470 throw new ArgumentNullException("source", "source can't be null");
471 Contract.EndContractBlock();
472 //The Headers getter initializes the property, it is never null
473 Contract.Assume(Headers!=null);
475 CopyHeaders(source.Headers,Headers);
479 /// Copies headers from one header collection to another
481 /// <param name="source">The source collection from which the headers are copied</param>
482 /// <param name="target">The target collection to which the headers are copied</param>
483 public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection target)
486 throw new ArgumentNullException("source", "source can't be null");
488 throw new ArgumentNullException("target", "target can't be null");
489 Contract.EndContractBlock();
491 for (int i = 0; i < source.Count; i++)
493 target.Add(source.GetKey(i), source[i]);
497 public void AssertStatusOK(string message)
499 if (StatusCode >= HttpStatusCode.BadRequest)
500 throw new WebException(String.Format("{0} with code {1} - {2}", message, StatusCode, StatusDescription));
504 private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null)
507 throw new ArgumentNullException("original");
508 Contract.EndContractBlock();
511 tcs = new TaskCompletionSource<T>();
512 Task.Factory.StartNew(original).ContinueWith(_original =>
514 if (!_original.IsFaulted)
515 tcs.SetFromTask(_original);
518 var e = _original.Exception.InnerException;
519 var we = (e as WebException);
524 var statusCode = GetStatusCode(we);
526 //Return null for 404
527 if (statusCode == HttpStatusCode.NotFound)
528 tcs.SetResult(default(T));
529 //Retry for timeouts and service unavailable
530 else if (we.Status == WebExceptionStatus.Timeout ||
531 (we.Status == WebExceptionStatus.ProtocolError && statusCode == HttpStatusCode.ServiceUnavailable))
536 Log.ErrorFormat("[ERROR] Timed out too many times. \n{0}\n",e);
537 tcs.SetException(new RetryException("Timed out too many times.", e));
542 "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout,
544 Retry(original, retryCount - 1, tcs);
555 private HttpStatusCode GetStatusCode(WebException we)
558 throw new ArgumentNullException("we");
559 var statusCode = HttpStatusCode.RequestTimeout;
560 if (we.Response != null)
562 statusCode = ((HttpWebResponse) we.Response).StatusCode;
563 this.StatusCode = statusCode;
568 public UriBuilder GetAddressBuilder(string container, string objectName)
570 var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName));
574 public Dictionary<string, string> GetMeta(string metaPrefix)
576 if (String.IsNullOrWhiteSpace(metaPrefix))
577 throw new ArgumentNullException("metaPrefix");
578 Contract.EndContractBlock();
580 var keys = ResponseHeaders.AllKeys.AsQueryable();
581 var dict = (from key in keys
582 where key.StartsWith(metaPrefix)
583 let name = key.Substring(metaPrefix.Length)
584 select new { Name = name, Value = ResponseHeaders[key] })
585 .ToDictionary(t => t.Name, t => t.Value);
591 public class RetryException:Exception
593 public RetryException()
599 public RetryException(string message)
605 public RetryException(string message,Exception innerException)
606 :base(message,innerException)
611 public RetryException(SerializationInfo info,StreamingContext context)