diff options
Diffstat (limited to 'MediaBrowser.Common.Implementations/HttpClientManager')
| -rw-r--r-- | MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs | 86 | ||||
| -rw-r--r-- | MediaBrowser.Common.Implementations/HttpClientManager/HttpResponseInfo.cs | 46 |
2 files changed, 86 insertions, 46 deletions
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<HttpResponseInfo> 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(); + } + } + } + } + /// <summary> /// Performs a GET request and returns the resulting stream /// </summary> 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 -{ - /// <summary> - /// Class HttpResponseOutput - /// </summary> - public class HttpResponseInfo - { - /// <summary> - /// Gets or sets the URL. - /// </summary> - /// <value>The URL.</value> - public string Url { get; set; } - - /// <summary> - /// Gets or sets the etag. - /// </summary> - /// <value>The etag.</value> - public string Etag { get; set; } - - /// <summary> - /// Gets or sets the last modified. - /// </summary> - /// <value>The last modified.</value> - public DateTime? LastModified { get; set; } - - /// <summary> - /// Gets or sets the expires. - /// </summary> - /// <value>The expires.</value> - public DateTime? Expires { get; set; } - - /// <summary> - /// Gets or sets a value indicating whether [must revalidate]. - /// </summary> - /// <value><c>true</c> if [must revalidate]; otherwise, <c>false</c>.</value> - public bool MustRevalidate { get; set; } - - /// <summary> - /// Gets or sets the request date. - /// </summary> - /// <value>The request date.</value> - public DateTime RequestDate { get; set; } - } -} |
