private async Task<Tuple<HttpResponseHeaders, HttpResponseMessage,Uri>> ExecuteWebDavRequest (
Uri url,
string httpMethod,
int? depth,
string ifMatch,
string ifNoneMatch,
string mediaType,
string requestBody,
HttpResponseHeaders headersFromFirstCall = null)
{
HttpResponseMessage response;
using (var requestMessage = new HttpRequestMessage())
{
requestMessage.RequestUri = url;
requestMessage.Headers.UserAgent.Add (_productInfo);
requestMessage.Method = new HttpMethod (httpMethod);
if (depth != null)
requestMessage.Headers.Add ("Depth", depth.ToString());
if (!string.IsNullOrEmpty (ifMatch))
{
if (_sendEtagsWithoutQuote)
requestMessage.Headers.TryAddWithoutValidation ("If-Match", ifMatch.Trim('\"'));
else
requestMessage.Headers.Add ("If-Match", ifMatch);
}
if (!string.IsNullOrEmpty (ifNoneMatch))
requestMessage.Headers.Add ("If-None-Match", ifNoneMatch);
if (!string.IsNullOrEmpty (requestBody))
{
requestMessage.Content = new StringContent (requestBody, Encoding.UTF8, mediaType);
}
if (_httpClient == null)
{
_httpClient = await _httpClientFactory();
if (_closeConnectionAfterEachRequest)
_httpClient.DefaultRequestHeaders.Add ("Connection", "close");
}
response = await _httpClient.SendAsync (requestMessage);
}
try
{
if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.TemporaryRedirect)
{
if (response.Headers.Location != null)
{
var location = response.Headers.Location;
response.Dispose();
var effectiveLocation = location.IsAbsoluteUri ? location : new Uri (url, location);
return await ExecuteWebDavRequest (effectiveLocation, httpMethod, depth, ifMatch, ifNoneMatch, mediaType, requestBody, headersFromFirstCall ?? response.Headers);
}
else
{
s_logger.Warn ("Ignoring Redirection without Location header.");
}
}
await EnsureSuccessStatusCode (response);
return Tuple.Create (headersFromFirstCall ?? response.Headers, response, url);
}
catch (Exception)
{
if (response != null)
response.Dispose();
throw;
}
}