HttpClientExtension.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.IO;
  2. using System.Net.Http;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Jellyfin.Networking.HappyEyeballs
  7. {
  8. /// <summary>
  9. /// Defines the <see cref="HttpClientExtension"/> class.
  10. ///
  11. /// Implementation taken from https://github.com/ppy/osu-framework/pull/4191 .
  12. /// </summary>
  13. public static class HttpClientExtension
  14. {
  15. /// <summary>
  16. /// Gets or sets a value indicating whether the client should use IPv6.
  17. /// </summary>
  18. public static bool UseIPv6 { get; set; } = true;
  19. // Implementation taken from https://github.com/rmkerr/corefx/blob/SocketsHttpHandler_Connect_HappyEyeballs/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs
  20. /// <summary>
  21. /// Implements the httpclient callback method.
  22. /// </summary>
  23. /// <param name="context">The <see cref="SocketsHttpConnectionContext"/> instance.</param>
  24. /// <param name="cancellationToken">The <see cref="CancellationToken"/> instance.</param>
  25. /// <returns>The http steam.</returns>
  26. public static async ValueTask<Stream> OnConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
  27. {
  28. if (!UseIPv6)
  29. {
  30. return await AttemptConnection(AddressFamily.InterNetwork, context, cancellationToken).ConfigureAwait(false);
  31. }
  32. using var cancelIPv6 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  33. var tryConnectAsyncIPv6 = AttemptConnection(AddressFamily.InterNetworkV6, context, cancelIPv6.Token);
  34. if (await Task.WhenAny(tryConnectAsyncIPv6, Task.Delay(200, cancelIPv6.Token)).ConfigureAwait(false) == tryConnectAsyncIPv6 && tryConnectAsyncIPv6.IsCompletedSuccessfully)
  35. {
  36. cancelIPv6.Cancel();
  37. return tryConnectAsyncIPv6.GetAwaiter().GetResult();
  38. }
  39. using var cancelIPv4 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  40. var tryConnectAsyncIPv4 = AttemptConnection(AddressFamily.InterNetwork, context, cancelIPv4.Token);
  41. if (await Task.WhenAny(tryConnectAsyncIPv6, tryConnectAsyncIPv4).ConfigureAwait(false) == tryConnectAsyncIPv6)
  42. {
  43. if (tryConnectAsyncIPv6.IsCompletedSuccessfully)
  44. {
  45. cancelIPv4.Cancel();
  46. return tryConnectAsyncIPv6.GetAwaiter().GetResult();
  47. }
  48. return tryConnectAsyncIPv4.GetAwaiter().GetResult();
  49. }
  50. else
  51. {
  52. if (tryConnectAsyncIPv4.IsCompletedSuccessfully)
  53. {
  54. cancelIPv6.Cancel();
  55. return tryConnectAsyncIPv4.GetAwaiter().GetResult();
  56. }
  57. return tryConnectAsyncIPv6.GetAwaiter().GetResult();
  58. }
  59. }
  60. private static async Task<Stream> AttemptConnection(AddressFamily addressFamily, SocketsHttpConnectionContext context, CancellationToken cancellationToken)
  61. {
  62. // The following socket constructor will create a dual-mode socket on systems where IPV6 is available.
  63. var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
  64. {
  65. // Turn off Nagle's algorithm since it degrades performance in most HttpClient scenarios.
  66. NoDelay = true
  67. };
  68. try
  69. {
  70. await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
  71. // The stream should take the ownership of the underlying socket,
  72. // closing it when it's disposed.
  73. return new NetworkStream(socket, ownsSocket: true);
  74. }
  75. catch
  76. {
  77. socket.Dispose();
  78. throw;
  79. }
  80. }
  81. }
  82. }