HostKey.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using Best.HTTP.Request.Settings;
  3. using UnityEngine;
  4. namespace Best.HTTP.HostSetting
  5. {
  6. /// <summary>
  7. /// The <see cref="HostKey"/> struct represents a unique key for identifying hosts based on their <see cref="System.Uri"/> and <see cref="ProxySettings"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// The <see cref="HostKey"/> struct is designed to uniquely identify a host based on its URI (Uniform Resource Identifier) and optional proxy settings.
  12. /// It provides a way to create, compare, and hash host keys, enabling efficient host variant management in the <see cref="HostManager"/>.
  13. /// </para>
  14. /// <para>
  15. /// Key features of the <see cref="HostKey"/> struct include:
  16. /// </para>
  17. /// <list type="bullet">
  18. /// <item>
  19. /// <term>Uniqueness</term>
  20. /// <description>
  21. /// Each <see cref="HostKey"/> is guaranteed to be unique for a specific host, considering both the URI and proxy settings.
  22. /// </description>
  23. /// </item>
  24. /// <item>
  25. /// <term>Hashing</term>
  26. /// <description>
  27. /// The struct provides a method to calculate a hash code for a <see cref="HostKey"/>, making it suitable for use as a dictionary key.
  28. /// </description>
  29. /// </item>
  30. /// <item>
  31. /// <term>Creation</term>
  32. /// <description>
  33. /// You can create a <see cref="HostKey"/> instance from a <see cref="System.Uri"/> and optional <see cref="ProxySettings"/>.
  34. /// </description>
  35. /// </item>
  36. /// </list>
  37. /// <para>
  38. /// Usage of the <see cref="HostKey"/> struct is typically handled internally by the BestHTTP library to manage unique hosts and optimize resource usage.
  39. /// Developers can use it when dealing with host-specific operations or customization of the library's behavior.
  40. /// </para>
  41. /// </remarks>
  42. public readonly struct HostKey
  43. {
  44. /// <summary>
  45. /// Gets the URI (Uniform Resource Identifier) associated with the host.
  46. /// </summary>
  47. public readonly Uri Uri;
  48. /// <summary>
  49. /// Gets the proxy settings associated with the host.
  50. /// </summary>
  51. public readonly ProxySettings Proxy;
  52. /// <summary>
  53. /// Gets the unique hash key for the host.
  54. /// </summary>
  55. public readonly Hash128 Key;
  56. /// <summary>
  57. /// Gets the host name from the URI or "file" if the URI is a file URI.
  58. /// </summary>
  59. public string Host { get => !this.Uri.IsFile ? this.Uri.Host : "file"; }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="HostKey"/> struct with the specified URI and proxy settings.
  62. /// </summary>
  63. /// <param name="uri">The URI of the host.</param>
  64. /// <param name="proxy">The proxy settings associated with the host, or <c>null</c> if no proxy is used.</param>
  65. public HostKey(Uri uri, ProxySettings proxy)
  66. {
  67. this.Uri = uri;
  68. this.Proxy = proxy;
  69. this.Key = CalculateHash(uri, proxy);
  70. }
  71. public override bool Equals(object obj) => obj switch
  72. {
  73. HostKey hostKey => hostKey.Equals(this),
  74. _ => false
  75. };
  76. public bool Equals(HostKey hostKey) => this.Key.Equals(hostKey.Key);
  77. public override int GetHashCode() => this.Key.GetHashCode();
  78. public override string ToString() => $"{{\"Uri\":\"{this.Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)}\", \"Proxy\": {this.Proxy.ToString()}, \"Key\": {this.Key.ToString()}}}";
  79. private static Hash128 CalculateHash(Uri uri, ProxySettings proxy)
  80. {
  81. Hash128 hash = new Hash128();
  82. Append(uri, ref hash);
  83. proxy?.AddToHash(uri, ref hash);
  84. return hash;
  85. }
  86. internal static void Append(Uri uri, ref Hash128 hash)
  87. {
  88. if (uri != null)
  89. {
  90. hash.Append(uri.Scheme);
  91. hash.Append(uri.Host);
  92. hash.Append(uri.Port);
  93. }
  94. }
  95. /// <summary>
  96. /// Creates a <see cref="HostKey"/> instance from an HTTP request.
  97. /// </summary>
  98. /// <param name="request">The HTTP request from which to extract the current URI and proxy settings.</param>
  99. /// <returns>A <see cref="HostKey"/> representing the host of the HTTP request.</returns>
  100. public static HostKey From(HTTPRequest request) => new HostKey(request.CurrentUri, request.ProxySettings);
  101. /// <summary>
  102. /// Creates a <see cref="HostKey"/> instance from a URI and proxy settings.
  103. /// </summary>
  104. /// <param name="uri">The URI of the host.</param>
  105. /// <param name="proxy">The proxy settings associated with the host, or <c>null</c> if no proxy is used.</param>
  106. /// <returns>A <see cref="HostKey"/> representing the host with the given URI and proxy settings.</returns>
  107. public static HostKey From(Uri uri, ProxySettings proxy) => new HostKey(uri, proxy);
  108. }
  109. }