Cookie.cs 4.1 KB

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