From 8d776abb488e53ba5acdb0a09a9450665337e980 Mon Sep 17 00:00:00 2001 From: margaale Date: Tue, 20 Dec 2016 16:54:44 -0300 Subject: [PATCH] Fixed: Handle download clients sending invalid content-type header. DownloadStation incorrectly surrounds charset with double-quotes. whereas the http standard specifies they must be without quotes. fixes #1586 --- .../Http/HttpHeaderFixture.cs | 42 +++++++++++++++++++ .../NzbDrone.Common.Test.csproj | 1 + src/NzbDrone.Common/Http/HttpHeader.cs | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Common.Test/Http/HttpHeaderFixture.cs diff --git a/src/NzbDrone.Common.Test/Http/HttpHeaderFixture.cs b/src/NzbDrone.Common.Test/Http/HttpHeaderFixture.cs new file mode 100644 index 000000000..15a5f345c --- /dev/null +++ b/src/NzbDrone.Common.Test/Http/HttpHeaderFixture.cs @@ -0,0 +1,42 @@ +using NUnit.Framework; +using FluentAssertions; +using NzbDrone.Test.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Common.Http; +using System.Collections.Specialized; + +namespace NzbDrone.Common.Test.Http +{ + [TestFixture] + public class HttpHeaderFixture : TestBase + { + [TestCase("text/html; charset=\"utf-8\"", "utf-8")] + [TestCase("text/html; charset=utf-8", "utf-8")] + public void should_get_encoding_from_content_type_header(string contentType, string charsetExpected) + { + var headers = new NameValueCollection(); + + headers.Add("Content-Type", contentType); + + var httpheader = new HttpHeader(headers); + + httpheader.GetEncodingFromContentType().Should().Be(Encoding.GetEncoding(charsetExpected)); + } + + [TestCase("text/html; charset=asdasd")] + public void should_throw_when_invalid_encoding_is_in_content_type_header(string contentType) + { + var headers = new NameValueCollection(); + + headers.Add("Content-Type", contentType); + + var httpheader = new HttpHeader(headers); + + Action action = () => httpheader.GetEncodingFromContentType(); + action.ShouldThrow(); + } + } +} diff --git a/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index 2d9675b9a..71feb03c3 100644 --- a/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -84,6 +84,7 @@ + diff --git a/src/NzbDrone.Common/Http/HttpHeader.cs b/src/NzbDrone.Common/Http/HttpHeader.cs index 0db356feb..fcfc825d7 100644 --- a/src/NzbDrone.Common/Http/HttpHeader.cs +++ b/src/NzbDrone.Common/Http/HttpHeader.cs @@ -145,7 +145,7 @@ namespace NzbDrone.Common.Http if (charset.IsNotNullOrWhiteSpace()) { - encoding = Encoding.GetEncoding(charset); + encoding = Encoding.GetEncoding(charset.Replace("\"", "")); } }