ProxySettings.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Text;
  3. using Best.HTTP.Hosts.Connections;
  4. using Best.HTTP.HostSetting;
  5. using Best.HTTP.Request.Authentication;
  6. using Best.HTTP.Shared;
  7. namespace Best.HTTP.Request.Settings
  8. {
  9. /// <summary>
  10. /// Represents settings related to using a proxy server for HTTP requests.
  11. /// </summary>
  12. public class ProxySettings
  13. {
  14. /// <summary>
  15. /// Checks if there is a proxy configured for the given URI.
  16. /// </summary>
  17. /// <param name="uri">The URI to check for proxy usage.</param>
  18. /// <returns><c>true</c> if a proxy is configured and should be used for the URI; otherwise, <c>false</c>.</returns>
  19. public bool HasProxyFor(Uri uri) => Proxy != null && Proxy.UseProxyForAddress(uri);
  20. /// <summary>
  21. /// Gets or sets the proxy object used for the request.
  22. /// </summary>
  23. public Proxies.Proxy Proxy { get; set; } = HTTPManager.Proxy;
  24. /// <summary>
  25. /// Sets up the HTTP request for passing through a proxy server.
  26. /// </summary>
  27. /// <param name="request">The HTTP request to set up.</param>
  28. public void SetupRequest(HTTPRequest request)
  29. {
  30. var currentUri = request.CurrentUri;
  31. bool tryToKeepAlive = HTTPManager.PerHostSettings.Get(currentUri.Host)
  32. .HTTP1ConnectionSettings
  33. .TryToReuseConnections;
  34. if (!HTTPProtocolFactory.IsSecureProtocol(currentUri) && this.HasProxyFor(currentUri) && !request.HasHeader("Proxy-Connection"))
  35. request.AddHeader("Proxy-Connection", tryToKeepAlive ? "Keep-Alive" : "Close");
  36. // Proxy Authentication
  37. if (!HTTPProtocolFactory.IsSecureProtocol(currentUri) && HasProxyFor(currentUri) && this.Proxy.Credentials != null)
  38. {
  39. switch (Proxy.Credentials.Type)
  40. {
  41. case AuthenticationTypes.Basic:
  42. // With Basic authentication we don't want to wait for a challenge, we will send the hash with the first request
  43. var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.Proxy.Credentials.UserName + ":" + this.Proxy.Credentials.Password));
  44. request.SetHeader("Proxy-Authorization", $"Basic {token}");
  45. break;
  46. case AuthenticationTypes.Unknown:
  47. case AuthenticationTypes.Digest:
  48. var digest = DigestStore.Get(this.Proxy.Address);
  49. if (digest != null)
  50. {
  51. string authentication = digest.GenerateResponseHeader(this.Proxy.Credentials, false, request.MethodType, currentUri);
  52. if (!string.IsNullOrEmpty(authentication))
  53. request.SetHeader("Proxy-Authorization", authentication);
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Handles the proxy's response with status code <c>407</c>.
  61. /// </summary>
  62. /// <param name="request">The HTTP request that received a <c>407</c> response.</param>
  63. /// <returns><c>true</c> to resend the request through the proxy; otherwise, <c>false</c>.</returns>
  64. public bool Handle407(HTTPRequest request)
  65. {
  66. if (this.Proxy == null)
  67. return false;
  68. return this.Proxy.SetupRequest(request);
  69. }
  70. /// <summary>
  71. /// Adds the proxy address to a hash for the given request URI.
  72. /// </summary>
  73. /// <param name="requestUri">The request URI for which the proxy address is added to the hash.</param>
  74. /// <param name="hash">The hash to which the proxy address is added.</param>
  75. public void AddToHash(Uri requestUri, ref UnityEngine.Hash128 hash)
  76. {
  77. if (HasProxyFor(requestUri))
  78. HostKey.Append(this.Proxy.Address, ref hash);
  79. }
  80. public override string ToString()
  81. {
  82. return this.Proxy?.Address?.ToString();
  83. }
  84. }
  85. }