From 8a1b12b7d805ce35f4e10acf2294f355f47e75e3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 28 Jun 2013 16:25:58 -0400 Subject: tightened up image saving to reduce knowledge of file names --- .../HttpClientManager/HttpClientManager.cs | 86 ++++++++++++++++++++++ .../HttpClientManager/HttpResponseInfo.cs | 46 ------------ 2 files changed, 86 insertions(+), 46 deletions(-) delete mode 100644 MediaBrowser.Common.Implementations/HttpClientManager/HttpResponseInfo.cs (limited to 'MediaBrowser.Common.Implementations/HttpClientManager') diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index f776cf675..d664d0bba 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -104,6 +104,92 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager return client; } + public async Task GetResponse(HttpRequestOptions options) + { + ValidateParams(options.Url, options.CancellationToken); + + options.CancellationToken.ThrowIfCancellationRequested(); + + var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression); + + if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < 30) + { + throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url)) { IsTimedOut = true }; + } + + using (var message = GetHttpRequestMessage(options)) + { + if (options.ResourcePool != null) + { + await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); + } + + if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < 30) + { + if (options.ResourcePool != null) + { + options.ResourcePool.Release(); + } + + throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true }; + } + + _logger.Info("HttpClientManager.Get url: {0}", options.Url); + + try + { + options.CancellationToken.ThrowIfCancellationRequested(); + + var response = await client.HttpClient.SendAsync(message, HttpCompletionOption.ResponseContentRead, options.CancellationToken).ConfigureAwait(false); + + EnsureSuccessStatusCode(response); + + options.CancellationToken.ThrowIfCancellationRequested(); + + return new HttpResponseInfo + { + Content = await response.Content.ReadAsStreamAsync().ConfigureAwait(false), + + StatusCode = response.StatusCode, + + ContentType = response.Content.Headers.ContentType.MediaType + }; + } + catch (OperationCanceledException ex) + { + var exception = GetCancellationException(options.Url, options.CancellationToken, ex); + + var httpException = exception as HttpException; + + if (httpException != null && httpException.IsTimedOut) + { + client.LastTimeout = DateTime.UtcNow; + } + + throw exception; + } + 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(); + } + } + } + } + /// /// Performs a GET request and returns the resulting stream /// diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpResponseInfo.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpResponseInfo.cs deleted file mode 100644 index 4a4612ffb..000000000 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpResponseInfo.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; - -namespace MediaBrowser.Common.Implementations.HttpClientManager -{ - /// - /// Class HttpResponseOutput - /// - public class HttpResponseInfo - { - /// - /// Gets or sets the URL. - /// - /// The URL. - public string Url { get; set; } - - /// - /// Gets or sets the etag. - /// - /// The etag. - public string Etag { get; set; } - - /// - /// Gets or sets the last modified. - /// - /// The last modified. - public DateTime? LastModified { get; set; } - - /// - /// Gets or sets the expires. - /// - /// The expires. - public DateTime? Expires { get; set; } - - /// - /// Gets or sets a value indicating whether [must revalidate]. - /// - /// true if [must revalidate]; otherwise, false. - public bool MustRevalidate { get; set; } - - /// - /// Gets or sets the request date. - /// - /// The request date. - public DateTime RequestDate { get; set; } - } -} -- cgit v1.2.3