using System; using Best.HTTP.Request.Settings; using UnityEngine; namespace Best.HTTP.HostSetting { /// /// The struct represents a unique key for identifying hosts based on their and . /// /// /// /// The struct is designed to uniquely identify a host based on its URI (Uniform Resource Identifier) and optional proxy settings. /// It provides a way to create, compare, and hash host keys, enabling efficient host variant management in the . /// /// /// Key features of the struct include: /// /// /// /// Uniqueness /// /// Each is guaranteed to be unique for a specific host, considering both the URI and proxy settings. /// /// /// /// Hashing /// /// The struct provides a method to calculate a hash code for a , making it suitable for use as a dictionary key. /// /// /// /// Creation /// /// You can create a instance from a and optional . /// /// /// /// /// Usage of the struct is typically handled internally by the BestHTTP library to manage unique hosts and optimize resource usage. /// Developers can use it when dealing with host-specific operations or customization of the library's behavior. /// /// public readonly struct HostKey { /// /// Gets the URI (Uniform Resource Identifier) associated with the host. /// public readonly Uri Uri; /// /// Gets the proxy settings associated with the host. /// public readonly ProxySettings Proxy; /// /// Gets the unique hash key for the host. /// public readonly Hash128 Key; /// /// Gets the host name from the URI or "file" if the URI is a file URI. /// public string Host { get => !this.Uri.IsFile ? this.Uri.Host : "file"; } /// /// Initializes a new instance of the struct with the specified URI and proxy settings. /// /// The URI of the host. /// The proxy settings associated with the host, or null if no proxy is used. public HostKey(Uri uri, ProxySettings proxy) { this.Uri = uri; this.Proxy = proxy; this.Key = CalculateHash(uri, proxy); } public override bool Equals(object obj) => obj switch { HostKey hostKey => hostKey.Equals(this), _ => false }; public bool Equals(HostKey hostKey) => this.Key.Equals(hostKey.Key); public override int GetHashCode() => this.Key.GetHashCode(); public override string ToString() => $"{{\"Uri\":\"{this.Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)}\", \"Proxy\": {this.Proxy.ToString()}, \"Key\": {this.Key.ToString()}}}"; private static Hash128 CalculateHash(Uri uri, ProxySettings proxy) { Hash128 hash = new Hash128(); Append(uri, ref hash); proxy?.AddToHash(uri, ref hash); return hash; } internal static void Append(Uri uri, ref Hash128 hash) { if (uri != null) { hash.Append(uri.Scheme); hash.Append(uri.Host); hash.Append(uri.Port); } } /// /// Creates a instance from an HTTP request. /// /// The HTTP request from which to extract the current URI and proxy settings. /// A representing the host of the HTTP request. public static HostKey From(HTTPRequest request) => new HostKey(request.CurrentUri, request.ProxySettings); /// /// Creates a instance from a URI and proxy settings. /// /// The URI of the host. /// The proxy settings associated with the host, or null if no proxy is used. /// A representing the host with the given URI and proxy settings. public static HostKey From(Uri uri, ProxySettings proxy) => new HostKey(uri, proxy); } }