From aac4938598b0ebc1df77d8776970b90ef05924c9 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 25 Oct 2015 22:57:37 +0100 Subject: [PATCH] Fixed handling cookies in different system languages. fixes #896... hopefully --- .../Http/HttpClientFixture.cs | 53 ++++++++++++------- .../Http/Dispatchers/CurlHttpDispatcher.cs | 13 +++-- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 302bae8dd..510f69d8a 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -1,17 +1,19 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Net; +using System.Threading; using FluentAssertions; +using Moq; +using NLog; using NUnit.Framework; using NzbDrone.Common.Cache; using NzbDrone.Common.Http; +using NzbDrone.Common.Http.Dispatchers; +using NzbDrone.Common.TPL; using NzbDrone.Test.Common; using NzbDrone.Test.Common.Categories; -using NLog; -using NzbDrone.Common.TPL; -using Moq; -using NzbDrone.Common.Http.Dispatchers; namespace NzbDrone.Common.Test.Http { @@ -309,30 +311,43 @@ namespace NzbDrone.Common.Test.Http .Verify(v => v.PostResponse(It.IsAny()), Times.Once()); } - public void should_parse_malformed_cloudflare_cookie() + [TestCase("en-US")] + [TestCase("es-ES")] + public void should_parse_malformed_cloudflare_cookie(string culture) { - // the date is bad in the below - should be 13-Jul-2016 - string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Wed, 13-Jul-16 16:19:50 GMT; path=/; HttpOnly"; - string url = "http://eu.httpbin.org/response-headers?Set-Cookie=" + - System.Uri.EscapeUriString(malformedCookie); + var origCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture); + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); + try + { + // the date is bad in the below - should be 13-Jul-2016 + string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Wed, 13-Jul-16 16:19:50 GMT; path=/; HttpOnly"; + string url = "http://eu.httpbin.org/response-headers?Set-Cookie=" + + System.Uri.EscapeUriString(malformedCookie); - var requestSet = new HttpRequest(url); - requestSet.AllowAutoRedirect = false; - requestSet.StoreResponseCookie = true; + var requestSet = new HttpRequest(url); + requestSet.AllowAutoRedirect = false; + requestSet.StoreResponseCookie = true; - var responseSet = Subject.Get(requestSet); + var responseSet = Subject.Get(requestSet); - var request = new HttpRequest("http://eu.httpbin.org/get"); + var request = new HttpRequest("http://eu.httpbin.org/get"); - var response = Subject.Get(request); + var response = Subject.Get(request); - response.Resource.Headers.Should().ContainKey("Cookie"); + response.Resource.Headers.Should().ContainKey("Cookie"); - var cookie = response.Resource.Headers["Cookie"].ToString(); + var cookie = response.Resource.Headers["Cookie"].ToString(); - cookie.Should().Contain("__cfduid=d29e686a9d65800021c66faca0a29b4261436890790"); + cookie.Should().Contain("__cfduid=d29e686a9d65800021c66faca0a29b4261436890790"); - ExceptionVerification.IgnoreErrors(); + ExceptionVerification.IgnoreErrors(); + } + finally + { + Thread.CurrentThread.CurrentCulture = origCulture; + Thread.CurrentThread.CurrentUICulture = origCulture; + } } } diff --git a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs index 2bd3f1bd2..cdcea3aae 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/CurlHttpDispatcher.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; @@ -175,9 +176,15 @@ namespace NzbDrone.Common.Http.Dispatchers // fix up the date if it was malformed var setCookieClean = ExpiryDate.Replace(setCookie, delegate(Match match) { - string format = "ddd, dd-MMM-yyyy HH:mm:ss"; - DateTime dt = Convert.ToDateTime(match.Groups[2].Value); - return match.Groups[1].Value + dt.ToUniversalTime().ToString(format) + " GMT"; + string shortFormat = "ddd, dd-MMM-yy HH:mm:ss"; + string longFormat = "ddd, dd-MMM-yyyy HH:mm:ss"; + DateTime dt; + if (DateTime.TryParseExact(match.Groups[2].Value, longFormat, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt) || + DateTime.TryParseExact(match.Groups[2].Value, shortFormat, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt) || + DateTime.TryParse(match.Groups[2].Value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt)) + return match.Groups[1].Value + dt.ToUniversalTime().ToString(longFormat, CultureInfo.InvariantCulture) + " GMT"; + else + return match.Value; }); return setCookieClean; }