Use modern HttpClient

Co-Authored-By: ta264 <ta264@users.noreply.github.com>
pull/5116/head
Qstick 3 years ago committed by Mark McDowall
parent bff1fe7890
commit 4c0fe62dda

@ -262,7 +262,6 @@ dotnet_diagnostic.CA5394.severity = suggestion
dotnet_diagnostic.CA5397.severity = suggestion dotnet_diagnostic.CA5397.severity = suggestion
dotnet_diagnostic.SYSLIB0006.severity = none dotnet_diagnostic.SYSLIB0006.severity = none
dotnet_diagnostic.SYSLIB0014.severity = none
[*.{js,html,js,hbs,less,css}] [*.{js,html,js,hbs,less,css}]
charset = utf-8 charset = utf-8

@ -4,6 +4,7 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Threading; using System.Threading;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
@ -15,12 +16,14 @@ using NzbDrone.Common.Http;
using NzbDrone.Common.Http.Dispatchers; using NzbDrone.Common.Http.Dispatchers;
using NzbDrone.Common.Http.Proxy; using NzbDrone.Common.Http.Proxy;
using NzbDrone.Common.TPL; using NzbDrone.Common.TPL;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Security;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Test.Common.Categories; using NzbDrone.Test.Common.Categories;
using HttpClient = NzbDrone.Common.Http.HttpClient;
namespace NzbDrone.Common.Test.Http namespace NzbDrone.Common.Test.Http
{ {
[Ignore("httpbin is bugged")]
[IntegrationTest] [IntegrationTest]
[TestFixture(typeof(ManagedHttpDispatcher))] [TestFixture(typeof(ManagedHttpDispatcher))]
public class HttpClientFixture<TDispatcher> : TestBase<HttpClient> public class HttpClientFixture<TDispatcher> : TestBase<HttpClient>
@ -32,39 +35,38 @@ namespace NzbDrone.Common.Test.Http
private string _httpBinHost; private string _httpBinHost;
private string _httpBinHost2; private string _httpBinHost2;
private System.Net.Http.HttpClient _httpClient = new ();
[OneTimeSetUp] [OneTimeSetUp]
public void FixtureSetUp() public void FixtureSetUp()
{ {
var candidates = new[] { "eu.httpbin.org", /*"httpbin.org",*/ "www.httpbin.org" }; // Always use our server for main tests
var mainHost = "httpbin.servarr.com";
// Use mirrors for tests that use two hosts
var candidates = new[] { "httpbin1.servarr.com" };
// httpbin.org is broken right now, occassionally redirecting to https if it's unavailable. // httpbin.org is broken right now, occassionally redirecting to https if it's unavailable.
_httpBinHost = mainHost;
_httpBinHosts = candidates.Where(IsTestSiteAvailable).ToArray(); _httpBinHosts = candidates.Where(IsTestSiteAvailable).ToArray();
TestLogger.Info($"{candidates.Length} TestSites available."); TestLogger.Info($"{candidates.Length} TestSites available.");
_httpBinSleep = _httpBinHosts.Count() < 2 ? 100 : 10; _httpBinSleep = 10;
} }
private bool IsTestSiteAvailable(string site) private bool IsTestSiteAvailable(string site)
{ {
try try
{ {
var req = WebRequest.Create($"http://{site}/get") as HttpWebRequest; var res = _httpClient.GetAsync($"https://{site}/get").GetAwaiter().GetResult();
var res = req.GetResponse() as HttpWebResponse;
if (res.StatusCode != HttpStatusCode.OK) if (res.StatusCode != HttpStatusCode.OK)
{ {
return false; return false;
} }
try res = _httpClient.GetAsync($"https://{site}/status/429").GetAwaiter().GetResult();
{
req = WebRequest.Create($"http://{site}/status/429") as HttpWebRequest;
res = req.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
res = ex.Response as HttpWebResponse;
}
if (res == null || res.StatusCode != (HttpStatusCode)429) if (res == null || res.StatusCode != (HttpStatusCode)429)
{ {
@ -91,10 +93,14 @@ namespace NzbDrone.Common.Test.Http
Mocker.GetMock<IOsInfo>().Setup(c => c.Name).Returns("TestOS"); Mocker.GetMock<IOsInfo>().Setup(c => c.Name).Returns("TestOS");
Mocker.GetMock<IOsInfo>().Setup(c => c.Version).Returns("9.0.0"); Mocker.GetMock<IOsInfo>().Setup(c => c.Version).Returns("9.0.0");
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(CertificateValidationType.Enabled);
Mocker.SetConstant<IUserAgentBuilder>(Mocker.Resolve<UserAgentBuilder>()); Mocker.SetConstant<IUserAgentBuilder>(Mocker.Resolve<UserAgentBuilder>());
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>()); Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
Mocker.SetConstant<ICreateManagedWebProxy>(Mocker.Resolve<ManagedWebProxyFactory>()); Mocker.SetConstant<ICreateManagedWebProxy>(Mocker.Resolve<ManagedWebProxyFactory>());
Mocker.SetConstant<ICertificateValidationService>(new X509CertificateValidationService(Mocker.GetMock<IConfigService>().Object, TestLogger));
Mocker.SetConstant<IRateLimitService>(Mocker.Resolve<RateLimitService>()); Mocker.SetConstant<IRateLimitService>(Mocker.Resolve<RateLimitService>());
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(new IHttpRequestInterceptor[0]); Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(new IHttpRequestInterceptor[0]);
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>()); Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
@ -105,8 +111,7 @@ namespace NzbDrone.Common.Test.Http
// .Returns(new HttpProxySettings(ProxyType.Socks5, "127.0.0.1", 5476, "", false)); // .Returns(new HttpProxySettings(ProxyType.Socks5, "127.0.0.1", 5476, "", false));
// Roundrobin over the two servers, to reduce the chance of hitting the ratelimiter. // Roundrobin over the two servers, to reduce the chance of hitting the ratelimiter.
_httpBinHost = _httpBinHosts[_httpBinRandom++ % _httpBinHosts.Length]; _httpBinHost2 = _httpBinHosts[_httpBinRandom++ % _httpBinHosts.Length];
_httpBinHost2 = _httpBinHosts[_httpBinRandom % _httpBinHosts.Length];
} }
[TearDown] [TearDown]
@ -118,7 +123,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_execute_simple_get() public void should_execute_simple_get()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
var response = Subject.Execute(request); var response = Subject.Execute(request);
@ -135,10 +140,32 @@ namespace NzbDrone.Common.Test.Http
response.Content.Should().NotBeNullOrWhiteSpace(); response.Content.Should().NotBeNullOrWhiteSpace();
} }
[TestCase(CertificateValidationType.Enabled)]
[TestCase(CertificateValidationType.DisabledForLocalAddresses)]
public void bad_ssl_should_fail_when_remote_validation_enabled(CertificateValidationType validationType)
{
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(validationType);
var request = new HttpRequest($"https://expired.badssl.com");
Assert.Throws<HttpRequestException>(() => Subject.Execute(request));
ExceptionVerification.ExpectedErrors(2);
}
[Test]
public void bad_ssl_should_pass_if_remote_validation_disabled()
{
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(CertificateValidationType.Disabled);
var request = new HttpRequest($"https://expired.badssl.com");
Subject.Execute(request);
ExceptionVerification.ExpectedErrors(0);
}
[Test] [Test]
public void should_execute_typed_get() public void should_execute_typed_get()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/get?test=1"); var request = new HttpRequest($"https://{_httpBinHost}/get?test=1");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -151,7 +178,7 @@ namespace NzbDrone.Common.Test.Http
{ {
var message = "{ my: 1 }"; var message = "{ my: 1 }";
var request = new HttpRequest($"http://{_httpBinHost}/post"); var request = new HttpRequest($"https://{_httpBinHost}/post");
request.SetContent(message); request.SetContent(message);
var response = Subject.Post<HttpBinResource>(request); var response = Subject.Post<HttpBinResource>(request);
@ -159,15 +186,42 @@ namespace NzbDrone.Common.Test.Http
response.Resource.Data.Should().Be(message); response.Resource.Data.Should().Be(message);
} }
[TestCase("gzip")] [Test]
public void should_execute_get_using_gzip(string compression) public void should_execute_post_with_content_type()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/{compression}"); var message = "{ my: 1 }";
var request = new HttpRequest($"https://{_httpBinHost}/post");
request.SetContent(message);
request.Headers.ContentType = "application/json";
var response = Subject.Post<HttpBinResource>(request);
response.Resource.Data.Should().Be(message);
}
[Test]
public void should_execute_get_using_gzip()
{
var request = new HttpRequest($"https://{_httpBinHost}/gzip");
var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("gzip");
response.Resource.Gzipped.Should().BeTrue();
response.Resource.Brotli.Should().BeFalse();
}
[Test]
public void should_execute_get_using_brotli()
{
var request = new HttpRequest($"https://{_httpBinHost}/brotli");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
response.Resource.Headers["Accept-Encoding"].ToString().Should().Be(compression); response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("br");
response.Headers.ContentLength.Should().BeLessOrEqualTo(response.Content.Length);
response.Resource.Gzipped.Should().BeFalse();
response.Resource.Brotli.Should().BeTrue();
} }
[TestCase(HttpStatusCode.Unauthorized)] [TestCase(HttpStatusCode.Unauthorized)]
@ -178,7 +232,7 @@ namespace NzbDrone.Common.Test.Http
[TestCase(HttpStatusCode.BadGateway)] [TestCase(HttpStatusCode.BadGateway)]
public void should_throw_on_unsuccessful_status_codes(int statusCode) public void should_throw_on_unsuccessful_status_codes(int statusCode)
{ {
var request = new HttpRequest($"http://{_httpBinHost}/status/{statusCode}"); var request = new HttpRequest($"https://{_httpBinHost}/status/{statusCode}");
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request)); var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
@ -190,10 +244,10 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_throw_on_suppressed_status_codes() public void should_not_throw_on_suppressed_status_codes()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/status/{HttpStatusCode.NotFound}"); var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.NotFound }; request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.NotFound };
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request)); Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
ExceptionVerification.IgnoreWarns(); ExceptionVerification.IgnoreWarns();
} }
@ -201,7 +255,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_log_unsuccessful_status_codes() public void should_log_unsuccessful_status_codes()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/status/{HttpStatusCode.NotFound}"); var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request)); var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
@ -211,10 +265,10 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_log_unsuccessful_status_codes() public void should_not_log_unsuccessful_status_codes()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/status/{HttpStatusCode.NotFound}"); var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
request.LogHttpError = false; request.LogHttpError = false;
var exception = Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request)); Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
ExceptionVerification.ExpectedWarns(0); ExceptionVerification.ExpectedWarns(0);
} }
@ -222,7 +276,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_follow_redirects_when_not_in_production() public void should_not_follow_redirects_when_not_in_production()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1"); var request = new HttpRequest($"https://{_httpBinHost}/redirect/1");
Subject.Get(request); Subject.Get(request);
@ -232,7 +286,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_follow_redirects() public void should_follow_redirects()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1"); var request = new HttpRequest($"https://{_httpBinHost}/redirect/1");
request.AllowAutoRedirect = true; request.AllowAutoRedirect = true;
var response = Subject.Get(request); var response = Subject.Get(request);
@ -245,7 +299,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_follow_redirects() public void should_not_follow_redirects()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1"); var request = new HttpRequest($"https://{_httpBinHost}/redirect/1");
request.AllowAutoRedirect = false; request.AllowAutoRedirect = false;
var response = Subject.Get(request); var response = Subject.Get(request);
@ -258,7 +312,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_follow_redirects_to_https() public void should_follow_redirects_to_https()
{ {
var request = new HttpRequestBuilder($"http://{_httpBinHost}/redirect-to") var request = new HttpRequestBuilder($"https://{_httpBinHost}/redirect-to")
.AddQueryParam("url", $"https://sonarr.tv/") .AddQueryParam("url", $"https://sonarr.tv/")
.Build(); .Build();
request.AllowAutoRedirect = true; request.AllowAutoRedirect = true;
@ -274,7 +328,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_throw_on_too_many_redirects() public void should_throw_on_too_many_redirects()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/redirect/6"); var request = new HttpRequest($"https://{_httpBinHost}/redirect/6");
request.AllowAutoRedirect = true; request.AllowAutoRedirect = true;
Assert.Throws<WebException>(() => Subject.Get(request)); Assert.Throws<WebException>(() => Subject.Get(request));
@ -285,7 +339,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_send_user_agent() public void should_send_user_agent()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -299,7 +353,7 @@ namespace NzbDrone.Common.Test.Http
[TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")] [TestCase("Accept", "text/xml, text/rss+xml, application/rss+xml")]
public void should_send_headers(string header, string value) public void should_send_headers(string header, string value)
{ {
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
request.Headers.Add(header, value); request.Headers.Add(header, value);
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -324,12 +378,30 @@ namespace NzbDrone.Common.Test.Http
fileInfo.Length.Should().Be(307054); fileInfo.Length.Should().Be(307054);
} }
[Test]
public void should_download_file_with_redirect()
{
var file = GetTempFilePath();
var request = new HttpRequestBuilder($"https://{_httpBinHost}/redirect-to")
.AddQueryParam("url", $"https://sonarr.tv/img/slider/seriesdetails.png")
.Build();
Subject.DownloadFile(request.Url.FullUri, file);
ExceptionVerification.ExpectedErrors(0);
var fileInfo = new FileInfo(file);
fileInfo.Exists.Should().BeTrue();
fileInfo.Length.Should().Be(307054);
}
[Test] [Test]
public void should_not_download_file_with_error() public void should_not_download_file_with_error()
{ {
var file = GetTempFilePath(); var file = GetTempFilePath();
Assert.Throws<HttpException>(() => Subject.DownloadFile("http://download.sonarr.tv/wrongpath", file)); Assert.Throws<HttpException>(() => Subject.DownloadFile("https://download.sonarr.tv/wrongpath", file));
File.Exists(file).Should().BeFalse(); File.Exists(file).Should().BeFalse();
File.Exists(file + ".part").Should().BeFalse(); File.Exists(file + ".part").Should().BeFalse();
@ -344,7 +416,7 @@ namespace NzbDrone.Common.Test.Http
using (var fileStream = new FileStream(file, FileMode.Create)) using (var fileStream = new FileStream(file, FileMode.Create))
{ {
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1"); var request = new HttpRequest($"https://{_httpBinHost}/redirect/1");
request.AllowAutoRedirect = false; request.AllowAutoRedirect = false;
request.ResponseStream = fileStream; request.ResponseStream = fileStream;
@ -365,7 +437,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_send_cookie() public void should_send_cookie()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
request.Cookies["my"] = "cookie"; request.Cookies["my"] = "cookie";
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -384,7 +456,7 @@ namespace NzbDrone.Common.Test.Http
Assert.Inconclusive("Need both httpbin.org and eu.httpbin.org to run this test."); Assert.Inconclusive("Need both httpbin.org and eu.httpbin.org to run this test.");
} }
var oldRequest = new HttpRequest($"http://{_httpBinHost2}/get"); var oldRequest = new HttpRequest($"https://{_httpBinHost2}/get");
oldRequest.Cookies["my"] = "cookie"; oldRequest.Cookies["my"] = "cookie";
var oldClient = new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.GetMock<IUserAgentBuilder>().Object, Mocker.Resolve<Logger>()); var oldClient = new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.GetMock<IUserAgentBuilder>().Object, Mocker.Resolve<Logger>());
@ -401,7 +473,7 @@ namespace NzbDrone.Common.Test.Http
{ {
GivenOldCookie(); GivenOldCookie();
var request = new HttpRequest($"http://{_httpBinHost2}/get"); var request = new HttpRequest($"https://{_httpBinHost2}/get");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -417,7 +489,7 @@ namespace NzbDrone.Common.Test.Http
{ {
GivenOldCookie(); GivenOldCookie();
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -427,14 +499,14 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_store_request_cookie() public void should_not_store_request_cookie()
{ {
var requestGet = new HttpRequest($"http://{_httpBinHost}/get"); var requestGet = new HttpRequest($"https://{_httpBinHost}/get");
requestGet.Cookies.Add("my", "cookie"); requestGet.Cookies.Add("my", "cookie");
requestGet.AllowAutoRedirect = false; requestGet.AllowAutoRedirect = false;
requestGet.StoreRequestCookie = false; requestGet.StoreRequestCookie = false;
requestGet.StoreResponseCookie = false; requestGet.StoreResponseCookie = false;
var responseGet = Subject.Get<HttpBinResource>(requestGet); var responseGet = Subject.Get<HttpBinResource>(requestGet);
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.AllowAutoRedirect = false; requestCookies.AllowAutoRedirect = false;
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -446,14 +518,14 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_store_request_cookie() public void should_store_request_cookie()
{ {
var requestGet = new HttpRequest($"http://{_httpBinHost}/get"); var requestGet = new HttpRequest($"https://{_httpBinHost}/get");
requestGet.Cookies.Add("my", "cookie"); requestGet.Cookies.Add("my", "cookie");
requestGet.AllowAutoRedirect = false; requestGet.AllowAutoRedirect = false;
requestGet.StoreRequestCookie.Should().BeTrue(); requestGet.StoreRequestCookie.Should().BeTrue();
requestGet.StoreResponseCookie = false; requestGet.StoreResponseCookie = false;
var responseGet = Subject.Get<HttpBinResource>(requestGet); var responseGet = Subject.Get<HttpBinResource>(requestGet);
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.AllowAutoRedirect = false; requestCookies.AllowAutoRedirect = false;
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -465,7 +537,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_delete_request_cookie() public void should_delete_request_cookie()
{ {
var requestDelete = new HttpRequest($"http://{_httpBinHost}/cookies/delete?my"); var requestDelete = new HttpRequest($"https://{_httpBinHost}/cookies/delete?my");
requestDelete.Cookies.Add("my", "cookie"); requestDelete.Cookies.Add("my", "cookie");
requestDelete.AllowAutoRedirect = true; requestDelete.AllowAutoRedirect = true;
requestDelete.StoreRequestCookie = false; requestDelete.StoreRequestCookie = false;
@ -480,7 +552,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_clear_request_cookie() public void should_clear_request_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies");
requestSet.Cookies.Add("my", "cookie"); requestSet.Cookies.Add("my", "cookie");
requestSet.AllowAutoRedirect = false; requestSet.AllowAutoRedirect = false;
requestSet.StoreRequestCookie = true; requestSet.StoreRequestCookie = true;
@ -488,7 +560,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject.Get<HttpCookieResource>(requestSet); var responseSet = Subject.Get<HttpCookieResource>(requestSet);
var requestClear = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestClear = new HttpRequest($"https://{_httpBinHost}/cookies");
requestClear.Cookies.Add("my", null); requestClear.Cookies.Add("my", null);
requestClear.AllowAutoRedirect = false; requestClear.AllowAutoRedirect = false;
requestClear.StoreRequestCookie = true; requestClear.StoreRequestCookie = true;
@ -502,14 +574,14 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_store_response_cookie() public void should_not_store_response_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies/set?my=cookie"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies/set?my=cookie");
requestSet.AllowAutoRedirect = false; requestSet.AllowAutoRedirect = false;
requestSet.StoreRequestCookie = false; requestSet.StoreRequestCookie = false;
requestSet.StoreResponseCookie.Should().BeFalse(); requestSet.StoreResponseCookie.Should().BeFalse();
var responseSet = Subject.Get(requestSet); var responseSet = Subject.Get(requestSet);
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -521,14 +593,14 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_store_response_cookie() public void should_store_response_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies/set?my=cookie"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies/set?my=cookie");
requestSet.AllowAutoRedirect = false; requestSet.AllowAutoRedirect = false;
requestSet.StoreRequestCookie = false; requestSet.StoreRequestCookie = false;
requestSet.StoreResponseCookie = true; requestSet.StoreResponseCookie = true;
var responseSet = Subject.Get(requestSet); var responseSet = Subject.Get(requestSet);
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -540,7 +612,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_temp_store_response_cookie() public void should_temp_store_response_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies/set?my=cookie"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies/set?my=cookie");
requestSet.AllowAutoRedirect = true; requestSet.AllowAutoRedirect = true;
requestSet.StoreRequestCookie = false; requestSet.StoreRequestCookie = false;
requestSet.StoreResponseCookie.Should().BeFalse(); requestSet.StoreResponseCookie.Should().BeFalse();
@ -555,7 +627,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_overwrite_response_cookie() public void should_overwrite_response_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies/set?my=cookie"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies/set?my=cookie");
requestSet.Cookies.Add("my", "oldcookie"); requestSet.Cookies.Add("my", "oldcookie");
requestSet.AllowAutoRedirect = false; requestSet.AllowAutoRedirect = false;
requestSet.StoreRequestCookie = false; requestSet.StoreRequestCookie = false;
@ -563,7 +635,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject.Get(requestSet); var responseSet = Subject.Get(requestSet);
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -575,7 +647,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_overwrite_temp_response_cookie() public void should_overwrite_temp_response_cookie()
{ {
var requestSet = new HttpRequest($"http://{_httpBinHost}/cookies/set?my=cookie"); var requestSet = new HttpRequest($"https://{_httpBinHost}/cookies/set?my=cookie");
requestSet.Cookies.Add("my", "oldcookie"); requestSet.Cookies.Add("my", "oldcookie");
requestSet.AllowAutoRedirect = true; requestSet.AllowAutoRedirect = true;
requestSet.StoreRequestCookie = true; requestSet.StoreRequestCookie = true;
@ -585,7 +657,7 @@ namespace NzbDrone.Common.Test.Http
responseSet.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie"); responseSet.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie");
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
var responseCookies = Subject.Get<HttpCookieResource>(requestCookies); var responseCookies = Subject.Get<HttpCookieResource>(requestCookies);
@ -597,7 +669,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_not_delete_response_cookie() public void should_not_delete_response_cookie()
{ {
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.Cookies.Add("my", "cookie"); requestCookies.Cookies.Add("my", "cookie");
requestCookies.AllowAutoRedirect = false; requestCookies.AllowAutoRedirect = false;
requestCookies.StoreRequestCookie = true; requestCookies.StoreRequestCookie = true;
@ -606,14 +678,14 @@ namespace NzbDrone.Common.Test.Http
responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie"); responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie");
var requestDelete = new HttpRequest($"http://{_httpBinHost}/cookies/delete?my"); var requestDelete = new HttpRequest($"https://{_httpBinHost}/cookies/delete?my");
requestDelete.AllowAutoRedirect = false; requestDelete.AllowAutoRedirect = false;
requestDelete.StoreRequestCookie = false; requestDelete.StoreRequestCookie = false;
requestDelete.StoreResponseCookie = false; requestDelete.StoreResponseCookie = false;
var responseDelete = Subject.Get(requestDelete); var responseDelete = Subject.Get(requestDelete);
requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.StoreRequestCookie = false; requestCookies.StoreRequestCookie = false;
requestCookies.StoreResponseCookie = false; requestCookies.StoreResponseCookie = false;
@ -627,7 +699,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_delete_response_cookie() public void should_delete_response_cookie()
{ {
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.Cookies.Add("my", "cookie"); requestCookies.Cookies.Add("my", "cookie");
requestCookies.AllowAutoRedirect = false; requestCookies.AllowAutoRedirect = false;
requestCookies.StoreRequestCookie = true; requestCookies.StoreRequestCookie = true;
@ -636,14 +708,14 @@ namespace NzbDrone.Common.Test.Http
responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie"); responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie");
var requestDelete = new HttpRequest($"http://{_httpBinHost}/cookies/delete?my"); var requestDelete = new HttpRequest($"https://{_httpBinHost}/cookies/delete?my");
requestDelete.AllowAutoRedirect = false; requestDelete.AllowAutoRedirect = false;
requestDelete.StoreRequestCookie = false; requestDelete.StoreRequestCookie = false;
requestDelete.StoreResponseCookie = true; requestDelete.StoreResponseCookie = true;
var responseDelete = Subject.Get(requestDelete); var responseDelete = Subject.Get(requestDelete);
requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.StoreRequestCookie = false; requestCookies.StoreRequestCookie = false;
requestCookies.StoreResponseCookie = false; requestCookies.StoreResponseCookie = false;
@ -657,7 +729,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_delete_temp_response_cookie() public void should_delete_temp_response_cookie()
{ {
var requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); var requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.Cookies.Add("my", "cookie"); requestCookies.Cookies.Add("my", "cookie");
requestCookies.AllowAutoRedirect = false; requestCookies.AllowAutoRedirect = false;
requestCookies.StoreRequestCookie = true; requestCookies.StoreRequestCookie = true;
@ -666,7 +738,7 @@ namespace NzbDrone.Common.Test.Http
responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie"); responseCookies.Resource.Cookies.Should().HaveCount(1).And.Contain("my", "cookie");
var requestDelete = new HttpRequest($"http://{_httpBinHost}/cookies/delete?my"); var requestDelete = new HttpRequest($"https://{_httpBinHost}/cookies/delete?my");
requestDelete.AllowAutoRedirect = true; requestDelete.AllowAutoRedirect = true;
requestDelete.StoreRequestCookie = false; requestDelete.StoreRequestCookie = false;
requestDelete.StoreResponseCookie = false; requestDelete.StoreResponseCookie = false;
@ -674,7 +746,7 @@ namespace NzbDrone.Common.Test.Http
responseDelete.Resource.Cookies.Should().BeEmpty(); responseDelete.Resource.Cookies.Should().BeEmpty();
requestCookies = new HttpRequest($"http://{_httpBinHost}/cookies"); requestCookies = new HttpRequest($"https://{_httpBinHost}/cookies");
requestCookies.StoreRequestCookie = false; requestCookies.StoreRequestCookie = false;
requestCookies.StoreResponseCookie = false; requestCookies.StoreResponseCookie = false;
@ -686,7 +758,7 @@ namespace NzbDrone.Common.Test.Http
[Test] [Test]
public void should_throw_on_http429_too_many_requests() public void should_throw_on_http429_too_many_requests()
{ {
var request = new HttpRequest($"http://{_httpBinHost}/status/429"); var request = new HttpRequest($"https://{_httpBinHost}/status/429");
Assert.Throws<TooManyRequestsException>(() => Subject.Get(request)); Assert.Throws<TooManyRequestsException>(() => Subject.Get(request));
@ -706,7 +778,7 @@ namespace NzbDrone.Common.Test.Http
.Setup(v => v.PostResponse(It.IsAny<HttpResponse>())) .Setup(v => v.PostResponse(It.IsAny<HttpResponse>()))
.Returns<HttpResponse>(r => r); .Returns<HttpResponse>(r => r);
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
Subject.Get(request); Subject.Get(request);
@ -728,7 +800,7 @@ namespace NzbDrone.Common.Test.Http
{ {
// the date is bad in the below - should be 13-Jul-2026 // the date is bad in the below - should be 13-Jul-2026
string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Mon, 13-Jul-26 16:19:50 GMT; path=/; HttpOnly"; string malformedCookie = @"__cfduid=d29e686a9d65800021c66faca0a29b4261436890790; expires=Mon, 13-Jul-26 16:19:50 GMT; path=/; HttpOnly";
var requestSet = new HttpRequestBuilder($"http://{_httpBinHost}/response-headers") var requestSet = new HttpRequestBuilder($"https://{_httpBinHost}/response-headers")
.AddQueryParam("Set-Cookie", malformedCookie) .AddQueryParam("Set-Cookie", malformedCookie)
.Build(); .Build();
@ -737,7 +809,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject.Get(requestSet); var responseSet = Subject.Get(requestSet);
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -761,7 +833,7 @@ namespace NzbDrone.Common.Test.Http
{ {
try try
{ {
string url = $"http://{_httpBinHost}/response-headers?Set-Cookie={Uri.EscapeDataString(malformedCookie)}"; string url = $"https://{_httpBinHost}/response-headers?Set-Cookie={Uri.EscapeDataString(malformedCookie)}";
var requestSet = new HttpRequest(url); var requestSet = new HttpRequest(url);
requestSet.AllowAutoRedirect = false; requestSet.AllowAutoRedirect = false;
@ -769,7 +841,7 @@ namespace NzbDrone.Common.Test.Http
var responseSet = Subject.Get(requestSet); var responseSet = Subject.Get(requestSet);
var request = new HttpRequest($"http://{_httpBinHost}/get"); var request = new HttpRequest($"https://{_httpBinHost}/get");
var response = Subject.Get<HttpBinResource>(request); var response = Subject.Get<HttpBinResource>(request);
@ -781,6 +853,17 @@ namespace NzbDrone.Common.Test.Http
{ {
} }
} }
[Test]
public void should_correctly_use_basic_auth()
{
var request = new HttpRequest($"https://{_httpBinHost}/basic-auth/username/password");
request.Credentials = new BasicNetworkCredential("username", "password");
var response = Subject.Execute(request);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
} }
public class HttpBinResource public class HttpBinResource
@ -790,6 +873,8 @@ namespace NzbDrone.Common.Test.Http
public string Origin { get; set; } public string Origin { get; set; }
public string Url { get; set; } public string Url { get; set; }
public string Data { get; set; } public string Data { get; set; }
public bool Gzipped { get; set; }
public bool Brotli { get; set; }
} }
public class HttpCookieResource public class HttpCookieResource

@ -194,5 +194,26 @@ namespace NzbDrone.Common.Extensions
return value; return value;
} }
public static string EncodeRFC3986(this string value)
{
// From Twitterizer http://www.twitterizer.net/
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
var encoded = Uri.EscapeDataString(value);
return Regex
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
.Replace("(", "%28")
.Replace(")", "%29")
.Replace("$", "%24")
.Replace("!", "%21")
.Replace("*", "%2A")
.Replace("'", "%27")
.Replace("%7E", "~");
}
} }
} }

@ -0,0 +1,12 @@
using System.Net;
namespace NzbDrone.Common.Http
{
public class BasicNetworkCredential : NetworkCredential
{
public BasicNetworkCredential(string user, string pass)
: base(user, pass)
{
}
}
}

@ -0,0 +1,10 @@
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace NzbDrone.Common.Http.Dispatchers
{
public interface ICertificateValidationService
{
bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);
}
}

@ -1,178 +1,234 @@
using System; using System;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Net; using System.Net;
using System.Reflection; using System.Net.Http;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog; using NLog;
using NLog.Fluent; using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http.Proxy; using NzbDrone.Common.Http.Proxy;
using NzbDrone.Common.Instrumentation.Extensions;
namespace NzbDrone.Common.Http.Dispatchers namespace NzbDrone.Common.Http.Dispatchers
{ {
public class ManagedHttpDispatcher : IHttpDispatcher public class ManagedHttpDispatcher : IHttpDispatcher
{ {
private const string NO_PROXY_KEY = "no-proxy";
private const int connection_establish_timeout = 2000;
private static bool useIPv6 = Socket.OSSupportsIPv6;
private static bool hasResolvedIPv6Availability;
private readonly IHttpProxySettingsProvider _proxySettingsProvider; private readonly IHttpProxySettingsProvider _proxySettingsProvider;
private readonly ICreateManagedWebProxy _createManagedWebProxy; private readonly ICreateManagedWebProxy _createManagedWebProxy;
private readonly ICertificateValidationService _certificateValidationService;
private readonly IUserAgentBuilder _userAgentBuilder; private readonly IUserAgentBuilder _userAgentBuilder;
private readonly IPlatformInfo _platformInfo; private readonly ICached<System.Net.Http.HttpClient> _httpClientCache;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<CredentialCache> _credentialCache;
public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider, ICreateManagedWebProxy createManagedWebProxy, IUserAgentBuilder userAgentBuilder, IPlatformInfo platformInfo, Logger logger) public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider,
ICreateManagedWebProxy createManagedWebProxy,
ICertificateValidationService certificateValidationService,
IUserAgentBuilder userAgentBuilder,
ICacheManager cacheManager,
Logger logger)
{ {
_proxySettingsProvider = proxySettingsProvider; _proxySettingsProvider = proxySettingsProvider;
_createManagedWebProxy = createManagedWebProxy; _createManagedWebProxy = createManagedWebProxy;
_certificateValidationService = certificateValidationService;
_userAgentBuilder = userAgentBuilder; _userAgentBuilder = userAgentBuilder;
_platformInfo = platformInfo;
_logger = logger; _logger = logger;
_httpClientCache = cacheManager.GetCache<System.Net.Http.HttpClient>(typeof(ManagedHttpDispatcher));
_credentialCache = cacheManager.GetCache<CredentialCache>(typeof(ManagedHttpDispatcher), "credentialcache");
} }
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
{ {
var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); var requestMessage = new HttpRequestMessage(request.Method, (Uri)request.Url);
requestMessage.Headers.UserAgent.ParseAdd(_userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent));
requestMessage.Headers.ConnectionClose = !request.ConnectionKeepAlive;
// Deflate is not a standard and could break depending on implementation. var cookieHeader = cookies.GetCookieHeader((Uri)request.Url);
// we should just stick with the more compatible Gzip if (cookieHeader.IsNotNullOrWhiteSpace())
//http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net {
webRequest.AutomaticDecompression = DecompressionMethods.GZip; requestMessage.Headers.Add("Cookie", cookieHeader);
}
webRequest.Method = request.Method.ToString();
webRequest.UserAgent = _userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent);
webRequest.KeepAlive = request.ConnectionKeepAlive;
webRequest.AllowAutoRedirect = false;
webRequest.CookieContainer = cookies;
using var cts = new CancellationTokenSource();
if (request.RequestTimeout != TimeSpan.Zero) if (request.RequestTimeout != TimeSpan.Zero)
{ {
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds); cts.CancelAfter(request.RequestTimeout);
} }
else
AddProxy(webRequest, request);
if (request.Headers != null)
{ {
AddRequestHeaders(webRequest, request.Headers); // The default for System.Net.Http.HttpClient
cts.CancelAfter(TimeSpan.FromSeconds(100));
} }
HttpWebResponse httpWebResponse; if (request.Credentials != null)
try
{ {
if (request.ContentData != null) if (request.Credentials is BasicNetworkCredential bc)
{
// Manually set header to avoid initial challenge response
var authInfo = bc.UserName + ":" + bc.Password;
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
requestMessage.Headers.Add("Authorization", "Basic " + authInfo);
}
else if (request.Credentials is NetworkCredential nc)
{ {
webRequest.ContentLength = request.ContentData.Length; var creds = GetCredentialCache();
using (var writeStream = webRequest.GetRequestStream()) foreach (var authtype in new[] { "Basic", "Digest" })
{ {
writeStream.Write(request.ContentData, 0, request.ContentData.Length); creds.Remove((Uri)request.Url, authtype);
creds.Add((Uri)request.Url, authtype, nc);
} }
} }
}
httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); if (request.ContentData != null)
{
requestMessage.Content = new ByteArrayContent(request.ContentData);
} }
catch (WebException e)
if (request.Headers != null)
{ {
httpWebResponse = (HttpWebResponse)e.Response; AddRequestHeaders(requestMessage, request.Headers);
}
if (httpWebResponse == null) var httpClient = GetClient(request.Url);
{
// Workaround for mono not closing connections properly in certain situations.
AbortWebRequest(webRequest);
// The default messages for WebException on mono are pretty horrible. HttpResponseMessage responseMessage;
if (e.Status == WebExceptionStatus.NameResolutionFailure)
{ try
throw new WebException($"DNS Name Resolution Failure: '{webRequest.RequestUri.Host}'", e.Status); {
} responseMessage = httpClient.Send(requestMessage, cts.Token);
else if (e.ToString().Contains("TLS Support not")) }
{ catch (HttpRequestException e)
throw new TlsFailureException(webRequest, e); {
} _logger.Error(e, "HttpClient error");
else if (e.ToString().Contains("The authentication or decryption has failed.")) throw;
{
throw new TlsFailureException(webRequest, e);
}
else if (OsInfo.IsNotWindows)
{
throw new WebException($"{e.Message}: '{webRequest.RequestUri}'", e, e.Status, e.Response);
}
else
{
throw;
}
}
} }
byte[] data = null; byte[] data = null;
using (var responseStream = httpWebResponse.GetResponseStream()) using (var responseStream = responseMessage.Content.ReadAsStream())
{ {
if (responseStream != null && responseStream != Stream.Null) if (responseStream != null && responseStream != Stream.Null)
{ {
try try
{ {
data = responseStream.ToBytes(); if (request.ResponseStream != null && responseMessage.StatusCode == HttpStatusCode.OK)
{
// A target ResponseStream was specified, write to that instead.
// But only on the OK status code, since we don't want to write failures and redirects.
responseStream.CopyTo(request.ResponseStream);
}
else
{
data = responseStream.ToBytes();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new WebException("Failed to read complete http response", ex, WebExceptionStatus.ReceiveFailure, httpWebResponse); throw new WebException("Failed to read complete http response", ex, WebExceptionStatus.ReceiveFailure, null);
} }
} }
} }
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode); var headers = responseMessage.Headers.ToNameValueCollection();
headers.Add(responseMessage.Content.Headers.ToNameValueCollection());
return new HttpResponse(request, new HttpHeader(responseMessage.Headers), data, responseMessage.StatusCode);
} }
protected virtual void AddProxy(HttpWebRequest webRequest, HttpRequest request) protected virtual System.Net.Http.HttpClient GetClient(HttpUri uri)
{ {
var proxySettings = _proxySettingsProvider.GetProxySettings(request); var proxySettings = _proxySettingsProvider.GetProxySettings(uri);
var key = proxySettings?.Key ?? NO_PROXY_KEY;
return _httpClientCache.Get(key, () => CreateHttpClient(proxySettings));
}
protected virtual System.Net.Http.HttpClient CreateHttpClient(HttpProxySettings proxySettings)
{
var handler = new SocketsHttpHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Brotli,
UseCookies = false, // sic - we don't want to use a shared cookie container
AllowAutoRedirect = false,
Credentials = GetCredentialCache(),
PreAuthenticate = true,
MaxConnectionsPerServer = 12,
ConnectCallback = onConnect,
SslOptions = new SslClientAuthenticationOptions
{
RemoteCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError
}
};
if (proxySettings != null) if (proxySettings != null)
{ {
webRequest.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings); handler.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
} }
var client = new System.Net.Http.HttpClient(handler)
{
Timeout = Timeout.InfiniteTimeSpan
};
return client;
} }
protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers) protected virtual void AddRequestHeaders(HttpRequestMessage webRequest, HttpHeader headers)
{ {
foreach (var header in headers) foreach (var header in headers)
{ {
switch (header.Key) switch (header.Key)
{ {
case "Accept": case "Accept":
webRequest.Accept = header.Value; webRequest.Headers.Accept.ParseAdd(header.Value);
break; break;
case "Connection": case "Connection":
webRequest.Connection = header.Value; webRequest.Headers.Connection.Clear();
webRequest.Headers.Connection.Add(header.Value);
break; break;
case "Content-Length": case "Content-Length":
webRequest.ContentLength = Convert.ToInt64(header.Value); AddContentHeader(webRequest, "Content-Length", header.Value);
break; break;
case "Content-Type": case "Content-Type":
webRequest.ContentType = header.Value; AddContentHeader(webRequest, "Content-Type", header.Value);
break; break;
case "Date": case "Date":
webRequest.Date = HttpHeader.ParseDateTime(header.Value); webRequest.Headers.Remove("Date");
webRequest.Headers.Date = HttpHeader.ParseDateTime(header.Value);
break; break;
case "Expect": case "Expect":
webRequest.Expect = header.Value; webRequest.Headers.Expect.ParseAdd(header.Value);
break; break;
case "Host": case "Host":
webRequest.Host = header.Value; webRequest.Headers.Host = header.Value;
break; break;
case "If-Modified-Since": case "If-Modified-Since":
webRequest.IfModifiedSince = HttpHeader.ParseDateTime(header.Value); webRequest.Headers.IfModifiedSince = HttpHeader.ParseDateTime(header.Value);
break; break;
case "Range": case "Range":
throw new NotImplementedException(); throw new NotImplementedException();
case "Referer": case "Referer":
webRequest.Referer = header.Value; webRequest.Headers.Add("Referer", header.Value);
break; break;
case "Transfer-Encoding": case "Transfer-Encoding":
webRequest.TransferEncoding = header.Value; webRequest.Headers.TransferEncoding.ParseAdd(header.Value);
break; break;
case "User-Agent": case "User-Agent":
throw new NotSupportedException("User-Agent other than Sonarr not allowed."); webRequest.Headers.UserAgent.ParseAdd(header.Value);
break;
case "Proxy-Connection": case "Proxy-Connection":
throw new NotImplementedException(); throw new NotImplementedException();
default: default:
@ -182,35 +238,82 @@ namespace NzbDrone.Common.Http.Dispatchers
} }
} }
// Workaround for mono not closing connections properly on timeouts private void AddContentHeader(HttpRequestMessage request, string header, string value)
private void AbortWebRequest(HttpWebRequest webRequest)
{ {
// First affected version was mono 5.16 var headers = request.Content?.Headers;
if (OsInfo.IsNotWindows && _platformInfo.Version >= new Version(5, 16)) if (headers == null)
{
return;
}
headers.Remove(header);
headers.Add(header, value);
}
private CredentialCache GetCredentialCache()
{
return _credentialCache.Get("credentialCache", () => new CredentialCache());
}
private static async ValueTask<Stream> onConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
// Until .NET supports an implementation of Happy Eyeballs (https://tools.ietf.org/html/rfc8305#section-2), let's make IPv4 fallback work in a simple way.
// This issue is being tracked at https://github.com/dotnet/runtime/issues/26177 and expected to be fixed in .NET 6.
if (useIPv6)
{ {
try try
{ {
var currentOperationInfo = webRequest.GetType().GetField("currentOperation", BindingFlags.NonPublic | BindingFlags.Instance); var localToken = cancellationToken;
var currentOperation = currentOperationInfo.GetValue(webRequest);
if (currentOperation != null) if (!hasResolvedIPv6Availability)
{ {
var responseStreamInfo = currentOperation.GetType().GetField("responseStream", BindingFlags.NonPublic | BindingFlags.Instance); // to make things move fast, use a very low timeout for the initial ipv6 attempt.
var responseStream = responseStreamInfo.GetValue(currentOperation) as Stream; var quickFailCts = new CancellationTokenSource(connection_establish_timeout);
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, quickFailCts.Token);
// Note that responseStream will likely be null once mono fixes it. localToken = linkedTokenSource.Token;
responseStream?.Dispose();
} }
return await attemptConnection(AddressFamily.InterNetworkV6, context, localToken);
} }
catch (Exception ex) catch
{ {
// This can fail randomly on future mono versions that have been changed/fixed. Log to sentry and ignore. // very naively fallback to ipv4 permanently for this execution based on the response of the first connection attempt.
_logger.Trace() // note that this may cause users to eventually get switched to ipv4 (on a random failure when they are switching networks, for instance)
.Exception(ex) // but in the interest of keeping this implementation simple, this is acceptable.
.Message("Unable to dispose responseStream on mono {0}", _platformInfo.Version) useIPv6 = false;
.WriteSentryWarn("MonoCloseWaitPatchFailed", ex.Message)
.Write();
} }
finally
{
hasResolvedIPv6Availability = true;
}
}
// fallback to IPv4.
return await attemptConnection(AddressFamily.InterNetwork, context, cancellationToken);
}
private static async ValueTask<Stream> attemptConnection(AddressFamily addressFamily, SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
// The following socket constructor will create a dual-mode socket on systems where IPV6 is available.
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
{
// Turn off Nagle's algorithm since it degrades performance in most HttpClient scenarios.
NoDelay = true
};
try
{
await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
// The stream should take the ownership of the underlying socket,
// closing it when it's disposed.
return new NetworkStream(socket, ownsSocket: true);
}
catch
{
socket.Dispose();
throw;
} }
} }
} }

@ -1,15 +0,0 @@
using System;
using System.Net;
namespace NzbDrone.Common.Http
{
public class GZipWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.AutomaticDecompression = DecompressionMethods.GZip;
return request;
}
}
}

@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -123,8 +124,6 @@ namespace NzbDrone.Common.Http
var stopWatch = Stopwatch.StartNew(); var stopWatch = Stopwatch.StartNew();
PrepareRequestCookies(request, cookieContainer);
var response = _httpDispatcher.GetResponse(request, cookieContainer); var response = _httpDispatcher.GetResponse(request, cookieContainer);
HandleResponseCookies(response, cookieContainer); HandleResponseCookies(response, cookieContainer);
@ -191,45 +190,44 @@ namespace NzbDrone.Common.Http
} }
} }
private void PrepareRequestCookies(HttpRequest request, CookieContainer cookieContainer) private void HandleResponseCookies(HttpResponse response, CookieContainer container)
{ {
// Don't collect persistnet cookies for intermediate/redirected urls. foreach (Cookie cookie in container.GetAllCookies())
/*lock (_cookieContainerCache)
{ {
var presistentContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); cookie.Expired = true;
var persistentCookies = presistentContainer.GetCookies((Uri)request.Url); }
var existingCookies = cookieContainer.GetCookies((Uri)request.Url);
cookieContainer.Add(persistentCookies);
cookieContainer.Add(existingCookies);
}*/
}
private void HandleResponseCookies(HttpResponse response, CookieContainer cookieContainer)
{
var cookieHeaders = response.GetCookieHeaders(); var cookieHeaders = response.GetCookieHeaders();
if (cookieHeaders.Empty()) if (cookieHeaders.Empty())
{ {
return; return;
} }
AddCookiesToContainer(response.Request.Url, cookieHeaders, container);
if (response.Request.StoreResponseCookie) if (response.Request.StoreResponseCookie)
{ {
lock (_cookieContainerCache) lock (_cookieContainerCache)
{ {
var persistentCookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); var persistentCookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
foreach (var cookieHeader in cookieHeaders) AddCookiesToContainer(response.Request.Url, cookieHeaders, persistentCookieContainer);
{ }
try }
{ }
persistentCookieContainer.SetCookies((Uri)response.Request.Url, cookieHeader);
} private void AddCookiesToContainer(HttpUri url, string[] cookieHeaders, CookieContainer container)
catch (Exception ex) {
{ foreach (var cookieHeader in cookieHeaders)
_logger.Debug(ex, "Invalid cookie in {0}", response.Request.Url); {
} try
} {
container.SetCookies((Uri)url, cookieHeader);
}
catch (Exception ex)
{
_logger.Debug(ex, "Invalid cookie in {0}", url);
} }
} }
} }
@ -252,6 +250,7 @@ namespace NzbDrone.Common.Http
using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite)) using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite))
{ {
var request = new HttpRequest(url); var request = new HttpRequest(url);
request.AllowAutoRedirect = true;
request.ResponseStream = fileStream; request.ResponseStream = fileStream;
var response = Get(request); var response = Get(request);
@ -281,7 +280,7 @@ namespace NzbDrone.Common.Http
public HttpResponse Get(HttpRequest request) public HttpResponse Get(HttpRequest request)
{ {
request.Method = HttpMethod.GET; request.Method = HttpMethod.Get;
return Execute(request); return Execute(request);
} }
@ -295,13 +294,13 @@ namespace NzbDrone.Common.Http
public HttpResponse Head(HttpRequest request) public HttpResponse Head(HttpRequest request)
{ {
request.Method = HttpMethod.HEAD; request.Method = HttpMethod.Head;
return Execute(request); return Execute(request);
} }
public HttpResponse Post(HttpRequest request) public HttpResponse Post(HttpRequest request)
{ {
request.Method = HttpMethod.POST; request.Method = HttpMethod.Post;
return Execute(request); return Execute(request);
} }

@ -1,14 +1,29 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
namespace NzbDrone.Common.Http namespace NzbDrone.Common.Http
{ {
public static class WebHeaderCollectionExtensions
{
public static NameValueCollection ToNameValueCollection(this HttpHeaders headers)
{
var result = new NameValueCollection();
foreach (var header in headers)
{
result.Add(header.Key, header.Value.ConcatToString(";"));
}
return result;
}
}
public class HttpHeader : NameValueCollection, IEnumerable<KeyValuePair<string, string>>, IEnumerable public class HttpHeader : NameValueCollection, IEnumerable<KeyValuePair<string, string>>, IEnumerable
{ {
public HttpHeader(NameValueCollection headers) public HttpHeader(NameValueCollection headers)
@ -16,6 +31,11 @@ namespace NzbDrone.Common.Http
{ {
} }
public HttpHeader(HttpHeaders headers)
: base(headers.ToNameValueCollection())
{
}
public HttpHeader() public HttpHeader()
{ {
} }

@ -1,14 +0,0 @@
namespace NzbDrone.Common.Http
{
public enum HttpMethod
{
GET,
POST,
PUT,
DELETE,
HEAD,
OPTIONS,
PATCH,
MERGE
}
}

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http;
using System.Text; using System.Text;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -12,6 +13,7 @@ namespace NzbDrone.Common.Http
{ {
public HttpRequest(string url, HttpAccept httpAccept = null) public HttpRequest(string url, HttpAccept httpAccept = null)
{ {
Method = HttpMethod.Get;
Url = new HttpUri(url); Url = new HttpUri(url);
Headers = new HttpHeader(); Headers = new HttpHeader();
AllowAutoRedirect = true; AllowAutoRedirect = true;
@ -35,6 +37,7 @@ namespace NzbDrone.Common.Http
public HttpHeader Headers { get; set; } public HttpHeader Headers { get; set; }
public byte[] ContentData { get; set; } public byte[] ContentData { get; set; }
public string ContentSummary { get; set; } public string ContentSummary { get; set; }
public ICredentials Credentials { get; set; }
public bool SuppressHttpError { get; set; } public bool SuppressHttpError { get; set; }
public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; } public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; }
public bool UseSimplifiedUserAgent { get; set; } public bool UseSimplifiedUserAgent { get; set; }
@ -85,12 +88,5 @@ namespace NzbDrone.Common.Http
var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType); var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);
ContentData = encoding.GetBytes(data); ContentData = encoding.GetBytes(data);
} }
public void AddBasicAuthentication(string username, string password)
{
var authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}"));
Headers.Set("Authorization", "Basic " + authInfo);
}
} }
} }

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Text; using System.Text;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -25,17 +26,16 @@ namespace NzbDrone.Common.Http
public bool ConnectionKeepAlive { get; set; } public bool ConnectionKeepAlive { get; set; }
public TimeSpan RateLimit { get; set; } public TimeSpan RateLimit { get; set; }
public bool LogResponseContent { get; set; } public bool LogResponseContent { get; set; }
public NetworkCredential NetworkCredential { get; set; } public ICredentials NetworkCredential { get; set; }
public Dictionary<string, string> Cookies { get; private set; } public Dictionary<string, string> Cookies { get; private set; }
public List<HttpFormData> FormData { get; private set; } public List<HttpFormData> FormData { get; private set; }
public Action<HttpRequest> PostProcess { get; set; } public Action<HttpRequest> PostProcess { get; set; }
public HttpRequestBuilder(string baseUrl) public HttpRequestBuilder(string baseUrl)
{ {
BaseUrl = new HttpUri(baseUrl); BaseUrl = new HttpUri(baseUrl);
ResourceUrl = string.Empty; ResourceUrl = string.Empty;
Method = HttpMethod.GET; Method = HttpMethod.Get;
QueryParams = new List<KeyValuePair<string, string>>(); QueryParams = new List<KeyValuePair<string, string>>();
SuffixQueryParams = new List<KeyValuePair<string, string>>(); SuffixQueryParams = new List<KeyValuePair<string, string>>();
Segments = new Dictionary<string, string>(); Segments = new Dictionary<string, string>();
@ -108,13 +108,7 @@ namespace NzbDrone.Common.Http
request.ConnectionKeepAlive = ConnectionKeepAlive; request.ConnectionKeepAlive = ConnectionKeepAlive;
request.RateLimit = RateLimit; request.RateLimit = RateLimit;
request.LogResponseContent = LogResponseContent; request.LogResponseContent = LogResponseContent;
request.Credentials = NetworkCredential;
if (NetworkCredential != null)
{
var authInfo = NetworkCredential.UserName + ":" + NetworkCredential.Password;
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
request.Headers.Set("Authorization", "Basic " + authInfo);
}
foreach (var header in Headers) foreach (var header in Headers)
{ {
@ -272,7 +266,7 @@ namespace NzbDrone.Common.Http
public virtual HttpRequestBuilder Post() public virtual HttpRequestBuilder Post()
{ {
Method = HttpMethod.POST; Method = HttpMethod.Post;
return this; return this;
} }
@ -363,7 +357,7 @@ namespace NzbDrone.Common.Http
public virtual HttpRequestBuilder AddFormParameter(string key, object value) public virtual HttpRequestBuilder AddFormParameter(string key, object value)
{ {
if (Method != HttpMethod.POST) if (Method != HttpMethod.Post)
{ {
throw new NotSupportedException("HttpRequest Method must be POST to add FormParameter."); throw new NotSupportedException("HttpRequest Method must be POST to add FormParameter.");
} }
@ -379,7 +373,7 @@ namespace NzbDrone.Common.Http
public virtual HttpRequestBuilder AddFormUpload(string name, string fileName, byte[] data, string contentType = "application/octet-stream") public virtual HttpRequestBuilder AddFormUpload(string name, string fileName, byte[] data, string contentType = "application/octet-stream")
{ {
if (Method != HttpMethod.POST) if (Method != HttpMethod.Post)
{ {
throw new NotSupportedException("HttpRequest Method must be POST to add FormUpload."); throw new NotSupportedException("HttpRequest Method must be POST to add FormUpload.");
} }

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
@ -17,14 +18,14 @@ namespace NzbDrone.Common.Http
public JsonRpcRequestBuilder(string baseUrl) public JsonRpcRequestBuilder(string baseUrl)
: base(baseUrl) : base(baseUrl)
{ {
Method = HttpMethod.POST; Method = HttpMethod.Post;
JsonParameters = new List<object>(); JsonParameters = new List<object>();
} }
public JsonRpcRequestBuilder(string baseUrl, string method, IEnumerable<object> parameters) public JsonRpcRequestBuilder(string baseUrl, string method, IEnumerable<object> parameters)
: base(baseUrl) : base(baseUrl)
{ {
Method = HttpMethod.POST; Method = HttpMethod.Post;
JsonMethod = method; JsonMethod = method;
JsonParameters = parameters.ToList(); JsonParameters = parameters.ToList();
} }

@ -1,20 +0,0 @@
using System;
using System.Net;
namespace NzbDrone.Common.Http
{
public class NzbDroneWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
((HttpWebRequest)request).KeepAlive = false;
((HttpWebRequest)request).ServicePoint.Expect100Continue = false;
}
return request;
}
}
}

@ -2,7 +2,7 @@ namespace NzbDrone.Common.Http.Proxy
{ {
public interface IHttpProxySettingsProvider public interface IHttpProxySettingsProvider
{ {
HttpProxySettings GetProxySettings(HttpRequest request); HttpProxySettings GetProxySettings(HttpUri uri);
HttpProxySettings GetProxySettings(); HttpProxySettings GetProxySettings();
} }
} }

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Xml.Linq;
using NLog;
using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Common.Http
{
public class XmlRpcRequestBuilder : HttpRequestBuilder
{
public static string XmlRpcContentType = "text/xml";
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(XmlRpcRequestBuilder));
public string XmlMethod { get; private set; }
public List<object> XmlParameters { get; private set; }
public XmlRpcRequestBuilder(string baseUrl)
: base(baseUrl)
{
Method = HttpMethod.Post;
XmlParameters = new List<object>();
}
public XmlRpcRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
: this(BuildBaseUrl(useHttps, host, port, urlBase))
{
}
public override HttpRequestBuilder Clone()
{
var clone = base.Clone() as XmlRpcRequestBuilder;
clone.XmlParameters = new List<object>(XmlParameters);
return clone;
}
public XmlRpcRequestBuilder Call(string method, params object[] parameters)
{
var clone = Clone() as XmlRpcRequestBuilder;
clone.XmlMethod = method;
clone.XmlParameters = parameters.ToList();
return clone;
}
protected override void Apply(HttpRequest request)
{
base.Apply(request);
request.Headers.ContentType = XmlRpcContentType;
var methodCallElements = new List<XElement> { new XElement("methodName", XmlMethod) };
if (XmlParameters.Any())
{
var argElements = XmlParameters.Select(x => new XElement("param", ConvertParameter(x))).ToList();
var paramsElement = new XElement("params", argElements);
methodCallElements.Add(paramsElement);
}
var message = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("methodCall", methodCallElements));
var body = message.ToString();
Logger.Debug($"Executing remote method: {XmlMethod}");
Logger.Trace($"methodCall {XmlMethod} body:\n{body}");
request.SetContent(body);
}
private static XElement ConvertParameter(object value)
{
XElement data;
if (value is string s)
{
data = new XElement("string", s);
}
else if (value is List<string> l)
{
data = new XElement("array", new XElement("data", l.Select(x => new XElement("value", new XElement("string", x)))));
}
else if (value is int i)
{
data = new XElement("int", i);
}
else if (value is byte[] bytes)
{
data = new XElement("base64", Convert.ToBase64String(bytes));
}
else
{
throw new InvalidOperationException($"Unhandled argument type {value.GetType().Name}");
}
return new XElement("value", data);
}
}
}

@ -1,4 +1,4 @@
using System; using System;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Cloud; using NzbDrone.Common.Cloud;
@ -9,6 +9,7 @@ using NzbDrone.Common.Http.Proxy;
using NzbDrone.Common.TPL; using NzbDrone.Common.TPL;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Http; using NzbDrone.Core.Http;
using NzbDrone.Core.Security;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Framework namespace NzbDrone.Core.Test.Framework
@ -23,7 +24,8 @@ namespace NzbDrone.Core.Test.Framework
Mocker.SetConstant<IHttpProxySettingsProvider>(new HttpProxySettingsProvider(Mocker.Resolve<ConfigService>())); Mocker.SetConstant<IHttpProxySettingsProvider>(new HttpProxySettingsProvider(Mocker.Resolve<ConfigService>()));
Mocker.SetConstant<ICreateManagedWebProxy>(new ManagedWebProxyFactory(Mocker.Resolve<CacheManager>())); Mocker.SetConstant<ICreateManagedWebProxy>(new ManagedWebProxyFactory(Mocker.Resolve<CacheManager>()));
Mocker.SetConstant<IHttpDispatcher>(new ManagedHttpDispatcher(Mocker.Resolve<IHttpProxySettingsProvider>(), Mocker.Resolve<ICreateManagedWebProxy>(), Mocker.Resolve<UserAgentBuilder>(), Mocker.Resolve<IPlatformInfo>(), TestLogger)); Mocker.SetConstant<ICertificateValidationService>(new X509CertificateValidationService(Mocker.Resolve<ConfigService>(), TestLogger));
Mocker.SetConstant<IHttpDispatcher>(new ManagedHttpDispatcher(Mocker.Resolve<IHttpProxySettingsProvider>(), Mocker.Resolve<ICreateManagedWebProxy>(), Mocker.Resolve<ICertificateValidationService>(), Mocker.Resolve<UserAgentBuilder>(), Mocker.Resolve<CacheManager>(), TestLogger));
Mocker.SetConstant<IHttpClient>(new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.Resolve<UserAgentBuilder>(), TestLogger)); Mocker.SetConstant<IHttpClient>(new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.Resolve<UserAgentBuilder>(), TestLogger));
Mocker.SetConstant<ISonarrCloudRequestBuilder>(new SonarrCloudRequestBuilder()); Mocker.SetConstant<ISonarrCloudRequestBuilder>(new SonarrCloudRequestBuilder());
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -31,7 +32,7 @@ namespace NzbDrone.Core.Test.IndexerTests.BroadcastheNetTests
var recentFeed = ReadAllText(@"Files/Indexers/BroadcastheNet/RecentFeed.json"); var recentFeed = ReadAllText(@"Files/Indexers/BroadcastheNet/RecentFeed.json");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.POST))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Post)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -144,7 +145,7 @@ namespace NzbDrone.Core.Test.IndexerTests.BroadcastheNetTests
recentFeed = recentFeed.Replace("http:", "https:"); recentFeed = recentFeed.Replace("http:", "https:");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.POST))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Post)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -29,7 +30,7 @@ namespace NzbDrone.Core.Test.IndexerTests.FanzubTests
var recentFeed = ReadAllText(@"Files/Indexers/Fanzub/fanzub.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Fanzub/fanzub.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -30,7 +31,7 @@ namespace NzbDrone.Core.Test.IndexerTests.FileListTests
var recentFeed = ReadAllText(@"Files/Indexers/FileList/RecentFeed.json"); var recentFeed = ReadAllText(@"Files/Indexers/FileList/RecentFeed.json");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Text; using System.Text;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
@ -34,7 +35,7 @@ namespace NzbDrone.Core.Test.IndexerTests.HDBitsTests
var responseJson = ReadAllText(fileName); var responseJson = ReadAllText(fileName);
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.POST))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Post)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), responseJson)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), responseJson));
var torrents = Subject.FetchRecent(); var torrents = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -88,7 +89,7 @@ namespace NzbDrone.Core.Test.IndexerTests.IPTorrentsTests
var recentFeed = ReadAllText(@"Files/Indexers/IPTorrents/IPTorrents.xml"); var recentFeed = ReadAllText(@"Files/Indexers/IPTorrents/IPTorrents.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -44,7 +45,7 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Newznab/newznab_nzb_su.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Newznab/newznab_nzb_su.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -70,7 +71,7 @@ namespace NzbDrone.Core.Test.IndexerTests.NewznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_animetosho.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_animetosho.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -32,7 +33,7 @@ namespace NzbDrone.Core.Test.IndexerTests.NyaaTests
var recentFeed = ReadAllText(@"Files/Indexers/Nyaa/Nyaa.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Nyaa/Nyaa.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -62,7 +63,7 @@ namespace NzbDrone.Core.Test.IndexerTests.NyaaTests
var recentFeed = ReadAllText(@"Files/Indexers/Nyaa/Nyaa2021.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Nyaa/Nyaa2021.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -33,7 +34,7 @@ namespace NzbDrone.Core.Test.IndexerTests.OmgwtfnzbsTests
var recentFeed = ReadAllText(@"Files/Indexers/Omgwtfnzbs/Omgwtfnzbs.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Omgwtfnzbs/Omgwtfnzbs.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -35,7 +36,7 @@ namespace NzbDrone.Core.Test.IndexerTests.RarbgTests
var recentFeed = ReadAllText(@"Files/Indexers/Rarbg/RecentFeed_v2.json"); var recentFeed = ReadAllText(@"Files/Indexers/Rarbg/RecentFeed_v2.json");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -64,7 +65,7 @@ namespace NzbDrone.Core.Test.IndexerTests.RarbgTests
public void should_parse_error_20_as_empty_results() public void should_parse_error_20_as_empty_results()
{ {
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "{ error_code: 20, error: \"some message\" }")); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "{ error_code: 20, error: \"some message\" }"));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -76,7 +77,7 @@ namespace NzbDrone.Core.Test.IndexerTests.RarbgTests
public void should_warn_on_unknown_error() public void should_warn_on_unknown_error()
{ {
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "{ error_code: 25, error: \"some message\" }")); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "{ error_code: 25, error: \"some message\" }"));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -22,7 +23,7 @@ namespace NzbDrone.Core.Test.IndexerTests
_series = Builder<Series>.CreateNew().Build(); _series = Builder<Series>.CreateNew().Build();
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "<xml></xml>")); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), "<xml></xml>"));
} }
@ -40,7 +41,7 @@ namespace NzbDrone.Core.Test.IndexerTests
var requests = Builder<IndexerRequest>.CreateListOfSize(paging ? 100 : 1) var requests = Builder<IndexerRequest>.CreateListOfSize(paging ? 100 : 1)
.All() .All()
.WithFactory(() => new IndexerRequest("http://my.feed.local/", HttpAccept.Rss)) .WithFactory(() => new IndexerRequest("http://my.feed.local/", HttpAccept.Rss))
.With(v => v.HttpRequest.Method = HttpMethod.GET) .With(v => v.HttpRequest.Method = HttpMethod.Get)
.Build(); .Build();
var pageable = new IndexerPageableRequestChain(); var pageable = new IndexerPageableRequestChain();

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -30,7 +31,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorrentleechTests
var recentFeed = ReadAllText(@"Files/Indexers/Torrentleech/Torrentleech.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torrentleech/Torrentleech.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();

@ -1,4 +1,4 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using FizzWare.NBuilder; using FizzWare.NBuilder;
@ -49,7 +49,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_hdaccess_net.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_hdaccess_net.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -80,7 +80,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_tpb.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_tpb.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -110,7 +110,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_animetosho.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_animetosho.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent(); var releases = Subject.FetchRecent();
@ -164,7 +164,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
(Subject.Definition.Settings as TorznabSettings).BaseUrl = baseUrl; (Subject.Definition.Settings as TorznabSettings).BaseUrl = baseUrl;
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var result = new NzbDroneValidationResult(Subject.Test()); var result = new NzbDroneValidationResult(Subject.Test());
@ -179,7 +179,7 @@ namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_tpb.xml"); var recentFeed = ReadAllText(@"Files/Indexers/Torznab/torznab_tpb.xml");
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET))) .Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.Get)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed)); .Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
(Subject.Definition.Settings as TorznabSettings).ApiPath = apiPath; (Subject.Definition.Settings as TorznabSettings).ApiPath = apiPath;

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using CookComputing.XmlRpc;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
@ -90,12 +89,7 @@ namespace NzbDrone.Core.Download.Clients.Aria2
var downloadSpeed = long.Parse(torrent.DownloadSpeed); var downloadSpeed = long.Parse(torrent.DownloadSpeed);
var status = DownloadItemStatus.Failed; var status = DownloadItemStatus.Failed;
var title = ""; var title = torrent.Bittorrent?.Name ?? "";
if (torrent.Bittorrent?.ContainsKey("info") == true && ((XmlRpcStruct)torrent.Bittorrent["info"]).ContainsKey("name"))
{
title = ((XmlRpcStruct)torrent.Bittorrent["info"])["name"].ToString();
}
switch (torrent.Status) switch (torrent.Status)
{ {

@ -1,111 +1,161 @@
using CookComputing.XmlRpc; using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using NzbDrone.Core.Download.Extensions;
namespace NzbDrone.Core.Download.Clients.Aria2 namespace NzbDrone.Core.Download.Clients.Aria2
{ {
public class Aria2Version public class Aria2Fault
{ {
[XmlRpcMember("version")] public Aria2Fault(XElement element)
public string Version; {
foreach (var e in element.XPathSelectElements("./value/struct/member"))
[XmlRpcMember("enabledFeatures")] {
public string[] EnabledFeatures; var name = e.ElementAsString("name");
if (name == "faultCode")
{
FaultCode = e.Element("value").ElementAsInt("int");
}
else if (name == "faultString")
{
FaultString = e.Element("value").GetStringValue();
}
}
}
public int FaultCode { get; set; }
public string FaultString { get; set; }
} }
public class Aria2Uri public class Aria2Version
{ {
[XmlRpcMember("status")] public Aria2Version(XElement element)
public string Status; {
foreach (var e in element.XPathSelectElements("./struct/member"))
[XmlRpcMember("uri")] {
public string Uri; if (e.ElementAsString("name") == "version")
{
Version = e.Element("value").GetStringValue();
}
}
}
public string Version { get; set; }
} }
public class Aria2File public class Aria2File
{ {
[XmlRpcMember("index")] public Aria2File(XElement element)
public string Index; {
foreach (var e in element.XPathSelectElements("./struct/member"))
[XmlRpcMember("length")] {
public string Length; var name = e.ElementAsString("name");
if (name == "path")
{
Path = e.Element("value").GetStringValue();
}
}
}
public string Path { get; set; }
}
[XmlRpcMember("completedLength")] public class Aria2Dict
public string CompletedLength; {
public Aria2Dict(XElement element)
{
Dict = new Dictionary<string, string>();
[XmlRpcMember("path")] foreach (var e in element.XPathSelectElements("./struct/member"))
public string Path; {
Dict.Add(e.ElementAsString("name"), e.Element("value").GetStringValue());
}
}
[XmlRpcMember("selected")] public Dictionary<string, string> Dict { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string Selected;
[XmlRpcMember("uris")] public class Aria2Bittorrent
[XmlRpcMissingMapping(MappingAction.Ignore)] {
public Aria2Uri[] Uris; public Aria2Bittorrent(XElement element)
{
foreach (var e in element.Descendants("member"))
{
if (e.ElementAsString("name") == "name")
{
Name = e.Element("value").GetStringValue();
}
}
}
public string Name;
} }
public class Aria2Status public class Aria2Status
{ {
[XmlRpcMember("bittorrent")] public Aria2Status(XElement element)
[XmlRpcMissingMapping(MappingAction.Ignore)] {
public XmlRpcStruct Bittorrent; foreach (var e in element.XPathSelectElements("./struct/member"))
{
[XmlRpcMember("bitfield")] var name = e.ElementAsString("name");
[XmlRpcMissingMapping(MappingAction.Ignore)]
public string Bitfield; if (name == "bittorrent")
{
[XmlRpcMember("infoHash")] Bittorrent = new Aria2Bittorrent(e.Element("value"));
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string InfoHash; else if (name == "infoHash")
{
[XmlRpcMember("completedLength")] InfoHash = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string CompletedLength; else if (name == "completedLength")
{
[XmlRpcMember("connections")] CompletedLength = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string Connections; else if (name == "downloadSpeed")
{
[XmlRpcMember("dir")] DownloadSpeed = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string Dir; else if (name == "files")
{
[XmlRpcMember("downloadSpeed")] Files = e.XPathSelectElement("./value/array/data")
[XmlRpcMissingMapping(MappingAction.Ignore)] .Elements()
public string DownloadSpeed; .Select(x => new Aria2File(x))
.ToArray();
[XmlRpcMember("files")] }
[XmlRpcMissingMapping(MappingAction.Ignore)] else if (name == "gid")
public Aria2File[] Files; {
Gid = e.Element("value").GetStringValue();
[XmlRpcMember("gid")] }
public string Gid; else if (name == "status")
{
[XmlRpcMember("numPieces")] Status = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string NumPieces; else if (name == "totalLength")
{
[XmlRpcMember("pieceLength")] TotalLength = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string PieceLength; else if (name == "uploadLength")
{
[XmlRpcMember("status")] UploadLength = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string Status; else if (name == "errorMessage")
{
[XmlRpcMember("totalLength")] ErrorMessage = e.Element("value").GetStringValue();
[XmlRpcMissingMapping(MappingAction.Ignore)] }
public string TotalLength; }
}
[XmlRpcMember("uploadLength")]
[XmlRpcMissingMapping(MappingAction.Ignore)] public Aria2Bittorrent Bittorrent { get; set; }
public string UploadLength; public string InfoHash { get; set; }
public string CompletedLength { get; set; }
[XmlRpcMember("uploadSpeed")] public string DownloadSpeed { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)] public Aria2File[] Files { get; set; }
public string UploadSpeed; public string Gid { get; set; }
public string Status { get; set; }
[XmlRpcMember("errorMessage")] public string TotalLength { get; set; }
[XmlRpcMissingMapping(MappingAction.Ignore)] public string UploadLength { get; set; }
public string ErrorMessage; public string ErrorMessage { get; set; }
} }
} }

@ -1,9 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Linq;
using CookComputing.XmlRpc; using System.Xml.Linq;
using NLog; using System.Xml.XPath;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download.Extensions;
namespace NzbDrone.Core.Download.Clients.Aria2 namespace NzbDrone.Core.Download.Clients.Aria2
{ {
@ -19,103 +19,61 @@ namespace NzbDrone.Core.Download.Clients.Aria2
Aria2Status GetFromGID(Aria2Settings settings, string gid); Aria2Status GetFromGID(Aria2Settings settings, string gid);
} }
public interface IAria2 : IXmlRpcProxy
{
[XmlRpcMethod("aria2.getVersion")]
Aria2Version GetVersion(string token);
[XmlRpcMethod("aria2.addUri")]
string AddUri(string token, string[] uri);
[XmlRpcMethod("aria2.addTorrent")]
string AddTorrent(string token, byte[] torrent);
[XmlRpcMethod("aria2.forceRemove")]
string Remove(string token, string gid);
[XmlRpcMethod("aria2.removeDownloadResult")]
string RemoveResult(string token, string gid);
[XmlRpcMethod("aria2.tellStatus")]
Aria2Status GetFromGid(string token, string gid);
[XmlRpcMethod("aria2.getGlobalOption")]
XmlRpcStruct GetGlobalOption(string token);
[XmlRpcMethod("aria2.tellActive")]
Aria2Status[] GetActive(string token);
[XmlRpcMethod("aria2.tellWaiting")]
Aria2Status[] GetWaiting(string token, int offset, int num);
[XmlRpcMethod("aria2.tellStopped")]
Aria2Status[] GetStopped(string token, int offset, int num);
}
public class Aria2Proxy : IAria2Proxy public class Aria2Proxy : IAria2Proxy
{ {
private readonly Logger _logger; private readonly IHttpClient _httpClient;
public Aria2Proxy(Logger logger) public Aria2Proxy(IHttpClient httpClient)
{ {
_logger = logger; _httpClient = httpClient;
}
private string GetToken(Aria2Settings settings)
{
return $"token:{settings?.SecretToken}";
}
private string GetURL(Aria2Settings settings)
{
return $"http{(settings.UseSsl ? "s" : "")}://{settings.Host}:{settings.Port}{settings.RpcPath}";
} }
public string GetVersion(Aria2Settings settings) public string GetVersion(Aria2Settings settings)
{ {
_logger.Trace("> aria2.getVersion"); var response = ExecuteRequest(settings, "aria2.getVersion", GetToken(settings));
var client = BuildClient(settings); var element = response.XPathSelectElement("./methodResponse/params/param/value");
var version = ExecuteRequest(() => client.GetVersion(GetToken(settings)));
_logger.Trace("< aria2.getVersion"); var version = new Aria2Version(element);
return version.Version; return version.Version;
} }
public Aria2Status GetFromGID(Aria2Settings settings, string gid) public Aria2Status GetFromGID(Aria2Settings settings, string gid)
{ {
_logger.Trace("> aria2.tellStatus"); var response = ExecuteRequest(settings, "aria2.tellStatus", GetToken(settings), gid);
var client = BuildClient(settings);
var found = ExecuteRequest(() => client.GetFromGid(GetToken(settings), gid));
_logger.Trace("< aria2.tellStatus"); var element = response.XPathSelectElement("./methodResponse/params/param/value");
return found; return new Aria2Status(element);
} }
public List<Aria2Status> GetTorrents(Aria2Settings settings) private List<Aria2Status> GetTorrentsMethod(Aria2Settings settings, string method, params object[] args)
{ {
_logger.Trace("> aria2.tellActive"); var allArgs = new List<object> { GetToken(settings) };
if (args.Any())
var client = BuildClient(settings); {
allArgs.AddRange(args);
var active = ExecuteRequest(() => client.GetActive(GetToken(settings))); }
_logger.Trace("< aria2.tellActive");
_logger.Trace("> aria2.tellWaiting"); var response = ExecuteRequest(settings, method, allArgs.ToArray());
var waiting = ExecuteRequest(() => client.GetWaiting(GetToken(settings), 0, 10 * 1024)); var element = response.XPathSelectElement("./methodResponse/params/param/value/array/data");
_logger.Trace("< aria2.tellWaiting"); var torrents = element?.Elements()
.Select(x => new Aria2Status(x))
.ToList()
?? new List<Aria2Status>();
return torrents;
}
_logger.Trace("> aria2.tellStopped"); public List<Aria2Status> GetTorrents(Aria2Settings settings)
{
var active = GetTorrentsMethod(settings, "aria2.tellActive");
var stopped = ExecuteRequest(() => client.GetStopped(GetToken(settings), 0, 10 * 1024)); var waiting = GetTorrentsMethod(settings, "aria2.tellWaiting", 0, 10 * 1024);
_logger.Trace("< aria2.tellStopped"); var stopped = GetTorrentsMethod(settings, "aria2.tellStopped", 0, 10 * 1024);
var items = new List<Aria2Status>(); var items = new List<Aria2Status>();
@ -128,98 +86,79 @@ namespace NzbDrone.Core.Download.Clients.Aria2
public Dictionary<string, string> GetGlobals(Aria2Settings settings) public Dictionary<string, string> GetGlobals(Aria2Settings settings)
{ {
_logger.Trace("> aria2.getGlobalOption"); var response = ExecuteRequest(settings, "aria2.getGlobalOption", GetToken(settings));
var client = BuildClient(settings);
var options = ExecuteRequest(() => client.GetGlobalOption(GetToken(settings)));
_logger.Trace("< aria2.getGlobalOption"); var element = response.XPathSelectElement("./methodResponse/params/param/value");
var ret = new Dictionary<string, string>(); var result = new Aria2Dict(element);
foreach (DictionaryEntry option in options) return result.Dict;
{
ret.Add(option.Key.ToString(), option.Value?.ToString());
}
return ret;
} }
public string AddMagnet(Aria2Settings settings, string magnet) public string AddMagnet(Aria2Settings settings, string magnet)
{ {
_logger.Trace("> aria2.addUri"); var response = ExecuteRequest(settings, "aria2.addUri", GetToken(settings), new List<string> { magnet });
var client = BuildClient(settings);
var gid = ExecuteRequest(() => client.AddUri(GetToken(settings), new[] { magnet }));
_logger.Trace("< aria2.addUri"); var gid = response.GetStringResponse();
return gid; return gid;
} }
public string AddTorrent(Aria2Settings settings, byte[] torrent) public string AddTorrent(Aria2Settings settings, byte[] torrent)
{ {
_logger.Trace("> aria2.addTorrent"); var response = ExecuteRequest(settings, "aria2.addTorrent", GetToken(settings), torrent);
var client = BuildClient(settings); var gid = response.GetStringResponse();
var gid = ExecuteRequest(() => client.AddTorrent(GetToken(settings), torrent));
_logger.Trace("< aria2.addTorrent");
return gid; return gid;
} }
public bool RemoveTorrent(Aria2Settings settings, string gid) public bool RemoveTorrent(Aria2Settings settings, string gid)
{ {
_logger.Trace("> aria2.forceRemove"); var response = ExecuteRequest(settings, "aria2.forceRemove", GetToken(settings), gid);
var client = BuildClient(settings);
var gidres = ExecuteRequest(() => client.Remove(GetToken(settings), gid));
_logger.Trace("< aria2.forceRemove"); var gidres = response.GetStringResponse();
return gid == gidres; return gid == gidres;
} }
public bool RemoveCompletedTorrent(Aria2Settings settings, string gid) public bool RemoveCompletedTorrent(Aria2Settings settings, string gid)
{ {
_logger.Trace("> aria2.removeDownloadResult"); var response = ExecuteRequest(settings, "aria2.removeDownloadResult", GetToken(settings), gid);
var client = BuildClient(settings);
var result = ExecuteRequest(() => client.RemoveResult(GetToken(settings), gid));
_logger.Trace("< aria2.removeDownloadResult"); var result = response.GetStringResponse();
return result == "OK"; return result == "OK";
} }
private IAria2 BuildClient(Aria2Settings settings) private string GetToken(Aria2Settings settings)
{ {
var client = XmlRpcProxyGen.Create<IAria2>(); return $"token:{settings?.SecretToken}";
client.Url = GetURL(settings);
return client;
} }
private T ExecuteRequest<T>(Func<T> task) private XDocument ExecuteRequest(Aria2Settings settings, string methodName, params object[] args)
{ {
try var requestBuilder = new XmlRpcRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.RpcPath)
{ {
return task(); LogResponseContent = true,
} };
catch (XmlRpcServerException ex)
{ var request = requestBuilder.Call(methodName, args).Build();
throw new DownloadClientException("Unable to connect to aria2, please check your settings", ex);
} var response = _httpClient.Execute(request);
catch (WebException ex)
var doc = XDocument.Parse(response.Content);
var faultElement = doc.XPathSelectElement("./methodResponse/fault");
if (faultElement != null)
{ {
if (ex.Status == WebExceptionStatus.TrustFailure) var fault = new Aria2Fault(faultElement);
{
throw new DownloadClientUnavailableException("Unable to connect to aria2, certificate validation failed.", ex);
}
throw new DownloadClientUnavailableException("Unable to connect to aria2, please check your settings", ex); throw new DownloadClientException($"Aria2 returned error code {fault.FaultCode}: {fault.FaultString}");
} }
return doc;
} }
} }
} }

@ -1,7 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
@ -143,15 +144,19 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
return authResponse.Data.SId; return authResponse.Data.SId;
} }
protected HttpRequestBuilder BuildRequest(DownloadStationSettings settings, string methodName, int apiVersion, HttpMethod httpVerb = HttpMethod.GET) protected HttpRequestBuilder BuildRequest(DownloadStationSettings settings, string methodName, int apiVersion, HttpMethod httpVerb = null)
{ {
httpVerb ??= HttpMethod.Get;
var info = GetApiInfo(_apiType, settings); var info = GetApiInfo(_apiType, settings);
return BuildRequest(settings, info, methodName, apiVersion, httpVerb); return BuildRequest(settings, info, methodName, apiVersion, httpVerb);
} }
private HttpRequestBuilder BuildRequest(DownloadStationSettings settings, DiskStationApiInfo apiInfo, string methodName, int apiVersion, HttpMethod httpVerb = HttpMethod.GET) private HttpRequestBuilder BuildRequest(DownloadStationSettings settings, DiskStationApiInfo apiInfo, string methodName, int apiVersion, HttpMethod httpVerb = null)
{ {
httpVerb ??= HttpMethod.Get;
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port).Resource($"webapi/{apiInfo.Path}"); var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port).Resource($"webapi/{apiInfo.Path}");
requestBuilder.Method = httpVerb; requestBuilder.Method = httpVerb;
requestBuilder.LogResponseContent = true; requestBuilder.LogResponseContent = true;
@ -164,7 +169,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
throw new ArgumentOutOfRangeException(nameof(apiVersion)); throw new ArgumentOutOfRangeException(nameof(apiVersion));
} }
if (httpVerb == HttpMethod.POST) if (httpVerb == HttpMethod.Post)
{ {
if (apiInfo.NeedsAuthentication) if (apiInfo.NeedsAuthentication)
{ {

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -21,7 +22,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
public void AddTaskFromData(byte[] data, string filename, string downloadDirectory, DownloadStationSettings settings) public void AddTaskFromData(byte[] data, string filename, string downloadDirectory, DownloadStationSettings settings)
{ {
var requestBuilder = BuildRequest(settings, "create", 2, HttpMethod.POST); var requestBuilder = BuildRequest(settings, "create", 2, HttpMethod.Post);
if (downloadDirectory.IsNotNullOrWhiteSpace()) if (downloadDirectory.IsNotNullOrWhiteSpace())
{ {

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -22,7 +23,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
public void AddTaskFromData(byte[] data, string filename, string downloadDirectory, DownloadStationSettings settings) public void AddTaskFromData(byte[] data, string filename, string downloadDirectory, DownloadStationSettings settings)
{ {
var requestBuilder = BuildRequest(settings, "create", 2, HttpMethod.POST); var requestBuilder = BuildRequest(settings, "create", 2, HttpMethod.Post);
requestBuilder.AddFormParameter("type", "\"file\""); requestBuilder.AddFormParameter("type", "\"file\"");
requestBuilder.AddFormParameter("file", "[\"fileData\"]"); requestBuilder.AddFormParameter("file", "[\"fileData\"]");

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
@ -108,7 +109,7 @@ namespace NzbDrone.Core.Download.Clients.Flood
{ {
var verifyRequest = BuildRequest(settings).Resource("/auth/verify").Build(); var verifyRequest = BuildRequest(settings).Resource("/auth/verify").Build();
verifyRequest.Method = HttpMethod.GET; verifyRequest.Method = HttpMethod.Get;
HandleRequest(verifyRequest, settings); HandleRequest(verifyRequest, settings);
} }
@ -181,7 +182,7 @@ namespace NzbDrone.Core.Download.Clients.Flood
{ {
var getTorrentsRequest = BuildRequest(settings).Resource("/torrents").Build(); var getTorrentsRequest = BuildRequest(settings).Resource("/torrents").Build();
getTorrentsRequest.Method = HttpMethod.GET; getTorrentsRequest.Method = HttpMethod.Get;
return Json.Deserialize<TorrentListSummary>(HandleRequest(getTorrentsRequest, settings).Content).Torrents; return Json.Deserialize<TorrentListSummary>(HandleRequest(getTorrentsRequest, settings).Content).Torrents;
} }
@ -190,7 +191,7 @@ namespace NzbDrone.Core.Download.Clients.Flood
{ {
var contentsRequest = BuildRequest(settings).Resource($"/torrents/{hash}/contents").Build(); var contentsRequest = BuildRequest(settings).Resource($"/torrents/{hash}/contents").Build();
contentsRequest.Method = HttpMethod.GET; contentsRequest.Method = HttpMethod.Get;
return Json.Deserialize<List<TorrentContent>>(HandleRequest(contentsRequest, settings).Content).ConvertAll(content => content.Path); return Json.Deserialize<List<TorrentContent>>(HandleRequest(contentsRequest, settings).Content).ConvertAll(content => content.Path);
} }
@ -199,7 +200,7 @@ namespace NzbDrone.Core.Download.Clients.Flood
{ {
var tagsRequest = BuildRequest(settings).Resource("/torrents/tags").Build(); var tagsRequest = BuildRequest(settings).Resource("/torrents/tags").Build();
tagsRequest.Method = HttpMethod.PATCH; tagsRequest.Method = HttpMethod.Patch;
var body = new Dictionary<string, object> var body = new Dictionary<string, object>
{ {
@ -215,7 +216,7 @@ namespace NzbDrone.Core.Download.Clients.Flood
{ {
var contentsRequest = BuildRequest(settings).Resource($"/client/settings").Build(); var contentsRequest = BuildRequest(settings).Resource($"/client/settings").Build();
contentsRequest.Method = HttpMethod.GET; contentsRequest.Method = HttpMethod.Get;
return Json.Deserialize<FloodClientSettings>(HandleRequest(contentsRequest, settings).Content); return Json.Deserialize<FloodClientSettings>(HandleRequest(contentsRequest, settings).Content);
} }

@ -74,7 +74,7 @@ namespace NzbDrone.Core.Download.Clients.Hadouken
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters); var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
requestBuilder.LogResponseContent = true; requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password); requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
requestBuilder.Headers.Add("Accept-Encoding", "gzip,deflate"); requestBuilder.Headers.Add("Accept-Encoding", "gzip,deflate");
var httpRequest = requestBuilder.Build(); var httpRequest = requestBuilder.Build();

@ -229,7 +229,7 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters); var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);
requestBuilder.LogResponseContent = true; requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password); requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
var httpRequest = requestBuilder.Build(); var httpRequest = requestBuilder.Build();

@ -294,7 +294,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase) var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{ {
LogResponseContent = true, LogResponseContent = true,
NetworkCredential = new NetworkCredential(settings.Username, settings.Password) NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
}; };
return requestBuilder; return requestBuilder;
} }

@ -339,7 +339,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase) var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{ {
LogResponseContent = true, LogResponseContent = true,
NetworkCredential = new NetworkCredential(settings.Username, settings.Password) NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password)
}; };
return requestBuilder; return requestBuilder;
} }

@ -201,7 +201,7 @@ namespace NzbDrone.Core.Download.Clients.Transmission
.Accept(HttpAccept.Json); .Accept(HttpAccept.Json);
requestBuilder.LogResponseContent = true; requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password); requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
requestBuilder.AllowAutoRedirect = false; requestBuilder.AllowAutoRedirect = false;
return requestBuilder; return requestBuilder;

@ -132,6 +132,12 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
continue; continue;
} }
// Ignore torrents with an empty path
if (torrent.Path.IsNullOrWhiteSpace())
{
continue;
}
if (torrent.Path.StartsWith(".")) if (torrent.Path.StartsWith("."))
{ {
throw new DownloadClientException("Download paths must be absolute. Please specify variable \"directory\" in rTorrent."); throw new DownloadClientException("Download paths must be absolute. Please specify variable \"directory\" in rTorrent.");

@ -0,0 +1,28 @@
using System.Xml.Linq;
using System.Xml.XPath;
using NzbDrone.Core.Download.Extensions;
namespace NzbDrone.Core.Download.Clients.RTorrent
{
public class RTorrentFault
{
public RTorrentFault(XElement element)
{
foreach (var e in element.XPathSelectElements("./value/struct/member"))
{
var name = e.ElementAsString("name");
if (name == "faultCode")
{
FaultCode = e.Element("value").GetIntValue();
}
else if (name == "faultString")
{
FaultString = e.Element("value").GetStringValue();
}
}
}
public int FaultCode { get; set; }
public string FaultString { get; set; }
}
}

@ -2,11 +2,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Xml.Linq;
using System.Runtime.InteropServices.ComTypes; using System.Xml.XPath;
using CookComputing.XmlRpc;
using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download.Extensions;
namespace NzbDrone.Core.Download.Clients.RTorrent namespace NzbDrone.Core.Download.Clients.RTorrent
{ {
@ -23,122 +23,67 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings); void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings);
} }
public interface IRTorrent : IXmlRpcProxy
{
[XmlRpcMethod("d.multicall2")]
object[] TorrentMulticall(params string[] parameters);
[XmlRpcMethod("load.normal")]
int LoadNormal(string target, string data, params string[] commands);
[XmlRpcMethod("load.start")]
int LoadStart(string target, string data, params string[] commands);
[XmlRpcMethod("load.raw")]
int LoadRaw(string target, byte[] data, params string[] commands);
[XmlRpcMethod("load.raw_start")]
int LoadRawStart(string target, byte[] data, params string[] commands);
[XmlRpcMethod("d.erase")]
int Remove(string hash);
[XmlRpcMethod("d.name")]
string GetName(string hash);
[XmlRpcMethod("d.custom1.set")]
string SetLabel(string hash, string label);
[XmlRpcMethod("d.views.push_back_unique")]
int PushUniqueView(string hash, string view);
[XmlRpcMethod("system.client_version")]
string GetVersion();
}
public class RTorrentProxy : IRTorrentProxy public class RTorrentProxy : IRTorrentProxy
{ {
private readonly Logger _logger; private readonly IHttpClient _httpClient;
public RTorrentProxy(Logger logger) public RTorrentProxy(IHttpClient httpClient)
{ {
_logger = logger; _httpClient = httpClient;
} }
public string GetVersion(RTorrentSettings settings) public string GetVersion(RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: system.client_version"); var document = ExecuteRequest(settings, "system.client_version");
var client = BuildClient(settings);
var version = ExecuteRequest(() => client.GetVersion());
return version; return document.Descendants("string").FirstOrDefault()?.Value ?? "0.0.0";
} }
public List<RTorrentTorrent> GetTorrents(RTorrentSettings settings) public List<RTorrentTorrent> GetTorrents(RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: d.multicall2"); var document = ExecuteRequest(settings,
"d.multicall2",
var client = BuildClient(settings); "",
var ret = ExecuteRequest(() => client.TorrentMulticall("", "",
"", "d.name=", // string
"d.name=", // string "d.hash=", // string
"d.hash=", // string "d.base_path=", // string
"d.base_path=", // string "d.custom1=", // string (label)
"d.custom1=", // string (label) "d.size_bytes=", // long
"d.size_bytes=", // long "d.left_bytes=", // long
"d.left_bytes=", // long "d.down.rate=", // long (in bytes / s)
"d.down.rate=", // long (in bytes / s) "d.ratio=", // long
"d.ratio=", // long "d.is_open=", // long
"d.is_open=", // long "d.is_active=", // long
"d.is_active=", // long "d.complete=", //long
"d.complete=", // long "d.timestamp.finished="); // long (unix timestamp)
"d.timestamp.finished=")); // long (unix timestamp)
var torrents = document.XPathSelectElement("./methodResponse/params/param/value/array/data")
var items = new List<RTorrentTorrent>(); ?.Elements()
.Select(x => new RTorrentTorrent(x))
foreach (object[] torrent in ret) .ToList()
{ ?? new List<RTorrentTorrent>();
var labelDecoded = System.Web.HttpUtility.UrlDecode((string)torrent[3]);
return torrents;
var item = new RTorrentTorrent();
item.Name = (string)torrent[0];
item.Hash = (string)torrent[1];
item.Path = (string)torrent[2];
item.Category = labelDecoded;
item.TotalSize = (long)torrent[4];
item.RemainingSize = (long)torrent[5];
item.DownRate = (long)torrent[6];
item.Ratio = (long)torrent[7];
item.IsOpen = Convert.ToBoolean((long)torrent[8]);
item.IsActive = Convert.ToBoolean((long)torrent[9]);
item.IsFinished = Convert.ToBoolean((long)torrent[10]);
item.FinishedTime = (long)torrent[11];
items.Add(item);
}
return items;
} }
public void AddTorrentFromUrl(string torrentUrl, string label, RTorrentPriority priority, string directory, RTorrentSettings settings) public void AddTorrentFromUrl(string torrentUrl, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
{ {
var client = BuildClient(settings); var args = new List<object> { "", torrentUrl };
var response = ExecuteRequest(() => args.AddRange(GetCommands(label, priority, directory));
XDocument response;
if (settings.AddStopped)
{ {
if (settings.AddStopped) response = ExecuteRequest(settings, "load.normal", args.ToArray());
{ }
_logger.Debug("Executing remote method: load.normal"); else
return client.LoadNormal("", torrentUrl, GetCommands(label, priority, directory)); {
} response = ExecuteRequest(settings, "load.start", args.ToArray());
else }
{
_logger.Debug("Executing remote method: load.start");
return client.LoadStart("", torrentUrl, GetCommands(label, priority, directory));
}
});
if (response != 0) if (response.GetIntResponse() != 0)
{ {
throw new DownloadClientException("Could not add torrent: {0}.", torrentUrl); throw new DownloadClientException("Could not add torrent: {0}.", torrentUrl);
} }
@ -146,22 +91,21 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
public void AddTorrentFromFile(string fileName, byte[] fileContent, string label, RTorrentPriority priority, string directory, RTorrentSettings settings) public void AddTorrentFromFile(string fileName, byte[] fileContent, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
{ {
var client = BuildClient(settings); var args = new List<object> { "", fileContent };
var response = ExecuteRequest(() => args.AddRange(GetCommands(label, priority, directory));
XDocument response;
if (settings.AddStopped)
{ {
if (settings.AddStopped) response = ExecuteRequest(settings, "load.raw", args.ToArray());
{ }
_logger.Debug("Executing remote method: load.raw"); else
return client.LoadRaw("", fileContent, GetCommands(label, priority, directory)); {
} response = ExecuteRequest(settings, "load.raw_start", args.ToArray());
else }
{
_logger.Debug("Executing remote method: load.raw_start");
return client.LoadRawStart("", fileContent, GetCommands(label, priority, directory));
}
});
if (response != 0) if (response.GetIntResponse() != 0)
{ {
throw new DownloadClientException("Could not add torrent: {0}.", fileName); throw new DownloadClientException("Could not add torrent: {0}.", fileName);
} }
@ -169,12 +113,9 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
public void SetTorrentLabel(string hash, string label, RTorrentSettings settings) public void SetTorrentLabel(string hash, string label, RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: d.custom1.set"); var response = ExecuteRequest(settings, "d.custom1.set", hash, label);
var client = BuildClient(settings); if (response.GetStringResponse() != label)
var response = ExecuteRequest(() => client.SetLabel(hash, label));
if (response != label)
{ {
throw new DownloadClientException("Could not set label to {1} for torrent: {0}.", hash, label); throw new DownloadClientException("Could not set label to {1} for torrent: {0}.", hash, label);
} }
@ -182,11 +123,9 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
public void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings) public void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: d.views.push_back_unique"); var response = ExecuteRequest(settings, "d.views.push_back_unique", hash, view);
var client = BuildClient(settings); if (response.GetIntResponse() != 0)
var response = ExecuteRequest(() => client.PushUniqueView(hash, view));
if (response != 0)
{ {
throw new DownloadClientException("Could not push unique view {0} for torrent: {1}.", view, hash); throw new DownloadClientException("Could not push unique view {0} for torrent: {1}.", view, hash);
} }
@ -194,12 +133,9 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
public void RemoveTorrent(string hash, RTorrentSettings settings) public void RemoveTorrent(string hash, RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: d.erase"); var response = ExecuteRequest(settings, "d.erase", hash);
var client = BuildClient(settings);
var response = ExecuteRequest(() => client.Remove(hash));
if (response != 0) if (response.GetIntResponse() != 0)
{ {
throw new DownloadClientException("Could not remove torrent: {0}.", hash); throw new DownloadClientException("Could not remove torrent: {0}.", hash);
} }
@ -207,13 +143,10 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
public bool HasHashTorrent(string hash, RTorrentSettings settings) public bool HasHashTorrent(string hash, RTorrentSettings settings)
{ {
_logger.Debug("Executing remote method: d.name");
var client = BuildClient(settings);
try try
{ {
var name = ExecuteRequest(() => client.GetName(hash)); var response = ExecuteRequest(settings, "d.name", hash);
var name = response.GetStringResponse();
if (name.IsNullOrWhiteSpace()) if (name.IsNullOrWhiteSpace())
{ {
@ -252,45 +185,34 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
return result.ToArray(); return result.ToArray();
} }
private IRTorrent BuildClient(RTorrentSettings settings) private XDocument ExecuteRequest(RTorrentSettings settings, string methodName, params object[] args)
{ {
var client = XmlRpcProxyGen.Create<IRTorrent>(); var requestBuilder = new XmlRpcRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{
client.Url = string.Format(@"{0}://{1}:{2}/{3}", LogResponseContent = true,
settings.UseSsl ? "https" : "http", };
settings.Host,
settings.Port,
settings.UrlBase);
client.EnableCompression = true;
if (!settings.Username.IsNullOrWhiteSpace()) if (!settings.Username.IsNullOrWhiteSpace())
{ {
client.Credentials = new NetworkCredential(settings.Username, settings.Password); requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
} }
return client; var request = requestBuilder.Call(methodName, args).Build();
}
private T ExecuteRequest<T>(Func<T> task) var response = _httpClient.Execute(request);
{
try var doc = XDocument.Parse(response.Content);
{
return task(); var faultElement = doc.XPathSelectElement("./methodResponse/fault");
}
catch (XmlRpcServerException ex) if (faultElement != null)
{
throw new DownloadClientException("Unable to connect to rTorrent, please check your settings", ex);
}
catch (WebException ex)
{ {
if (ex.Status == WebExceptionStatus.TrustFailure) var fault = new RTorrentFault(faultElement);
{
throw new DownloadClientUnavailableException("Unable to connect to rTorrent, certificate validation failed.", ex);
}
throw new DownloadClientUnavailableException("Unable to connect to rTorrent, please check your settings", ex); throw new DownloadClientException($"rTorrent returned error code {fault.FaultCode}: {fault.FaultString}");
} }
return doc;
} }
} }
} }

@ -1,7 +1,35 @@
namespace NzbDrone.Core.Download.Clients.RTorrent using System;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using NzbDrone.Core.Download.Extensions;
namespace NzbDrone.Core.Download.Clients.RTorrent
{ {
public class RTorrentTorrent public class RTorrentTorrent
{ {
public RTorrentTorrent()
{
}
public RTorrentTorrent(XElement element)
{
var data = element.Descendants("value").ToList();
Name = data[0].GetStringValue();
Hash = data[1].GetStringValue();
Path = data[2].GetStringValue();
Category = HttpUtility.UrlDecode(data[3].GetStringValue());
TotalSize = data[4].GetLongValue();
RemainingSize = data[5].GetLongValue();
DownRate = data[6].GetLongValue();
Ratio = data[7].GetLongValue();
IsOpen = Convert.ToBoolean(data[8].GetLongValue());
IsActive = Convert.ToBoolean(data[9].GetLongValue());
IsFinished = Convert.ToBoolean(data[10].GetLongValue());
FinishedTime = data[11].GetLongValue();
}
public string Name { get; set; } public string Name { get; set; }
public string Hash { get; set; } public string Hash { get; set; }
public string Path { get; set; } public string Path { get; set; }

@ -196,7 +196,7 @@ namespace NzbDrone.Core.Download.Clients.UTorrent
.Accept(HttpAccept.Json); .Accept(HttpAccept.Json);
requestBuilder.LogResponseContent = true; requestBuilder.LogResponseContent = true;
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password); requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.Username, settings.Password);
return requestBuilder; return requestBuilder;
} }

@ -0,0 +1,55 @@
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
namespace NzbDrone.Core.Download.Extensions
{
internal static class XmlExtensions
{
public static string GetStringValue(this XElement element)
{
return element.ElementAsString("string");
}
public static long GetLongValue(this XElement element)
{
return element.ElementAsLong("i8");
}
public static int GetIntValue(this XElement element)
{
return element.ElementAsInt("i4");
}
public static string ElementAsString(this XElement element, XName name, bool trim = false)
{
var el = element.Element(name);
return string.IsNullOrWhiteSpace(el?.Value)
? null
: (trim ? el.Value.Trim() : el.Value);
}
public static long ElementAsLong(this XElement element, XName name)
{
var el = element.Element(name);
return long.TryParse(el?.Value, out long value) ? value : default;
}
public static int ElementAsInt(this XElement element, XName name)
{
var el = element.Element(name);
return int.TryParse(el?.Value, out int value) ? value : default(int);
}
public static int GetIntResponse(this XDocument document)
{
return document.XPathSelectElement("./methodResponse/params/param/value").GetIntValue();
}
public static string GetStringResponse(this XDocument document)
{
return document.XPathSelectElement("./methodResponse/params/param/value").GetStringValue();
}
}
}

@ -1,4 +1,4 @@
using System; using System;
using System.Net; using System.Net;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Http.Proxy; using NzbDrone.Common.Http.Proxy;
@ -15,7 +15,7 @@ namespace NzbDrone.Core.Http
_configService = configService; _configService = configService;
} }
public HttpProxySettings GetProxySettings(HttpRequest request) public HttpProxySettings GetProxySettings(HttpUri uri)
{ {
var proxySettings = GetProxySettings(); var proxySettings = GetProxySettings();
if (proxySettings == null) if (proxySettings == null)
@ -23,7 +23,7 @@ namespace NzbDrone.Core.Http
return null; return null;
} }
if (ShouldProxyBeBypassed(proxySettings, request.Url)) if (ShouldProxyBeBypassed(proxySettings, uri))
{ {
return null; return null;
} }

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -131,7 +131,7 @@ namespace NzbDrone.Core.Indexers.FileList
var baseUrl = string.Format("{0}/api.php?action={1}&category={2}{3}", Settings.BaseUrl.TrimEnd('/'), searchType, categoriesQuery, parameters); var baseUrl = string.Format("{0}/api.php?action={1}&category={2}{3}", Settings.BaseUrl.TrimEnd('/'), searchType, categoriesQuery, parameters);
var request = new IndexerRequest(baseUrl, HttpAccept.Json); var request = new IndexerRequest(baseUrl, HttpAccept.Json);
request.HttpRequest.AddBasicAuthentication(Settings.Username.Trim(), Settings.Passkey.Trim()); request.HttpRequest.Credentials = new BasicNetworkCredential(Settings.Username.Trim(), Settings.Passkey.Trim());
yield return request; yield return request;
} }

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.IndexerSearch.Definitions;
@ -132,7 +133,7 @@ namespace NzbDrone.Core.Indexers.HDBits
.Resource("/api/torrents") .Resource("/api/torrents")
.Build(); .Build();
request.Method = HttpMethod.POST; request.Method = HttpMethod.Post;
const string appJson = "application/json"; const string appJson = "application/json";
request.Headers.Accept = appJson; request.Headers.Accept = appJson;
request.Headers.ContentType = appJson; request.Headers.ContentType = appJson;

@ -1,3 +1,4 @@
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
@ -29,7 +30,7 @@ namespace NzbDrone.Core.Notifications.Discord
.Accept(HttpAccept.Json) .Accept(HttpAccept.Json)
.Build(); .Build();
request.Method = HttpMethod.POST; request.Method = HttpMethod.Post;
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson()); request.SetContent(payload.ToJson());

@ -7,17 +7,21 @@ using MailKit.Security;
using MimeKit; using MimeKit;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http.Dispatchers;
using NzbDrone.Core.Security;
namespace NzbDrone.Core.Notifications.Email namespace NzbDrone.Core.Notifications.Email
{ {
public class Email : NotificationBase<EmailSettings> public class Email : NotificationBase<EmailSettings>
{ {
private readonly ICertificateValidationService _certificateValidationService;
private readonly Logger _logger; private readonly Logger _logger;
public override string Name => "Email"; public override string Name => "Email";
public Email(Logger logger) public Email(ICertificateValidationService certificateValidationService, Logger logger)
{ {
_certificateValidationService = certificateValidationService;
_logger = logger; _logger = logger;
} }
@ -124,6 +128,8 @@ namespace NzbDrone.Core.Notifications.Email
} }
} }
client.ServerCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError;
_logger.Debug("Connecting to mail server"); _logger.Debug("Connecting to mail server");
client.Connect(settings.Server, settings.Port, serverOption); client.Connect(settings.Server, settings.Port, serverOption);

@ -1,4 +1,5 @@
using System; using System;
using System.Net.Http;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -28,7 +29,7 @@ namespace NzbDrone.Core.Notifications.Join
public void SendNotification(string title, string message, JoinSettings settings) public void SendNotification(string title, string message, JoinSettings settings)
{ {
var method = HttpMethod.GET; var method = HttpMethod.Get;
try try
{ {

@ -1,7 +1,7 @@
using System.Net; using System.Net;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using HttpMethod = NzbDrone.Common.Http.HttpMethod;
namespace NzbDrone.Core.Notifications.Mailgun namespace NzbDrone.Core.Notifications.Mailgun
{ {
@ -28,7 +28,7 @@ namespace NzbDrone.Core.Notifications.Mailgun
{ {
try try
{ {
var request = BuildRequest(settings, $"{settings.SenderDomain}/messages", HttpMethod.POST, title, message).Build(); var request = BuildRequest(settings, $"{settings.SenderDomain}/messages", HttpMethod.Post, title, message).Build();
_httpClient.Execute(request); _httpClient.Execute(request);
} }
catch (HttpException ex) catch (HttpException ex)

@ -1,4 +1,5 @@
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Text; using System.Text;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
@ -40,7 +41,7 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv
.AddQueryParam("X-Plex-Version", BuildInfo.Version.ToString()) .AddQueryParam("X-Plex-Version", BuildInfo.Version.ToString())
.AddQueryParam("strong", true); .AddQueryParam("strong", true);
requestBuilder.Method = HttpMethod.POST; requestBuilder.Method = HttpMethod.Post;
var request = requestBuilder.Build(); var request = requestBuilder.Build();

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -35,7 +36,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public List<PlexSection> GetTvSections(PlexServerSettings settings) public List<PlexSection> GetTvSections(PlexServerSettings settings)
{ {
var request = BuildRequest("library/sections", HttpMethod.GET, settings); var request = BuildRequest("library/sections", HttpMethod.Get, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);
@ -65,7 +66,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public void Update(int sectionId, PlexServerSettings settings) public void Update(int sectionId, PlexServerSettings settings)
{ {
var resource = $"library/sections/{sectionId}/refresh"; var resource = $"library/sections/{sectionId}/refresh";
var request = BuildRequest(resource, HttpMethod.GET, settings); var request = BuildRequest(resource, HttpMethod.Get, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);
@ -74,7 +75,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public void UpdateSeries(string metadataId, PlexServerSettings settings) public void UpdateSeries(string metadataId, PlexServerSettings settings)
{ {
var resource = $"library/metadata/{metadataId}/refresh"; var resource = $"library/metadata/{metadataId}/refresh";
var request = BuildRequest(resource, HttpMethod.PUT, settings); var request = BuildRequest(resource, HttpMethod.Put, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);
@ -82,7 +83,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public string Version(PlexServerSettings settings) public string Version(PlexServerSettings settings)
{ {
var request = BuildRequest("identity", HttpMethod.GET, settings); var request = BuildRequest("identity", HttpMethod.Get, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);
@ -100,7 +101,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public List<PlexPreference> Preferences(PlexServerSettings settings) public List<PlexPreference> Preferences(PlexServerSettings settings)
{ {
var request = BuildRequest(":/prefs", HttpMethod.GET, settings); var request = BuildRequest(":/prefs", HttpMethod.Get, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);
@ -120,7 +121,7 @@ namespace NzbDrone.Core.Notifications.Plex.Server
{ {
var guid = $"com.plexapp.agents.thetvdb://{tvdbId}?lang={language}"; var guid = $"com.plexapp.agents.thetvdb://{tvdbId}?lang={language}";
var resource = $"library/sections/{sectionId}/all?guid={System.Web.HttpUtility.UrlEncode(guid)}"; var resource = $"library/sections/{sectionId}/all?guid={System.Web.HttpUtility.UrlEncode(guid)}";
var request = BuildRequest(resource, HttpMethod.GET, settings); var request = BuildRequest(resource, HttpMethod.Get, settings);
var response = ProcessRequest(request); var response = ProcessRequest(request);
CheckForError(response); CheckForError(response);

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -100,8 +101,8 @@ namespace NzbDrone.Core.Notifications.PushBullet
var request = requestBuilder.Build(); var request = requestBuilder.Build();
request.Method = HttpMethod.GET; request.Method = HttpMethod.Get;
request.AddBasicAuthentication(settings.ApiKey, string.Empty); request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
var response = _httpClient.Execute(request); var response = _httpClient.Execute(request);
@ -197,7 +198,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
var request = requestBuilder.Build(); var request = requestBuilder.Build();
request.AddBasicAuthentication(settings.ApiKey, string.Empty); request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
_httpClient.Execute(request); _httpClient.Execute(request);
} }

@ -1,4 +1,5 @@
using System.Net; using System.Net;
using System.Net.Http;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
@ -22,7 +23,7 @@ namespace NzbDrone.Core.Notifications.SendGrid
{ {
try try
{ {
var request = BuildRequest(settings, "mail/send", HttpMethod.POST); var request = BuildRequest(settings, "mail/send", HttpMethod.Post);
var payload = new SendGridPayload var payload = new SendGridPayload
{ {

@ -1,3 +1,4 @@
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
@ -29,7 +30,7 @@ namespace NzbDrone.Core.Notifications.Slack
.Accept(HttpAccept.Json) .Accept(HttpAccept.Json)
.Build(); .Build();
request.Method = HttpMethod.POST; request.Method = HttpMethod.Post;
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson()); request.SetContent(payload.ToJson());

@ -1,3 +1,4 @@
using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
@ -35,7 +36,7 @@ namespace NzbDrone.Core.Notifications.Trakt
public void AddToCollection(TraktCollectShowsResource payload, string accessToken) public void AddToCollection(TraktCollectShowsResource payload, string accessToken)
{ {
var request = BuildTraktRequest("sync/collection", HttpMethod.POST, accessToken); var request = BuildTraktRequest("sync/collection", HttpMethod.Post, accessToken);
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson()); request.SetContent(payload.ToJson());
@ -53,7 +54,7 @@ namespace NzbDrone.Core.Notifications.Trakt
public void RemoveFromCollection(TraktCollectShowsResource payload, string accessToken) public void RemoveFromCollection(TraktCollectShowsResource payload, string accessToken)
{ {
var request = BuildTraktRequest("sync/collection/remove", HttpMethod.POST, accessToken); var request = BuildTraktRequest("sync/collection/remove", HttpMethod.Post, accessToken);
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
var temp = payload.ToJson(); var temp = payload.ToJson();
@ -72,7 +73,7 @@ namespace NzbDrone.Core.Notifications.Trakt
public string GetUserName(string accessToken) public string GetUserName(string accessToken)
{ {
var request = BuildTraktRequest("users/settings", HttpMethod.GET, accessToken); var request = BuildTraktRequest("users/settings", HttpMethod.Get, accessToken);
try try
{ {

@ -0,0 +1,110 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.OAuth;
namespace NzbDrone.Core.Notifications.Twitter
{
public interface ITwitterProxy
{
NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier);
string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl);
void UpdateStatus(string message, TwitterSettings settings);
void DirectMessage(string message, TwitterSettings settings);
}
public class TwitterProxy : ITwitterProxy
{
private readonly IHttpClient _httpClient;
public TwitterProxy(IHttpClient httpClient)
{
_httpClient = httpClient;
}
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
{
// Creating a new instance with a helper method
var oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callbackUrl);
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
var qscoll = HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
return string.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
}
public NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
{
// Creating a new instance with a helper method
var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
return HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
}
public void UpdateStatus(string message, TwitterSettings settings)
{
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/statuses/update.json";
var customParams = new Dictionary<string, string>
{
{ "status", message.EncodeRFC3986() }
};
var request = GetRequest(oAuthRequest, customParams);
request.Headers.ContentType = "application/x-www-form-urlencoded";
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
ExecuteRequest(request);
}
public void DirectMessage(string message, TwitterSettings settings)
{
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/direct_messages/new.json";
var customParams = new Dictionary<string, string>
{
{ "text", message.EncodeRFC3986() },
{ "screenname", settings.Mention.EncodeRFC3986() }
};
var request = GetRequest(oAuthRequest, customParams);
request.Headers.ContentType = "application/x-www-form-urlencoded";
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
ExecuteRequest(request);
}
private string GetCustomParametersString(Dictionary<string, string> customParams)
{
return customParams.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
}
private HttpRequest GetRequest(OAuthRequest oAuthRequest, Dictionary<string, string> customParams)
{
var auth = oAuthRequest.GetAuthorizationHeader(customParams);
var request = new HttpRequest(oAuthRequest.RequestUrl);
request.Headers.Add("Authorization", auth);
request.Method = oAuthRequest.Method == "POST" ? HttpMethod.Post : HttpMethod.Get;
return request;
}
private HttpResponse ExecuteRequest(HttpRequest request)
{
return _httpClient.Execute(request);
}
}
}

@ -1,13 +1,9 @@
using System; using System;
using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Web;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Common.OAuth;
namespace NzbDrone.Core.Notifications.Twitter namespace NzbDrone.Core.Notifications.Twitter
{ {
@ -21,31 +17,18 @@ namespace NzbDrone.Core.Notifications.Twitter
public class TwitterService : ITwitterService public class TwitterService : ITwitterService
{ {
private readonly IHttpClient _httpClient; private readonly ITwitterProxy _twitterProxy;
private readonly Logger _logger; private readonly Logger _logger;
public TwitterService(IHttpClient httpClient, Logger logger) public TwitterService(ITwitterProxy twitterProxy, Logger logger)
{ {
_httpClient = httpClient; _twitterProxy = twitterProxy;
_logger = logger; _logger = logger;
} }
private NameValueCollection OAuthQuery(OAuthRequest oAuthRequest)
{
var auth = oAuthRequest.GetAuthorizationHeader();
var request = new Common.Http.HttpRequest(oAuthRequest.RequestUrl);
request.Headers.Add("Authorization", auth);
var response = _httpClient.Get(request);
return HttpUtility.ParseQueryString(response.Content);
}
public OAuthToken GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier) public OAuthToken GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
{ {
// Creating a new instance with a helper method var qscoll = _twitterProxy.GetOAuthToken(consumerKey, consumerSecret, oauthToken, oauthVerifier);
var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
var qscoll = OAuthQuery(oAuthRequest);
return new OAuthToken return new OAuthToken
{ {
@ -56,31 +39,16 @@ namespace NzbDrone.Core.Notifications.Twitter
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl) public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
{ {
// Creating a new instance with a helper method return _twitterProxy.GetOAuthRedirect(consumerKey, consumerSecret, callbackUrl);
var oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callbackUrl);
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
var qscoll = OAuthQuery(oAuthRequest);
return string.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
} }
public void SendNotification(string message, TwitterSettings settings) public void SendNotification(string message, TwitterSettings settings)
{ {
try try
{ {
var oAuth = new TinyTwitter.OAuthInfo
{
ConsumerKey = settings.ConsumerKey,
ConsumerSecret = settings.ConsumerSecret,
AccessToken = settings.AccessToken,
AccessSecret = settings.AccessTokenSecret
};
var twitter = new TinyTwitter.TinyTwitter(oAuth);
if (settings.DirectMessage) if (settings.DirectMessage)
{ {
twitter.DirectMessage(message, settings.Mention); _twitterProxy.DirectMessage(message, settings);
} }
else else
{ {
@ -89,7 +57,7 @@ namespace NzbDrone.Core.Notifications.Twitter
message += string.Format(" @{0}", settings.Mention); message += string.Format(" @{0}", settings.Mention);
} }
twitter.UpdateStatus(message); _twitterProxy.UpdateStatus(message, settings);
} }
} }
catch (WebException ex) catch (WebException ex)

@ -1,10 +1,10 @@
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
namespace NzbDrone.Core.Notifications.Webhook namespace NzbDrone.Core.Notifications.Webhook
{ {
public enum WebhookMethod public enum WebhookMethod
{ {
POST = HttpMethod.POST, POST = 1,
PUT = HttpMethod.PUT PUT = 2
} }
} }

@ -1,3 +1,5 @@
using System;
using System.Net.Http;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
@ -26,13 +28,19 @@ namespace NzbDrone.Core.Notifications.Webhook
.Accept(HttpAccept.Json) .Accept(HttpAccept.Json)
.Build(); .Build();
request.Method = (HttpMethod)settings.Method; request.Method = settings.Method switch
{
(int)WebhookMethod.POST => HttpMethod.Post,
(int)WebhookMethod.PUT => HttpMethod.Put,
_ => throw new ArgumentOutOfRangeException($"Invalid Webhook method {settings.Method}")
};
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
request.SetContent(body.ToJson()); request.SetContent(body.ToJson());
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace()) if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
{ {
request.AddBasicAuthentication(settings.Username, settings.Password); request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
} }
_httpClient.Execute(request); _httpClient.Execute(request);

@ -84,7 +84,7 @@ namespace NzbDrone.Core.Notifications.Xbmc
if (!settings.Username.IsNullOrWhiteSpace()) if (!settings.Username.IsNullOrWhiteSpace())
{ {
request.AddBasicAuthentication(settings.Username, settings.Password); request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
} }
var response = _httpClient.Execute(request); var response = _httpClient.Execute(request);

@ -1,16 +1,15 @@
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Security; using System.Net.Security;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http.Dispatchers;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Security namespace NzbDrone.Core.Security
{ {
public class X509CertificateValidationService : IHandle<ApplicationStartedEvent> public class X509CertificateValidationService : ICertificateValidationService
{ {
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly Logger _logger; private readonly Logger _logger;
@ -21,19 +20,29 @@ namespace NzbDrone.Core.Security
_logger = logger; _logger = logger;
} }
private bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) public bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ {
var request = sender as HttpWebRequest; var targetHostName = string.Empty;
if (request == null) if (sender is not SslStream && sender is not string)
{ {
return true; return true;
} }
var cert2 = certificate as X509Certificate2; if (sender is SslStream request)
if (cert2 != null && request != null && cert2.SignatureAlgorithm.FriendlyName == "md5RSA")
{ {
_logger.Error("https://{0} uses the obsolete md5 hash in it's https certificate, if that is your certificate, please (re)create certificate with better algorithm as soon as possible.", request.RequestUri.Authority); targetHostName = request.TargetHostName;
}
// Mailkit passes host in sender as string
if (sender is string stringHost)
{
targetHostName = stringHost;
}
if (certificate is X509Certificate2 cert2 && cert2.SignatureAlgorithm.FriendlyName == "md5RSA")
{
_logger.Error("https://{0} uses the obsolete md5 hash in it's https certificate, if that is your certificate, please (re)create certificate with better algorithm as soon as possible.", targetHostName);
} }
if (sslPolicyErrors == SslPolicyErrors.None) if (sslPolicyErrors == SslPolicyErrors.None)
@ -41,12 +50,12 @@ namespace NzbDrone.Core.Security
return true; return true;
} }
if (request.RequestUri.Host == "localhost" || request.RequestUri.Host == "127.0.0.1") if (targetHostName == "localhost" || targetHostName == "127.0.0.1")
{ {
return true; return true;
} }
var ipAddresses = GetIPAddresses(request.RequestUri.Host); var ipAddresses = GetIPAddresses(targetHostName);
var certificateValidation = _configService.CertificateValidation; var certificateValidation = _configService.CertificateValidation;
if (certificateValidation == CertificateValidationType.Disabled) if (certificateValidation == CertificateValidationType.Disabled)
@ -60,7 +69,7 @@ namespace NzbDrone.Core.Security
return true; return true;
} }
_logger.Error("Certificate validation for {0} failed. {1}", request.Address, sslPolicyErrors); _logger.Error("Certificate validation for {0} failed. {1}", targetHostName, sslPolicyErrors);
return false; return false;
} }
@ -74,10 +83,5 @@ namespace NzbDrone.Core.Security
return Dns.GetHostEntry(host).AddressList; return Dns.GetHostEntry(host).AddressList;
} }
public void Handle(ApplicationStartedEvent message)
{
ServicePointManager.ServerCertificateValidationCallback = ShouldByPassValidationError;
}
} }
} }

@ -15,7 +15,6 @@
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.1" /> <PackageReference Include="SixLabors.ImageSharp" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.14" /> <PackageReference Include="NLog" Version="4.7.14" />
<PackageReference Include="Kveer.XmlRPC" Version="1.2.0" />
<PackageReference Include="MonoTorrent" Version="2.0.5" /> <PackageReference Include="MonoTorrent" Version="2.0.5" />
<PackageReference Include="System.Data.SQLite.Core.Servarr" Version="1.0.115.5-18" /> <PackageReference Include="System.Data.SQLite.Core.Servarr" Version="1.0.115.5-18" />
<PackageReference Include="System.Text.Json" Version="6.0.4" /> <PackageReference Include="System.Text.Json" Version="6.0.4" />

@ -1,238 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace TinyTwitter
{
public class OAuthInfo
{
public string ConsumerKey { get; set; }
public string ConsumerSecret { get; set; }
public string AccessToken { get; set; }
public string AccessSecret { get; set; }
}
public class Tweet
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }
public string UserName { get; set; }
public string ScreenName { get; set; }
public string Text { get; set; }
}
public class TinyTwitter
{
private readonly OAuthInfo _oauth;
public TinyTwitter(OAuthInfo oauth)
{
_oauth = oauth;
}
public void UpdateStatus(string message)
{
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
.AddParameter("status", message)
.Execute();
}
/**
*
* As of June 26th 2015 Direct Messaging is not part of TinyTwitter.
* I have added it to Sonarr's copy to make our implementation easier
* and added this banner so it's not blindly updated.
*
**/
public void DirectMessage(string message, string screenName)
{
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
.AddParameter("text", message)
.AddParameter("screen_name", screenName)
.Execute();
}
public class RequestBuilder
{
private const string VERSION = "1.0";
private const string SIGNATURE_METHOD = "HMAC-SHA1";
private readonly OAuthInfo _oauth;
private readonly string _method;
private readonly IDictionary<string, string> _customParameters;
private readonly string _url;
public RequestBuilder(OAuthInfo oauth, string method, string url)
{
_oauth = oauth;
_method = method;
_url = url;
_customParameters = new Dictionary<string, string>();
}
public RequestBuilder AddParameter(string name, string value)
{
_customParameters.Add(name, value.EncodeRFC3986());
return this;
}
public string Execute()
{
var timespan = GetTimestamp();
var nonce = CreateNonce();
var parameters = new Dictionary<string, string>(_customParameters);
AddOAuthParameters(parameters, timespan, nonce);
var signature = GenerateSignature(parameters);
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
request.Method = _method;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Authorization", headerValue);
WriteRequestBody(request);
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
// after some requests. Abort the request seems to work. More info:
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly
var response = request.GetResponse();
string content;
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
}
request.Abort();
return content;
}
private void WriteRequestBody(HttpWebRequest request)
{
if (_method == "GET")
{
return;
}
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
using (var stream = request.GetRequestStream())
{
stream.Write(requestBody, 0, requestBody.Length);
}
}
private string GetRequestUrl()
{
if (_method != "GET" || _customParameters.Count == 0)
{
return _url;
}
return string.Format("{0}?{1}", _url, GetCustomParametersString());
}
private string GetCustomParametersString()
{
return _customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
}
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
{
return new StringBuilder("OAuth ")
.Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature))
.Where(x => x.Key.StartsWith("oauth_"))
.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986()))
.Join(","))
.ToString();
}
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
{
var dataToSign = new StringBuilder()
.Append(_method).Append('&')
.Append(_url.EncodeRFC3986()).Append('&')
.Append(parameters
.OrderBy(x => x.Key)
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
.Join("&")
.EncodeRFC3986());
var signatureKey = string.Format("{0}&{1}", _oauth.ConsumerSecret.EncodeRFC3986(), _oauth.AccessSecret.EncodeRFC3986());
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
return Convert.ToBase64String(signatureBytes);
}
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
{
parameters.Add("oauth_version", VERSION);
parameters.Add("oauth_consumer_key", _oauth.ConsumerKey);
parameters.Add("oauth_nonce", nonce);
parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
parameters.Add("oauth_timestamp", timestamp);
parameters.Add("oauth_token", _oauth.AccessToken);
}
private static string GetTimestamp()
{
return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
}
private static string CreateNonce()
{
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
}
}
}
public static class TinyTwitterHelperExtensions
{
public static string Join<T>(this IEnumerable<T> items, string separator)
{
return string.Join(separator, items.ToArray());
}
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
{
return items.Concat(new[] { value });
}
public static string EncodeRFC3986(this string value)
{
// From Twitterizer http://www.twitterizer.net/
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
var encoded = Uri.EscapeDataString(value);
return Regex
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
.Replace("(", "%28")
.Replace(")", "%29")
.Replace("$", "%24")
.Replace("!", "%21")
.Replace("*", "%2A")
.Replace("'", "%27")
.Replace("%7E", "~");
}
}
}

@ -1,5 +1,8 @@
using System.Linq; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
@ -8,25 +11,30 @@ namespace NzbDrone.Integration.Test
[TestFixture] [TestFixture]
public class IndexHtmlFixture : IntegrationTest public class IndexHtmlFixture : IntegrationTest
{ {
private HttpClient _httpClient = new HttpClient();
[Test] [Test]
public void should_get_index_html() public void should_get_index_html()
{ {
var text = new WebClient().DownloadString(RootUrl); var request = new HttpRequestMessage(HttpMethod.Get, RootUrl);
var response = _httpClient.Send(request);
var text = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
text.Should().NotBeNullOrWhiteSpace(); text.Should().NotBeNullOrWhiteSpace();
} }
[Test] [Test]
public void index_should_not_be_cached() public void index_should_not_be_cached()
{ {
var client = new WebClient(); var request = new HttpRequestMessage(HttpMethod.Get, RootUrl);
_ = client.DownloadString(RootUrl); var response = _httpClient.Send(request);
var headers = response.Headers;
var headers = client.ResponseHeaders; headers.CacheControl.NoStore.Should().BeTrue();
headers.CacheControl.NoCache.Should().BeTrue();
headers.Pragma.Should().Contain(new NameValueHeaderValue("no-cache"));
headers.Get("Cache-Control").Split(',').Select(x => x.Trim()) response.Content.Headers.Expires.Should().BeBefore(DateTime.UtcNow);
.Should().BeEquivalentTo("no-store, no-cache".Split(',').Select(x => x.Trim()));
headers.Get("Pragma").Should().Be("no-cache");
headers.Get("Expires").Should().Be("-1");
} }
} }
} }

Loading…
Cancel
Save