From e28b2e83284f689d3620d8d394ef35fdfd55646f Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 15 Nov 2019 18:38:34 +0100 Subject: [PATCH] Fixed: Corrupt image files when downloading from redirecting Url closes #3401 --- .../Http/HttpClientFixture.cs | 25 +++++++++++++++++++ .../Http/Dispatchers/ManagedHttpDispatcher.cs | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 8ccb60bfc..6efe9a1d9 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -301,6 +301,31 @@ namespace NzbDrone.Common.Test.Http ExceptionVerification.ExpectedWarns(1); } + [Test] + public void should_not_write_redirect_content_to_stream() + { + var file = GetTempFilePath(); + + using (var fileStream = new FileStream(file, FileMode.Create)) + { + var request = new HttpRequest($"http://{_httpBinHost}/redirect/1"); + request.AllowAutoRedirect = false; + request.ResponseStream = fileStream; + + var response = Subject.Get(request); + + response.StatusCode.Should().Be(HttpStatusCode.Redirect); + } + + ExceptionVerification.ExpectedErrors(1); + + File.Exists(file).Should().BeTrue(); + + var fileInfo = new FileInfo(file); + + fileInfo.Length.Should().Be(0); + } + [Test] public void should_send_cookie() { diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 83773b2b2..d0f92c331 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -121,9 +121,10 @@ namespace NzbDrone.Common.Http.Dispatchers { try { - if (request.ResponseStream != null) + if (request.ResponseStream != null && httpWebResponse.StatusCode == HttpStatusCode.OK) { // A target ResponseStream was specified, write to that instead. + // But only on the OK status code, since we don't want to write failures and redirects. responseStream.CopyTo(request.ResponseStream); } else