From 958b29415202ff9e4a10f8fb71b0767bad881823 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 17 Jan 2016 16:07:07 +0100 Subject: [PATCH] Updated URL rewriter to handle torcache Referer weirdness. --- .../Http/TorCacheHttpRequestInterceptorFixture.cs | 10 ++++++++++ src/NzbDrone.Core/Http/TorcacheHttpInterceptor.cs | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Core.Test/Http/TorCacheHttpRequestInterceptorFixture.cs b/src/NzbDrone.Core.Test/Http/TorCacheHttpRequestInterceptorFixture.cs index 165510b55..57de87470 100644 --- a/src/NzbDrone.Core.Test/Http/TorCacheHttpRequestInterceptorFixture.cs +++ b/src/NzbDrone.Core.Test/Http/TorCacheHttpRequestInterceptorFixture.cs @@ -23,6 +23,16 @@ public void should_remove_query_params_from_torcache_request() 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://torcache.net.com/download?url=123")] public void should_not_remove_query_params_from_other_requests(string url) diff --git a/src/NzbDrone.Core/Http/TorcacheHttpInterceptor.cs b/src/NzbDrone.Core/Http/TorcacheHttpInterceptor.cs index 31cfbba0a..e5b19e4a2 100644 --- a/src/NzbDrone.Core/Http/TorcacheHttpInterceptor.cs +++ b/src/NzbDrone.Core/Http/TorcacheHttpInterceptor.cs @@ -11,9 +11,12 @@ public class TorCacheHttpRequestInterceptor : IHttpRequestInterceptor { 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.Headers.Add("Referer", request.Url.Scheme + @"://torcache.net/"); } return request;