Updated URL rewriter to handle torcache Referer weirdness.

This commit is contained in:
Taloth Saldono 2016-01-17 16:07:07 +01:00
parent cf25097cd1
commit 958b294152
2 changed files with 14 additions and 1 deletions

View File

@ -23,6 +23,16 @@ namespace NzbDrone.Core.Test.Http
newRequest.Url.AbsoluteUri.Should().Be("http://torcache.net/download/123.torrent"); newRequest.Url.AbsoluteUri.Should().Be("http://torcache.net/download/123.torrent");
} }
[Test]
public void should_add_referrer_torcache_request()
{
var request = new HttpRequest("http://torcache.net/download/123.torrent?title=something");
var newRequest = Subject.PreRequest(request);
newRequest.Headers.Should().Contain("Referer", "http://torcache.net/");
}
[TestCase("http://site.com/download?url=torcache.net&blaat=1")] [TestCase("http://site.com/download?url=torcache.net&blaat=1")]
[TestCase("http://torcache.net.com/download?url=123")] [TestCase("http://torcache.net.com/download?url=123")]
public void should_not_remove_query_params_from_other_requests(string url) public void should_not_remove_query_params_from_other_requests(string url)

View File

@ -11,9 +11,12 @@ namespace NzbDrone.Core.Http
{ {
public HttpRequest PreRequest(HttpRequest request) public HttpRequest PreRequest(HttpRequest request)
{ {
if (request.Url.Host == "torcache.net" && request.UriBuilder.Query.IsNotNullOrWhiteSpace()) // torcache behaves strangely when it has query params and/or no Referer or browser User-Agent.
// It's a bit vague, and we don't need the query params. So we remove the query params and set a Referer to be safe.
if (request.Url.Host == "torcache.net")
{ {
request.UriBuilder.Query = string.Empty; request.UriBuilder.Query = string.Empty;
request.Headers.Add("Referer", request.Url.Scheme + @"://torcache.net/");
} }
return request; return request;