core: fix cookie parsing (part 2) (#8150)

* core: fix cookie parsing (part 2)

After fixing cookie storage in #8133 I noticed that I still have a lot of '.json.bak' files in the Jackett configuration folder. After deleting them they were created again in each request. The cause was we were parsing bad the cookies with '=' character in the value. Most Cloudflare cookies include if so we were sending bad cookies and solving the callenge in each request.

This PR should increase performance in several ways: we are not solving the challenge again (it takes time), we are not making extra requests and we are not updating the Jackett configuration in each request (both files '.json' and '.json.bak').

Tested with the client HttpWebClient2NetCore only. Please do some tests with the site 1337x.
This commit is contained in:
Diego Heras
2020-04-13 06:22:50 +02:00
committed by GitHub
parent 10737431aa
commit 9cbc78b1c2
7 changed files with 183 additions and 80 deletions

View File

@@ -179,26 +179,16 @@ namespace Jackett.Common.Utils.Clients
// clear cookies from cookiecontainer
var oldCookies = cookies.GetCookies(request.RequestUri);
foreach (Cookie oldCookie in oldCookies)
{
oldCookie.Expired = true;
}
if (!string.IsNullOrEmpty(webRequest.Cookies))
// add cookies to cookiecontainer
if (!string.IsNullOrWhiteSpace(webRequest.Cookies))
{
// add cookies to cookiecontainer
var cookieUrl = new Uri(request.RequestUri.Scheme + "://" + request.RequestUri.Host); // don't include the path, Scheme is needed for mono compatibility
foreach (var ccookiestr in webRequest.Cookies.Split(';'))
{
var cookiestrparts = ccookiestr.Split('=');
var name = cookiestrparts[0].Trim();
if (string.IsNullOrWhiteSpace(name))
continue;
var value = "";
if (cookiestrparts.Length >= 2)
value = cookiestrparts[1].Trim();
var cookie = new Cookie(name, value);
cookies.Add(cookieUrl, cookie);
}
// don't include the path, Scheme is needed for mono compatibility
var cookieUrl = new Uri(request.RequestUri.Scheme + "://" + request.RequestUri.Host);
var cookieDictionary = CookieUtil.CookieHeaderToDictionary(webRequest.Cookies);
foreach (var kv in cookieDictionary)
cookies.Add(cookieUrl, new Cookie(kv.Key, kv.Value));
}
if (webRequest.Headers != null)
@@ -254,7 +244,7 @@ namespace Jackett.Common.Utils.Clients
}
// some cloudflare clients are using a refresh header
// Pull it out manually
// Pull it out manually
if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && response.Headers.Contains("Refresh"))
{
var refreshHeaders = response.Headers.GetValues("Refresh");