diff options
| author | Tthecreator <epostvanthomas@kpnmail.nl> | 2019-01-22 15:22:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-22 15:22:42 +0000 |
| commit | 189b99df16bd4c93cc96422d7282d01d9ff5b82f (patch) | |
| tree | 26d7da95fe3e3b2772b8b39a2463a6c0ac7652fc /Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs | |
| parent | a00c0defa8cb22774f5dc8a7d566eb36ac7307e8 (diff) | |
| parent | edcfd8b565f632088c8b1f826db8e2fbecf9790d (diff) | |
Merge pull request #1 from jellyfin/dev
Update from jellyfin repo
Diffstat (limited to 'Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs')
| -rw-r--r-- | Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs | 74 |
1 files changed, 31 insertions, 43 deletions
diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs index 2f5eed547..ea620cb2e 100644 --- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs @@ -13,8 +13,8 @@ using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net; using MediaBrowser.Model.IO; -using Microsoft.Extensions.Logging; using MediaBrowser.Model.Net; +using Microsoft.Extensions.Logging; namespace Emby.Server.Implementations.HttpClientManager { @@ -44,18 +44,22 @@ namespace Emby.Server.Implementations.HttpClientManager /// <summary> /// Initializes a new instance of the <see cref="HttpClientManager" /> class. /// </summary> - public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, Func<string> defaultUserAgentFn) + public HttpClientManager( + IApplicationPaths appPaths, + ILoggerFactory loggerFactory, + IFileSystem fileSystem, + Func<string> defaultUserAgentFn) { if (appPaths == null) { throw new ArgumentNullException(nameof(appPaths)); } - if (logger == null) + if (loggerFactory == null) { - throw new ArgumentNullException(nameof(logger)); + throw new ArgumentNullException(nameof(loggerFactory)); } - _logger = logger; + _logger = loggerFactory.CreateLogger("HttpClient"); _fileSystem = fileSystem; _appPaths = appPaths; _defaultUserAgentFn = defaultUserAgentFn; @@ -82,7 +86,7 @@ namespace Emby.Server.Implementations.HttpClientManager /// <param name="host">The host.</param> /// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param> /// <returns>HttpClient.</returns> - /// <exception cref="System.ArgumentNullException">host</exception> + /// <exception cref="ArgumentNullException">host</exception> private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression) { if (string.IsNullOrEmpty(host)) @@ -90,11 +94,9 @@ namespace Emby.Server.Implementations.HttpClientManager throw new ArgumentNullException(nameof(host)); } - HttpClientInfo client; - var key = host + enableHttpCompression; - if (!_httpClients.TryGetValue(key, out client)) + if (!_httpClients.TryGetValue(key, out var client)) { client = new HttpClientInfo(); @@ -125,7 +127,7 @@ namespace Emby.Server.Implementations.HttpClientManager { string url = options.Url; - Uri uriAddress = new Uri(url); + var uriAddress = new Uri(url); string userInfo = uriAddress.UserInfo; if (!string.IsNullOrWhiteSpace(userInfo)) { @@ -133,7 +135,7 @@ namespace Emby.Server.Implementations.HttpClientManager url = url.Replace(userInfo + "@", string.Empty); } - WebRequest request = CreateWebRequest(url); + var request = CreateWebRequest(url); if (request is HttpWebRequest httpWebRequest) { @@ -188,7 +190,7 @@ namespace Emby.Server.Implementations.HttpClientManager private static CredentialCache GetCredential(string url, string username, string password) { //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; - CredentialCache credentialCache = new CredentialCache(); + var credentialCache = new CredentialCache(); credentialCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password)); return credentialCache; } @@ -266,7 +268,7 @@ namespace Emby.Server.Implementations.HttpClientManager var responseCachePath = Path.Combine(_appPaths.CachePath, "httpclient", urlHash); - var response = await GetCachedResponse(responseCachePath, options.CacheLength, url).ConfigureAwait(false); + var response = GetCachedResponse(responseCachePath, options.CacheLength, url); if (response != null) { return response; @@ -282,30 +284,24 @@ namespace Emby.Server.Implementations.HttpClientManager return response; } - private async Task<HttpResponseInfo> GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) + private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) { try { if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) { - using (var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true)) - { - var memoryStream = new MemoryStream(); - - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; + var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true); - return new HttpResponseInfo - { - ResponseUrl = url, - Content = memoryStream, - StatusCode = HttpStatusCode.OK, - ContentLength = memoryStream.Length - }; - } + return new HttpResponseInfo + { + ResponseUrl = url, + Content = stream, + StatusCode = HttpStatusCode.OK, + ContentLength = stream.Length + }; } } - catch (FileNotFoundException) + catch (FileNotFoundException) // REVIEW: @bond Is this really faster? { } @@ -321,19 +317,11 @@ namespace Emby.Server.Implementations.HttpClientManager { _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(responseCachePath)); - using (var responseStream = response.Content) + using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true)) { - var memoryStream = new MemoryStream(); - await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; + await response.Content.CopyToAsync(fileStream).ConfigureAwait(false); - using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true)) - { - await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); - - memoryStream.Position = 0; - response.Content = memoryStream; - } + response.Content.Position = 0; } } @@ -541,7 +529,7 @@ namespace Emby.Server.Implementations.HttpClientManager if (options.Progress == null) { - throw new ArgumentException("Options did not have a Progress value.",nameof(options)); + throw new ArgumentException("Options did not have a Progress value.", nameof(options)); } options.CancellationToken.ThrowIfCancellationRequested(); @@ -807,7 +795,7 @@ namespace Emby.Server.Implementations.HttpClientManager { var taskCompletion = new TaskCompletionSource<WebResponse>(); - Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null); + var asyncTask = Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null); ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, TimeoutCallback, request, timeout, true); var callback = new TaskCallback { taskCompletion = taskCompletion }; @@ -823,7 +811,7 @@ namespace Emby.Server.Implementations.HttpClientManager { if (timedOut && state != null) { - WebRequest request = (WebRequest)state; + var request = (WebRequest)state; request.Abort(); } } |
