2020-04-13 04:22:50 +00:00
|
|
|
using System;
|
2022-07-30 17:12:38 +00:00
|
|
|
using System.Collections;
|
2020-04-13 04:22:50 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-07-30 17:12:38 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Reflection;
|
2020-04-13 04:22:50 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace Jackett.Common.Utils
|
|
|
|
{
|
|
|
|
public static class CookieUtil
|
|
|
|
{
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
|
|
|
|
// NOTE: we are not checking non-ascii characters and we should
|
|
|
|
private static readonly Regex _CookieRegex = new Regex(@"([^\(\)<>@,;:\\""/\[\]\?=\{\}\s]+)=([^,;\\""\s]+)");
|
2021-05-16 18:13:54 +00:00
|
|
|
private static readonly char[] InvalidKeyChars = { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t', '\n' };
|
|
|
|
private static readonly char[] InvalidValueChars = { '"', ',', ';', '\\', ' ', '\t', '\n' };
|
2020-04-13 04:22:50 +00:00
|
|
|
|
|
|
|
public static Dictionary<string, string> CookieHeaderToDictionary(string cookieHeader)
|
|
|
|
{
|
|
|
|
var cookieDictionary = new Dictionary<string, string>();
|
|
|
|
if (cookieHeader == null)
|
|
|
|
return cookieDictionary;
|
|
|
|
var matches = _CookieRegex.Match(cookieHeader);
|
|
|
|
while (matches.Success)
|
|
|
|
{
|
|
|
|
if (matches.Groups.Count > 2)
|
|
|
|
cookieDictionary[matches.Groups[1].Value] = matches.Groups[2].Value;
|
|
|
|
matches = matches.NextMatch();
|
|
|
|
}
|
|
|
|
return cookieDictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string CookieDictionaryToHeader(Dictionary<string, string> cookieDictionary)
|
|
|
|
{
|
|
|
|
if (cookieDictionary == null)
|
|
|
|
return "";
|
|
|
|
foreach (var kv in cookieDictionary)
|
|
|
|
if (kv.Key.IndexOfAny(InvalidKeyChars) > -1 || kv.Value.IndexOfAny(InvalidValueChars) > -1)
|
|
|
|
throw new FormatException($"The cookie '{kv.Key}={kv.Value}' is malformed.");
|
|
|
|
return string.Join("; ", cookieDictionary.Select(kv => kv.Key + "=" + kv.Value));
|
|
|
|
}
|
2022-07-30 17:12:38 +00:00
|
|
|
|
|
|
|
/// <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 |
|
2023-04-02 15:57:30 +00:00
|
|
|
BindingFlags.Instance, null, cookieJar, Array.Empty<object>());
|
2022-07-30 17:12:38 +00:00
|
|
|
foreach (var key in table.Keys)
|
2022-07-31 01:11:39 +00:00
|
|
|
{
|
|
|
|
var domain = (string)key;
|
|
|
|
if (domain.StartsWith("."))
|
|
|
|
domain = domain.Substring(1);
|
|
|
|
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"http://{domain}")))
|
|
|
|
cookie.Expired = true;
|
|
|
|
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"https://{domain}")))
|
|
|
|
cookie.Expired = true;
|
|
|
|
}
|
2022-07-30 17:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 04:22:50 +00:00
|
|
|
}
|
|
|
|
}
|