From ccfd66260dded4eb1febb2ddd9af7e035087a1a8 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 2 Apr 2015 20:58:10 +0200 Subject: [PATCH] Fixed: BitMeTv cookie will now also be used for the fetching the torrent file. --- .../Http/HttpClientFixture.cs | 65 ++++++++++++++++++- src/NzbDrone.Common/Http/HttpClient.cs | 15 ++++- src/NzbDrone.Core.Test/Framework/CoreTest.cs | 3 +- src/TVDBSharp/Models/DAO/DataProvider.cs | 3 +- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 0b7d0fb9b..7039e34a1 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -4,9 +4,11 @@ using System.IO; using System.Net; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common.Cache; using NzbDrone.Common.Http; using NzbDrone.Test.Common; using NzbDrone.Test.Common.Categories; +using NLog; namespace NzbDrone.Common.Test.Http { @@ -14,6 +16,12 @@ namespace NzbDrone.Common.Test.Http [IntegrationTest] public class HttpClientFixture : TestBase { + [SetUp] + public void SetUp() + { + Mocker.SetConstant(Mocker.Resolve()); + } + [Test] public void should_execute_simple_get() { @@ -100,7 +108,6 @@ namespace NzbDrone.Common.Test.Http response.Resource.Headers[header].ToString().Should().Be(value); } - [Test] public void should_not_download_file_with_error() { @@ -111,7 +118,63 @@ namespace NzbDrone.Common.Test.Http File.Exists(file).Should().BeFalse(); ExceptionVerification.ExpectedWarns(1); + } + + [Test] + public void should_send_cookie() + { + var request = new HttpRequest("http://eu.httpbin.org/get"); + request.AddCookie("my", "cookie"); + + var response = Subject.Get(request); + + response.Resource.Headers.Should().ContainKey("Cookie"); + + var cookie = response.Resource.Headers["Cookie"].ToString(); + + cookie.Should().Contain("my=cookie"); + } + + public void GivenOldCookie() + { + var oldRequest = new HttpRequest("http://eu.httpbin.org/get"); + oldRequest.AddCookie("my", "cookie"); + + var oldClient = new HttpClient(Mocker.Resolve(), Mocker.Resolve()); + + oldClient.Should().NotBeSameAs(Subject); + + var oldResponse = oldClient.Get(oldRequest); + + oldResponse.Resource.Headers.Should().ContainKey("Cookie"); + } + + [Test] + public void should_preserve_cookie_during_session() + { + GivenOldCookie(); + + var request = new HttpRequest("http://eu.httpbin.org/get"); + + var response = Subject.Get(request); + + response.Resource.Headers.Should().ContainKey("Cookie"); + + var cookie = response.Resource.Headers["Cookie"].ToString(); + + cookie.Should().Contain("my=cookie"); + } + + [Test] + public void should_not_send_cookie_to_other_host() + { + GivenOldCookie(); + + var request = new HttpRequest("http://httpbin.org/get"); + + var response = Subject.Get(request); + response.Resource.Headers.Should().NotContainKey("Cookie"); } } diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index d5585250b..9f88541b1 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using System.Net; using NLog; +using NzbDrone.Common.Cache; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -23,10 +24,14 @@ namespace NzbDrone.Common.Http { private readonly Logger _logger; - public HttpClient(Logger logger) + private readonly ICached _cookieContainerCache; + + public HttpClient(ICacheManager cacheManager, Logger logger) { _logger = logger; ServicePointManager.DefaultConnectionLimit = 12; + + _cookieContainerCache = cacheManager.GetCache(typeof(HttpClient)); } public HttpResponse Execute(HttpRequest request) @@ -47,6 +52,8 @@ namespace NzbDrone.Common.Http webRequest.AllowAutoRedirect = request.AllowAutoRedirect; webRequest.ContentLength = 0; + webRequest.CookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); + if (!RuntimeInfoBase.IsProduction) { webRequest.AllowAutoRedirect = false; @@ -61,10 +68,12 @@ namespace NzbDrone.Common.Http if (request.Cookies.Count != 0) { - webRequest.CookieContainer = new CookieContainer(); foreach (var pair in request.Cookies) { - webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host)); + webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host) + { + Expires = DateTime.UtcNow.AddHours(1) + }); } } diff --git a/src/NzbDrone.Core.Test/Framework/CoreTest.cs b/src/NzbDrone.Core.Test/Framework/CoreTest.cs index aa045bda9..bf3d2bd7a 100644 --- a/src/NzbDrone.Core.Test/Framework/CoreTest.cs +++ b/src/NzbDrone.Core.Test/Framework/CoreTest.cs @@ -1,5 +1,6 @@ using System.IO; using NUnit.Framework; +using NzbDrone.Common.Cache; using NzbDrone.Common.Cloud; using NzbDrone.Common.Http; using NzbDrone.Test.Common; @@ -16,7 +17,7 @@ namespace NzbDrone.Core.Test.Framework protected void UseRealHttp() { Mocker.SetConstant(new HttpProvider(TestLogger)); - Mocker.SetConstant(new HttpClient(TestLogger)); + Mocker.SetConstant(new HttpClient(Mocker.Resolve(), TestLogger)); Mocker.SetConstant(new DroneServicesHttpRequestBuilder()); } } diff --git a/src/TVDBSharp/Models/DAO/DataProvider.cs b/src/TVDBSharp/Models/DAO/DataProvider.cs index d55220b33..069233ac1 100644 --- a/src/TVDBSharp/Models/DAO/DataProvider.cs +++ b/src/TVDBSharp/Models/DAO/DataProvider.cs @@ -1,6 +1,7 @@ using System.IO; using System.Net; using System.Xml.Linq; +using NzbDrone.Common.Cache; using NzbDrone.Common.Http; using NzbDrone.Common.Instrumentation; using TVDBSharp.Models.Enums; @@ -16,7 +17,7 @@ namespace TVDBSharp.Models.DAO private const string BaseUrl = "http://thetvdb.com"; - private static HttpClient httpClient = new HttpClient(NzbDroneLogger.GetLogger(typeof(DataProvider))); + private static HttpClient httpClient = new HttpClient(new CacheManager(), NzbDroneLogger.GetLogger(typeof(DataProvider))); public XDocument GetShow(int showID) {