Increase the number of cookies per domain and remove old cookies (#13418)

- Max number of cookies per domain is just 20 by default -> now 100
- When the indexer domain changes, old cookies where kept in memory
This commit is contained in:
Diego Heras
2022-07-30 19:12:38 +02:00
committed by GitHub
parent 3cf64cab8a
commit 4dfe9d91ff
4 changed files with 51 additions and 7 deletions

View File

@@ -66,7 +66,10 @@ namespace Jackett.Common.Utils.Clients
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
var cookies = new CookieContainer();
var cookies = new CookieContainer
{
PerDomainCapacity = 100 // By default only 20 cookies are allowed per domain
};
if (!string.IsNullOrWhiteSpace(webRequest.Cookies))
{
// don't include the path, Scheme is needed for mono compatibility

View File

@@ -31,7 +31,10 @@ namespace Jackett.Common.Utils.Clients
c: c,
sc: sc)
{
cookies = new CookieContainer();
cookies = new CookieContainer
{
PerDomainCapacity = 100 // By default only 20 cookies are allowed per domain
};
CreateClient();
}
@@ -112,12 +115,10 @@ namespace Jackett.Common.Utils.Clients
request.Headers.ExpectContinue = false;
request.RequestUri = new Uri(webRequest.Url);
// clear cookies from cookiecontainer
var oldCookies = cookies.GetCookies(request.RequestUri);
foreach (Cookie oldCookie in oldCookies)
oldCookie.Expired = true;
// clear all the cookies from CookieContainer
CookieUtil.RemoveAllCookies(cookies);
// add cookies to cookiecontainer
// add cookies to CookieContainer
if (!string.IsNullOrWhiteSpace(webRequest.Cookies))
{
// don't include the path, Scheme is needed for mono compatibility

View File

@@ -1,6 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Jackett.Common.Utils
@@ -37,5 +40,25 @@ namespace Jackett.Common.Utils
throw new FormatException($"The cookie '{kv.Key}={kv.Value}' is malformed.");
return string.Join("; ", cookieDictionary.Select(kv => kv.Key + "=" + kv.Value));
}
/// <summary>
/// Remove all the cookies from a CookieContainer. That includes all domains and protocols.
/// </summary>
/// <param name="cookieJar">A cookie container</param>
public static void RemoveAllCookies(CookieContainer cookieJar)
{
var table = (Hashtable)cookieJar
.GetType()
.InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField |
BindingFlags.Instance, null, cookieJar, new object[] { });
foreach (var key in table.Keys)
{
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"http://{key}")))
cookie.Expired = true;
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"https://{key}")))
cookie.Expired = true;
}
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Jackett.Common.Utils;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;
@@ -100,5 +101,21 @@ namespace Jackett.Test.Common.Utils
var expectedCookieHeader = "";
CollectionAssert.AreEqual(expectedCookieHeader, CookieUtil.CookieDictionaryToHeader(null));
}
[Test]
public void RemoveAllCookies()
{
var cookiesContainer = new CookieContainer();
var domainHttp = new Uri("http://testdomain1.com");
cookiesContainer.Add(domainHttp, new Cookie("cookie1", "value1"));
var domainHttps = new Uri("https://testdomain2.com");
cookiesContainer.Add(domainHttps, new Cookie("cookie2", "value2"));
Assert.AreEqual(1, cookiesContainer.GetCookies(domainHttp).Count);
Assert.AreEqual(1, cookiesContainer.GetCookies(domainHttps).Count);
CookieUtil.RemoveAllCookies(cookiesContainer);
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttp).Count);
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttps).Count);
}
}
}