aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
diff options
context:
space:
mode:
authorEric Reed <ebr@mediabrowser3.com>2013-05-22 15:17:49 -0400
committerEric Reed <ebr@mediabrowser3.com>2013-05-22 15:17:49 -0400
commitc6de61d7bd04e8bdc27719472491bf33bf5d2504 (patch)
treecf53a7da5a10aa8be97e3e84cac1ef9ac348b78f /MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
parent0c8e0d7fcf84b6943df4b07414d5cabff5bc1872 (diff)
parentabecd422009daaa173e4b2c1784a4a5f710ca35e (diff)
Merge branch 'master' of https://github.com/MediaBrowser/MediaBrowser
Diffstat (limited to 'MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs')
-rw-r--r--MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs128
1 files changed, 65 insertions, 63 deletions
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
index 9afc24c01..1d7b4a4f3 100644
--- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
+++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
@@ -161,91 +161,93 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
options.CancellationToken.ThrowIfCancellationRequested();
- var message = GetHttpRequestMessage(options);
-
- //if (options.EnableResponseCache && cachedInfo != null)
- //{
- // if (!string.IsNullOrEmpty(cachedInfo.Etag))
- // {
- // message.Headers.Add("If-None-Match", cachedInfo.Etag);
- // }
- // else if (cachedInfo.LastModified.HasValue)
- // {
- // message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value);
- // }
- //}
-
- if (options.ResourcePool != null)
+ using (var message = GetHttpRequestMessage(options))
{
- await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
- }
-
- _logger.Info("HttpClientManager.Get url: {0}", options.Url);
-
- try
- {
- options.CancellationToken.ThrowIfCancellationRequested();
-
- var response = await GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression).SendAsync(message, HttpCompletionOption.ResponseContentRead, options.CancellationToken).ConfigureAwait(false);
-
- if (options.EnableResponseCache)
+ if (options.EnableResponseCache && cachedInfo != null)
{
- if (response.StatusCode != HttpStatusCode.NotModified)
+ if (!string.IsNullOrEmpty(cachedInfo.Etag))
{
- EnsureSuccessStatusCode(response);
+ message.Headers.Add("If-None-Match", cachedInfo.Etag);
+ }
+ else if (cachedInfo.LastModified.HasValue)
+ {
+ message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value);
}
+ }
+ if (options.ResourcePool != null)
+ {
+ await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
+ }
+
+ _logger.Info("HttpClientManager.Get url: {0}", options.Url);
+
+ try
+ {
options.CancellationToken.ThrowIfCancellationRequested();
- cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response);
+ var response = await GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression).SendAsync(message, HttpCompletionOption.ResponseContentRead, options.CancellationToken).ConfigureAwait(false);
- if (response.StatusCode == HttpStatusCode.NotModified)
+ if (options.EnableResponseCache)
{
- _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url);
+ if (response.StatusCode != HttpStatusCode.NotModified)
+ {
+ EnsureSuccessStatusCode(response);
+ }
- return GetCachedResponse(cachedReponsePath);
- }
+ options.CancellationToken.ThrowIfCancellationRequested();
+
+ cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response);
+
+ if (response.StatusCode == HttpStatusCode.NotModified)
+ {
+ _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url);
- if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue ||
- (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow))
+ return GetCachedResponse(cachedReponsePath);
+ }
+
+ if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue ||
+ (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow))
+ {
+ await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false);
+
+ return GetCachedResponse(cachedReponsePath);
+ }
+ }
+ else
{
- await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false);
+ EnsureSuccessStatusCode(response);
- return GetCachedResponse(cachedReponsePath);
+ options.CancellationToken.ThrowIfCancellationRequested();
}
+
+ return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
- else
+ catch (OperationCanceledException ex)
{
- EnsureSuccessStatusCode(response);
-
- options.CancellationToken.ThrowIfCancellationRequested();
+ throw GetCancellationException(options.Url, options.CancellationToken, ex);
}
+ catch (HttpRequestException ex)
+ {
+ _logger.ErrorException("Error getting response from " + options.Url, ex);
- return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- }
- catch (OperationCanceledException ex)
- {
- throw GetCancellationException(options.Url, options.CancellationToken, ex);
- }
- catch (HttpRequestException ex)
- {
- _logger.ErrorException("Error getting response from " + options.Url, ex);
-
- throw new HttpException(ex.Message, ex);
- }
- catch (Exception ex)
- {
- _logger.ErrorException("Error getting response from " + options.Url, ex);
+ throw new HttpException(ex.Message, ex);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error getting response from " + options.Url, ex);
- throw;
- }
- finally
- {
- if (options.ResourcePool != null)
+ throw;
+ }
+ finally
{
- options.ResourcePool.Release();
+ if (options.ResourcePool != null)
+ {
+ options.ResourcePool.Release();
+ }
}
}
+
}
/// <summary>