diff --git a/src/Jackett.Common/Utils/Clients/HttpWebClient.cs b/src/Jackett.Common/Utils/Clients/HttpWebClient.cs
index 3f5053261..3d68cb845 100644
--- a/src/Jackett.Common/Utils/Clients/HttpWebClient.cs
+++ b/src/Jackett.Common/Utils/Clients/HttpWebClient.cs
@@ -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
diff --git a/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs b/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
index 9bea61fc7..c85bbb667 100644
--- a/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
+++ b/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
@@ -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
diff --git a/src/Jackett.Common/Utils/CookieUtil.cs b/src/Jackett.Common/Utils/CookieUtil.cs
index a228734c6..3ee7ad2b9 100644
--- a/src/Jackett.Common/Utils/CookieUtil.cs
+++ b/src/Jackett.Common/Utils/CookieUtil.cs
@@ -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));
}
+
+ ///
+ /// Remove all the cookies from a CookieContainer. That includes all domains and protocols.
+ ///
+ /// A cookie container
+ 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;
+ }
+ }
+
}
}
diff --git a/src/Jackett.Test/Common/Utils/CookieUtilTests.cs b/src/Jackett.Test/Common/Utils/CookieUtilTests.cs
index e59334dba..f0bd5ef91 100644
--- a/src/Jackett.Test/Common/Utils/CookieUtilTests.cs
+++ b/src/Jackett.Test/Common/Utils/CookieUtilTests.cs
@@ -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);
+ }
}
}