Fixed: Follow redirects when getting torrent from KAT

This commit is contained in:
Mark McDowall 2014-12-13 15:56:11 -08:00
parent a066dcbc07
commit ca9c6c651f
3 changed files with 35 additions and 8 deletions

View File

@ -104,6 +104,16 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.UTorrentTests
.Returns<HttpRequest>(r => new HttpResponse(r, httpHeader, new Byte[0], System.Net.HttpStatusCode.SeeOther)); .Returns<HttpRequest>(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<IHttpClient>()
.Setup(s => s.Get(It.Is<HttpRequest>(h => h.Url.AbsoluteUri == _downloadUrl)))
.Returns<HttpRequest>(r => new HttpResponse(r, httpHeader, new Byte[0], System.Net.HttpStatusCode.Found));
}
protected void GivenFailedDownload() protected void GivenFailedDownload()
{ {
Mocker.GetMock<IUTorrentProxy>() Mocker.GetMock<IUTorrentProxy>()
@ -330,5 +340,18 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.UTorrentTests
id.Should().NotBeNullOrEmpty(); 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();
}
} }
} }

View File

@ -134,7 +134,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
String.Format("ma_username={0}&ma_password={1}", settings.Username, settings.Password) : String.Format("ma_username={0}&ma_password={1}", settings.Username, settings.Password) :
String.Format("apikey={0}", settings.ApiKey); 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, protocol,
settings.Host, settings.Host,
settings.Port, settings.Port,

View File

@ -70,7 +70,7 @@ namespace NzbDrone.Core.Download
String hash = null; String hash = null;
if (!magnetUrl.IsNullOrWhiteSpace()) if (magnetUrl.IsNotNullOrWhiteSpace())
{ {
hash = DownloadFromMagnetUrl(remoteEpisode, magnetUrl); hash = DownloadFromMagnetUrl(remoteEpisode, magnetUrl);
} }
@ -100,19 +100,26 @@ namespace NzbDrone.Core.Download
var response = _httpClient.Get(request); 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); 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."); throw new WebException("Remote website tried to redirect without providing a location.");
} }
torrentFile = response.ResponseData; torrentFile = response.ResponseData;
} }
catch (WebException ex) catch (WebException ex)
{ {
_logger.ErrorException(String.Format("Downloading torrentfile for episode '{0}' failed ({1})", _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); throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed", ex);
} }
var filename = String.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteEpisode.Release.Title)); var filename = String.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteEpisode.Release.Title));
var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile); var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile);
var actualHash = AddFromTorrentFile(remoteEpisode, hash, filename, torrentFile); var actualHash = AddFromTorrentFile(remoteEpisode, hash, filename, torrentFile);
if (hash != actualHash) if (hash != actualHash)