Cookie.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using UnityEngine;
  4. using NativeCookie = ZenFulcrum.EmbeddedBrowser.BrowserNative.NativeCookie;
  5. namespace ZenFulcrum.EmbeddedBrowser {
  6. public class Cookie {
  7. public static void Init() {
  8. //Empty function on this class to call so we can get the cctor to call on the correct thread.
  9. //(Regex construction tends to crash if it tries to run from certain threads.)
  10. }
  11. private CookieManager cookies;
  12. private NativeCookie original;
  13. public string name = "", value = "", domain = "", path = "";
  14. /** Creation/access time of the cookie. Mostly untested/unsupported at present. */
  15. public DateTime creation, lastAccess;
  16. /** Null for normal cookies, a time for cookies that expire. Mostly untested/unsupported at present. */
  17. public DateTime? expires;
  18. public bool secure, httpOnly;
  19. public Cookie(CookieManager cookies) {
  20. this.cookies = cookies;
  21. }
  22. internal Cookie(CookieManager cookies, NativeCookie cookie) {
  23. this.cookies = cookies;
  24. original = cookie;
  25. Copy(original, this);
  26. }
  27. /** Deletes this cookie from the browser. */
  28. public void Delete() {
  29. if (original == null) return;
  30. BrowserNative.zfb_editCookie(cookies.browser.browserId, original, BrowserNative.CookieAction.Delete);
  31. original = null;
  32. }
  33. /** Updates any changes to this cookie in the browser, creating the cookie if it's new. */
  34. public void Update() {
  35. if (original != null) Delete();
  36. original = new NativeCookie();
  37. Copy(this, original);
  38. BrowserNative.zfb_editCookie(cookies.browser.browserId, original, BrowserNative.CookieAction.Create);
  39. }
  40. static readonly Regex dateRegex = new Regex(@"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}).(\d{3})");
  41. public static void Copy(NativeCookie src, Cookie dest) {
  42. dest.name = src.name;
  43. dest.value = src.value;
  44. dest.domain = src.domain;
  45. dest.path = src.path;
  46. Func<string, DateTime> convert = s => {
  47. var m = dateRegex.Match(s);
  48. return new DateTime(
  49. int.Parse(m.Groups[1].ToString()),
  50. int.Parse(m.Groups[2].ToString()),
  51. int.Parse(m.Groups[3].ToString()),
  52. int.Parse(m.Groups[4].ToString()),
  53. int.Parse(m.Groups[5].ToString()),
  54. int.Parse(m.Groups[6].ToString()),
  55. int.Parse(m.Groups[7].ToString())
  56. );
  57. };
  58. dest.creation = convert(src.creation);
  59. dest.expires = src.expires == null ? (DateTime?)null : convert(src.expires);
  60. dest.lastAccess = convert(src.lastAccess);
  61. dest.secure = src.secure != 0;
  62. dest.httpOnly = src.httpOnly != 0;
  63. }
  64. public static void Copy(Cookie src, NativeCookie dest) {
  65. dest.name = src.name;
  66. dest.value = src.value;
  67. dest.domain = src.domain;
  68. dest.path = src.path;
  69. Func<DateTime, string> convert = s => s.ToString("yyyy-MM-dd hh:mm:ss.fff");
  70. dest.creation = convert(src.creation);
  71. dest.expires = src.expires == null ? null : convert(src.expires.Value);
  72. dest.lastAccess = convert(src.lastAccess);
  73. dest.secure = src.secure ? (byte)1 : (byte)0;
  74. dest.httpOnly = src.httpOnly ? (byte)1 : (byte)0;
  75. }
  76. }
  77. }