// Copyright (c) 2024 Vuplex Inc. All rights reserved.
//
// Licensed under the Vuplex Commercial Software Library License, you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// https://vuplex.com/commercial-library-license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using UnityEngine;
using Vuplex.WebView.Internal;
namespace Vuplex.WebView {
///
/// An HTTP cookie.
///
[Serializable]
public class Cookie {
///
/// The name of the cookie.
///
public string Name;
///
/// The value of the cookie.
///
public string Value;
///
/// The domain to which the cookie belongs (e.g. "www.vuplex.com", "example.com").
///
public string Domain;
///
/// The URL path of the cookie (e.g. "/", "/products/1234").
/// The default is "/".
///
public string Path = "/";
///
/// A number representing the expiration date of the cookie as the number of seconds since the UNIX epoch, or 0
/// if there is no expiration date. An expiration date is not provided for session cookies.
///
public int ExpirationDate;
///
/// Indicates whether the cookie is marked as HttpOnly (i.e. the cookie is inaccessible to client-side scripts).
///
public bool HttpOnly;
///
/// Indicates whether cookie is marked as secure (i.e. its scope is limited to secure channels, typically HTTPS).
///
public bool Secure;
///
/// Indicates whether the cookie is valid.
///
public bool IsValid {
get {
var isValid = true;
if (Name == null) {
WebViewLogger.LogWarning("Invalid value for Cookie.Name: " + Name);
isValid = false;
}
if (Value == null) {
WebViewLogger.LogWarning("Invalid value for Cookie.Value: " + Value);
isValid = false;
}
if (Domain == null || !Domain.Contains(".") || Domain.Contains("/")) {
WebViewLogger.LogWarning("Invalid value for Cookie.Domain: " + Domain);
isValid = false;
}
if (Path == null) {
WebViewLogger.LogWarning("Invalid value for Cookie.Path: " + Path);
isValid = false;
}
return isValid;
}
}
///
/// Deserializes a Cookie array from JSON.
///
public static Cookie[] ArrayFromJson(string serializedCookies) {
if (serializedCookies == "null") {
return new Cookie[0];
}
var cookiesWrapper = JsonUtility.FromJson>(serializedCookies);
var cookies = cookiesWrapper.Items ?? new Cookie[0];
return cookies;
}
///
/// Deserializes a Cookie from JSON.
///
public static Cookie FromJson(string serializedCookie) {
if (serializedCookie == "null") {
return null;
}
return JsonUtility.FromJson(serializedCookie);
}
///
/// Serializes the instance to JSON.
///
public string ToJson() => JsonUtility.ToJson(this);
public override string ToString() => ToJson();
}
}