From 410b46f05dd55b87704131b5ed9ffd957e17ecda Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 22 May 2013 12:51:39 -0400 Subject: [PATCH] put request message disposal back in --- .../HttpClientManager/HttpClientManager.cs | 148 +++++++++--------- 1 file changed, 75 insertions(+), 73 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); } - - options.CancellationToken.ThrowIfCancellationRequested(); - - cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); - - if (response.StatusCode == HttpStatusCode.NotModified) + else if (cachedInfo.LastModified.HasValue) { - _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); - - 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); + message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); } } - else - { - EnsureSuccessStatusCode(response); - options.CancellationToken.ThrowIfCancellationRequested(); - } - - 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; - } - finally - { if (options.ResourcePool != null) { - options.ResourcePool.Release(); + 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 (response.StatusCode != HttpStatusCode.NotModified) + { + EnsureSuccessStatusCode(response); + } + + 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); + + 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 + { + EnsureSuccessStatusCode(response); + + options.CancellationToken.ThrowIfCancellationRequested(); + } + + 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; + } + finally + { + if (options.ResourcePool != null) + { + options.ResourcePool.Release(); + } } } + } ///