Skip to content

Commit

Permalink
core: refactor http webclient part 16 #8529
Browse files Browse the repository at this point in the history
Rename RequestWithCookiesAsync method
  • Loading branch information
ngosang committed Sep 21, 2020
1 parent 5ad1c9c commit 621a473
Show file tree
Hide file tree
Showing 58 changed files with 123 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/Abstract/AvistazTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private async Task RenewalTokenAsync()
{ "password", configData.Password.Value.Trim() },
{ "pid", configData.Pid.Value.Trim() }
};
var result = await WebRequestWithCookiesAsync(AuthUrl, method: RequestType.POST, data: body, headers: AuthHeaders);
var result = await RequestWithCookiesAsync(AuthUrl, method: RequestType.POST, data: body, headers: AuthHeaders);
var json = JObject.Parse(result.ContentString);
_token = json.Value<string>("token");
if (_token == null)
Expand Down
10 changes: 5 additions & 5 deletions src/Jackett.Common/Indexers/Abstract/XtremeZoneTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private async Task RenewalTokenAsync()
{ "password", configData.Password.Value.Trim() }
};
var jsonData = JsonConvert.SerializeObject(body);
var result = await WebRequestWithCookiesAsync(
var result = await RequestWithCookiesAsync(
LoginUrl, method: RequestType.POST, headers: ApiHeaders, rawbody: jsonData);
var json = JObject.Parse(result.ContentString);
_token = json.Value<string>("token");
Expand Down Expand Up @@ -107,11 +107,11 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
await RenewalTokenAsync();

var searchUrl = SearchUrl + "?" + qc.GetQueryString();
var response = await WebRequestWithCookiesAsync(searchUrl, headers: GetSearchHeaders());
var response = await RequestWithCookiesAsync(searchUrl, headers: GetSearchHeaders());
if (response.Status == HttpStatusCode.Unauthorized)
{
await RenewalTokenAsync(); // re-login
response = await WebRequestWithCookiesAsync(searchUrl, headers: GetSearchHeaders());
response = await RequestWithCookiesAsync(searchUrl, headers: GetSearchHeaders());
}
else if (response.Status != HttpStatusCode.OK)
throw new Exception($"Unknown error in search: {response.ContentString}");
Expand Down Expand Up @@ -169,11 +169,11 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer

public override async Task<byte[]> Download(Uri link)
{
var response = await WebRequestWithCookiesAsync(link.ToString(), headers: GetSearchHeaders());
var response = await RequestWithCookiesAsync(link.ToString(), headers: GetSearchHeaders());
if (response.Status == HttpStatusCode.Unauthorized)
{
await RenewalTokenAsync();
response = await WebRequestWithCookiesAsync(link.ToString(), headers: GetSearchHeaders());
response = await RequestWithCookiesAsync(link.ToString(), headers: GetSearchHeaders());
}
else if (response.Status != HttpStatusCode.OK)
throw new Exception($"Unknown error in download: {response.ContentBytes}");
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/Anidex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private IEnumerable<ReleaseInfo> ParseResult(string response)
private async Task ConfigureDDoSGuardCookie()
{
const string ddosPostUrl = "https://check.ddos-guard.net/check.js";
var response = await WebRequestWithCookiesAsync(ddosPostUrl, string.Empty);
var response = await RequestWithCookiesAsync(ddosPostUrl, string.Empty);
if (response.Status != System.Net.HttpStatusCode.OK)
throw new WebException($"Unexpected DDOS Guard response: Status: {response.Status}", WebExceptionStatus.ProtocolError);
if (response.IsRedirect)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/Anthelion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
qc.Add($"filter_cat[{cat}]", "1");

var searchUrl = BrowseUrl + "?" + qc.GetQueryString();
var results = await WebRequestWithCookiesAsync(searchUrl);
var results = await RequestWithCookiesAsync(searchUrl);
try
{
var parser = new HtmlParser();
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/AwesomeHD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
}

var searchUrl = SearchUrl + "?" + qc.GetQueryString();
var results = await WebRequestWithCookiesAsync(searchUrl);
var results = await RequestWithCookiesAsync(searchUrl);
if (string.IsNullOrWhiteSpace(results.ContentString))
throw new Exception("Empty response. Please, check the Passkey.");

Expand Down
8 changes: 4 additions & 4 deletions src/Jackett.Common/Indexers/BJShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ private async Task<List<ReleaseInfo>> ParseUserSearchAsync(TorznabQuery query)
foreach (var cat in MapTorznabCapsToTrackers(query))
queryCollection.Add("filter_cat[" + cat + "]", "1");
searchUrl += "?" + queryCollection.GetQueryString();
var results = await WebRequestWithCookiesAsync(searchUrl);
var results = await RequestWithCookiesAsync(searchUrl);
if (IsSessionIsClosed(results))
{
// re-login
await ApplyConfiguration(null);
results = await WebRequestWithCookiesAsync(searchUrl);
results = await RequestWithCookiesAsync(searchUrl);
}

try
Expand Down Expand Up @@ -389,12 +389,12 @@ private async Task<List<ReleaseInfo>> ParseUserSearchAsync(TorznabQuery query)
private async Task<List<ReleaseInfo>> ParseLast24HoursAsync()
{
var releases = new List<ReleaseInfo>();
var results = await WebRequestWithCookiesAsync(TodayUrl);
var results = await RequestWithCookiesAsync(TodayUrl);
if (IsSessionIsClosed(results))
{
// re-login
await ApplyConfiguration(null);
results = await WebRequestWithCookiesAsync(TodayUrl);
results = await RequestWithCookiesAsync(TodayUrl);
}

try
Expand Down
4 changes: 2 additions & 2 deletions src/Jackett.Common/Indexers/BakaBT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ private string GetCategoryName(IElement row)

public override async Task<byte[]> Download(Uri link)
{
var downloadPage = await WebRequestWithCookiesAsync(link.ToString());
var downloadPage = await RequestWithCookiesAsync(link.ToString());
var parser = new HtmlParser();
var dom = parser.ParseDocument(downloadPage.ContentString);
var downloadLink = dom.QuerySelectorAll(".download_link").First().GetAttribute("href");

if (string.IsNullOrWhiteSpace(downloadLink))
throw new Exception("Unable to find download link.");

var response = await WebRequestWithCookiesAsync(SiteLink + downloadLink);
var response = await RequestWithCookiesAsync(SiteLink + downloadLink);
return response.ContentBytes;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Jackett.Common/Indexers/BaseIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ protected async Task<byte[]> Download(Uri link, RequestType method, string refer
{
try
{
return await WebRequestWithCookiesAsync(
return await RequestWithCookiesAsync(
url, cookieOverride, method, referer, data, headers, rawbody, emulateBrowser);
}
catch (Exception e)
Expand All @@ -438,7 +438,7 @@ protected async Task<byte[]> Download(Uri link, RequestType method, string refer
throw lastException;
}

protected virtual async Task<WebResult> WebRequestWithCookiesAsync(
protected virtual async Task<WebResult> RequestWithCookiesAsync(
string url, string cookieOverride = null, RequestType method = RequestType.GET,
string referer = null, IEnumerable<KeyValuePair<string, string>> data = null,
Dictionary<string, string> headers = null, string rawbody = null, bool? emulateBrowser = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/BitHDTV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BitHDTV(IIndexerConfigurationService configService, WebClient w, Logger l
public override async Task<ConfigurationData> GetConfigurationForSetup()
{
var result = configData;
var loginPage = await WebRequestWithCookiesAsync(LoginUrl, configData.CookieHeader.Value);
var loginPage = await RequestWithCookiesAsync(LoginUrl, configData.CookieHeader.Value);
if (loginPage.IsRedirect)
return result; // already logged in
var parser = new HtmlParser();
Expand Down
26 changes: 13 additions & 13 deletions src/Jackett.Common/Indexers/CardigannIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,12 @@ protected async Task<bool> DoLogin()
["g-recaptcha-response"] = CaptchaConfigItem.Value
};
var ClearanceUrl = resolvePath("/cdn-cgi/l/chk_captcha?" + CloudFlareQueryCollection.GetQueryString());
var ClearanceResult = await WebRequestWithCookiesAsync(ClearanceUrl.ToString(), referer: SiteLink);
var ClearanceResult = await RequestWithCookiesAsync(ClearanceUrl.ToString(), referer: SiteLink);

if (ClearanceResult.IsRedirect) // clearance successfull
{
// request real login page again
landingResult = await WebRequestWithCookiesAsync(LoginUrl, referer: SiteLink);
landingResult = await RequestWithCookiesAsync(LoginUrl, referer: SiteLink);
var htmlParser = new HtmlParser();
landingResultDocument = htmlParser.ParseDocument(landingResult.ContentString);
}
Expand Down Expand Up @@ -653,7 +653,7 @@ protected async Task<bool> DoLogin()
if (simpleCaptchaPresent != null)
{
var captchaUrl = resolvePath("simpleCaptcha.php?numImages=1");
var simpleCaptchaResult = await WebRequestWithCookiesAsync(captchaUrl.ToString(), referer: LoginUrl);
var simpleCaptchaResult = await RequestWithCookiesAsync(captchaUrl.ToString(), referer: LoginUrl);
var simpleCaptchaJSON = JObject.Parse(simpleCaptchaResult.ContentString);
var captchaSelection = simpleCaptchaJSON["images"][0]["hash"].ToString();
pairs["captchaSelection"] = captchaSelection;
Expand Down Expand Up @@ -722,7 +722,7 @@ protected async Task<bool> DoLogin()

headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
var body = string.Join("\r\n", bodyParts);
loginResult = await WebRequestWithCookiesAsync(
loginResult = await RequestWithCookiesAsync(
submitUrl.ToString(), configData.CookieHeader.Value, RequestType.POST, SiteLink, pairs, headers,
body);
}
Expand Down Expand Up @@ -750,7 +750,7 @@ protected async Task<bool> DoLogin()

var LoginUrl = resolvePath(Login.Path + "?" + queryCollection.GetQueryString()).ToString();
configData.CookieHeader.Value = null;
var loginResult = await WebRequestWithCookiesAsync(LoginUrl, referer: SiteLink);
var loginResult = await RequestWithCookiesAsync(LoginUrl, referer: SiteLink);
configData.CookieHeader.Value = loginResult.Cookies;

checkForError(loginResult, Definition.Login.Error);
Expand All @@ -760,7 +760,7 @@ protected async Task<bool> DoLogin()
var OneUrl = applyGoTemplateText(Definition.Login.Inputs["oneurl"]);
var LoginUrl = resolvePath(Login.Path + OneUrl).ToString();
configData.CookieHeader.Value = null;
var loginResult = await WebRequestWithCookiesAsync(LoginUrl, referer: SiteLink);
var loginResult = await RequestWithCookiesAsync(LoginUrl, referer: SiteLink);
configData.CookieHeader.Value = loginResult.Cookies;

checkForError(loginResult, Definition.Login.Error);
Expand Down Expand Up @@ -794,7 +794,7 @@ protected async Task<bool> TestLogin()

// test if login was successful
var LoginTestUrl = resolvePath(Login.Test.Path).ToString();
var testResult = await WebRequestWithCookiesAsync(LoginTestUrl);
var testResult = await RequestWithCookiesAsync(LoginTestUrl);

if (testResult.IsRedirect)
{
Expand Down Expand Up @@ -887,7 +887,7 @@ public async Task<ConfigurationData> GetConfigurationForSetup(bool automaticlogi
configData.CookieHeader.Value = null;
if (Login.Cookies != null)
configData.CookieHeader.Value = string.Join("; ", Login.Cookies);
landingResult = await WebRequestWithCookiesAsync(LoginUrl.AbsoluteUri, referer: SiteLink);
landingResult = await RequestWithCookiesAsync(LoginUrl.AbsoluteUri, referer: SiteLink);

var htmlParser = new HtmlParser();
landingResultDocument = htmlParser.ParseDocument(landingResult.ContentString);
Expand Down Expand Up @@ -930,7 +930,7 @@ public async Task<ConfigurationData> GetConfigurationForSetup(bool automaticlogi
hasCaptcha = true;

var CaptchaUrl = resolvePath(captchaElement.GetAttribute("src"), LoginUrl);
var captchaImageData = await WebRequestWithCookiesAsync(
var captchaImageData = await RequestWithCookiesAsync(
CaptchaUrl.ToString(), landingResult.Cookies, referer: LoginUrl.AbsoluteUri);
var CaptchaImage = new ImageItem { Name = "Captcha Image" };
var CaptchaText = new StringItem { Name = "Captcha Text" };
Expand Down Expand Up @@ -1345,7 +1345,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
headers.Add(header.Key, header.Value[0]);
}

var response = await WebRequestWithCookiesAsync(
var response = await RequestWithCookiesAsync(
searchUrl, method: method, headers: headers, data: queryCollection);

if (response.IsRedirect && SearchPath.Followredirect)
Expand All @@ -1368,7 +1368,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
if (!LoginResult)
throw new Exception(string.Format("Relogin failed"));
await TestLogin();
response = await WebRequestWithCookiesAsync(searchUrl, method: method, data: queryCollection);
response = await RequestWithCookiesAsync(searchUrl, method: method, data: queryCollection);
if (response.IsRedirect && SearchPath.Followredirect)
await FollowIfRedirect(response);

Expand Down Expand Up @@ -1792,9 +1792,9 @@ public override async Task<byte[]> Download(Uri link)
if (Download.Selector != null)
{
var selector = applyGoTemplateText(Download.Selector, variables);
var response = await WebRequestWithCookiesAsync(link.ToString());
var response = await RequestWithCookiesAsync(link.ToString());
if (response.IsRedirect)
response = await WebRequestWithCookiesAsync(response.RedirectingTo);
response = await RequestWithCookiesAsync(response.RedirectingTo);
var results = response.ContentString;
var searchResultParser = new HtmlParser();
var searchResultDocument = searchResultParser.ParseDocument(results);
Expand Down
4 changes: 2 additions & 2 deletions src/Jackett.Common/Indexers/Cinecalidad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer

public override async Task<byte[]> Download(Uri link)
{
var results = await WebRequestWithCookiesAsync(link.ToString());
var results = await RequestWithCookiesAsync(link.ToString());

try
{
Expand All @@ -129,7 +129,7 @@ public override async Task<byte[]> Download(Uri link)
var preotectedLink = dom.QuerySelector("a[service=BitTorrent]").GetAttribute("href");
preotectedLink = SiteLink + preotectedLink.TrimStart('/');

results = await WebRequestWithCookiesAsync(preotectedLink);
results = await RequestWithCookiesAsync(preotectedLink);
dom = parser.ParseDocument(results.ContentString);
var magnetUrl = dom.QuerySelector("a[href^=magnet]").GetAttribute("href");
return await base.Download(new Uri(magnetUrl));
Expand Down
4 changes: 2 additions & 2 deletions src/Jackett.Common/Indexers/DanishBits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ protected override string GetSearchString(TorznabQuery query)
return searchString;
}

protected override async Task<WebResult> WebRequestWithCookiesAsync(
protected override async Task<WebResult> RequestWithCookiesAsync(
string url, string cookieOverride = null, RequestType method = RequestType.GET, string referer = null,
IEnumerable<KeyValuePair<string, string>> data = null, Dictionary<string, string> headers = null,
string rawbody = null, bool? emulateBrowser = null)
{
CookieHeader = null; // Download fill fail with cookies set
return await base.WebRequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers);
return await base.RequestWithCookiesAsync(url, cookieOverride, method, referer, data, headers);
}
}
}
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/DigitalCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
searchUrl += "?" + queryCollection.GetQueryString();
foreach (var cat in MapTorznabCapsToTrackers(query))
searchUrl += "&categories[]=" + cat;
var results = await WebRequestWithCookiesAsync(searchUrl, referer: SiteLink);
var results = await RequestWithCookiesAsync(searchUrl, referer: SiteLink);

try
{
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/DigitalHive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DigitalHive(IIndexerConfigurationService configService, WebClient w, Logg

public override async Task<ConfigurationData> GetConfigurationForSetup()
{
var loginPage = await WebRequestWithCookiesAsync(LoginUrl, configData.CookieHeader.Value);
var loginPage = await RequestWithCookiesAsync(LoginUrl, configData.CookieHeader.Value);
var parser = new HtmlParser();
var cq = parser.ParseDocument(loginPage.ContentString);
var recaptchaSiteKey = cq.QuerySelector(".g-recaptcha")?.GetAttribute("data-sitekey");
Expand Down
6 changes: 3 additions & 3 deletions src/Jackett.Common/Indexers/DivxTotal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
do
{
var url = SiteLink + "page/" + page + "/?" + qc.GetQueryString();
var result = await WebRequestWithCookiesAsync(url);
var result = await RequestWithCookiesAsync(url);

if (result.Status != HttpStatusCode.OK)
throw new ExceptionWithConfigData(result.ContentString, configData);
Expand Down Expand Up @@ -153,7 +153,7 @@ public override async Task<byte[]> Download(Uri link)
// for other categories we have to do another step
if (!downloadUrl.Contains(DownloadLink))
{
var result = await WebRequestWithCookiesAsync(downloadUrl);
var result = await RequestWithCookiesAsync(downloadUrl);

if (result.Status != HttpStatusCode.OK)
throw new ExceptionWithConfigData(result.ContentString, configData);
Expand Down Expand Up @@ -205,7 +205,7 @@ public override async Task<byte[]> Download(Uri link)
private async Task ParseSeriesRelease(ICollection<ReleaseInfo> releases, TorznabQuery query,
string commentsLink, string cat, DateTime publishDate)
{
var result = await WebRequestWithCookiesAsync(commentsLink);
var result = await RequestWithCookiesAsync(commentsLink);

if (result.Status != HttpStatusCode.OK)
throw new ExceptionWithConfigData(result.ContentString, configData);
Expand Down

0 comments on commit 621a473

Please sign in to comment.