| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Net.NetworkInformation; |
| | | 9 | | using System.Runtime.ExceptionServices; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | namespace System.Net.Http |
| | | 14 | | { |
| | | 15 | | // General flow of requests through the various layers: |
| | | 16 | | // |
| | | 17 | | // (1) HttpConnectionPoolManager.SendAsync: Does proxy lookup |
| | | 18 | | // (2) HttpConnectionPoolManager.SendAsyncCore: Find or create connection pool |
| | | 19 | | // (3) HttpConnectionPool.SendAsync: Handle basic/digest request auth |
| | | 20 | | // (4) HttpConnectionPool.SendWithProxyAuthAsync: Handle basic/digest proxy auth |
| | | 21 | | // (5) HttpConnectionPool.SendWithRetryAsync: Retrieve connection from pool, or create new |
| | | 22 | | // Also, handle retry for failures on connection reuse |
| | | 23 | | // (6) HttpConnection.SendAsync: Handle negotiate/ntlm connection auth |
| | | 24 | | // (7) HttpConnection.SendWithNtProxyAuthAsync: Handle negotiate/ntlm proxy auth |
| | | 25 | | // (8) HttpConnection.SendAsyncCore: Write request to connection and read response |
| | | 26 | | // Also, handle cookie processing |
| | | 27 | | // |
| | | 28 | | // Redirect and decompression handling are done above HttpConnectionPoolManager, |
| | | 29 | | // in RedirectHandler and DecompressionHandler respectively. |
| | | 30 | | |
| | | 31 | | /// <summary>Provides a set of connection pools, each for its own endpoint.</summary> |
| | | 32 | | internal sealed class HttpConnectionPoolManager : IDisposable |
| | | 33 | | { |
| | | 34 | | /// <summary>How frequently an operation should be initiated to clean out old pools and connections in those poo |
| | | 35 | | private readonly TimeSpan _cleanPoolTimeout; |
| | | 36 | | /// <summary>The pools, indexed by endpoint.</summary> |
| | | 37 | | private readonly ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool> _pools; |
| | | 38 | | /// <summary>Timer used to initiate cleaning of the pools.</summary> |
| | | 39 | | private readonly Timer? _cleaningTimer; |
| | | 40 | | /// <summary>Heart beat timer currently used for Http2 ping only.</summary> |
| | | 41 | | private readonly Timer? _heartBeatTimer; |
| | | 42 | | |
| | | 43 | | private readonly HttpConnectionSettings _settings; |
| | | 44 | | private readonly IWebProxy? _proxy; |
| | | 45 | | private readonly ICredentials? _proxyCredentials; |
| | | 46 | | |
| | | 47 | | #if !ILLUMOS && !SOLARIS && !HAIKU |
| | | 48 | | private NetworkChangeCleanup? _networkChangeCleanup; |
| | | 49 | | #endif |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Keeps track of whether or not the cleanup timer is running. It helps us avoid the expensive |
| | | 53 | | /// <see cref="ConcurrentDictionary{TKey,TValue}.IsEmpty"/> call. |
| | | 54 | | /// </summary> |
| | | 55 | | private bool _timerIsRunning; |
| | | 56 | | /// <summary>Object used to synchronize access to state in the pool.</summary> |
| | 0 | 57 | | private object SyncObj => _pools; |
| | | 58 | | |
| | | 59 | | /// <summary>Initializes the pools.</summary> |
| | 0 | 60 | | public HttpConnectionPoolManager(HttpConnectionSettings settings) |
| | 0 | 61 | | { |
| | 0 | 62 | | _settings = settings; |
| | 0 | 63 | | _pools = new ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>(); |
| | | 64 | | |
| | | 65 | | // As an optimization, we can sometimes avoid the overheads associated with |
| | | 66 | | // storing connections. This is possible when we would immediately terminate |
| | | 67 | | // connections anyway due to either the idle timeout or the lifetime being |
| | | 68 | | // set to zero, as in that case the timeout effectively immediately expires. |
| | | 69 | | // However, we can only do such optimizations if we're not also tracking |
| | | 70 | | // connections per server, as we use data in the associated data structures |
| | | 71 | | // to do that tracking. |
| | | 72 | | // Additionally, we should not avoid storing connections if keep-alive ping is configured, |
| | | 73 | | // as the heartbeat timer is needed for ping functionality. |
| | 0 | 74 | | bool avoidStoringConnections = |
| | 0 | 75 | | settings._maxConnectionsPerServer == int.MaxValue && |
| | 0 | 76 | | (settings._pooledConnectionIdleTimeout == TimeSpan.Zero || |
| | 0 | 77 | | settings._pooledConnectionLifetime == TimeSpan.Zero) && |
| | 0 | 78 | | settings._keepAlivePingDelay == Timeout.InfiniteTimeSpan; |
| | | 79 | | |
| | | 80 | | // Start out with the timer not running, since we have no pools. |
| | | 81 | | // When it does run, run it with a frequency based on the idle timeout. |
| | 0 | 82 | | if (!avoidStoringConnections) |
| | 0 | 83 | | { |
| | 0 | 84 | | if (settings._pooledConnectionIdleTimeout == Timeout.InfiniteTimeSpan) |
| | 0 | 85 | | { |
| | | 86 | | const int DefaultScavengeSeconds = 30; |
| | 0 | 87 | | _cleanPoolTimeout = TimeSpan.FromSeconds(DefaultScavengeSeconds); |
| | 0 | 88 | | } |
| | | 89 | | else |
| | 0 | 90 | | { |
| | | 91 | | const int ScavengesPerIdle = 4; |
| | | 92 | | const int MinScavengeSeconds = 1; |
| | 0 | 93 | | TimeSpan timerPeriod = settings._pooledConnectionIdleTimeout / ScavengesPerIdle; |
| | 0 | 94 | | _cleanPoolTimeout = timerPeriod.TotalSeconds >= MinScavengeSeconds ? timerPeriod : TimeSpan.FromSeco |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | using (ExecutionContext.SuppressFlow()) // Don't capture the current ExecutionContext and its AsyncLocal |
| | 0 | 98 | | { |
| | | 99 | | // Create the timer. Ensure the Timer has a weak reference to this manager; otherwise, it |
| | | 100 | | // can introduce a cycle that keeps the HttpConnectionPoolManager rooted by the Timer |
| | | 101 | | // implementation until the handler is Disposed (or indefinitely if it's not). |
| | 0 | 102 | | var thisRef = new WeakReference<HttpConnectionPoolManager>(this); |
| | | 103 | | |
| | 0 | 104 | | _cleaningTimer = new Timer(static s => |
| | 0 | 105 | | { |
| | 0 | 106 | | var wr = (WeakReference<HttpConnectionPoolManager>)s!; |
| | 0 | 107 | | if (wr.TryGetTarget(out HttpConnectionPoolManager? thisRef)) |
| | 0 | 108 | | { |
| | 0 | 109 | | thisRef.RemoveStalePools(); |
| | 0 | 110 | | } |
| | 0 | 111 | | }, thisRef, Timeout.Infinite, Timeout.Infinite); |
| | | 112 | | |
| | | 113 | | |
| | | 114 | | // For now heart beat is used only for ping functionality. |
| | 0 | 115 | | if (_settings._keepAlivePingDelay != Timeout.InfiniteTimeSpan) |
| | 0 | 116 | | { |
| | 0 | 117 | | long heartBeatInterval = (long)Math.Max(1000, Math.Min(_settings._keepAlivePingDelay.TotalMillis |
| | | 118 | | |
| | 0 | 119 | | _heartBeatTimer = new Timer(static state => |
| | 0 | 120 | | { |
| | 0 | 121 | | var wr = (WeakReference<HttpConnectionPoolManager>)state!; |
| | 0 | 122 | | if (wr.TryGetTarget(out HttpConnectionPoolManager? thisRef)) |
| | 0 | 123 | | { |
| | 0 | 124 | | thisRef.HeartBeat(); |
| | 0 | 125 | | } |
| | 0 | 126 | | }, thisRef, heartBeatInterval, heartBeatInterval); |
| | 0 | 127 | | } |
| | 0 | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | // Figure out proxy stuff. |
| | 0 | 132 | | if (settings._useProxy) |
| | 0 | 133 | | { |
| | 0 | 134 | | _proxy = settings._proxy ?? HttpClient.DefaultProxy; |
| | 0 | 135 | | if (_proxy != null) |
| | 0 | 136 | | { |
| | 0 | 137 | | _proxyCredentials = _proxy.Credentials ?? settings._defaultProxyCredentials; |
| | 0 | 138 | | } |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | #if !ILLUMOS && !SOLARIS && !HAIKU |
| | | 143 | | /// <summary> |
| | | 144 | | /// Starts monitoring for network changes. Upon a change, <see cref="HttpConnectionPool.OnNetworkChanged"/> will |
| | | 145 | | /// called for every <see cref="HttpConnectionPool"/> in the <see cref="HttpConnectionPoolManager"/>. |
| | | 146 | | /// </summary> |
| | | 147 | | public void StartMonitoringNetworkChanges() |
| | 0 | 148 | | { |
| | 0 | 149 | | if (_networkChangeCleanup != null) |
| | 0 | 150 | | { |
| | 0 | 151 | | return; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | // Monitor network changes to invalidate Alt-Svc headers. |
| | | 155 | | // A weak reference is used to avoid NetworkChange.NetworkAddressChanged keeping a non-disposed connection p |
| | | 156 | | NetworkAddressChangedEventHandler networkChangedDelegate; |
| | 0 | 157 | | { // scope to avoid closure if _networkChangeCleanup != null |
| | 0 | 158 | | var poolsRef = new WeakReference<ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>>(_pools); |
| | 0 | 159 | | networkChangedDelegate = delegate |
| | 0 | 160 | | { |
| | 0 | 161 | | if (poolsRef.TryGetTarget(out ConcurrentDictionary<HttpConnectionKey, HttpConnectionPool>? pools)) |
| | 0 | 162 | | { |
| | 0 | 163 | | foreach (HttpConnectionPool pool in pools.Values) |
| | 0 | 164 | | { |
| | 0 | 165 | | pool.OnNetworkChanged(); |
| | 0 | 166 | | } |
| | 0 | 167 | | } |
| | 0 | 168 | | }; |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | var cleanup = new NetworkChangeCleanup(networkChangedDelegate); |
| | | 172 | | |
| | 0 | 173 | | if (Interlocked.CompareExchange(ref _networkChangeCleanup, cleanup, null) != null) |
| | 0 | 174 | | { |
| | | 175 | | // We lost a race, another thread already started monitoring. |
| | 0 | 176 | | GC.SuppressFinalize(cleanup); |
| | 0 | 177 | | return; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | // RFC: https://tools.ietf.org/html/rfc7838#section-2.2 |
| | | 181 | | // When alternative services are used to send a client to the most |
| | | 182 | | // optimal server, a change in network configuration can result in |
| | | 183 | | // cached values becoming suboptimal. Therefore, clients SHOULD remove |
| | | 184 | | // from cache all alternative services that lack the "persist" flag with |
| | | 185 | | // the value "1" when they detect such a change, when information about |
| | | 186 | | // network state is available. |
| | | 187 | | try |
| | 0 | 188 | | { |
| | 0 | 189 | | using (ExecutionContext.SuppressFlow()) |
| | 0 | 190 | | { |
| | 0 | 191 | | NetworkChange.NetworkAddressChanged += networkChangedDelegate; |
| | 0 | 192 | | } |
| | 0 | 193 | | } |
| | 0 | 194 | | catch (NetworkInformationException e) |
| | 0 | 195 | | { |
| | 0 | 196 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"Exception when subscribing to NetworkCh |
| | | 197 | | |
| | | 198 | | // We can't monitor network changes, so technically "information |
| | | 199 | | // about network state is not available" and we can just keep |
| | | 200 | | // all Alt-Svc entries until their expiration time. |
| | | 201 | | // |
| | | 202 | | // keep the _networkChangeCleanup field assigned so we don't try again needlessly |
| | 0 | 203 | | } |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | private sealed class NetworkChangeCleanup : IDisposable |
| | | 207 | | { |
| | | 208 | | private readonly NetworkAddressChangedEventHandler _handler; |
| | | 209 | | |
| | 0 | 210 | | public NetworkChangeCleanup(NetworkAddressChangedEventHandler handler) |
| | 0 | 211 | | { |
| | 0 | 212 | | _handler = handler; |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | // If user never disposes the HttpClient, use finalizer to remove from NetworkChange.NetworkAddressChanged. |
| | | 216 | | // _handler will be rooted in NetworkChange, so should be safe to use here. |
| | 0 | 217 | | ~NetworkChangeCleanup() => NetworkChange.NetworkAddressChanged -= _handler; |
| | | 218 | | |
| | | 219 | | public void Dispose() |
| | 0 | 220 | | { |
| | 0 | 221 | | NetworkChange.NetworkAddressChanged -= _handler; |
| | 0 | 222 | | GC.SuppressFinalize(this); |
| | 0 | 223 | | } |
| | | 224 | | } |
| | | 225 | | #endif |
| | | 226 | | |
| | 0 | 227 | | public HttpConnectionSettings Settings => _settings; |
| | 0 | 228 | | public ICredentials? ProxyCredentials => _proxyCredentials; |
| | | 229 | | |
| | | 230 | | private HttpConnectionKey GetConnectionKey(HttpRequestMessage request, Uri? proxyUri, bool isProxyConnect) |
| | 0 | 231 | | { |
| | 0 | 232 | | Uri? uri = request.RequestUri; |
| | 0 | 233 | | Debug.Assert(uri != null); |
| | | 234 | | |
| | 0 | 235 | | if (isProxyConnect) |
| | 0 | 236 | | { |
| | 0 | 237 | | Debug.Assert(uri == proxyUri); |
| | 0 | 238 | | return new HttpConnectionKey(HttpConnectionKind.ProxyConnect, uri.IdnHost, uri.Port, null, proxyUri, Get |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | string? sslHostName = null; |
| | 0 | 242 | | if (HttpUtilities.IsSupportedSecureScheme(uri.Scheme)) |
| | 0 | 243 | | { |
| | 0 | 244 | | string? hostHeader = request.Headers.Host; |
| | 0 | 245 | | if (hostHeader != null) |
| | 0 | 246 | | { |
| | 0 | 247 | | sslHostName = HttpUtilities.ParseHostNameFromHeader(hostHeader); |
| | 0 | 248 | | } |
| | | 249 | | else |
| | 0 | 250 | | { |
| | | 251 | | // No explicit Host header. Use host from uri. |
| | 0 | 252 | | sslHostName = uri.IdnHost; |
| | 0 | 253 | | } |
| | 0 | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | string identity = GetIdentityIfDefaultCredentialsUsed(proxyUri != null ? _settings._defaultCredentialsUsedFo |
| | | 257 | | |
| | 0 | 258 | | if (proxyUri != null) |
| | 0 | 259 | | { |
| | 0 | 260 | | Debug.Assert(HttpUtilities.IsSupportedProxyScheme(proxyUri.Scheme)); |
| | 0 | 261 | | if (HttpUtilities.IsSocksScheme(proxyUri.Scheme)) |
| | 0 | 262 | | { |
| | | 263 | | // Socks proxy |
| | 0 | 264 | | if (sslHostName != null) |
| | 0 | 265 | | { |
| | 0 | 266 | | return new HttpConnectionKey(HttpConnectionKind.SslSocksTunnel, uri.IdnHost, uri.Port, sslHostNa |
| | | 267 | | } |
| | | 268 | | else |
| | 0 | 269 | | { |
| | 0 | 270 | | return new HttpConnectionKey(HttpConnectionKind.SocksTunnel, uri.IdnHost, uri.Port, null, proxyU |
| | | 271 | | } |
| | | 272 | | } |
| | 0 | 273 | | else if (sslHostName == null) |
| | 0 | 274 | | { |
| | 0 | 275 | | if (HttpUtilities.IsNonSecureWebSocketScheme(uri.Scheme)) |
| | 0 | 276 | | { |
| | | 277 | | // Non-secure websocket connection through proxy to the destination. |
| | 0 | 278 | | return new HttpConnectionKey(HttpConnectionKind.ProxyTunnel, uri.IdnHost, uri.Port, null, proxyU |
| | | 279 | | } |
| | | 280 | | else |
| | 0 | 281 | | { |
| | | 282 | | // Standard HTTP proxy usage for non-secure requests |
| | | 283 | | // The destination host and port are ignored here, since these connections |
| | | 284 | | // will be shared across any requests that use the proxy. |
| | 0 | 285 | | return new HttpConnectionKey(HttpConnectionKind.Proxy, null, 0, null, proxyUri, identity); |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | else |
| | 0 | 289 | | { |
| | | 290 | | // Tunnel SSL connection through proxy to the destination. |
| | 0 | 291 | | return new HttpConnectionKey(HttpConnectionKind.SslProxyTunnel, uri.IdnHost, uri.Port, sslHostName, |
| | | 292 | | } |
| | | 293 | | } |
| | 0 | 294 | | else if (sslHostName != null) |
| | 0 | 295 | | { |
| | 0 | 296 | | return new HttpConnectionKey(HttpConnectionKind.Https, uri.IdnHost, uri.Port, sslHostName, null, identit |
| | | 297 | | } |
| | | 298 | | else |
| | 0 | 299 | | { |
| | 0 | 300 | | return new HttpConnectionKey(HttpConnectionKind.Http, uri.IdnHost, uri.Port, null, null, identity); |
| | | 301 | | } |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | // Picks the value of the 'server.address' tag following rules specified in |
| | | 305 | | // https://github.com/open-telemetry/semantic-conventions/blob/728e5d1/docs/http/http-spans.md#http-client-span |
| | | 306 | | // When there is no proxy, we need to prioritize the contents of the Host header. |
| | | 307 | | private static string? GetTelemetryServerAddress(HttpRequestMessage request, HttpConnectionKey key) |
| | 0 | 308 | | { |
| | 0 | 309 | | if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled || GlobalHttpSettings.DiagnosticsHandler.EnableActiv |
| | 0 | 310 | | { |
| | 0 | 311 | | Uri? uri = request.RequestUri; |
| | 0 | 312 | | Debug.Assert(uri is not null); |
| | | 313 | | |
| | 0 | 314 | | if (key.SslHostName is not null) |
| | 0 | 315 | | { |
| | 0 | 316 | | return key.SslHostName; |
| | | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | if (key.ProxyUri is not null && key.Kind == HttpConnectionKind.Proxy) |
| | 0 | 320 | | { |
| | | 321 | | // In case there is no tunnel, return the proxy address since the connection is shared. |
| | 0 | 322 | | return key.ProxyUri.IdnHost; |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | string? hostHeader = request.Headers.Host; |
| | 0 | 326 | | return hostHeader is null ? uri.IdnHost : HttpUtilities.ParseHostNameFromHeader(hostHeader); |
| | | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | return null; |
| | 0 | 330 | | } |
| | | 331 | | |
| | | 332 | | public ValueTask<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, Uri? proxyUri, bool async, bool |
| | 0 | 333 | | { |
| | 0 | 334 | | HttpConnectionKey key = GetConnectionKey(request, proxyUri, isProxyConnect); |
| | | 335 | | |
| | | 336 | | HttpConnectionPool? pool; |
| | 0 | 337 | | while (!_pools.TryGetValue(key, out pool)) |
| | 0 | 338 | | { |
| | 0 | 339 | | pool = new HttpConnectionPool(this, key.Kind, key.Host, key.Port, key.SslHostName, key.ProxyUri, GetTele |
| | | 340 | | |
| | 0 | 341 | | if (_cleaningTimer == null) |
| | 0 | 342 | | { |
| | | 343 | | // There's no cleaning timer, which means we're not adding connections into pools, but we still need |
| | | 344 | | // the pool object for this request. We don't need or want to add the pool to the pools, though, |
| | | 345 | | // since we don't want it to sit there forever, which it would without the cleaning timer. |
| | 0 | 346 | | break; |
| | | 347 | | } |
| | | 348 | | |
| | 0 | 349 | | if (_pools.TryAdd(key, pool)) |
| | 0 | 350 | | { |
| | | 351 | | // We need to ensure the cleanup timer is running if it isn't |
| | | 352 | | // already now that we added a new connection pool. |
| | 0 | 353 | | lock (SyncObj) |
| | 0 | 354 | | { |
| | 0 | 355 | | if (!_timerIsRunning) |
| | 0 | 356 | | { |
| | 0 | 357 | | SetCleaningTimer(_cleanPoolTimeout); |
| | 0 | 358 | | } |
| | 0 | 359 | | } |
| | 0 | 360 | | break; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | // We created a pool and tried to add it to our pools, but some other thread got there before us. |
| | | 364 | | // We don't need to Dispose the pool, as that's only needed when it contains connections |
| | | 365 | | // that need to be closed. |
| | 0 | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | return pool.SendAsync(request, async, doRequestAuth, cancellationToken); |
| | 0 | 369 | | } |
| | | 370 | | |
| | | 371 | | public ValueTask<HttpResponseMessage> SendProxyConnectAsync(HttpRequestMessage request, Uri proxyUri, bool async |
| | 0 | 372 | | { |
| | 0 | 373 | | return SendAsyncCore(request, proxyUri, async, doRequestAuth: false, isProxyConnect: true, cancellationToken |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | public ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, bool doRequestAuth, Canc |
| | 0 | 377 | | { |
| | 0 | 378 | | if (_proxy == null) |
| | 0 | 379 | | { |
| | 0 | 380 | | return SendAsyncCore(request, null, async, doRequestAuth, isProxyConnect: false, cancellationToken); |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | // Do proxy lookup. |
| | 0 | 384 | | Uri? proxyUri = null; |
| | | 385 | | try |
| | 0 | 386 | | { |
| | 0 | 387 | | Debug.Assert(request.RequestUri != null); |
| | 0 | 388 | | if (!_proxy.IsBypassed(request.RequestUri)) |
| | 0 | 389 | | { |
| | 0 | 390 | | if (_proxy is IMultiWebProxy multiWebProxy) |
| | 0 | 391 | | { |
| | 0 | 392 | | MultiProxy multiProxy = multiWebProxy.GetMultiProxy(request.RequestUri); |
| | | 393 | | |
| | 0 | 394 | | if (multiProxy.ReadNext(out proxyUri, out bool isFinalProxy) && !isFinalProxy) |
| | 0 | 395 | | { |
| | 0 | 396 | | return SendAsyncMultiProxy(request, async, doRequestAuth, multiProxy, proxyUri, cancellation |
| | | 397 | | } |
| | 0 | 398 | | } |
| | | 399 | | else |
| | 0 | 400 | | { |
| | 0 | 401 | | proxyUri = _proxy.GetProxy(request.RequestUri); |
| | 0 | 402 | | } |
| | 0 | 403 | | } |
| | 0 | 404 | | } |
| | 0 | 405 | | catch (Exception ex) |
| | 0 | 406 | | { |
| | | 407 | | // Eat any exception from the IWebProxy and just treat it as no proxy. |
| | | 408 | | // This matches the behavior of other handlers. |
| | 0 | 409 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"Exception from {_proxy.GetType().Name}. |
| | 0 | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | if (proxyUri != null && !HttpUtilities.IsSupportedProxyScheme(proxyUri.Scheme)) |
| | 0 | 413 | | { |
| | 0 | 414 | | throw new NotSupportedException(SR.net_http_invalid_proxy_scheme); |
| | | 415 | | } |
| | | 416 | | |
| | 0 | 417 | | return SendAsyncCore(request, proxyUri, async, doRequestAuth, isProxyConnect: false, cancellationToken); |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Iterates a request over a set of proxies until one works, or all proxies have failed. |
| | | 422 | | /// </summary> |
| | | 423 | | /// <param name="request">The request message.</param> |
| | | 424 | | /// <param name="async">Whether to execute the request synchronously or asynchronously.</param> |
| | | 425 | | /// <param name="doRequestAuth">Whether to perform request authentication.</param> |
| | | 426 | | /// <param name="multiProxy">The set of proxies to use.</param> |
| | | 427 | | /// <param name="firstProxy">The first proxy try.</param> |
| | | 428 | | /// <param name="cancellationToken">The cancellation token to use for the operation.</param> |
| | | 429 | | private async ValueTask<HttpResponseMessage> SendAsyncMultiProxy(HttpRequestMessage request, bool async, bool do |
| | 0 | 430 | | { |
| | | 431 | | HttpRequestException rethrowException; |
| | | 432 | | |
| | | 433 | | do |
| | 0 | 434 | | { |
| | | 435 | | try |
| | 0 | 436 | | { |
| | 0 | 437 | | return await SendAsyncCore(request, firstProxy, async, doRequestAuth, isProxyConnect: false, cancell |
| | | 438 | | } |
| | 0 | 439 | | catch (HttpRequestException ex) when (ex.AllowRetry != RequestRetryType.NoRetry) |
| | 0 | 440 | | { |
| | 0 | 441 | | rethrowException = ex; |
| | 0 | 442 | | } |
| | 0 | 443 | | } |
| | 0 | 444 | | while (multiProxy.ReadNext(out firstProxy, out _)); |
| | | 445 | | |
| | 0 | 446 | | ExceptionDispatchInfo.Throw(rethrowException); |
| | | 447 | | return null; // should never be reached: VS doesn't realize Throw() never returns. |
| | 0 | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary>Disposes of the pools, disposing of each individual pool.</summary> |
| | | 451 | | public void Dispose() |
| | 0 | 452 | | { |
| | 0 | 453 | | _cleaningTimer?.Dispose(); |
| | 0 | 454 | | _heartBeatTimer?.Dispose(); |
| | 0 | 455 | | foreach (KeyValuePair<HttpConnectionKey, HttpConnectionPool> pool in _pools) |
| | 0 | 456 | | { |
| | 0 | 457 | | pool.Value.Dispose(); |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | #if !ILLUMOS && !SOLARIS && !HAIKU |
| | 0 | 461 | | _networkChangeCleanup?.Dispose(); |
| | | 462 | | #endif |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary>Sets <see cref="_cleaningTimer"/> and <see cref="_timerIsRunning"/> based on the specified timeout. |
| | | 466 | | private void SetCleaningTimer(TimeSpan timeout) |
| | 0 | 467 | | { |
| | 0 | 468 | | if (_cleaningTimer!.Change(timeout, Timeout.InfiniteTimeSpan)) |
| | 0 | 469 | | { |
| | 0 | 470 | | _timerIsRunning = timeout != Timeout.InfiniteTimeSpan; |
| | 0 | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | /// <summary>Removes unusable connections from each pool, and removes stale pools entirely.</summary> |
| | | 475 | | private void RemoveStalePools() |
| | 0 | 476 | | { |
| | 0 | 477 | | Debug.Assert(_cleaningTimer != null); |
| | | 478 | | |
| | | 479 | | // Iterate through each pool in the set of pools. For each, ask it to clear out |
| | | 480 | | // any unusable connections (e.g. those which have expired, those which have been closed, etc.) |
| | | 481 | | // The pool may detect that it's empty and long unused, in which case it'll dispose of itself, |
| | | 482 | | // such that any connections returned to the pool to be cached will be disposed of. In such |
| | | 483 | | // a case, we also remove the pool from the set of pools to avoid a leak. |
| | 0 | 484 | | foreach (KeyValuePair<HttpConnectionKey, HttpConnectionPool> entry in _pools) |
| | 0 | 485 | | { |
| | 0 | 486 | | if (entry.Value.CleanCacheAndDisposeIfUnused()) |
| | 0 | 487 | | { |
| | 0 | 488 | | _pools.TryRemove(entry.Key, out _); |
| | 0 | 489 | | } |
| | 0 | 490 | | } |
| | | 491 | | |
| | | 492 | | // Restart the timer if we have any pools to clean up. |
| | 0 | 493 | | lock (SyncObj) |
| | 0 | 494 | | { |
| | 0 | 495 | | SetCleaningTimer(!_pools.IsEmpty ? _cleanPoolTimeout : Timeout.InfiniteTimeSpan); |
| | 0 | 496 | | } |
| | | 497 | | |
| | | 498 | | // NOTE: There is a possible race condition with regards to a pool getting cleaned up at the same |
| | | 499 | | // time it's about to be used for another request. The timer cleanup could start running, see that |
| | | 500 | | // a pool is empty, and initiate its disposal. Concurrently, the pools could hand out the pool |
| | | 501 | | // to a request looking to get a connection, because the pool may not have been removed yet |
| | | 502 | | // from the pools. Worst case here is that connection will end up getting returned to an |
| | | 503 | | // already disposed pool, in which case the connection will also end up getting disposed rather |
| | | 504 | | // than reused. This should be a rare occurrence, so for now we don't worry about it. In the |
| | | 505 | | // future, there are a variety of possible ways to address it, such as allowing connections to |
| | | 506 | | // be returned to pools they weren't associated with. |
| | 0 | 507 | | } |
| | | 508 | | |
| | | 509 | | private void HeartBeat() |
| | 0 | 510 | | { |
| | 0 | 511 | | foreach (KeyValuePair<HttpConnectionKey, HttpConnectionPool> pool in _pools) |
| | 0 | 512 | | { |
| | 0 | 513 | | pool.Value.HeartBeat(); |
| | 0 | 514 | | } |
| | 0 | 515 | | } |
| | | 516 | | |
| | | 517 | | private static string GetIdentityIfDefaultCredentialsUsed(bool defaultCredentialsUsed) |
| | 0 | 518 | | { |
| | 0 | 519 | | return defaultCredentialsUsed ? CurrentUserIdentityProvider.GetIdentity() : string.Empty; |
| | 0 | 520 | | } |
| | | 521 | | |
| | | 522 | | internal readonly struct HttpConnectionKey : IEquatable<HttpConnectionKey> |
| | | 523 | | { |
| | | 524 | | public readonly HttpConnectionKind Kind; |
| | | 525 | | public readonly string? Host; |
| | | 526 | | public readonly int Port; |
| | | 527 | | public readonly string? SslHostName; // null if not SSL |
| | | 528 | | public readonly Uri? ProxyUri; |
| | | 529 | | public readonly string Identity; |
| | | 530 | | |
| | | 531 | | public HttpConnectionKey(HttpConnectionKind kind, string? host, int port, string? sslHostName, Uri? proxyUri |
| | 0 | 532 | | { |
| | 0 | 533 | | Kind = kind; |
| | 0 | 534 | | Host = host; |
| | 0 | 535 | | Port = port; |
| | 0 | 536 | | SslHostName = sslHostName; |
| | 0 | 537 | | ProxyUri = proxyUri; |
| | 0 | 538 | | Identity = identity; |
| | 0 | 539 | | } |
| | | 540 | | |
| | | 541 | | // In the common case, SslHostName (when present) is equal to Host. If so, don't include in hash. |
| | | 542 | | public override int GetHashCode() => |
| | 0 | 543 | | (SslHostName == Host ? |
| | 0 | 544 | | HashCode.Combine(Kind, Host, Port, ProxyUri, Identity) : |
| | 0 | 545 | | HashCode.Combine(Kind, Host, Port, SslHostName, ProxyUri, Identity)); |
| | | 546 | | |
| | | 547 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 548 | | obj is HttpConnectionKey hck && |
| | 0 | 549 | | Equals(hck); |
| | | 550 | | |
| | | 551 | | public bool Equals(HttpConnectionKey other) => |
| | 0 | 552 | | Kind == other.Kind && |
| | 0 | 553 | | Host == other.Host && |
| | 0 | 554 | | Port == other.Port && |
| | 0 | 555 | | ProxyUri == other.ProxyUri && |
| | 0 | 556 | | SslHostName == other.SslHostName && |
| | 0 | 557 | | Identity == other.Identity; |
| | | 558 | | } |
| | | 559 | | } |
| | | 560 | | } |