From 138a188cc9c9b55698dc124526e5bbb91134b4dc Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Tue, 30 Jun 2020 21:46:01 +0100 Subject: [PATCH] Fixed: Download mediacover using configured proxy. --- .../Http/HttpClientFixture.cs | 12 +++++++++++ .../Http/Dispatchers/ManagedHttpDispatcher.cs | 20 +++++++++++++------ src/NzbDrone.Common/Http/HttpClient.cs | 18 +++++++++++++---- src/NzbDrone.Common/Http/HttpRequest.cs | 2 ++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 4f81cd35f..3ff4f1444 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -278,6 +278,18 @@ namespace NzbDrone.Common.Test.Http response.Resource.Headers[header].ToString().Should().Be(value); } + [Test] + public void should_download_file() + { + var file = GetTempFilePath(); + + var url = "https://sonarr.tv/img/slider/seriesdetails.png"; + + Subject.DownloadFile(url, file); + + File.Exists(file).Should().BeTrue(); + } + [Test] public void should_not_download_file_with_error() { diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 08dd23811..1eb490ad6 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -32,7 +32,7 @@ namespace NzbDrone.Common.Http.Dispatchers { var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); - if (PlatformInfo.IsMono) + if (PlatformInfo.IsMono && request.ResponseStream == null) { // On Mono GZipStream/DeflateStream leaks memory if an exception is thrown, use an intermediate buffer in that case. webRequest.AutomaticDecompression = DecompressionMethods.None; @@ -120,12 +120,20 @@ namespace NzbDrone.Common.Http.Dispatchers { try { - data = responseStream.ToBytes(); - - if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip") + if (request.ResponseStream != null) + { + // A target ResponseStream was specified, write to that instead. + responseStream.CopyTo(request.ResponseStream); + } + else { - data = data.Decompress(); - httpWebResponse.Headers.Remove("Content-Encoding"); + data = responseStream.ToBytes(); + + if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip") + { + data = data.Decompress(); + httpWebResponse.Headers.Remove("Content-Encoding"); + } } } catch (Exception ex) diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 43e235321..0e6499866 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -133,7 +133,7 @@ namespace NzbDrone.Common.Http response = interceptor.PostResponse(response); } - if (request.LogResponseContent) + if (request.LogResponseContent && response.ResponseData != null) { _logger.Trace("Response content ({0} bytes): {1}", response.ResponseData.Length, response.Content); } @@ -242,9 +242,19 @@ namespace NzbDrone.Common.Http _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); - var webClient = new GZipWebClient(); - webClient.Headers.Add(HttpRequestHeader.UserAgent, userAgent ?? _userAgentBuilder.GetUserAgent()); - webClient.DownloadFile(url, fileName); + using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite)) + { + var request = new HttpRequest(url); + + if (userAgent.IsNotNullOrWhiteSpace()) + { + request.Headers.Set("User-Agent", userAgent); + } + + request.ResponseStream = fileStream; + var response = Get(request); + } + stopWatch.Stop(); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index 12efd6229..840f4b614 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -42,6 +43,7 @@ namespace NzbDrone.Common.Http public bool StoreResponseCookie { get; set; } public TimeSpan RequestTimeout { get; set; } public TimeSpan RateLimit { get; set; } + public Stream ResponseStream { get; set; } public override string ToString() {