From 0ec54906c6256d7b762d055f2831e4bb3b588264 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Mon, 20 Feb 2023 13:54:38 +0200 Subject: [PATCH] Fixed: (Caridgann) Custom headers in login and download blocks --- .../Definitions/Cardigann/CardigannBase.cs | 3 +- .../Cardigann/CardigannDefinition.cs | 2 + .../Cardigann/CardigannRequestGenerator.cs | 65 ++++++++++--------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs index af6637b26..f4e98c1be 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs @@ -763,8 +763,7 @@ namespace NzbDrone.Core.Indexers.Cardigann return data; } - protected Dictionary ParseCustomHeaders(Dictionary> customHeaders, - Dictionary variables) + protected Dictionary ParseCustomHeaders(Dictionary> customHeaders, Dictionary variables) { if (customHeaders == null) { diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannDefinition.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannDefinition.cs index 491bae1d3..ff53b3df6 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannDefinition.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannDefinition.cs @@ -102,6 +102,7 @@ namespace NzbDrone.Core.Indexers.Cardigann public List Error { get; set; } public PageTestBlock Test { get; set; } public CaptchaBlock Captcha { get; set; } + public Dictionary> Headers { get; set; } } public class ErrorBlock @@ -182,6 +183,7 @@ namespace NzbDrone.Core.Indexers.Cardigann public string Method { get; set; } public BeforeBlock Before { get; set; } public InfohashBlock Infohash { get; set; } + public Dictionary> Headers { get; set; } } public class InfohashBlock diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs index 0742c98aa..c9e90a653 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannRequestGenerator.cs @@ -193,6 +193,9 @@ namespace NzbDrone.Core.Indexers.Cardigann { var login = _definition.Login; + var variables = GetBaseTemplateVariables(); + var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables); + if (login.Method == "post") { var pairs = new Dictionary(); @@ -221,7 +224,15 @@ namespace NzbDrone.Core.Indexers.Cardigann requestBuilder.AddFormParameter(pair.Key, pair.Value); } + Cookies = null; + if (login.Cookies != null) + { + Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies)); + } + var request = requestBuilder + .SetCookies(Cookies ?? new Dictionary()) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", SiteLink) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -241,13 +252,9 @@ namespace NzbDrone.Core.Indexers.Cardigann var queryCollection = new NameValueCollection(); var pairs = new Dictionary(); - var formSelector = login.Form; - if (formSelector == null) - { - formSelector = "form"; - } + var formSelector = login.Form ?? "form"; - // landingResultDocument might not be initiated if the login is caused by a relogin during a query + // landingResultDocument might not be initiated if the login is caused by a re-login during a query if (landingResultDocument == null) { await GetConfigurationForSetup(true); @@ -279,11 +286,7 @@ namespace NzbDrone.Core.Indexers.Cardigann continue; } - var value = input.GetAttribute("value"); - if (value == null) - { - value = ""; - } + var value = input.GetAttribute("value") ?? ""; pairs[name] = value; } @@ -364,6 +367,7 @@ namespace NzbDrone.Core.Indexers.Cardigann var request = requestBuilder .SetCookies(Cookies) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", loginUrl) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -406,7 +410,6 @@ namespace NzbDrone.Core.Indexers.Cardigann var enctype = form.GetAttribute("enctype"); if (enctype == "multipart/form-data") { - var headers = new Dictionary(); var boundary = "---------------------------" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString().Replace(".", ""); var bodyParts = new List(); @@ -437,13 +440,9 @@ namespace NzbDrone.Core.Indexers.Cardigann requestBuilder.AddFormParameter(pair.Key, pair.Value); } - foreach (var header in headers) - { - requestBuilder.SetHeader(header.Key, header.Value); - } - var request = requestBuilder .SetCookies(Cookies) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", SiteLink) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -470,6 +469,7 @@ namespace NzbDrone.Core.Indexers.Cardigann var request = requestBuilder .SetCookies(Cookies) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", loginUrl) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -509,6 +509,7 @@ namespace NzbDrone.Core.Indexers.Cardigann }; var request = requestBuilder + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", SiteLink) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -537,6 +538,7 @@ namespace NzbDrone.Core.Indexers.Cardigann }; var request = requestBuilder + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", SiteLink) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -551,7 +553,7 @@ namespace NzbDrone.Core.Indexers.Cardigann } else { - throw new NotImplementedException("Login method " + login.Method + " not implemented"); + throw new NotImplementedException($"Login method {login.Method} not implemented"); } } @@ -596,15 +598,11 @@ namespace NzbDrone.Core.Indexers.Cardigann return null; } + var variables = GetBaseTemplateVariables(); + var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables); + var loginUrl = ResolvePath(login.Path); - Cookies = null; - - if (login.Cookies != null) - { - Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies)); - } - var requestBuilder = new HttpRequestBuilder(loginUrl.AbsoluteUri) { LogResponseContent = true, @@ -612,12 +610,15 @@ namespace NzbDrone.Core.Indexers.Cardigann Encoding = _encoding }; - if (Cookies != null) + Cookies = null; + if (login.Cookies != null) { - requestBuilder.SetCookies(Cookies); + Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies)); } var request = requestBuilder + .SetCookies(Cookies ?? new Dictionary()) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", SiteLink) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -653,6 +654,9 @@ namespace NzbDrone.Core.Indexers.Cardigann { var captcha = login.Captcha; + var variables = GetBaseTemplateVariables(); + var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables); + if (captcha.Type == "image") { var captchaElement = landingResultDocument.QuerySelector(captcha.Selector); @@ -663,6 +667,7 @@ namespace NzbDrone.Core.Indexers.Cardigann var request = new HttpRequestBuilder(captchaUrl.ToString()) .SetCookies(landingResult.GetCookies()) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", loginUrl.AbsoluteUri) .SetEncoding(_encoding) .WithRateLimit(_rateLimit.TotalSeconds) @@ -757,8 +762,11 @@ namespace NzbDrone.Core.Indexers.Cardigann } } + var headers = ParseCustomHeaders(_definition.Download?.Headers ?? _definition.Search?.Headers, variables); + var httpRequest = httpRequestBuilder .SetCookies(Cookies ?? new Dictionary()) + .SetHeaders(headers ?? new Dictionary()) .SetHeader("Referer", referer) .WithRateLimit(_rateLimit.TotalSeconds) .Build(); @@ -773,11 +781,10 @@ namespace NzbDrone.Core.Indexers.Cardigann { Cookies = GetCookies(); var method = HttpMethod.Get; - var headers = new Dictionary(); var variables = GetBaseTemplateVariables(); AddTemplateVariablesFromUri(variables, link, ".DownloadUri"); - headers = ParseCustomHeaders(_definition.Search?.Headers, variables); + var headers = ParseCustomHeaders(_definition.Download?.Headers ?? _definition.Search?.Headers, variables); if (_definition.Download != null) {