Fixed: Removing torcache url query params to avoid redirect.

fixes #799
Removing query param was cleaner coz it avoids spoofing the referrer.
This commit is contained in:
Taloth Saldono 2015-09-21 21:59:35 +02:00
parent d7eae958b7
commit 57afa668e1
9 changed files with 122 additions and 3 deletions

View File

@ -10,6 +10,7 @@ using NzbDrone.Test.Common;
using NzbDrone.Test.Common.Categories;
using NLog;
using NzbDrone.Common.TPL;
using Moq;
namespace NzbDrone.Common.Test.Http
{
@ -22,6 +23,7 @@ namespace NzbDrone.Common.Test.Http
{
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
Mocker.SetConstant<IRateLimitService>(Mocker.Resolve<RateLimitService>());
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(new IHttpRequestInterceptor[0]);
}
[Test]
@ -161,7 +163,7 @@ namespace NzbDrone.Common.Test.Http
var oldRequest = new HttpRequest("http://eu.httpbin.org/get");
oldRequest.AddCookie("my", "cookie");
var oldClient = new HttpClient(Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<Logger>());
var oldClient = new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<Logger>());
oldClient.Should().NotBeSameAs(Subject);
@ -269,6 +271,30 @@ namespace NzbDrone.Common.Test.Http
ExceptionVerification.IgnoreWarns();
}
[Test]
public void should_call_interceptor()
{
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(new [] { Mocker.GetMock<IHttpRequestInterceptor>().Object });
Mocker.GetMock<IHttpRequestInterceptor>()
.Setup(v => v.PreRequest(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => r);
Mocker.GetMock<IHttpRequestInterceptor>()
.Setup(v => v.PostResponse(It.IsAny<HttpResponse>()))
.Returns<HttpResponse>(r => r);
var request = new HttpRequest("http://eu.httpbin.org/get");
Subject.Get(request);
Mocker.GetMock<IHttpRequestInterceptor>()
.Verify(v => v.PreRequest(It.IsAny<HttpRequest>()), Times.Once());
Mocker.GetMock<IHttpRequestInterceptor>()
.Verify(v => v.PostResponse(It.IsAny<HttpResponse>()), Times.Once());
}
}
public class HttpBinResource

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
@ -27,11 +28,13 @@ namespace NzbDrone.Common.Http
private readonly IRateLimitService _rateLimitService;
private readonly ICached<CookieContainer> _cookieContainerCache;
private readonly ICached<bool> _curlTLSFallbackCache;
private readonly IEnumerable<IHttpRequestInterceptor> _requestInterceptors;
public HttpClient(ICacheManager cacheManager, IRateLimitService rateLimitService, Logger logger)
public HttpClient(IEnumerable<IHttpRequestInterceptor> requestInterceptors, ICacheManager cacheManager, IRateLimitService rateLimitService, Logger logger)
{
_logger = logger;
_rateLimitService = rateLimitService;
_requestInterceptors = requestInterceptors;
ServicePointManager.DefaultConnectionLimit = 12;
_cookieContainerCache = cacheManager.GetCache<CookieContainer>(typeof(HttpClient));
@ -40,6 +43,11 @@ namespace NzbDrone.Common.Http
public HttpResponse Execute(HttpRequest request)
{
foreach (var interceptor in _requestInterceptors)
{
request = interceptor.PreRequest(request);
}
if (request.RateLimit != TimeSpan.Zero)
{
_rateLimitService.WaitAndPulse(request.Url.Host, request.RateLimit);
@ -100,6 +108,11 @@ namespace NzbDrone.Common.Http
}
}
foreach (var interceptor in _requestInterceptors)
{
response = interceptor.PostResponse(response);
}
return response;
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Common.Http
{
public interface IHttpRequestInterceptor
{
HttpRequest PreRequest(HttpRequest request);
HttpResponse PostResponse(HttpResponse response);
}
}

View File

@ -153,6 +153,7 @@
<Compile Include="Http\HttpProvider.cs" />
<Compile Include="Http\HttpRequest.cs" />
<Compile Include="Http\HttpResponse.cs" />
<Compile Include="Http\IHttpRequestInterceptor.cs" />
<Compile Include="Http\JsonRpcRequestBuilder.cs" />
<Compile Include="Http\JsonRpcResponse.cs" />
<Compile Include="Http\NzbDroneWebClient.cs">

View File

@ -18,7 +18,7 @@ namespace NzbDrone.Core.Test.Framework
protected void UseRealHttp()
{
Mocker.SetConstant<IHttpProvider>(new HttpProvider(TestLogger));
Mocker.SetConstant<IHttpClient>(new HttpClient(Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), TestLogger));
Mocker.SetConstant<IHttpClient>(new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), TestLogger));
Mocker.SetConstant<IDroneServicesRequestBuilder>(new DroneServicesHttpRequestBuilder());
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.Http;
using NzbDrone.Test.Common;
using FluentAssertions;
namespace NzbDrone.Core.Test.Http
{
[TestFixture]
public class TorCacheHttpRequestInterceptorFixture : TestBase<TorCacheHttpRequestInterceptor>
{
[Test]
public void should_remove_query_params_from_torcache_request()
{
var request = new HttpRequest("http://torcache.net/download/123.torrent?title=something");
var newRequest = Subject.PreRequest(request);
newRequest.Url.AbsoluteUri.Should().Be("http://torcache.net/download/123.torrent");
}
[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)
{
var request = new HttpRequest(url);
var newRequest = Subject.PreRequest(request);
newRequest.Url.AbsoluteUri.Should().Be(url);
}
}
}

View File

@ -210,6 +210,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedPendingReleasesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasksFixture.cs" />
<Compile Include="Http\TorCacheHttpRequestInterceptorFixture.cs" />
<Compile Include="IndexerSearchTests\SeriesSearchServiceFixture.cs" />
<Compile Include="IndexerSearchTests\NzbSearchServiceFixture.cs" />
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Common.Http;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Core.Http
{
public class TorCacheHttpRequestInterceptor : IHttpRequestInterceptor
{
public HttpRequest PreRequest(HttpRequest request)
{
if (request.Url.Host == "torcache.net" && request.UriBuilder.Query.IsNotNullOrWhiteSpace())
{
request.UriBuilder.Query = string.Empty;
}
return request;
}
public HttpResponse PostResponse(HttpResponse response)
{
return response;
}
}
}

View File

@ -471,6 +471,7 @@
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
<Compile Include="Housekeeping\HousekeepingService.cs" />
<Compile Include="Housekeeping\IHousekeepingTask.cs" />
<Compile Include="Http\TorcacheHttpInterceptor.cs" />
<Compile Include="Indexers\BitMeTv\BitMeTv.cs" />
<Compile Include="Indexers\BitMeTv\BitMeTvSettings.cs" />
<Compile Include="Indexers\BitMeTv\BitMeTvRequestGenerator.cs" />