123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- #if NETFX_CORE
- using FileStream = BestHTTP.PlatformSupport.IO.FileStream;
- using Directory = BestHTTP.PlatformSupport.IO.Directory;
- using File = BestHTTP.PlatformSupport.IO.File;
- using BestHTTP.PlatformSupport.IO;
- #else
- using FileStream = System.IO.FileStream;
- using Directory = System.IO.Directory;
- using System.IO;
- #endif
- namespace BestHTTP.Cookies
- {
-
-
-
- public static class CookieJar
- {
-
- private const int Version = 1;
-
-
-
- public static bool IsSavingSupported
- {
- get
- {
- if (IsSupportCheckDone)
- return _isSavingSupported;
- try
- {
- File.Exists(HTTPManager.GetRootCacheFolder());
- _isSavingSupported = true;
- }
- catch
- {
- _isSavingSupported = false;
- HTTPManager.Logger.Warning("CookieJar", "Cookie saving and loading disabled!");
- }
- finally
- {
- IsSupportCheckDone = true;
- }
- return _isSavingSupported;
- }
- }
-
-
-
- public static TimeSpan AccessThreshold = TimeSpan.FromDays(7);
- #region Privates
-
-
-
- private static List<Cookie> Cookies = new List<Cookie>();
- private static string CookieFolder { get; set; }
- private static string LibraryPath { get; set; }
-
-
-
- private static object Locker = new object();
- private static bool _isSavingSupported;
- private static bool IsSupportCheckDone;
- private static bool Loaded;
- #endregion
- #region Internal Functions
- internal static void SetupFolder()
- {
- if (!CookieJar.IsSavingSupported)
- return;
- try
- {
- if (string.IsNullOrEmpty(CookieFolder) || string.IsNullOrEmpty(LibraryPath))
- {
- CookieFolder = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "Cookies");
- LibraryPath = System.IO.Path.Combine(CookieFolder, "Library");
- }
- }
- catch
- { }
- }
-
-
-
- internal static void Set(HTTPResponse response)
- {
- if (response == null)
- return;
- lock(Locker)
- {
- try
- {
- Maintain();
- List<Cookie> newCookies = new List<Cookie>();
- var setCookieHeaders = response.GetHeaderValues("set-cookie");
-
- if (setCookieHeaders == null)
- return;
- foreach (var cookieHeader in setCookieHeaders)
- {
- try
- {
- Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri);
- if (cookie != null)
- {
- int idx;
- var old = Find(cookie, out idx);
-
- bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();
- if (!expired)
- {
-
- if (old == null)
- {
- Cookies.Add(cookie);
- newCookies.Add(cookie);
- }
- else
- {
-
- cookie.Date = old.Date;
- Cookies[idx] = cookie;
- newCookies.Add(cookie);
- }
- }
- else if (idx != -1)
- Cookies.RemoveAt(idx);
- }
- }
- catch
- {
-
- }
- }
- response.Cookies = newCookies;
- }
- catch
- {}
- }
- }
-
-
-
- internal static void Maintain()
- {
-
-
- lock (Locker)
- {
- try
- {
- uint size = 0;
- for (int i = 0; i < Cookies.Count; )
- {
- var cookie = Cookies[i];
-
- if (!cookie.WillExpireInTheFuture() || (cookie.LastAccess + AccessThreshold) < DateTime.UtcNow)
- Cookies.RemoveAt(i);
- else
- {
- if (!cookie.IsSession)
- size += cookie.GuessSize();
- i++;
- }
- }
- if (size > HTTPManager.CookieJarSize)
- {
- Cookies.Sort();
- while (size > HTTPManager.CookieJarSize && Cookies.Count > 0)
- {
- var cookie = Cookies[0];
- Cookies.RemoveAt(0);
- size -= cookie.GuessSize();
- }
- }
- }
- catch
- { }
- }
- }
-
-
-
-
- internal static void Persist()
- {
- if (!IsSavingSupported)
- return;
- lock (Locker)
- {
- if (!Loaded)
- return;
- try
- {
-
- Maintain();
- if (!Directory.Exists(CookieFolder))
- Directory.CreateDirectory(CookieFolder);
- using (var fs = new FileStream(LibraryPath, FileMode.Create))
- using (var bw = new System.IO.BinaryWriter(fs))
- {
- bw.Write(Version);
-
- int count = 0;
- foreach (var cookie in Cookies)
- if (!cookie.IsSession)
- count++;
- bw.Write(count);
-
- foreach (var cookie in Cookies)
- if (!cookie.IsSession)
- cookie.SaveTo(bw);
- }
- }
- catch
- { }
- }
- }
-
-
-
- internal static void Load()
- {
- if (!IsSavingSupported)
- return;
- lock (Locker)
- {
- if (Loaded)
- return;
- SetupFolder();
- try
- {
- Cookies.Clear();
- if (!Directory.Exists(CookieFolder))
- Directory.CreateDirectory(CookieFolder);
- if (!File.Exists(LibraryPath))
- return;
- using (var fs = new FileStream(LibraryPath, FileMode.Open))
- using (var br = new System.IO.BinaryReader(fs))
- {
- br.ReadInt32();
- int cookieCount = br.ReadInt32();
- for (int i = 0; i < cookieCount; ++i)
- {
- Cookie cookie = new Cookie();
- cookie.LoadFrom(br);
- if (cookie.WillExpireInTheFuture())
- Cookies.Add(cookie);
- }
- }
- }
- catch
- {
- Cookies.Clear();
- }
- finally
- {
- Loaded = true;
- }
- }
- }
- #endregion
- #region Public Functions
-
-
-
- public static List<Cookie> Get(Uri uri)
- {
- lock (Locker)
- {
- Load();
- List<Cookie> result = null;
- for (int i = 0; i < Cookies.Count; ++i)
- {
- Cookie cookie = Cookies[i];
- if (cookie.WillExpireInTheFuture() && uri.Host.IndexOf(cookie.Domain) != -1 && uri.AbsolutePath.StartsWith(cookie.Path))
- {
- if (result == null)
- result = new List<Cookie>();
- result.Add(cookie);
- }
- }
- return result;
- }
- }
-
-
-
- public static void Set(Uri uri, Cookie cookie)
- {
- Set(cookie);
- }
-
-
-
- public static void Set(Cookie cookie)
- {
- lock (Locker)
- {
- Load();
- int idx;
- Find(cookie, out idx);
- if (idx >= 0)
- Cookies[idx] = cookie;
- else
- Cookies.Add(cookie);
- }
- }
- public static List<Cookie> GetAll()
- {
- lock (Locker)
- {
- Load();
- return Cookies;
- }
- }
-
-
-
- public static void Clear()
- {
- lock (Locker)
- {
- Load();
- Cookies.Clear();
- }
- }
-
-
-
- public static void Clear(TimeSpan olderThan)
- {
- lock (Locker)
- {
- Load();
- for (int i = 0; i < Cookies.Count; )
- {
- var cookie = Cookies[i];
-
- if (!cookie.WillExpireInTheFuture() || (cookie.Date + olderThan) < DateTime.UtcNow)
- Cookies.RemoveAt(i);
- else
- i++;
- }
- }
- }
-
-
-
- public static void Clear(string domain)
- {
- lock (Locker)
- {
- Load();
- for (int i = 0; i < Cookies.Count; )
- {
- var cookie = Cookies[i];
-
- if (!cookie.WillExpireInTheFuture() || cookie.Domain.IndexOf(domain) != -1)
- Cookies.RemoveAt(i);
- else
- i++;
- }
- }
- }
- public static void Remove(Uri uri, string name)
- {
- lock(Locker)
- {
- Load();
- for (int i = 0; i < Cookies.Count; )
- {
- var cookie = Cookies[i];
- if (cookie.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && uri.Host.IndexOf(cookie.Domain) != -1)
- Cookies.RemoveAt(i);
- else
- i++;
- }
- }
- }
- #endregion
- #region Private Helper Functions
-
-
-
- private static Cookie Find(Cookie cookie, out int idx)
- {
- for (int i = 0; i < Cookies.Count; ++i)
- {
- Cookie c = Cookies[i];
- if (c.Equals(cookie))
- {
- idx = i;
- return c;
- }
- }
- idx = -1;
- return null;
- }
- #endregion
- }
- }
- #endif
|