/
CookieUtil.cs
72 lines (66 loc) · 3.13 KB
/
CookieUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using NLog;
namespace Jackett.Common.Utils
{
public static class CookieUtil
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
// 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]+)");
private static readonly char[] InvalidKeyChars = { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t', '\n' };
private static readonly char[] InvalidValueChars = { '"', ',', ';', '\\', ' ', '\t', '\n' };
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));
}
/// <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)
try
{
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;
}
catch (Exception)
{
logger.Warn("Unable to delete the cookies of domain: " + key);
}
}
}
}