-// -----------------------------------------------------------------------
-// <copyright file="RestClient.cs" company="Microsoft">
-// TODO: Update copyright text.
-// </copyright>
-// -----------------------------------------------------------------------
-
+#region
+/* -----------------------------------------------------------------------
+ * <copyright file="RestClient.cs" company="GRNet">
+ *
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ * </copyright>
+ * -----------------------------------------------------------------------
+ */
+#endregion
using System.Collections.Specialized;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
+using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using log4net;
/// </summary>
public class RestClient:WebClient
{
+ private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
public int Timeout { get; set; }
public bool TimedOut { get; set; }
}
}
- private static readonly ILog Log = LogManager.GetLogger("RestClient");
-
[ContractInvariantMethod]
private void Invariants()
public RestClient():base()
{
-
+ //The maximum error response must be large because missing server hashes are return as a Conflivt (409) error response
+ //Any value above 2^21-1 will result in an empty response.
+ //-1 essentially ignores the maximum length
+ HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
}
: base()
{
if (other==null)
+ //Log.ErrorFormat("[ERROR] No parameters provided to the rest client. \n{0}\n", other);
throw new ArgumentNullException("other");
Contract.EndContractBlock();
+ //The maximum error response must be large because missing server hashes are return as a Conflivt (409) error response
+ //Any value above 2^21-1 will result in an empty response.
+ //-1 essentially ignores the maximum length
+ HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
+
+
CopyHeaders(other);
Timeout = other.Timeout;
Retries = other.Retries;
}
- private WebHeaderCollection _responseHeaders;
-
- public new WebHeaderCollection ResponseHeaders
- {
- get
- {
- if (base.ResponseHeaders==null)
- {
- return _responseHeaders;
- }
- else
- {
- _responseHeaders = null;
- return base.ResponseHeaders;
- }
-
- }
-
- set { _responseHeaders = value; }
- }
protected override WebRequest GetWebRequest(Uri address)
{
TimedOut = false;
var webRequest = base.GetWebRequest(address);
var request = (HttpWebRequest)webRequest;
+ request.CookieContainer=new CookieContainer();
+ request.ServicePoint.ConnectionLimit = 50;
if (IfModifiedSince.HasValue)
request.IfModifiedSince = IfModifiedSince.Value;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
//Asynchronous version
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
- {
- Log.InfoFormat("ASYNC [{0}] {1}",request.Method, request.RequestUri);
+ {
+ Log.InfoFormat("[{0}] {1}", request.Method, request.RequestUri);
HttpWebResponse response = null;
try
}
catch (WebException exc)
{
- if (!TryGetResponse(exc, out response))
+ if (!TryGetResponse(exc, request,out response))
throw;
}
{
HttpWebResponse response = null;
try
- {
+ {
+ Log.InfoFormat("[{0}] {1}",request.Method,request.RequestUri);
response = (HttpWebResponse)base.GetWebResponse(request);
}
catch (WebException exc)
{
- if (!TryGetResponse(exc, out response))
+ if (!TryGetResponse(exc, request,out response))
throw;
}
return response;
}
- private bool TryGetResponse(WebException exc, out HttpWebResponse response)
+ private bool TryGetResponse(WebException exc, WebRequest request,out HttpWebResponse response)
{
response = null;
//Fail on empty response
if (exc.Response == null)
+ {
+ Log.WarnFormat("[{0}] {1} {2}", request.Method, exc.Status, request.RequestUri);
return false;
+ }
response = (exc.Response as HttpWebResponse);
+ var statusCode = (int)response.StatusCode;
//Succeed on allowed status codes
if (AllowedStatusCodes.Contains(response.StatusCode))
+ {
+ if (Log.IsDebugEnabled)
+ Log.DebugFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri);
return true;
+ }
+
+ Log.WarnFormat("[{0}] {1} {2}", request.Method, statusCode, request.RequestUri);
//Does the response have any content to log?
if (exc.Response.ContentLength > 0)
{
- var content = GetContent(exc.Response);
+ var content = LogContent(exc.Response);
Log.ErrorFormat(content);
}
return false;
public DateTime LastModified { get; private set; }
- private static string GetContent(WebResponse webResponse)
+ private static string LogContent(WebResponse webResponse)
{
if (webResponse == null)
throw new ArgumentNullException("webResponse");
Contract.EndContractBlock();
- string content;
- using (var stream = webResponse.GetResponseStream())
- using (var reader = new StreamReader(stream))
+ //The response stream must be copied to avoid affecting other code by disposing of the
+ //original response stream.
+ var stream = webResponse.GetResponseStream();
+ using(var memStream=new MemoryStream())
+ using (var reader = new StreamReader(memStream))
{
- content = reader.ReadToEnd();
+ stream.CopyTo(memStream);
+ string content = reader.ReadToEnd();
+
+ stream.Seek(0,SeekOrigin.Begin);
+ return content;
}
- return content;
}
public string DownloadStringWithRetry(string address,int retries=0)
var actualRetries = (retries == 0) ? Retries : retries;
-
+ var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
+
var task = Retry(() =>
- {
- var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
- var content = base.DownloadString(uriString);
+ {
+ var content = DownloadString(uriString);
if (StatusCode == HttpStatusCode.NoContent)
return String.Empty;
}, actualRetries);
- var result = task.Result;
- return result;
+ try
+ {
+ var result = task.Result;
+ return result;
+
+ }
+ catch (AggregateException exc)
+ {
+ //If the task fails, propagate the original exception
+ if (exc.InnerException!=null)
+ throw exc.InnerException;
+ throw;
+ }
}
public void Head(string address,int retries=0)
RetryWithoutContent(address, retries, "HEAD");
}
- public void PutWithRetry(string address, int retries = 0)
+ public void PutWithRetry(string address, int retries = 0, string contentType=null)
{
- RetryWithoutContent(address, retries, "PUT");
+ RetryWithoutContent(address, retries, "PUT",contentType);
+ }
+
+ public void PostWithRetry(string address,string contentType)
+ {
+ RetryWithoutContent(address, 0, "POST",contentType);
}
public void DeleteWithRetry(string address,int retries=0)
Headers.Add(headerName,value);
}
- private void RetryWithoutContent(string address, int retries, string method)
+ private void RetryWithoutContent(string address, int retries, string method,string contentType=null)
{
if (address == null)
throw new ArgumentNullException("address");
var uriString = String.Join("/",BaseAddress ,actualAddress);
var uri = new Uri(uriString);
var request = GetWebRequest(uri);
+ if (contentType!=null)
+ {
+ request.ContentType = contentType;
+ request.ContentLength = 0;
+ }
request.Method = method;
if (ResponseHeaders!=null)
ResponseHeaders.Clear();
if (method == "PUT")
request.ContentLength = 0;
+ //Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object
+ //in order to return response headers
var response = (HttpWebResponse)GetWebResponse(request);
- //var response = (HttpWebResponse)request.GetResponse();
+ try
+ {
+ LastModified = response.LastModified;
+ StatusCode = response.StatusCode;
+ StatusDescription = response.StatusDescription;
+ }
+ finally
+ {
+ response.Close();
+ }
- //ResponseHeaders= response.Headers;
-
- LastModified = response.LastModified;
- StatusCode = response.StatusCode;
- StatusDescription = response.StatusDescription;
- response.Close();
return 0;
}, actualRetries);
.ToDictionary(t => t.Name, t => t.Value);
return dict;
}
+
}
public class RetryException:Exception