diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs index b8c52a53fb..ae62f34e06 100644 --- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs @@ -21,19 +21,18 @@ namespace Emby.Server.Implementations.HttpClientManager /// public class HttpClientManager : IHttpClient { - /// - /// The _logger - /// private readonly ILogger _logger; - - /// - /// The _app paths - /// private readonly IApplicationPaths _appPaths; - private readonly IFileSystem _fileSystem; private readonly Func _defaultUserAgentFn; + /// + /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests. + /// DON'T dispose it after use. + /// + /// The HTTP clients. + private readonly ConcurrentDictionary _httpClients = new ConcurrentDictionary(); + /// /// Initializes a new instance of the class. /// @@ -60,19 +59,10 @@ namespace Emby.Server.Implementations.HttpClientManager } /// - /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests. - /// DON'T dispose it after use. + /// Gets the correct http client for the given url. /// - /// The HTTP clients. - private readonly ConcurrentDictionary _httpClients = new ConcurrentDictionary(); - - /// - /// Gets - /// - /// The host. - /// if set to true [enable HTTP compression]. + /// The url. /// HttpClient. - /// host private HttpClient GetHttpClient(string url) { var key = GetHostFromUrl(url); @@ -116,7 +106,6 @@ namespace Emby.Server.Implementations.HttpClientManager case CompressionMethod.Gzip: request.Headers.Add(HeaderNames.AcceptEncoding, "gzip"); break; - case 0: default: break; } @@ -187,8 +176,6 @@ namespace Emby.Server.Implementations.HttpClientManager /// The options. /// The HTTP method. /// Task{HttpResponseInfo}. - /// - /// public Task SendAsync(HttpRequestOptions options, string httpMethod) { var httpMethod2 = GetHttpMethod(httpMethod); @@ -201,8 +188,6 @@ namespace Emby.Server.Implementations.HttpClientManager /// The options. /// The HTTP method. /// Task{HttpResponseInfo}. - /// - /// public async Task SendAsync(HttpRequestOptions options, HttpMethod httpMethod) { if (options.CacheMode == CacheMode.None) @@ -310,7 +295,6 @@ namespace Emby.Server.Implementations.HttpClientManager || !string.IsNullOrEmpty(options.RequestContent) || httpMethod == HttpMethod.Post) { - if (options.RequestContentBytes != null) { httpWebRequest.Content = new ByteArrayContent(options.RequestContentBytes); @@ -323,6 +307,8 @@ namespace Emby.Server.Implementations.HttpClientManager { httpWebRequest.Content = new ByteArrayContent(Array.Empty()); } + + // TODO: add correct content type /* var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded"; @@ -341,16 +327,24 @@ namespace Emby.Server.Implementations.HttpClientManager options.CancellationToken.ThrowIfCancellationRequested(); - /*if (!options.BufferContent) + if (!options.BufferContent) { - var response = await client.HttpClient.SendAsync(httpWebRequest).ConfigureAwait(false); + var response = await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false); - await EnsureSuccessStatusCode(client, response, options).ConfigureAwait(false); + await EnsureSuccessStatusCode(response, options).ConfigureAwait(false); options.CancellationToken.ThrowIfCancellationRequested(); - return GetResponseInfo(response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false), response.Content.Headers.ContentLength, response); - }*/ + var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + return new HttpResponseInfo(response.Headers) + { + Content = stream, + StatusCode = response.StatusCode, + ContentType = response.Content.Headers.ContentType?.MediaType, + ContentLength = stream.Length, + ResponseUrl = response.Content.Headers.ContentLocation?.ToString() + }; + } using (var response = await client.SendAsync(httpWebRequest, options.CancellationToken).ConfigureAwait(false)) { @@ -364,7 +358,7 @@ namespace Emby.Server.Implementations.HttpClientManager await stream.CopyToAsync(memoryStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); memoryStream.Position = 0; - var responseInfo = new HttpResponseInfo(response.Headers) + return new HttpResponseInfo(response.Headers) { Content = memoryStream, StatusCode = response.StatusCode, @@ -372,8 +366,6 @@ namespace Emby.Server.Implementations.HttpClientManager ContentLength = memoryStream.Length, ResponseUrl = response.Content.Headers.ContentLocation?.ToString() }; - - return responseInfo; } } } diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index 432e389d3b..0576a1a5d9 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -120,6 +120,7 @@ namespace MediaBrowser.Common.Net Unconditional = 1 } + [Flags] public enum CompressionMethod { None = 0b00000001,