Cookie.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using UnityEngine;
  16. using Vuplex.WebView.Internal;
  17. namespace Vuplex.WebView {
  18. /// <summary>
  19. /// An HTTP cookie.
  20. /// </summary>
  21. [Serializable]
  22. public class Cookie {
  23. /// <summary>
  24. /// The name of the cookie.
  25. /// </summary>
  26. public string Name;
  27. /// <summary>
  28. /// The value of the cookie.
  29. /// </summary>
  30. public string Value;
  31. /// <summary>
  32. /// The domain to which the cookie belongs (e.g. "www.vuplex.com", "example.com").
  33. /// </summary>
  34. public string Domain;
  35. /// <summary>
  36. /// The URL path of the cookie (e.g. "/", "/products/1234").
  37. /// The default is "/".
  38. /// </summary>
  39. public string Path = "/";
  40. /// <summary>
  41. /// A number representing the expiration date of the cookie as the number of seconds since the UNIX epoch, or 0
  42. /// if there is no expiration date. An expiration date is not provided for session cookies.
  43. /// </summary>
  44. public int ExpirationDate;
  45. /// <summary>
  46. /// Indicates whether the cookie is marked as HttpOnly (i.e. the cookie is inaccessible to client-side scripts).
  47. /// </summary>
  48. public bool HttpOnly;
  49. /// <summary>
  50. /// Indicates whether cookie is marked as secure (i.e. its scope is limited to secure channels, typically HTTPS).
  51. /// </summary>
  52. public bool Secure;
  53. /// <summary>
  54. /// Indicates whether the cookie is valid.
  55. /// </summary>
  56. public bool IsValid {
  57. get {
  58. var isValid = true;
  59. if (Name == null) {
  60. WebViewLogger.LogWarning("Invalid value for Cookie.Name: " + Name);
  61. isValid = false;
  62. }
  63. if (Value == null) {
  64. WebViewLogger.LogWarning("Invalid value for Cookie.Value: " + Value);
  65. isValid = false;
  66. }
  67. if (Domain == null || !Domain.Contains(".") || Domain.Contains("/")) {
  68. WebViewLogger.LogWarning("Invalid value for Cookie.Domain: " + Domain);
  69. isValid = false;
  70. }
  71. if (Path == null) {
  72. WebViewLogger.LogWarning("Invalid value for Cookie.Path: " + Path);
  73. isValid = false;
  74. }
  75. return isValid;
  76. }
  77. }
  78. /// <summary>
  79. /// Deserializes a Cookie array from JSON.
  80. /// </summary>
  81. public static Cookie[] ArrayFromJson(string serializedCookies) {
  82. if (serializedCookies == "null") {
  83. return new Cookie[0];
  84. }
  85. var cookiesWrapper = JsonUtility.FromJson<JsonArrayWrapper<Cookie>>(serializedCookies);
  86. var cookies = cookiesWrapper.Items ?? new Cookie[0];
  87. return cookies;
  88. }
  89. /// <summary>
  90. /// Deserializes a Cookie from JSON.
  91. /// </summary>
  92. public static Cookie FromJson(string serializedCookie) {
  93. if (serializedCookie == "null") {
  94. return null;
  95. }
  96. return JsonUtility.FromJson<Cookie>(serializedCookie);
  97. }
  98. /// <summary>
  99. /// Serializes the instance to JSON.
  100. /// </summary>
  101. public string ToJson() => JsonUtility.ToJson(this);
  102. public override string ToString() => ToJson();
  103. }
  104. }