From ca9c6c651fe3249b6f776a2ce2f7f5d65ccb9f38 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 13 Dec 2014 15:56:11 -0800 Subject: [PATCH] Fixed: Follow redirects when getting torrent from KAT --- .../UTorrentTests/UTorrentFixture.cs | 23 +++++++++++++++++++ .../Download/Clients/Sabnzbd/SabnzbdProxy.cs | 2 +- .../Download/TorrentClientBase.cs | 18 +++++++++------ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/UTorrentTests/UTorrentFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/UTorrentTests/UTorrentFixture.cs index 2efff07f7..c29a07879 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/UTorrentTests/UTorrentFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/UTorrentTests/UTorrentFixture.cs @@ -104,6 +104,16 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.UTorrentTests .Returns(r => new HttpResponse(r, httpHeader, new Byte[0], System.Net.HttpStatusCode.SeeOther)); } + protected void GivenRedirectToTorrent() + { + var httpHeader = new HttpHeader(); + httpHeader["Location"] = "http://test.sonarr.tv/not-a-real-torrent.torrent"; + + Mocker.GetMock() + .Setup(s => s.Get(It.Is(h => h.Url.AbsoluteUri == _downloadUrl))) + .Returns(r => new HttpResponse(r, httpHeader, new Byte[0], System.Net.HttpStatusCode.Found)); + } + protected void GivenFailedDownload() { Mocker.GetMock() @@ -330,5 +340,18 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.UTorrentTests id.Should().NotBeNullOrEmpty(); } + + [Test] + public void Download_should_handle_http_redirect_to_torrent() + { + GivenRedirectToTorrent(); + GivenSuccessfulDownload(); + + var remoteEpisode = CreateRemoteEpisode(); + + var id = Subject.Download(remoteEpisode); + + id.Should().NotBeNullOrEmpty(); + } } } diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs index ff4948540..271d9f7b4 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs @@ -134,7 +134,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd String.Format("ma_username={0}&ma_password={1}", settings.Username, settings.Password) : String.Format("apikey={0}", settings.ApiKey); - var url = string.Format(@"{0}://{1}:{2}/api?{3}&{4}&output=json", + var url = String.Format(@"{0}://{1}:{2}/api?{3}&{4}&output=json", protocol, settings.Host, settings.Port, diff --git a/src/NzbDrone.Core/Download/TorrentClientBase.cs b/src/NzbDrone.Core/Download/TorrentClientBase.cs index 984d39758..8524d7828 100644 --- a/src/NzbDrone.Core/Download/TorrentClientBase.cs +++ b/src/NzbDrone.Core/Download/TorrentClientBase.cs @@ -70,7 +70,7 @@ namespace NzbDrone.Core.Download String hash = null; - if (!magnetUrl.IsNullOrWhiteSpace()) + if (magnetUrl.IsNotNullOrWhiteSpace()) { hash = DownloadFromMagnetUrl(remoteEpisode, magnetUrl); } @@ -100,19 +100,26 @@ namespace NzbDrone.Core.Download var response = _httpClient.Get(request); - if (response.StatusCode == HttpStatusCode.SeeOther) + if (response.StatusCode == HttpStatusCode.SeeOther || response.StatusCode == HttpStatusCode.Found) { var locationHeader = (string)response.Headers.GetValueOrDefault("Location", null); - if (locationHeader != null && locationHeader.StartsWith("magnet:")) + if (locationHeader != null) { - return DownloadFromMagnetUrl(remoteEpisode, locationHeader); + if (locationHeader.StartsWith("magnet:")) + { + return DownloadFromMagnetUrl(remoteEpisode, locationHeader); + } + + return DownloadFromWebUrl(remoteEpisode, locationHeader); } + throw new WebException("Remote website tried to redirect without providing a location."); } torrentFile = response.ResponseData; } + catch (WebException ex) { _logger.ErrorException(String.Format("Downloading torrentfile for episode '{0}' failed ({1})", @@ -121,11 +128,8 @@ namespace NzbDrone.Core.Download throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed", ex); } - var filename = String.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteEpisode.Release.Title)); - var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile); - var actualHash = AddFromTorrentFile(remoteEpisode, hash, filename, torrentFile); if (hash != actualHash)