From cb9b570a2a19463c16e4644b60a2916579a7806c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 16 Dec 2013 13:44:03 -0500 Subject: live tv updates --- .../HttpClientManager/HttpClientManager.cs | 207 ++++++++------------- 1 file changed, 81 insertions(+), 126 deletions(-) (limited to 'MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs') diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index b5317319f..07c0f8ab7 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -132,7 +132,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager #if __MonoCS__ return GetMonoRequest(options, method, enableHttpCompression); #endif - + var request = HttpWebRequest.CreateHttp(options.Url); if (!string.IsNullOrEmpty(options.AcceptHeader)) @@ -172,9 +172,64 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// Task{HttpResponseInfo}. /// /// - public async Task GetResponse(HttpRequestOptions options) + public Task GetResponse(HttpRequestOptions options) + { + return SendAsync(options, "GET"); + } + + /// + /// Performs a GET request and returns the resulting stream + /// + /// The options. + /// Task{Stream}. + /// + /// + public async Task Get(HttpRequestOptions options) + { + var response = await GetResponse(options).ConfigureAwait(false); + + return response.Content; + } + + /// + /// Performs a GET request and returns the resulting stream + /// + /// The URL. + /// The resource pool. + /// The cancellation token. + /// Task{Stream}. + public Task Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) { - ValidateParams(options.Url, options.CancellationToken); + return Get(new HttpRequestOptions + { + Url = url, + ResourcePool = resourcePool, + CancellationToken = cancellationToken, + }); + } + + /// + /// Gets the specified URL. + /// + /// The URL. + /// The cancellation token. + /// Task{Stream}. + public Task Get(string url, CancellationToken cancellationToken) + { + return Get(url, null, cancellationToken); + } + + /// + /// send as an asynchronous operation. + /// + /// The options. + /// The HTTP method. + /// Task{HttpResponseInfo}. + /// + /// + private async Task SendAsync(HttpRequestOptions options, string httpMethod) + { + ValidateParams(options); options.CancellationToken.ThrowIfCancellationRequested(); @@ -185,7 +240,17 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url)) { IsTimedOut = true }; } - var httpWebRequest = GetRequest(options, "GET", options.EnableHttpCompression); + var httpWebRequest = GetRequest(options, httpMethod, options.EnableHttpCompression); + + if (!string.IsNullOrEmpty(options.RequestContent) || string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase)) + { + var content = options.RequestContent ?? string.Empty; + var bytes = Encoding.UTF8.GetBytes(content); + + httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded"; + httpWebRequest.ContentLength = bytes.Length; + httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length); + } if (options.ResourcePool != null) { @@ -202,7 +267,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true }; } - _logger.Info("HttpClientManager.GET url: {0}", options.Url); + _logger.Info("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url); try { @@ -275,46 +340,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } } - /// - /// Performs a GET request and returns the resulting stream - /// - /// The options. - /// Task{Stream}. - /// - /// - public async Task Get(HttpRequestOptions options) + public Task Post(HttpRequestOptions options) { - var response = await GetResponse(options).ConfigureAwait(false); - - return response.Content; - } - - /// - /// Performs a GET request and returns the resulting stream - /// - /// The URL. - /// The resource pool. - /// The cancellation token. - /// Task{Stream}. - public Task Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) - { - return Get(new HttpRequestOptions - { - Url = url, - ResourcePool = resourcePool, - CancellationToken = cancellationToken, - }); - } - - /// - /// Gets the specified URL. - /// - /// The URL. - /// The cancellation token. - /// Task{Stream}. - public Task Get(string url, CancellationToken cancellationToken) - { - return Get(url, null, cancellationToken); + return SendAsync(options, "POST"); } /// @@ -329,82 +357,15 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// public async Task Post(HttpRequestOptions options, Dictionary postData) { - ValidateParams(options.Url, options.CancellationToken); - - options.CancellationToken.ThrowIfCancellationRequested(); - - var httpWebRequest = GetRequest(options, "POST", options.EnableHttpCompression); - var strings = postData.Keys.Select(key => string.Format("{0}={1}", key, postData[key])); var postContent = string.Join("&", strings.ToArray()); - var bytes = Encoding.UTF8.GetBytes(postContent); - - httpWebRequest.ContentType = "application/x-www-form-urlencoded"; - httpWebRequest.ContentLength = bytes.Length; - httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length); - - if (options.ResourcePool != null) - { - await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); - } - - _logger.Info("HttpClientManager.POST url: {0}", options.Url); - - try - { - options.CancellationToken.ThrowIfCancellationRequested(); - - using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false)) - { - var httpResponse = (HttpWebResponse)response; - - EnsureSuccessStatusCode(httpResponse); - - options.CancellationToken.ThrowIfCancellationRequested(); - - using (var stream = httpResponse.GetResponseStream()) - { - var memoryStream = new MemoryStream(); - - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; + options.RequestContent = postContent; + options.RequestContentType = "application/x-www-form-urlencoded"; - return memoryStream; - } - } - } - catch (OperationCanceledException ex) - { - var exception = GetCancellationException(options.Url, options.CancellationToken, ex); + var response = await Post(options).ConfigureAwait(false); - throw exception; - } - catch (HttpRequestException ex) - { - _logger.ErrorException("Error getting response from " + options.Url, ex); - - throw new HttpException(ex.Message, ex); - } - catch (WebException 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(); - } - } + return response.Content; } /// @@ -443,7 +404,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager public async Task GetTempFileResponse(HttpRequestOptions options) { - ValidateParams(options.Url, options.CancellationToken); + ValidateParams(options); Directory.CreateDirectory(_appPaths.TempDirectory); @@ -592,7 +553,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { return new HttpException(ex.Message, ex); } - + return ex; } @@ -608,17 +569,11 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } } - /// - /// Validates the params. - /// - /// The URL. - /// The cancellation token. - /// url - private void ValidateParams(string url, CancellationToken cancellationToken) + private void ValidateParams(HttpRequestOptions options) { - if (string.IsNullOrEmpty(url)) + if (string.IsNullOrEmpty(options.Url)) { - throw new ArgumentNullException("url"); + throw new ArgumentNullException("options"); } } -- cgit v1.2.3