mirror of https://github.com/Radarr/Radarr
Use modern HttpClient
This commit is contained in:
parent
025634cd19
commit
c5b736e422
|
@ -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,8 +16,11 @@ 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
|
||||||
{
|
{
|
||||||
|
@ -31,6 +35,8 @@ 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()
|
||||||
{
|
{
|
||||||
|
@ -38,7 +44,7 @@ namespace NzbDrone.Common.Test.Http
|
||||||
var mainHost = "httpbin.servarr.com";
|
var mainHost = "httpbin.servarr.com";
|
||||||
|
|
||||||
// Use mirrors for tests that use two hosts
|
// Use mirrors for tests that use two hosts
|
||||||
var candidates = new[] { "eu.httpbin.org", /* "httpbin.org", */ "www.httpbin.org" };
|
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;
|
_httpBinHost = mainHost;
|
||||||
|
@ -46,29 +52,20 @@ namespace NzbDrone.Common.Test.Http
|
||||||
|
|
||||||
TestLogger.Info($"{candidates.Length} TestSites available.");
|
TestLogger.Info($"{candidates.Length} TestSites available.");
|
||||||
|
|
||||||
_httpBinSleep = _httpBinHosts.Length < 2 ? 100 : 10;
|
_httpBinSleep = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsTestSiteAvailable(string site)
|
private bool IsTestSiteAvailable(string site)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var req = WebRequest.Create($"https://{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($"https://{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)
|
||||||
{
|
{
|
||||||
|
@ -95,10 +92,13 @@ 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>>(Array.Empty<IHttpRequestInterceptor>());
|
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(Array.Empty<IHttpRequestInterceptor>());
|
||||||
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
||||||
|
@ -138,6 +138,28 @@ 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()
|
||||||
{
|
{
|
||||||
|
@ -162,15 +184,44 @@ 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($"https://{_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);
|
var response = Subject.Get<HttpBinResource>(request);
|
||||||
|
|
||||||
response.Resource.Headers["Accept-Encoding"].ToString().Should().Be(compression);
|
response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("gzip");
|
||||||
|
|
||||||
response.Resource.Gzipped.Should().BeTrue();
|
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);
|
||||||
|
|
||||||
|
response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("br");
|
||||||
|
|
||||||
|
response.Resource.Gzipped.Should().BeFalse();
|
||||||
|
response.Resource.Brotli.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(HttpStatusCode.Unauthorized)]
|
[TestCase(HttpStatusCode.Unauthorized)]
|
||||||
|
@ -337,13 +388,38 @@ namespace NzbDrone.Common.Test.Http
|
||||||
{
|
{
|
||||||
var file = GetTempFilePath();
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
Assert.Throws<WebException>(() => Subject.DownloadFile("https://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();
|
||||||
|
|
||||||
ExceptionVerification.ExpectedWarns(1);
|
ExceptionVerification.ExpectedWarns(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_write_redirect_content_to_stream()
|
||||||
|
{
|
||||||
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
|
using (var fileStream = new FileStream(file, FileMode.Create))
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1");
|
||||||
|
request.AllowAutoRedirect = false;
|
||||||
|
request.ResponseStream = fileStream;
|
||||||
|
|
||||||
|
var response = Subject.Get(request);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.Moved);
|
||||||
|
}
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
|
||||||
|
File.Exists(file).Should().BeTrue();
|
||||||
|
|
||||||
|
var fileInfo = new FileInfo(file);
|
||||||
|
|
||||||
|
fileInfo.Length.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_send_cookie()
|
public void should_send_cookie()
|
||||||
{
|
{
|
||||||
|
@ -773,6 +849,7 @@ namespace NzbDrone.Common.Test.Http
|
||||||
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 Gzipped { get; set; }
|
||||||
|
public bool Brotli { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class HttpCookieResource
|
public class HttpCookieResource
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System.Net.Http;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,5 @@ namespace NzbDrone.Common.Http.Dispatchers
|
||||||
public interface IHttpDispatcher
|
public interface IHttpDispatcher
|
||||||
{
|
{
|
||||||
HttpResponse GetResponse(HttpRequest request, CookieContainer cookies);
|
HttpResponse GetResponse(HttpRequest request, CookieContainer cookies);
|
||||||
void DownloadFile(string url, string fileName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
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.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;
|
||||||
|
|
||||||
|
@ -14,200 +15,187 @@ 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;
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
webRequest.Method = request.Method.ToString();
|
|
||||||
webRequest.UserAgent = _userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent);
|
|
||||||
webRequest.KeepAlive = request.ConnectionKeepAlive;
|
|
||||||
webRequest.AllowAutoRedirect = false;
|
|
||||||
webRequest.CookieContainer = cookies;
|
|
||||||
|
|
||||||
if (request.RequestTimeout != TimeSpan.Zero)
|
|
||||||
{
|
{
|
||||||
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
|
requestMessage.Headers.Add("Cookie", cookieHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
webRequest.Proxy = GetProxy(request.Url);
|
using var cts = new CancellationTokenSource();
|
||||||
|
if (request.RequestTimeout != TimeSpan.Zero)
|
||||||
|
{
|
||||||
|
cts.CancelAfter(request.RequestTimeout);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The default for System.Net.Http.HttpClient
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.ContentData != null)
|
||||||
|
{
|
||||||
|
requestMessage.Content = new ByteArrayContent(request.ContentData);
|
||||||
|
}
|
||||||
|
|
||||||
if (request.Headers != null)
|
if (request.Headers != null)
|
||||||
{
|
{
|
||||||
AddRequestHeaders(webRequest, request.Headers);
|
AddRequestHeaders(requestMessage, request.Headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpWebResponse httpWebResponse;
|
var httpClient = GetClient(request.Url);
|
||||||
|
|
||||||
|
HttpResponseMessage responseMessage;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (request.ContentData != null)
|
responseMessage = httpClient.Send(requestMessage, cts.Token);
|
||||||
{
|
|
||||||
webRequest.ContentLength = request.ContentData.Length;
|
|
||||||
using (var writeStream = webRequest.GetRequestStream())
|
|
||||||
{
|
|
||||||
writeStream.Write(request.ContentData, 0, request.ContentData.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
|
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (HttpRequestException e)
|
||||||
{
|
{
|
||||||
httpWebResponse = (HttpWebResponse)e.Response;
|
_logger.Error(e, "HttpClient error");
|
||||||
|
throw;
|
||||||
if (httpWebResponse == null)
|
|
||||||
{
|
|
||||||
// The default messages for WebException on mono are pretty horrible.
|
|
||||||
if (e.Status == WebExceptionStatus.NameResolutionFailure)
|
|
||||||
{
|
|
||||||
throw new WebException($"DNS Name Resolution Failure: '{webRequest.RequestUri.Host}'", e.Status);
|
|
||||||
}
|
|
||||||
else if (e.ToString().Contains("TLS Support not"))
|
|
||||||
{
|
|
||||||
throw new TlsFailureException(webRequest, e);
|
|
||||||
}
|
|
||||||
else if (e.ToString().Contains("The authentication or decryption has failed."))
|
|
||||||
{
|
|
||||||
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);
|
return new HttpResponse(request, new HttpHeader(responseMessage.Headers), data, responseMessage.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DownloadFile(string url, string fileName)
|
protected virtual System.Net.Http.HttpClient GetClient(HttpUri uri)
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
var fileInfo = new FileInfo(fileName);
|
|
||||||
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
|
|
||||||
{
|
|
||||||
fileInfo.Directory.Create();
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
|
|
||||||
|
|
||||||
var stopWatch = Stopwatch.StartNew();
|
|
||||||
var uri = new HttpUri(url);
|
|
||||||
|
|
||||||
using (var webClient = new GZipWebClient())
|
|
||||||
{
|
|
||||||
webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent());
|
|
||||||
webClient.Proxy = GetProxy(uri);
|
|
||||||
webClient.DownloadFile(uri.FullUri, fileName);
|
|
||||||
stopWatch.Stop();
|
|
||||||
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (WebException e)
|
|
||||||
{
|
|
||||||
_logger.Warn("Failed to get response from: {0} {1}", url, e.Message);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_logger.Warn(e, "Failed to get response from: " + url);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual IWebProxy GetProxy(HttpUri uri)
|
|
||||||
{
|
|
||||||
IWebProxy proxy = null;
|
|
||||||
|
|
||||||
var proxySettings = _proxySettingsProvider.GetProxySettings(uri);
|
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,
|
||||||
|
MaxConnectionsPerServer = 12,
|
||||||
|
ConnectCallback = onConnect,
|
||||||
|
SslOptions = new SslClientAuthenticationOptions
|
||||||
|
{
|
||||||
|
RemoteCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (proxySettings != null)
|
if (proxySettings != null)
|
||||||
{
|
{
|
||||||
proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
handler.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxy;
|
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 Radarr not allowed.");
|
throw new NotSupportedException("User-Agent other than Radarr not allowed.");
|
||||||
|
@ -219,5 +207,79 @@ namespace NzbDrone.Common.Http.Dispatchers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AddContentHeader(HttpRequestMessage request, string header, string value)
|
||||||
|
{
|
||||||
|
var headers = request.Content?.Headers;
|
||||||
|
if (headers == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.Remove(header);
|
||||||
|
headers.Add(header, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
var localToken = cancellationToken;
|
||||||
|
|
||||||
|
if (!hasResolvedIPv6Availability)
|
||||||
|
{
|
||||||
|
// to make things move fast, use a very low timeout for the initial ipv6 attempt.
|
||||||
|
var quickFailCts = new CancellationTokenSource(connection_establish_timeout);
|
||||||
|
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, quickFailCts.Token);
|
||||||
|
|
||||||
|
localToken = linkedTokenSource.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await attemptConnection(AddressFamily.InterNetworkV6, context, localToken);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// very naively fallback to ipv4 permanently for this execution based on the response of the first connection attempt.
|
||||||
|
// note that this may cause users to eventually get switched to ipv4 (on a random failure when they are switching networks, for instance)
|
||||||
|
// but in the interest of keeping this implementation simple, this is acceptable.
|
||||||
|
useIPv6 = false;
|
||||||
|
}
|
||||||
|
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,8 +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.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;
|
||||||
|
@ -119,8 +121,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);
|
||||||
|
@ -187,57 +187,98 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DownloadFile(string url, string fileName)
|
public void DownloadFile(string url, string fileName)
|
||||||
{
|
{
|
||||||
_httpDispatcher.DownloadFile(url, fileName);
|
var fileNamePart = fileName + ".part";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var fileInfo = new FileInfo(fileName);
|
||||||
|
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
|
||||||
|
{
|
||||||
|
fileInfo.Directory.Create();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
|
||||||
|
|
||||||
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite))
|
||||||
|
{
|
||||||
|
var request = new HttpRequest(url);
|
||||||
|
request.AllowAutoRedirect = true;
|
||||||
|
request.ResponseStream = fileStream;
|
||||||
|
var response = Get(request);
|
||||||
|
|
||||||
|
if (response.Headers.ContentType != null && response.Headers.ContentType.Contains("text/html"))
|
||||||
|
{
|
||||||
|
throw new HttpException(request, response, "Site responded with html content.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stopWatch.Stop();
|
||||||
|
|
||||||
|
if (File.Exists(fileName))
|
||||||
|
{
|
||||||
|
File.Delete(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
File.Move(fileNamePart, fileName);
|
||||||
|
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (File.Exists(fileNamePart))
|
||||||
|
{
|
||||||
|
File.Delete(fileNamePart);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse Get(HttpRequest request)
|
public HttpResponse Get(HttpRequest request)
|
||||||
{
|
{
|
||||||
request.Method = HttpMethod.GET;
|
request.Method = HttpMethod.Get;
|
||||||
return Execute(request);
|
return Execute(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,13 +292,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,30 @@
|
||||||
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;
|
||||||
|
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 +32,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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
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;
|
||||||
|
@ -11,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;
|
||||||
|
@ -49,6 +52,7 @@ namespace NzbDrone.Common.Http
|
||||||
public TimeSpan RequestTimeout { get; set; }
|
public TimeSpan RequestTimeout { get; set; }
|
||||||
public TimeSpan RateLimit { get; set; }
|
public TimeSpan RateLimit { get; set; }
|
||||||
public string RateLimitKey { get; set; }
|
public string RateLimitKey { get; set; }
|
||||||
|
public Stream ResponseStream { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ namespace NzbDrone.Common.Http
|
||||||
{
|
{
|
||||||
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>();
|
||||||
|
@ -271,7 +272,7 @@ namespace NzbDrone.Common.Http
|
||||||
|
|
||||||
public virtual HttpRequestBuilder Post()
|
public virtual HttpRequestBuilder Post()
|
||||||
{
|
{
|
||||||
Method = HttpMethod.POST;
|
Method = HttpMethod.Post;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -362,7 +363,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.");
|
||||||
}
|
}
|
||||||
|
@ -378,7 +379,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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ using NzbDrone.Common.TPL;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
using NzbDrone.Core.Http;
|
using NzbDrone.Core.Http;
|
||||||
using NzbDrone.Core.Parser;
|
using NzbDrone.Core.Parser;
|
||||||
|
using NzbDrone.Core.Security;
|
||||||
using NzbDrone.Test.Common;
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Test.Framework
|
namespace NzbDrone.Core.Test.Framework
|
||||||
|
@ -25,7 +26,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(Array.Empty<IHttpRequestInterceptor>(), Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), TestLogger));
|
Mocker.SetConstant<IHttpClient>(new HttpClient(Array.Empty<IHttpRequestInterceptor>(), Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), TestLogger));
|
||||||
Mocker.SetConstant<IRadarrCloudRequestBuilder>(new RadarrCloudRequestBuilder());
|
Mocker.SetConstant<IRadarrCloudRequestBuilder>(new RadarrCloudRequestBuilder());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,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;
|
||||||
|
@ -41,7 +42,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();
|
||||||
|
|
|
@ -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.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();
|
||||||
|
|
|
@ -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,4 +1,5 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
@ -34,11 +35,11 @@ namespace NzbDrone.Core.Test.IndexerTests.PTPTests
|
||||||
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(), authStream.ToString()));
|
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), authStream.ToString()));
|
||||||
|
|
||||||
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 { ContentType = HttpAccept.Json.Value }, responseJson));
|
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader { ContentType = HttpAccept.Json.Value }, 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;
|
||||||
|
@ -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();
|
||||||
|
@ -62,7 +63,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();
|
||||||
|
@ -74,7 +75,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,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;
|
||||||
|
@ -42,7 +43,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();
|
||||||
|
@ -71,7 +72,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();
|
||||||
|
@ -101,7 +102,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();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
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;
|
||||||
|
@ -142,15 +143,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;
|
||||||
|
@ -163,7 +168,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;
|
||||||
|
@ -25,7 +26,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;
|
||||||
|
@ -107,7 +108,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);
|
||||||
}
|
}
|
||||||
|
@ -180,7 +181,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;
|
||||||
}
|
}
|
||||||
|
@ -189,7 +190,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);
|
||||||
}
|
}
|
||||||
|
@ -198,7 +199,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>
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Cloud;
|
using NzbDrone.Common.Cloud;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
|
@ -53,7 +54,7 @@ namespace NzbDrone.Core.ImportLists.TMDb.User
|
||||||
.SetSegment("secondaryRoute", "request_token")
|
.SetSegment("secondaryRoute", "request_token")
|
||||||
.AddQueryParam("redirect_to", query["callbackUrl"]);
|
.AddQueryParam("redirect_to", query["callbackUrl"]);
|
||||||
|
|
||||||
requestBuilder.Method = HttpMethod.POST;
|
requestBuilder.Method = HttpMethod.Post;
|
||||||
|
|
||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ namespace NzbDrone.Core.ImportLists.TMDb.User
|
||||||
.SetSegment("secondaryRoute", "access_token")
|
.SetSegment("secondaryRoute", "access_token")
|
||||||
.AddQueryParam("request_token", query["requestToken"]);
|
.AddQueryParam("request_token", query["requestToken"]);
|
||||||
|
|
||||||
requestBuilder.Method = HttpMethod.POST;
|
requestBuilder.Method = HttpMethod.Post;
|
||||||
|
|
||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
|
@ -53,7 +54,7 @@ namespace NzbDrone.Core.ImportLists.TMDb.User
|
||||||
|
|
||||||
requestBuilder.Accept(HttpAccept.Json);
|
requestBuilder.Accept(HttpAccept.Json);
|
||||||
|
|
||||||
requestBuilder.Method = HttpMethod.GET;
|
requestBuilder.Method = HttpMethod.Get;
|
||||||
|
|
||||||
var jsonResponse = JsonConvert.DeserializeObject<MovieSearchResource>(HttpClient.Execute(requestBuilder.Build()).Content);
|
var jsonResponse = JsonConvert.DeserializeObject<MovieSearchResource>(HttpClient.Execute(requestBuilder.Build()).Content);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using NzbDrone.Core.Notifications.Trakt;
|
using NzbDrone.Core.Notifications.Trakt;
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.List
|
||||||
var listName = Parser.Parser.ToUrlSlug(Settings.Listname.Trim());
|
var listName = Parser.Parser.ToUrlSlug(Settings.Listname.Trim());
|
||||||
link += $"users/{Settings.Username.Trim()}/lists/{listName}/items/movies?limit={Settings.Limit}";
|
link += $"users/{Settings.Username.Trim()}/lists/{listName}/items/movies?limit={Settings.Limit}";
|
||||||
|
|
||||||
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.GET, Settings.AccessToken));
|
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.Get, Settings.AccessToken));
|
||||||
|
|
||||||
yield return request;
|
yield return request;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using NzbDrone.Core.Notifications.Trakt;
|
using NzbDrone.Core.Notifications.Trakt;
|
||||||
|
|
||||||
|
@ -71,7 +72,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.Popular
|
||||||
|
|
||||||
link += filtersAndLimit;
|
link += filtersAndLimit;
|
||||||
|
|
||||||
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.GET, Settings.AccessToken));
|
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.Get, Settings.AccessToken));
|
||||||
|
|
||||||
yield return request;
|
yield return request;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using NzbDrone.Core.Notifications.Trakt;
|
using NzbDrone.Core.Notifications.Trakt;
|
||||||
|
@ -42,7 +43,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.User
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.GET, Settings.AccessToken));
|
var request = new ImportListRequest(_traktProxy.BuildTraktRequest(link, HttpMethod.Get, Settings.AccessToken));
|
||||||
|
|
||||||
yield return request;
|
yield return request;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using NzbDrone.Common.Serializer;
|
using NzbDrone.Common.Serializer;
|
||||||
|
@ -60,7 +61,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;
|
||||||
|
|
|
@ -49,11 +49,6 @@ namespace NzbDrone.Core.Messaging.Commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ThreadAbortException ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex, "Thread aborted");
|
|
||||||
Thread.ResetAbort();
|
|
||||||
}
|
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
_logger.Trace("Stopped one command execution pipeline");
|
_logger.Trace("Stopped one command execution pipeline");
|
||||||
|
|
|
@ -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());
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -27,7 +28,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
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,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 NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
|
@ -37,7 +38,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> GetMovieSections(PlexServerSettings settings)
|
public List<PlexSection> GetMovieSections(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 UpdateMovie(int metadataId, PlexServerSettings settings)
|
public void UpdateMovie(int 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.imdb://{imdbId}?lang={language}";
|
var guid = $"com.plexapp.agents.imdb://{imdbId}?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,7 +101,7 @@ 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.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
||||||
|
|
||||||
var response = _httpClient.Execute(request);
|
var response = _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(TraktCollectMoviesResource payload, string accessToken)
|
public void AddToCollection(TraktCollectMoviesResource 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(TraktCollectMoviesResource payload, string accessToken)
|
public void RemoveFromCollection(TraktCollectMoviesResource 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";
|
||||||
request.SetContent(payload.ToJson());
|
request.SetContent(payload.ToJson());
|
||||||
|
@ -71,7 +72,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
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
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,7 +28,13 @@ 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());
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,12 @@ 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,16 @@ 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;
|
if (sender is not SslStream request)
|
||||||
|
|
||||||
if (request == null)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cert2 = certificate as X509Certificate2;
|
if (certificate is X509Certificate2 cert2 && cert2.SignatureAlgorithm.FriendlyName == "md5RSA")
|
||||||
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);
|
_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.TargetHostName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sslPolicyErrors == SslPolicyErrors.None)
|
if (sslPolicyErrors == SslPolicyErrors.None)
|
||||||
|
@ -41,12 +37,12 @@ namespace NzbDrone.Core.Security
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.RequestUri.Host == "localhost" || request.RequestUri.Host == "127.0.0.1")
|
if (request.TargetHostName == "localhost" || request.TargetHostName == "127.0.0.1")
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ipAddresses = GetIPAddresses(request.RequestUri.Host);
|
var ipAddresses = GetIPAddresses(request.TargetHostName);
|
||||||
var certificateValidation = _configService.CertificateValidation;
|
var certificateValidation = _configService.CertificateValidation;
|
||||||
|
|
||||||
if (certificateValidation == CertificateValidationType.Disabled)
|
if (certificateValidation == CertificateValidationType.Disabled)
|
||||||
|
@ -60,7 +56,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}", request.TargetHostName, sslPolicyErrors);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -74,10 +70,5 @@ namespace NzbDrone.Core.Security
|
||||||
|
|
||||||
return Dns.GetHostEntry(host).AddressList;
|
return Dns.GetHostEntry(host).AddressList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(ApplicationStartedEvent message)
|
|
||||||
{
|
|
||||||
ServicePointManager.ServerCertificateValidationCallback = ShouldByPassValidationError;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
@ -37,7 +38,7 @@ namespace TinyTwitter
|
||||||
|
|
||||||
public void UpdateStatus(string message)
|
public void UpdateStatus(string message)
|
||||||
{
|
{
|
||||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
new RequestBuilder(_oauth, HttpMethod.Post, "https://api.twitter.com/1.1/statuses/update.json")
|
||||||
.AddParameter("status", message)
|
.AddParameter("status", message)
|
||||||
.Execute();
|
.Execute();
|
||||||
}
|
}
|
||||||
|
@ -51,7 +52,7 @@ namespace TinyTwitter
|
||||||
**/
|
**/
|
||||||
public void DirectMessage(string message, string screenName)
|
public void DirectMessage(string message, string screenName)
|
||||||
{
|
{
|
||||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
new RequestBuilder(_oauth, HttpMethod.Post, "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||||
.AddParameter("text", message)
|
.AddParameter("text", message)
|
||||||
.AddParameter("screen_name", screenName)
|
.AddParameter("screen_name", screenName)
|
||||||
.Execute();
|
.Execute();
|
||||||
|
@ -63,16 +64,18 @@ namespace TinyTwitter
|
||||||
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
||||||
|
|
||||||
private readonly OAuthInfo _oauth;
|
private readonly OAuthInfo _oauth;
|
||||||
private readonly string _method;
|
private readonly HttpMethod _method;
|
||||||
private readonly IDictionary<string, string> _customParameters;
|
private readonly IDictionary<string, string> _customParameters;
|
||||||
private readonly string _url;
|
private readonly string _url;
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
|
||||||
public RequestBuilder(OAuthInfo oauth, string method, string url)
|
public RequestBuilder(OAuthInfo oauth, HttpMethod method, string url)
|
||||||
{
|
{
|
||||||
_oauth = oauth;
|
_oauth = oauth;
|
||||||
_method = method;
|
_method = method;
|
||||||
_url = url;
|
_url = url;
|
||||||
_customParameters = new Dictionary<string, string>();
|
_customParameters = new Dictionary<string, string>();
|
||||||
|
_httpClient = new ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public RequestBuilder AddParameter(string name, string value)
|
public RequestBuilder AddParameter(string name, string value)
|
||||||
|
@ -92,61 +95,13 @@ namespace TinyTwitter
|
||||||
var signature = GenerateSignature(parameters);
|
var signature = GenerateSignature(parameters);
|
||||||
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
||||||
|
|
||||||
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
|
var request = new HttpRequestMessage(_method, _url);
|
||||||
request.Method = _method;
|
request.Content = new FormUrlEncodedContent(_customParameters);
|
||||||
request.ContentType = "application/x-www-form-urlencoded";
|
|
||||||
|
|
||||||
request.Headers.Add("Authorization", headerValue);
|
request.Headers.Add("Authorization", headerValue);
|
||||||
|
|
||||||
WriteRequestBody(request);
|
var response = _httpClient.Send(request);
|
||||||
|
return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||||
// 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)
|
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System.Linq;
|
using System;
|
||||||
using System.Net;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
@ -8,25 +9,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 = client.ResponseHeaders;
|
var headers = response.Headers;
|
||||||
|
|
||||||
headers.Get("Cache-Control").Split(',').Select(x => x.Trim())
|
headers.CacheControl.NoStore.Should().BeTrue();
|
||||||
.Should().BeEquivalentTo("no-store, no-cache".Split(',').Select(x => x.Trim()));
|
headers.CacheControl.NoCache.Should().BeTrue();
|
||||||
headers.Get("Pragma").Should().Be("no-cache");
|
headers.Pragma.Should().Contain(new NameValueHeaderValue("no-cache"));
|
||||||
headers.Get("Expires").Should().Be("-1");
|
|
||||||
|
response.Content.Headers.Expires.Should().BeBefore(DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue