| | | 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.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Net.Http.Headers; |
| | | 9 | | using System.Net.Http.HPack; |
| | | 10 | | using System.Net.Http.QPack; |
| | | 11 | | using System.Net.Quic; |
| | | 12 | | using System.Net.Security; |
| | | 13 | | using System.Net.Sockets; |
| | | 14 | | using System.Runtime.CompilerServices; |
| | | 15 | | using System.Runtime.ExceptionServices; |
| | | 16 | | using System.Runtime.InteropServices; |
| | | 17 | | using System.Security.Authentication; |
| | | 18 | | using System.Text; |
| | | 19 | | using System.Threading; |
| | | 20 | | using System.Threading.Tasks; |
| | | 21 | | |
| | | 22 | | namespace System.Net.Http |
| | | 23 | | { |
| | | 24 | | /// <summary>Provides a pool of connections to the same endpoint.</summary> |
| | | 25 | | internal sealed partial class HttpConnectionPool : IDisposable |
| | | 26 | | { |
| | | 27 | | /// <summary>The maximum number of times to retry a request after a failure on an established connection.</summa |
| | | 28 | | private const int MaxConnectionFailureRetries = 3; |
| | | 29 | | public const int DefaultHttpPort = 80; |
| | | 30 | | public const int DefaultHttpsPort = 443; |
| | | 31 | | |
| | 0 | 32 | | private static readonly List<SslApplicationProtocol> s_http3ApplicationProtocols = new List<SslApplicationProtoc |
| | 0 | 33 | | private static readonly List<SslApplicationProtocol> s_http2ApplicationProtocols = new List<SslApplicationProtoc |
| | 0 | 34 | | private static readonly List<SslApplicationProtocol> s_http2OnlyApplicationProtocols = new List<SslApplicationPr |
| | | 35 | | |
| | | 36 | | private readonly HttpConnectionPoolManager _poolManager; |
| | | 37 | | private readonly HttpConnectionKind _kind; |
| | | 38 | | private readonly Uri? _proxyUri; |
| | | 39 | | private readonly string? _telemetryServerAddress; |
| | | 40 | | |
| | | 41 | | /// <summary>The origin authority used to construct the <see cref="HttpConnectionPool"/>.</summary> |
| | | 42 | | private readonly HttpAuthority _originAuthority; |
| | | 43 | | |
| | | 44 | | /// <summary>The User-Agent header to use when creating a CONNECT tunnel.</summary> |
| | | 45 | | private string? _connectTunnelUserAgent; |
| | | 46 | | |
| | | 47 | | // These settings are advertised by the server via SETTINGS_MAX_HEADER_LIST_SIZE and SETTINGS_MAX_FIELD_SECTION_ |
| | | 48 | | // If we had previous connections to the same host in this pool, memorize the last value seen. |
| | | 49 | | // This value is used as an initial value for new connections before they have a chance to observe the SETTINGS |
| | | 50 | | // Doing so avoids immediately exceeding the server limit on the first request, potentially causing the connecti |
| | | 51 | | // 0 means there were no previous connections, or they hadn't advertised this limit. |
| | | 52 | | // There is no need to lock when updating these values - we're only interested in saving _a_ value, not necessar |
| | | 53 | | internal uint _lastSeenHttp2MaxHeaderListSize; |
| | | 54 | | internal uint _lastSeenHttp3MaxHeaderListSize; |
| | | 55 | | |
| | | 56 | | // Same as the above, but for SETTINGS_MAX_CONCURRENT_STREAMS. |
| | | 57 | | // Unlike the values above, this one starts out at SocketsHttpHandler.InitialHttp2MaxConcurrentStreams, |
| | | 58 | | // and we only ever memorize server-advertised values that are lower than that. That is, the setting |
| | | 59 | | // acts as the upper bound for what every new connection starts with. |
| | | 60 | | internal uint _lastSeenHttp2MaxConcurrentStreams; |
| | | 61 | | |
| | | 62 | | /// <summary>Options specialized and cached for this pool and its key.</summary> |
| | | 63 | | private readonly SslClientAuthenticationOptions? _sslOptionsHttp11; |
| | | 64 | | private readonly SslClientAuthenticationOptions? _sslOptionsHttp2; |
| | | 65 | | private readonly SslClientAuthenticationOptions? _sslOptionsHttp2Only; |
| | | 66 | | private SslClientAuthenticationOptions? _sslOptionsHttp3; |
| | | 67 | | private readonly SslClientAuthenticationOptions? _sslOptionsProxy; |
| | | 68 | | |
| | | 69 | | private readonly PreAuthCredentialCache? _preAuthCredentials; |
| | | 70 | | |
| | | 71 | | /// <summary>Whether the pool has been used since the last time a cleanup occurred.</summary> |
| | 0 | 72 | | private bool _usedSinceLastCleanup = true; |
| | | 73 | | /// <summary>Whether the pool has been disposed.</summary> |
| | | 74 | | private bool _disposed; |
| | | 75 | | |
| | | 76 | | /// <summary>Initializes the pool.</summary> |
| | | 77 | | /// <param name="poolManager">The manager associated with this pool.</param> |
| | | 78 | | /// <param name="kind">The kind of HTTP connections stored in this pool.</param> |
| | | 79 | | /// <param name="host">The host with which this pool is associated.</param> |
| | | 80 | | /// <param name="port">The port with which this pool is associated.</param> |
| | | 81 | | /// <param name="sslHostName">The SSL host with which this pool is associated.</param> |
| | | 82 | | /// <param name="proxyUri">The proxy this pool targets (optional).</param> |
| | | 83 | | /// <param name="telemetryServerAddress">The value of the 'server.address' tag to be emitted by Metrics and Dist |
| | 0 | 84 | | public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionKind kind, string? host, int port |
| | 0 | 85 | | { |
| | 0 | 86 | | _poolManager = poolManager; |
| | 0 | 87 | | _kind = kind; |
| | 0 | 88 | | _proxyUri = proxyUri; |
| | 0 | 89 | | _maxHttp11Connections = Settings._maxConnectionsPerServer; |
| | 0 | 90 | | _telemetryServerAddress = telemetryServerAddress; |
| | 0 | 91 | | _lastSeenHttp2MaxConcurrentStreams = (uint)Settings._initialHttp2MaxConcurrentStreams; |
| | | 92 | | |
| | | 93 | | // The only case where 'host' will not be set is if this is a Proxy connection pool. In that case the |
| | | 94 | | // connection targets the proxy itself, so use the proxy's host and port for the origin authority. |
| | 0 | 95 | | Debug.Assert(host is not null || (kind == HttpConnectionKind.Proxy && proxyUri is not null)); |
| | 0 | 96 | | _originAuthority = host is not null |
| | 0 | 97 | | ? new HttpAuthority(host, port) |
| | 0 | 98 | | : new HttpAuthority(proxyUri!.IdnHost, proxyUri.Port); |
| | | 99 | | |
| | 0 | 100 | | _http2Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version20; |
| | | 101 | | |
| | 0 | 102 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3) |
| | 0 | 103 | | { |
| | 0 | 104 | | _http3Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version30; |
| | 0 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | switch (kind) |
| | | 108 | | { |
| | | 109 | | case HttpConnectionKind.Http: |
| | 0 | 110 | | Debug.Assert(host != null); |
| | 0 | 111 | | Debug.Assert(port != 0); |
| | 0 | 112 | | Debug.Assert(sslHostName == null); |
| | 0 | 113 | | Debug.Assert(proxyUri == null); |
| | | 114 | | |
| | 0 | 115 | | _http3Enabled = false; |
| | 0 | 116 | | break; |
| | | 117 | | |
| | | 118 | | case HttpConnectionKind.Https: |
| | 0 | 119 | | Debug.Assert(host != null); |
| | 0 | 120 | | Debug.Assert(port != 0); |
| | 0 | 121 | | Debug.Assert(sslHostName != null); |
| | 0 | 122 | | Debug.Assert(proxyUri == null); |
| | 0 | 123 | | break; |
| | | 124 | | |
| | | 125 | | case HttpConnectionKind.Proxy: |
| | 0 | 126 | | Debug.Assert(host == null); |
| | 0 | 127 | | Debug.Assert(port == 0); |
| | 0 | 128 | | Debug.Assert(sslHostName == null); |
| | 0 | 129 | | Debug.Assert(proxyUri != null); |
| | | 130 | | |
| | 0 | 131 | | _http2Enabled = false; |
| | 0 | 132 | | _http3Enabled = false; |
| | 0 | 133 | | break; |
| | | 134 | | |
| | | 135 | | case HttpConnectionKind.ProxyTunnel: |
| | 0 | 136 | | Debug.Assert(host != null); |
| | 0 | 137 | | Debug.Assert(port != 0); |
| | 0 | 138 | | Debug.Assert(sslHostName == null); |
| | 0 | 139 | | Debug.Assert(proxyUri != null); |
| | | 140 | | |
| | | 141 | | // A CONNECT tunnel to the origin server behaves like a direct connection once established, |
| | | 142 | | // so cleartext HTTP/2 (h2c) can be used over it. HTTP/1.1 WebSockets keep working because |
| | | 143 | | // the WebSocket upgrade request uses HTTP/1.1 and never attempts HTTP/2. |
| | 0 | 144 | | _http3Enabled = false; |
| | 0 | 145 | | break; |
| | | 146 | | |
| | | 147 | | case HttpConnectionKind.SslProxyTunnel: |
| | 0 | 148 | | Debug.Assert(host != null); |
| | 0 | 149 | | Debug.Assert(port != 0); |
| | 0 | 150 | | Debug.Assert(sslHostName != null); |
| | 0 | 151 | | Debug.Assert(proxyUri != null); |
| | | 152 | | |
| | 0 | 153 | | _http3Enabled = false; // TODO: how do we tunnel HTTP3? |
| | 0 | 154 | | break; |
| | | 155 | | |
| | | 156 | | case HttpConnectionKind.ProxyConnect: |
| | 0 | 157 | | Debug.Assert(host != null); |
| | 0 | 158 | | Debug.Assert(port != 0); |
| | 0 | 159 | | Debug.Assert(sslHostName == null); |
| | 0 | 160 | | Debug.Assert(proxyUri != null); |
| | | 161 | | |
| | | 162 | | // Don't enforce the max connections limit on proxy tunnels; this would mean that connections to dif |
| | | 163 | | // would compete for the same limited number of connections. |
| | | 164 | | // We will still enforce this limit on the user of the tunnel (i.e. ProxyTunnel or SslProxyTunnel). |
| | 0 | 165 | | _maxHttp11Connections = int.MaxValue; |
| | | 166 | | |
| | 0 | 167 | | _http2Enabled = false; |
| | 0 | 168 | | _http3Enabled = false; |
| | 0 | 169 | | break; |
| | | 170 | | |
| | | 171 | | case HttpConnectionKind.SocksTunnel: |
| | | 172 | | case HttpConnectionKind.SslSocksTunnel: |
| | 0 | 173 | | Debug.Assert(host != null); |
| | 0 | 174 | | Debug.Assert(port != 0); |
| | 0 | 175 | | Debug.Assert(proxyUri != null); |
| | | 176 | | |
| | 0 | 177 | | _http3Enabled = false; // TODO: SOCKS supports UDP and may be used for HTTP3 |
| | 0 | 178 | | break; |
| | | 179 | | |
| | | 180 | | default: |
| | 0 | 181 | | Debug.Fail("Unknown HttpConnectionKind in HttpConnectionPool.ctor"); |
| | | 182 | | break; |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | if (!_http3Enabled) |
| | 0 | 186 | | { |
| | | 187 | | // Avoid parsing Alt-Svc headers if they won't be used. |
| | 0 | 188 | | _altSvcEnabled = false; |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | string? hostHeader = null; |
| | 0 | 192 | | if (host is not null) |
| | 0 | 193 | | { |
| | | 194 | | // Precalculate ASCII bytes for Host header |
| | | 195 | | // Note that if _host is null, this is a (non-tunneled) proxy connection, and we can't cache the hostnam |
| | 0 | 196 | | hostHeader = IsDefaultPort |
| | 0 | 197 | | ? _originAuthority.HostValue |
| | 0 | 198 | | : $"{_originAuthority.HostValue}:{_originAuthority.Port}"; |
| | | 199 | | |
| | | 200 | | // Note the IDN hostname should always be ASCII, since it's already been IDNA encoded. |
| | 0 | 201 | | byte[] hostHeaderLine = new byte[6 + hostHeader.Length + 2]; // Host: foo\r\n |
| | 0 | 202 | | "Host: "u8.CopyTo(hostHeaderLine); |
| | 0 | 203 | | Encoding.ASCII.GetBytes(hostHeader, hostHeaderLine.AsSpan(6)); |
| | 0 | 204 | | hostHeaderLine[^2] = (byte)'\r'; |
| | 0 | 205 | | hostHeaderLine[^1] = (byte)'\n'; |
| | 0 | 206 | | _hostHeaderLineBytes = hostHeaderLine; |
| | | 207 | | |
| | 0 | 208 | | Debug.Assert(Encoding.ASCII.GetString(_hostHeaderLineBytes) == $"Host: {hostHeader}\r\n"); |
| | 0 | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (sslHostName != null) |
| | 0 | 212 | | { |
| | 0 | 213 | | _sslOptionsHttp11 = ConstructSslOptions(poolManager, sslHostName); |
| | 0 | 214 | | _sslOptionsHttp11.ApplicationProtocols = null; |
| | | 215 | | |
| | 0 | 216 | | if (_http2Enabled) |
| | 0 | 217 | | { |
| | 0 | 218 | | _sslOptionsHttp2 = ConstructSslOptions(poolManager, sslHostName); |
| | 0 | 219 | | _sslOptionsHttp2.ApplicationProtocols = s_http2ApplicationProtocols; |
| | 0 | 220 | | _sslOptionsHttp2Only = ConstructSslOptions(poolManager, sslHostName); |
| | 0 | 221 | | _sslOptionsHttp2Only.ApplicationProtocols = s_http2OnlyApplicationProtocols; |
| | | 222 | | |
| | | 223 | | // Note: |
| | | 224 | | // The HTTP/2 specification states: |
| | | 225 | | // "A deployment of HTTP/2 over TLS 1.2 MUST disable renegotiation. |
| | | 226 | | // An endpoint MUST treat a TLS renegotiation as a connection error (Section 5.4.1) |
| | | 227 | | // of type PROTOCOL_ERROR." |
| | | 228 | | // which suggests we should do: |
| | | 229 | | // _sslOptionsHttp2.AllowRenegotiation = false; |
| | | 230 | | // However, if AllowRenegotiation is set to false, that will also prevent |
| | | 231 | | // renegotation if the server denies the HTTP/2 request and causes a |
| | | 232 | | // downgrade to HTTP/1.1, and the current APIs don't provide a mechanism |
| | | 233 | | // by which AllowRenegotiation could be set back to true in that case. |
| | | 234 | | // For now, if an HTTP/2 server erroneously issues a renegotiation, we'll |
| | | 235 | | // allow it. |
| | 0 | 236 | | } |
| | 0 | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | if (hostHeader is not null) |
| | 0 | 240 | | { |
| | 0 | 241 | | if (_http2Enabled) |
| | 0 | 242 | | { |
| | 0 | 243 | | _http2EncodedAuthorityHostHeader = HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingToAllocatedAr |
| | 0 | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && _http3Enabled) |
| | 0 | 247 | | { |
| | 0 | 248 | | _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArr |
| | 0 | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | // Set up for PreAuthenticate. Access to this cache is guarded by a lock on the cache itself. |
| | 0 | 253 | | if (_poolManager.Settings._preAuthenticate) |
| | 0 | 254 | | { |
| | 0 | 255 | | _preAuthCredentials = new PreAuthCredentialCache(); |
| | 0 | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | _http11RequestQueue = new RequestQueue<HttpConnection>(); |
| | 0 | 259 | | if (_http2Enabled) |
| | 0 | 260 | | { |
| | 0 | 261 | | _http2RequestQueue = new RequestQueue<Http2Connection?>(); |
| | 0 | 262 | | } |
| | 0 | 263 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && _http3Enabled) |
| | 0 | 264 | | { |
| | 0 | 265 | | _http3RequestQueue = new RequestQueue<Http3Connection?>(); |
| | 0 | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | if (_proxyUri != null && HttpUtilities.IsSupportedSecureScheme(_proxyUri.Scheme)) |
| | 0 | 269 | | { |
| | 0 | 270 | | _sslOptionsProxy = ConstructSslOptions(poolManager, _proxyUri.IdnHost); |
| | 0 | 271 | | _sslOptionsProxy.ApplicationProtocols = null; |
| | 0 | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | if (NetEventSource.Log.IsEnabled()) Trace($"{this}"); |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | private static SslClientAuthenticationOptions ConstructSslOptions(HttpConnectionPoolManager poolManager, string |
| | 0 | 278 | | { |
| | 0 | 279 | | Debug.Assert(sslHostName != null); |
| | | 280 | | |
| | 0 | 281 | | SslClientAuthenticationOptions sslOptions = poolManager.Settings._sslOptions?.ShallowClone() ?? new SslClien |
| | | 282 | | |
| | | 283 | | // This is only set if we are underlying handler for HttpClientHandler |
| | 0 | 284 | | if (poolManager.Settings._clientCertificateOptions == ClientCertificateOption.Manual && sslOptions.LocalCert |
| | 0 | 285 | | (sslOptions.ClientCertificates == null || sslOptions.ClientCertificates.Count == 0)) |
| | 0 | 286 | | { |
| | | 287 | | // If we have no client certificates do not set callback when internal selection is used. |
| | | 288 | | // It breaks TLS resume on Linux |
| | 0 | 289 | | sslOptions.LocalCertificateSelectionCallback = null; |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | // Set TargetHost for SNI |
| | 0 | 293 | | sslOptions.TargetHost = sslHostName; |
| | | 294 | | |
| | 0 | 295 | | return sslOptions; |
| | 0 | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | public string? TelemetryServerAddress => _telemetryServerAddress; |
| | 0 | 299 | | public HttpAuthority OriginAuthority => _originAuthority; |
| | 0 | 300 | | public HttpConnectionSettings Settings => _poolManager.Settings; |
| | 0 | 301 | | public HttpConnectionKind Kind => _kind; |
| | 0 | 302 | | public bool IsSecure => _kind == HttpConnectionKind.Https || _kind == HttpConnectionKind.SslProxyTunnel || _kind |
| | 0 | 303 | | public Uri? ProxyUri => _proxyUri; |
| | 0 | 304 | | public ICredentials? ProxyCredentials => _poolManager.ProxyCredentials; |
| | 0 | 305 | | public PreAuthCredentialCache? PreAuthCredentials => _preAuthCredentials; |
| | 0 | 306 | | public bool IsDefaultPort => OriginAuthority.Port == (IsSecure ? DefaultHttpsPort : DefaultHttpPort); |
| | 0 | 307 | | private bool DoProxyAuth => (_kind == HttpConnectionKind.Proxy || _kind == HttpConnectionKind.ProxyConnect); |
| | | 308 | | |
| | | 309 | | /// <summary>Object used to synchronize access to state in the pool.</summary> |
| | | 310 | | private object SyncObj |
| | | 311 | | { |
| | | 312 | | get |
| | 0 | 313 | | { |
| | 0 | 314 | | Debug.Assert(!Monitor.IsEntered(_http11Connections)); |
| | 0 | 315 | | return _http11Connections; |
| | 0 | 316 | | } |
| | | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | public bool HasSyncObjLock => Monitor.IsEntered(_http11Connections); |
| | | 320 | | |
| | | 321 | | // Overview of connection management (mostly HTTP version independent): |
| | | 322 | | // |
| | | 323 | | // Each version of HTTP (1.1, 2, 3) has its own connection pool, and each of these work in a similar manner, |
| | | 324 | | // allowing for differences between the versions (most notably, HTTP/1.1 is not multiplexed.) |
| | | 325 | | // |
| | | 326 | | // When a request is submitted for a particular version (e.g. HTTP/1.1), we first look in the pool for available |
| | | 327 | | // An "available" connection is one that is (hopefully) usable for a new request. |
| | | 328 | | // For HTTP/1.1, this is just an idle connection. |
| | | 329 | | // For HTTP2/3, this is a connection that (hopefully) has available streams to use for new requests. |
| | | 330 | | // If we find an available connection, we will attempt to validate it and then use it. |
| | | 331 | | // We check the lifetime of the connection and discard it if the lifetime is exceeded. |
| | | 332 | | // We check that the connection has not shut down; if so we discard it. |
| | | 333 | | // For HTTP2/3, we reserve a stream on the connection. If this fails, we cannot use the connection right no |
| | | 334 | | // If validation fails, we will attempt to find a different available connection. |
| | | 335 | | // |
| | | 336 | | // Once we have found a usable connection, we use it to process the request. |
| | | 337 | | // For HTTP/1.1, a connection can handle only a single request at a time, thus it is immediately removed fr |
| | | 338 | | // For HTTP2/3, a connection is only removed from the available list when it has no more available streams. |
| | | 339 | | // In either case, the connection still counts against the total associated connection count for the pool. |
| | | 340 | | // |
| | | 341 | | // If we cannot find a usable available connection, then the request is added the to the request queue for the a |
| | | 342 | | // |
| | | 343 | | // Whenever a request is queued, or an existing connection shuts down, we will check to see if we should inject |
| | | 344 | | // Injection policy depends on both user settings and some simple heuristics. |
| | | 345 | | // See comments on the relevant routines for details on connection injection policy. |
| | | 346 | | // |
| | | 347 | | // When a new connection is successfully created, or an existing unavailable connection becomes available again, |
| | | 348 | | // we will attempt to use this connection to handle any queued requests (subject to lifetime restrictions on exi |
| | | 349 | | // This may result in the connection becoming unavailable again, because it cannot handle any more requests at t |
| | | 350 | | // If not, we will return the connection to the pool as an available connection for use by new requests. |
| | | 351 | | // |
| | | 352 | | // When a connection shuts down, either gracefully (e.g. GOAWAY) or abortively (e.g. IOException), |
| | | 353 | | // we will remove it from the list of available connections, if it is present there. |
| | | 354 | | // If not, then it must be unavailable at the moment; we will detect this and ensure it is not added back to the |
| | | 355 | | |
| | | 356 | | public ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, bool doRequestAuth, Canc |
| | 0 | 357 | | { |
| | | 358 | | // We need the User-Agent header when we send a CONNECT request to the proxy. |
| | | 359 | | // We must read the header early, before we return the ownership of the request back to the user. |
| | 0 | 360 | | if ((Kind is HttpConnectionKind.ProxyTunnel or HttpConnectionKind.SslProxyTunnel) && |
| | 0 | 361 | | request.HasHeaders && |
| | 0 | 362 | | request.Headers.NonValidated.TryGetValues(HttpKnownHeaderNames.UserAgent, out HeaderStringValues userAge |
| | 0 | 363 | | { |
| | 0 | 364 | | _connectTunnelUserAgent = userAgent.ToString(); |
| | 0 | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (doRequestAuth && Settings._credentials != null) |
| | 0 | 368 | | { |
| | 0 | 369 | | return AuthenticationHelper.SendWithRequestAuthAsync(request, async, Settings._credentials, Settings._pr |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | return SendWithProxyAuthAsync(request, async, doRequestAuth, cancellationToken); |
| | 0 | 373 | | } |
| | | 374 | | |
| | | 375 | | public ValueTask<HttpResponseMessage> SendWithProxyAuthAsync(HttpRequestMessage request, bool async, bool doRequ |
| | 0 | 376 | | { |
| | 0 | 377 | | if (DoProxyAuth && ProxyCredentials is not null) |
| | 0 | 378 | | { |
| | 0 | 379 | | return AuthenticationHelper.SendWithProxyAuthAsync(request, _proxyUri!, async, ProxyCredentials, doReque |
| | | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | return SendWithVersionDetectionAndRetryAsync(request, async, doRequestAuth, cancellationToken); |
| | 0 | 383 | | } |
| | | 384 | | |
| | | 385 | | private Task<HttpResponseMessage> SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage re |
| | 0 | 386 | | { |
| | 0 | 387 | | if (doRequestAuth && Settings._credentials != null) |
| | 0 | 388 | | { |
| | 0 | 389 | | return AuthenticationHelper.SendWithNtConnectionAuthAsync(request, async, Settings._credentials, Setting |
| | | 390 | | } |
| | | 391 | | |
| | 0 | 392 | | return SendWithNtProxyAuthAsync(connection, request, async, cancellationToken); |
| | 0 | 393 | | } |
| | | 394 | | |
| | | 395 | | public Task<HttpResponseMessage> SendWithNtProxyAuthAsync(HttpConnection connection, HttpRequestMessage request, |
| | 0 | 396 | | { |
| | 0 | 397 | | if (DoProxyAuth && ProxyCredentials is not null) |
| | 0 | 398 | | { |
| | 0 | 399 | | return AuthenticationHelper.SendWithNtProxyAuthAsync(request, ProxyUri!, async, ProxyCredentials, HttpHa |
| | | 400 | | } |
| | | 401 | | |
| | 0 | 402 | | return connection.SendAsync(request, async, cancellationToken); |
| | 0 | 403 | | } |
| | | 404 | | |
| | | 405 | | public async ValueTask<HttpResponseMessage> SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, bo |
| | 0 | 406 | | { |
| | 0 | 407 | | _usedSinceLastCleanup = true; |
| | | 408 | | |
| | | 409 | | // Loop on connection failures (or other problems like version downgrade) and retry if possible. |
| | 0 | 410 | | int retryCount = 0; |
| | 0 | 411 | | while (true) |
| | 0 | 412 | | { |
| | | 413 | | // Reset any connection id stamped by a previous attempt. Each connection sets it again in its |
| | | 414 | | // SendAsync, so if this attempt is abandoned (e.g. we time out while waiting for the next |
| | | 415 | | // connection after a graceful retry) the request won't point at a connection that didn't serve it. |
| | 0 | 416 | | request.ConnectionId = null; |
| | | 417 | | |
| | 0 | 418 | | HttpConnectionWaiter<HttpConnection>? http11ConnectionWaiter = null; |
| | 0 | 419 | | HttpConnectionWaiter<Http2Connection?>? http2ConnectionWaiter = null; |
| | | 420 | | try |
| | 0 | 421 | | { |
| | 0 | 422 | | HttpResponseMessage? response = null; |
| | | 423 | | |
| | | 424 | | // Use HTTP/3 if possible. |
| | 0 | 425 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && // guard to enable trimming HTTP/3 support |
| | 0 | 426 | | _http3Enabled && |
| | 0 | 427 | | (request.Version.Major >= 3 || (request.VersionPolicy == HttpVersionPolicy.RequestVersionOrHighe |
| | 0 | 428 | | !request.IsExtendedConnectRequest) |
| | 0 | 429 | | { |
| | 0 | 430 | | Debug.Assert(async); |
| | 0 | 431 | | if (QuicConnection.IsSupported) |
| | 0 | 432 | | { |
| | 0 | 433 | | if (_sslOptionsHttp3 == null) |
| | 0 | 434 | | { |
| | | 435 | | // deferred creation. We use atomic exchange to be sure all threads point to single obje |
| | 0 | 436 | | SslClientAuthenticationOptions sslOptionsHttp3 = ConstructSslOptions(_poolManager, _sslO |
| | 0 | 437 | | sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; |
| | 0 | 438 | | Interlocked.CompareExchange(ref _sslOptionsHttp3, sslOptionsHttp3, null); |
| | 0 | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | response = await TrySendUsingHttp3Async(request, cancellationToken).ConfigureAwait(false); |
| | 0 | 442 | | } |
| | | 443 | | else |
| | 0 | 444 | | { |
| | 0 | 445 | | _altSvcEnabled = false; |
| | 0 | 446 | | _http3Enabled = false; |
| | 0 | 447 | | } |
| | 0 | 448 | | } |
| | | 449 | | |
| | 0 | 450 | | if (response is null) |
| | 0 | 451 | | { |
| | | 452 | | // We could not use HTTP/3. Do not continue if downgrade is not allowed. |
| | 0 | 453 | | if (request.Version.Major >= 3 && request.VersionPolicy != HttpVersionPolicy.RequestVersionOrLow |
| | 0 | 454 | | { |
| | 0 | 455 | | ThrowGetVersionException(request, 3); |
| | | 456 | | } |
| | | 457 | | |
| | | 458 | | // Use HTTP/2 if possible. |
| | 0 | 459 | | if (_http2Enabled && |
| | 0 | 460 | | (request.Version.Major >= 2 || (request.VersionPolicy == HttpVersionPolicy.RequestVersionOrH |
| | 0 | 461 | | (request.VersionPolicy != HttpVersionPolicy.RequestVersionOrLower || IsSecure) && // prefer |
| | 0 | 462 | | !(_http2SessionAuthSeen && CanFallBackToHttp11(request))) // skip HTTP/2 for requests that c |
| | 0 | 463 | | { |
| | 0 | 464 | | if (!TryGetPooledHttp2Connection(request, out Http2Connection? connection, out http2Connecti |
| | 0 | 465 | | http2ConnectionWaiter != null) |
| | 0 | 466 | | { |
| | 0 | 467 | | connection = await http2ConnectionWaiter.WaitForConnectionAsync(request, this, async, ca |
| | 0 | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | Debug.Assert(connection is not null || !_http2Enabled); |
| | 0 | 471 | | if (connection is not null) |
| | 0 | 472 | | { |
| | 0 | 473 | | if (request.IsExtendedConnectRequest) |
| | 0 | 474 | | { |
| | 0 | 475 | | await connection.InitialSettingsReceived.WaitWithCancellationAsync(cancellationToken |
| | 0 | 476 | | if (!connection.IsConnectEnabled) |
| | 0 | 477 | | { |
| | 0 | 478 | | HttpRequestException exception = new(HttpRequestError.ExtendedConnectNotSupporte |
| | 0 | 479 | | exception.Data["SETTINGS_ENABLE_CONNECT_PROTOCOL"] = false; |
| | 0 | 480 | | throw exception; |
| | | 481 | | } |
| | 0 | 482 | | } |
| | | 483 | | |
| | 0 | 484 | | response = await connection.SendAsync(request, async, cancellationToken).ConfigureAwait( |
| | 0 | 485 | | } |
| | 0 | 486 | | } |
| | | 487 | | |
| | 0 | 488 | | if (response is null) |
| | 0 | 489 | | { |
| | | 490 | | // We could not use HTTP/2. Do not continue if downgrade is not allowed. |
| | 0 | 491 | | if (request.Version.Major >= 2 && request.VersionPolicy != HttpVersionPolicy.RequestVersionO |
| | 0 | 492 | | { |
| | 0 | 493 | | ThrowGetVersionException(request, 2); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | // Use HTTP/1.x. |
| | 0 | 497 | | if (!TryGetPooledHttp11Connection(request, async, out HttpConnection? connection, out http11 |
| | 0 | 498 | | { |
| | 0 | 499 | | connection = await http11ConnectionWaiter.WaitForConnectionAsync(request, this, async, c |
| | 0 | 500 | | } |
| | | 501 | | |
| | 0 | 502 | | connection.Acquire(); // In case we are doing Windows (i.e. connection-based) auth, we need |
| | | 503 | | try |
| | 0 | 504 | | { |
| | 0 | 505 | | response = await SendWithNtConnectionAuthAsync(connection, request, async, doRequestAuth |
| | 0 | 506 | | } |
| | | 507 | | finally |
| | 0 | 508 | | { |
| | 0 | 509 | | connection.Release(); |
| | 0 | 510 | | } |
| | 0 | 511 | | } |
| | 0 | 512 | | } |
| | | 513 | | |
| | 0 | 514 | | ProcessAltSvc(response); |
| | 0 | 515 | | return response; |
| | | 516 | | } |
| | 0 | 517 | | catch (HttpRequestException e) when (e.AllowRetry == RequestRetryType.RetryOnConnectionFailure) |
| | 0 | 518 | | { |
| | 0 | 519 | | Debug.Assert(retryCount >= 0 && retryCount <= MaxConnectionFailureRetries); |
| | | 520 | | |
| | 0 | 521 | | if (retryCount == MaxConnectionFailureRetries) |
| | 0 | 522 | | { |
| | 0 | 523 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 524 | | { |
| | 0 | 525 | | Trace($"MaxConnectionFailureRetries limit of {MaxConnectionFailureRetries} hit. Retryable re |
| | 0 | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | throw; |
| | | 529 | | } |
| | | 530 | | |
| | 0 | 531 | | retryCount++; |
| | | 532 | | |
| | 0 | 533 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 534 | | { |
| | 0 | 535 | | Trace($"Retry attempt {retryCount} after connection failure. Connection exception: {e}"); |
| | 0 | 536 | | } |
| | | 537 | | |
| | | 538 | | // Eat exception and try again. |
| | 0 | 539 | | } |
| | 0 | 540 | | catch (HttpRequestException e) when (e.AllowRetry == RequestRetryType.RetryOnLowerHttpVersion) |
| | | 541 | | { |
| | | 542 | | // Throw if fallback is not allowed by the version policy. |
| | | 543 | | if (request.VersionPolicy != HttpVersionPolicy.RequestVersionOrLower) |
| | | 544 | | { |
| | | 545 | | throw new HttpRequestException(HttpRequestError.VersionNegotiationError, SR.Format(SR.net_http_r |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | if (NetEventSource.Log.IsEnabled()) |
| | | 549 | | { |
| | | 550 | | Trace($"Retrying request because server requested version fallback: {e}"); |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | // Eat exception and try again on a lower protocol version. |
| | | 554 | | request.Version = HttpVersion.Version11; |
| | | 555 | | } |
| | 0 | 556 | | catch (HttpRequestException e) when (e.AllowRetry == RequestRetryType.RetryOnSessionAuthenticationChalle |
| | | 557 | | { |
| | | 558 | | // Server sent a session-based authentication challenge (Negotiate/NTLM) on HTTP/2. |
| | | 559 | | // These authentication schemes require a persistent connection and don't work properly over HTTP/2. |
| | | 560 | | // The pool flag was already set in Http2Connection.SendAsync so future requests that can use |
| | | 561 | | // HTTP/1.1 will go directly to HTTP/1.1. Retry this request on HTTP/1.1. |
| | | 562 | | Debug.Assert(CanFallBackToHttp11(request)); |
| | | 563 | | Debug.Assert(_http2SessionAuthSeen); |
| | | 564 | | |
| | | 565 | | request.Version = HttpVersion.Version11; |
| | | 566 | | } |
| | | 567 | | finally |
| | 0 | 568 | | { |
| | | 569 | | // We never cancel both attempts at the same time. When downgrade happens, it's possible that both w |
| | | 570 | | // but in that case http2ConnectionWaiter.ConnectionCancellationTokenSource shall be null. |
| | 0 | 571 | | Debug.Assert(http11ConnectionWaiter is null || http2ConnectionWaiter?.ConnectionCancellationTokenSou |
| | 0 | 572 | | http11ConnectionWaiter?.SetTimeoutToPendingConnectionAttempt(this, cancellationToken.IsCancellationR |
| | 0 | 573 | | http2ConnectionWaiter?.SetTimeoutToPendingConnectionAttempt(this, cancellationToken.IsCancellationRe |
| | 0 | 574 | | } |
| | 0 | 575 | | } |
| | 0 | 576 | | } |
| | | 577 | | |
| | | 578 | | private async ValueTask<(Stream, TransportContext?, Activity?, IPEndPoint?, long)> ConnectAsync(HttpRequestMessa |
| | 0 | 579 | | { |
| | 0 | 580 | | Stream? stream = null; |
| | 0 | 581 | | IPEndPoint? remoteEndPoint = null; |
| | 0 | 582 | | Exception? exception = null; |
| | 0 | 583 | | TransportContext? transportContext = null; |
| | | 584 | | |
| | | 585 | | // Allocate the connection id up front so it can be surfaced to a custom ConnectCallback (via |
| | | 586 | | // SocketsHttpConnectionContext) and reused as the final connection's Id, allowing the caller to |
| | | 587 | | // correlate connect-time state with the connection (e.g. in the ShouldEvictConnection callback). |
| | 0 | 588 | | long connectionId = HttpConnectionBase.GetNextConnectionId(); |
| | | 589 | | |
| | 0 | 590 | | Activity? activity = ConnectionSetupDistributedTracing.StartConnectionSetupActivity(IsSecure, _telemetryServ |
| | | 591 | | |
| | | 592 | | try |
| | 0 | 593 | | { |
| | 0 | 594 | | switch (_kind) |
| | | 595 | | { |
| | | 596 | | case HttpConnectionKind.Http: |
| | | 597 | | case HttpConnectionKind.Https: |
| | | 598 | | case HttpConnectionKind.ProxyConnect: |
| | 0 | 599 | | stream = await ConnectToTcpHostAsync(_originAuthority.IdnHost, _originAuthority.Port, request, a |
| | | 600 | | // remoteEndPoint is returned for diagnostic purposes. |
| | 0 | 601 | | remoteEndPoint = GetRemoteEndPoint(stream); |
| | 0 | 602 | | if (_kind == HttpConnectionKind.ProxyConnect && _sslOptionsProxy != null) |
| | 0 | 603 | | { |
| | 0 | 604 | | stream = await ConnectHelper.EstablishSslConnectionAsync(_sslOptionsProxy, request, async, s |
| | 0 | 605 | | } |
| | 0 | 606 | | break; |
| | | 607 | | |
| | | 608 | | case HttpConnectionKind.Proxy: |
| | 0 | 609 | | stream = await ConnectToTcpHostAsync(_proxyUri!.IdnHost, _proxyUri.Port, request, async, connect |
| | | 610 | | // remoteEndPoint is returned for diagnostic purposes. |
| | 0 | 611 | | remoteEndPoint = GetRemoteEndPoint(stream); |
| | 0 | 612 | | if (_sslOptionsProxy != null) |
| | 0 | 613 | | { |
| | 0 | 614 | | stream = await ConnectHelper.EstablishSslConnectionAsync(_sslOptionsProxy, request, async, s |
| | 0 | 615 | | } |
| | 0 | 616 | | break; |
| | | 617 | | |
| | | 618 | | case HttpConnectionKind.ProxyTunnel: |
| | | 619 | | case HttpConnectionKind.SslProxyTunnel: |
| | 0 | 620 | | stream = await EstablishProxyTunnelAsync(async, cancellationToken).ConfigureAwait(false); |
| | | 621 | | |
| | 0 | 622 | | if (stream is HttpContentStream contentStream && contentStream._connection?._stream is Stream in |
| | 0 | 623 | | { |
| | 0 | 624 | | remoteEndPoint = GetRemoteEndPoint(innerStream); |
| | 0 | 625 | | } |
| | | 626 | | |
| | 0 | 627 | | break; |
| | | 628 | | |
| | | 629 | | case HttpConnectionKind.SocksTunnel: |
| | | 630 | | case HttpConnectionKind.SslSocksTunnel: |
| | 0 | 631 | | stream = await EstablishSocksTunnel(request, async, connectionId, cancellationToken).ConfigureAw |
| | | 632 | | // remoteEndPoint is returned for diagnostic purposes. |
| | 0 | 633 | | remoteEndPoint = GetRemoteEndPoint(stream); |
| | 0 | 634 | | break; |
| | | 635 | | } |
| | | 636 | | |
| | 0 | 637 | | Debug.Assert(stream != null); |
| | | 638 | | |
| | 0 | 639 | | if (IsSecure) |
| | 0 | 640 | | { |
| | 0 | 641 | | SslStream? sslStream = stream as SslStream; |
| | 0 | 642 | | if (sslStream == null) |
| | 0 | 643 | | { |
| | 0 | 644 | | sslStream = await ConnectHelper.EstablishSslConnectionAsync(GetSslOptionsForRequest(request, isF |
| | 0 | 645 | | } |
| | | 646 | | else |
| | 0 | 647 | | { |
| | 0 | 648 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 649 | | { |
| | 0 | 650 | | Trace($"Connected with custom SslStream: alpn='${sslStream.NegotiatedApplicationProtocol}'") |
| | 0 | 651 | | } |
| | 0 | 652 | | } |
| | 0 | 653 | | transportContext = sslStream.TransportContext; |
| | 0 | 654 | | stream = sslStream; |
| | 0 | 655 | | } |
| | 0 | 656 | | } |
| | 0 | 657 | | catch (Exception ex) when (activity is not null) |
| | 0 | 658 | | { |
| | 0 | 659 | | exception = ex; |
| | 0 | 660 | | throw; |
| | | 661 | | } |
| | | 662 | | finally |
| | 0 | 663 | | { |
| | 0 | 664 | | if (activity is not null) |
| | 0 | 665 | | { |
| | 0 | 666 | | ConnectionSetupDistributedTracing.StopConnectionSetupActivity(activity, exception, remoteEndPoint); |
| | 0 | 667 | | } |
| | 0 | 668 | | } |
| | | 669 | | |
| | 0 | 670 | | return (stream, transportContext, activity, remoteEndPoint, connectionId); |
| | | 671 | | |
| | 0 | 672 | | static IPEndPoint? GetRemoteEndPoint(Stream stream) => (stream as NetworkStream)?.Socket?.RemoteEndPoint as |
| | 0 | 673 | | } |
| | | 674 | | |
| | | 675 | | private async ValueTask<Stream> ConnectToTcpHostAsync(string host, int port, HttpRequestMessage initialRequest, |
| | 0 | 676 | | { |
| | 0 | 677 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 678 | | |
| | 0 | 679 | | var endPoint = new DnsEndPoint(host, port); |
| | 0 | 680 | | Stream? stream = null; |
| | | 681 | | try |
| | 0 | 682 | | { |
| | | 683 | | // If a ConnectCallback was supplied, use that to establish the connection. |
| | 0 | 684 | | if (Settings._connectCallback != null) |
| | 0 | 685 | | { |
| | 0 | 686 | | ValueTask<Stream> streamTask = Settings._connectCallback(new SocketsHttpConnectionContext(endPoint, |
| | | 687 | | |
| | 0 | 688 | | if (!async && !streamTask.IsCompleted) |
| | 0 | 689 | | { |
| | | 690 | | // User-provided ConnectCallback is completing asynchronously but the user is making a synchrono |
| | | 691 | | // set it up so that synchronous requests are made on a handler with a synchronously-completing |
| | | 692 | | // we could add a Boolean to SocketsHttpConnectionContext (https://github.com/dotnet/runtime/iss |
| | | 693 | | // this request is sync or async. |
| | 0 | 694 | | Trace($"{nameof(SocketsHttpHandler.ConnectCallback)} completing asynchronously for a synchronous |
| | 0 | 695 | | } |
| | | 696 | | |
| | 0 | 697 | | stream = await streamTask.ConfigureAwait(false) ?? throw new HttpRequestException(SR.net_http_null_f |
| | 0 | 698 | | } |
| | | 699 | | else |
| | 0 | 700 | | { |
| | | 701 | | // Otherwise, create and connect a socket using default settings. |
| | 0 | 702 | | Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; |
| | | 703 | | try |
| | 0 | 704 | | { |
| | 0 | 705 | | if (async) |
| | 0 | 706 | | { |
| | 0 | 707 | | await socket.ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false); |
| | 0 | 708 | | } |
| | | 709 | | else |
| | 0 | 710 | | { |
| | 0 | 711 | | using (cancellationToken.UnsafeRegister(static s => ((Socket)s!).Dispose(), socket)) |
| | 0 | 712 | | { |
| | 0 | 713 | | socket.Connect(endPoint); |
| | 0 | 714 | | } |
| | 0 | 715 | | } |
| | | 716 | | |
| | 0 | 717 | | stream = new NetworkStream(socket, ownsSocket: true); |
| | 0 | 718 | | } |
| | 0 | 719 | | catch |
| | 0 | 720 | | { |
| | 0 | 721 | | socket.Dispose(); |
| | 0 | 722 | | throw; |
| | | 723 | | } |
| | 0 | 724 | | } |
| | | 725 | | |
| | 0 | 726 | | return stream; |
| | | 727 | | } |
| | 0 | 728 | | catch (Exception ex) |
| | 0 | 729 | | { |
| | 0 | 730 | | throw ex is OperationCanceledException oce && oce.CancellationToken == cancellationToken ? |
| | 0 | 731 | | CancellationHelper.CreateOperationCanceledException(innerException: null, cancellationToken) : |
| | 0 | 732 | | ConnectHelper.CreateWrappedException(ex, host, port, cancellationToken); |
| | | 733 | | } |
| | 0 | 734 | | } |
| | | 735 | | |
| | | 736 | | private SslClientAuthenticationOptions GetSslOptionsForRequest(HttpRequestMessage request, bool isForHttp2) |
| | 0 | 737 | | { |
| | | 738 | | // Even if a request could use HTTP/2, we may have chosen to establish an HTTP/1.1 connection |
| | | 739 | | // for it instead (e.g. when _http2SessionAuthSeen is set for downgradeable requests). |
| | 0 | 740 | | if (_http2Enabled && isForHttp2) |
| | 0 | 741 | | { |
| | 0 | 742 | | if (request.Version.Major >= 2 && request.VersionPolicy != HttpVersionPolicy.RequestVersionOrLower) |
| | 0 | 743 | | { |
| | 0 | 744 | | return _sslOptionsHttp2Only!; |
| | | 745 | | } |
| | | 746 | | |
| | 0 | 747 | | if (request.Version.Major >= 2 || request.VersionPolicy == HttpVersionPolicy.RequestVersionOrHigher) |
| | 0 | 748 | | { |
| | 0 | 749 | | return _sslOptionsHttp2!; |
| | | 750 | | } |
| | 0 | 751 | | } |
| | 0 | 752 | | return _sslOptionsHttp11!; |
| | 0 | 753 | | } |
| | | 754 | | |
| | | 755 | | private async ValueTask<Stream> ApplyPlaintextFilterAsync(bool async, Stream stream, Version httpVersion, HttpRe |
| | 0 | 756 | | { |
| | 0 | 757 | | if (Settings._plaintextStreamFilter is null) |
| | 0 | 758 | | { |
| | 0 | 759 | | return stream; |
| | | 760 | | } |
| | | 761 | | |
| | | 762 | | Stream newStream; |
| | | 763 | | try |
| | 0 | 764 | | { |
| | 0 | 765 | | ValueTask<Stream> streamTask = Settings._plaintextStreamFilter(new SocketsHttpPlaintextStreamFilterConte |
| | | 766 | | |
| | 0 | 767 | | if (!async && !streamTask.IsCompleted) |
| | 0 | 768 | | { |
| | | 769 | | // User-provided PlaintextStreamFilter is completing asynchronously but the user is making a synchro |
| | | 770 | | // set it up so that synchronous requests are made on a handler with a synchronously-completing Plai |
| | | 771 | | // we could add a Boolean to SocketsHttpPlaintextStreamFilterContext (https://github.com/dotnet/runt |
| | | 772 | | // this request is sync or async. |
| | 0 | 773 | | Trace($"{nameof(SocketsHttpHandler.PlaintextStreamFilter)} completing asynchronously for a synchrono |
| | 0 | 774 | | } |
| | | 775 | | |
| | 0 | 776 | | newStream = await streamTask.ConfigureAwait(false); |
| | 0 | 777 | | } |
| | 0 | 778 | | catch (OperationCanceledException oce) when (oce.CancellationToken == cancellationToken) |
| | 0 | 779 | | { |
| | 0 | 780 | | stream.Dispose(); |
| | 0 | 781 | | throw; |
| | | 782 | | } |
| | 0 | 783 | | catch (Exception e) |
| | 0 | 784 | | { |
| | 0 | 785 | | stream.Dispose(); |
| | 0 | 786 | | throw new HttpRequestException(SR.net_http_exception_during_plaintext_filter, e); |
| | | 787 | | } |
| | | 788 | | |
| | 0 | 789 | | if (newStream == null) |
| | 0 | 790 | | { |
| | 0 | 791 | | stream.Dispose(); |
| | 0 | 792 | | throw new HttpRequestException(SR.net_http_null_from_plaintext_filter); |
| | | 793 | | } |
| | | 794 | | |
| | 0 | 795 | | return newStream; |
| | 0 | 796 | | } |
| | | 797 | | |
| | | 798 | | private async ValueTask<Stream> EstablishProxyTunnelAsync(bool async, CancellationToken cancellationToken) |
| | 0 | 799 | | { |
| | | 800 | | // Send a CONNECT request to the proxy server to establish a tunnel. |
| | 0 | 801 | | HttpRequestMessage tunnelRequest = new HttpRequestMessage(HttpMethod.Connect, _proxyUri); |
| | 0 | 802 | | tunnelRequest.Headers.Host = $"{_originAuthority.IdnHost}:{_originAuthority.Port}"; // This specifies des |
| | | 803 | | |
| | 0 | 804 | | if (_connectTunnelUserAgent is not null) |
| | 0 | 805 | | { |
| | 0 | 806 | | tunnelRequest.Headers.TryAddWithoutValidation(KnownHeaders.UserAgent.Descriptor, _connectTunnelUserAgent |
| | 0 | 807 | | } |
| | | 808 | | |
| | 0 | 809 | | HttpResponseMessage tunnelResponse = await _poolManager.SendProxyConnectAsync(tunnelRequest, _proxyUri!, asy |
| | | 810 | | |
| | 0 | 811 | | if (!tunnelResponse.IsSuccessStatusCode) |
| | 0 | 812 | | { |
| | 0 | 813 | | tunnelResponse.Dispose(); |
| | 0 | 814 | | throw new HttpRequestException(HttpRequestError.ProxyTunnelError, SR.Format(SR.net_http_proxy_tunnel_ret |
| | | 815 | | } |
| | | 816 | | |
| | | 817 | | try |
| | 0 | 818 | | { |
| | 0 | 819 | | return tunnelResponse.Content.ReadAsStream(cancellationToken); |
| | | 820 | | } |
| | 0 | 821 | | catch |
| | 0 | 822 | | { |
| | 0 | 823 | | tunnelResponse.Dispose(); |
| | 0 | 824 | | throw; |
| | | 825 | | } |
| | 0 | 826 | | } |
| | | 827 | | |
| | | 828 | | private async ValueTask<Stream> EstablishSocksTunnel(HttpRequestMessage request, bool async, long connectionId, |
| | 0 | 829 | | { |
| | 0 | 830 | | Debug.Assert(_proxyUri != null); |
| | | 831 | | |
| | 0 | 832 | | Stream stream = await ConnectToTcpHostAsync(_proxyUri.IdnHost, _proxyUri.Port, request, async, connectionId, |
| | | 833 | | |
| | | 834 | | try |
| | 0 | 835 | | { |
| | 0 | 836 | | await SocksHelper.EstablishSocksTunnelAsync(stream, _originAuthority.IdnHost, _originAuthority.Port, _pr |
| | 0 | 837 | | } |
| | 0 | 838 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 0 | 839 | | { |
| | 0 | 840 | | Debug.Assert(e is not HttpRequestException); |
| | 0 | 841 | | throw new HttpRequestException(HttpRequestError.ProxyTunnelError, SR.net_http_proxy_tunnel_error, e); |
| | | 842 | | } |
| | | 843 | | |
| | 0 | 844 | | return stream; |
| | 0 | 845 | | } |
| | | 846 | | |
| | | 847 | | private CancellationTokenSource GetConnectTimeoutCancellationTokenSource<T>(HttpConnectionWaiter<T> waiter) |
| | | 848 | | where T : HttpConnectionBase? |
| | 0 | 849 | | { |
| | 0 | 850 | | var cts = new CancellationTokenSource(Settings._connectTimeout); |
| | | 851 | | |
| | 0 | 852 | | lock (waiter) |
| | 0 | 853 | | { |
| | | 854 | | // After a request completes (or is canceled), it will call into SetTimeoutToPendingConnectionAttempt, |
| | | 855 | | // which will no-op if ConnectionCancellationTokenSource is not set, assuming that the connection attemp |
| | | 856 | | // As the initiating request for this connection attempt may complete concurrently at any time, |
| | | 857 | | // there is a race condition where the first call to SetTimeoutToPendingConnectionAttempt may happen |
| | | 858 | | // before we were able to set the CTS, so no timeout will be applied even though the request is already |
| | 0 | 859 | | waiter.ConnectionCancellationTokenSource = cts; |
| | | 860 | | |
| | | 861 | | // To fix that, we check whether the waiter already completed now that we're holding a lock. |
| | | 862 | | // If it had, call SetTimeoutToPendingConnectionAttempt again now that the CTS is set. |
| | 0 | 863 | | if (waiter.Task.IsCompleted) |
| | 0 | 864 | | { |
| | 0 | 865 | | waiter.SetTimeoutToPendingConnectionAttempt(this, requestCancelled: waiter.Task.IsCanceled); |
| | 0 | 866 | | waiter.ConnectionCancellationTokenSource = null; |
| | 0 | 867 | | } |
| | 0 | 868 | | } |
| | | 869 | | |
| | 0 | 870 | | return cts; |
| | 0 | 871 | | } |
| | | 872 | | |
| | | 873 | | private static Exception CreateConnectTimeoutException(OperationCanceledException oce) |
| | 0 | 874 | | { |
| | | 875 | | // The pattern for request timeouts (on HttpClient) is to throw an OCE with an inner exception of TimeoutExc |
| | | 876 | | // Do the same for ConnectTimeout-based timeouts. |
| | 0 | 877 | | TimeoutException te = new TimeoutException(SR.net_http_connect_timedout, oce.InnerException); |
| | 0 | 878 | | Exception newException = CancellationHelper.CreateOperationCanceledException(te, oce.CancellationToken); |
| | 0 | 879 | | ExceptionDispatchInfo.SetCurrentStackTrace(newException); |
| | 0 | 880 | | return newException; |
| | 0 | 881 | | } |
| | | 882 | | |
| | | 883 | | [DoesNotReturn] |
| | | 884 | | private static void ThrowGetVersionException(HttpRequestMessage request, int desiredVersion, Exception? inner = |
| | 0 | 885 | | { |
| | 0 | 886 | | Debug.Assert(desiredVersion == 2 || desiredVersion == 3); |
| | | 887 | | |
| | 0 | 888 | | HttpRequestException ex = new(HttpRequestError.VersionNegotiationError, SR.Format(SR.net_http_requested_vers |
| | 0 | 889 | | if (request.IsExtendedConnectRequest && desiredVersion == 2) |
| | 0 | 890 | | { |
| | 0 | 891 | | ex.Data["HTTP2_ENABLED"] = false; |
| | 0 | 892 | | } |
| | | 893 | | |
| | 0 | 894 | | throw ex; |
| | | 895 | | } |
| | | 896 | | |
| | | 897 | | /// <summary> |
| | | 898 | | /// Determines whether a request that was sent over HTTP/2 can fall back to HTTP/1.1. |
| | | 899 | | /// This matches the version negotiation logic: a request can use HTTP/1.1 if its |
| | | 900 | | /// <see cref="HttpRequestMessage.Version"/> is less than 2.0 or if its |
| | | 901 | | /// <see cref="HttpRequestMessage.VersionPolicy"/> is <see cref="HttpVersionPolicy.RequestVersionOrLower"/>. |
| | | 902 | | /// </summary> |
| | | 903 | | internal static bool CanFallBackToHttp11(HttpRequestMessage request) => |
| | 0 | 904 | | request.Version.Major < 2 || request.VersionPolicy == HttpVersionPolicy.RequestVersionOrLower; |
| | | 905 | | |
| | | 906 | | private bool CheckExpirationOnGet(HttpConnectionBase connection) |
| | 0 | 907 | | { |
| | 0 | 908 | | Debug.Assert(!HasSyncObjLock); |
| | | 909 | | |
| | 0 | 910 | | if (connection.MarkedForEviction) |
| | 0 | 911 | | { |
| | 0 | 912 | | return true; |
| | | 913 | | } |
| | | 914 | | |
| | 0 | 915 | | TimeSpan pooledConnectionLifetime = _poolManager.Settings._pooledConnectionLifetime; |
| | 0 | 916 | | if (pooledConnectionLifetime != Timeout.InfiniteTimeSpan) |
| | 0 | 917 | | { |
| | 0 | 918 | | return connection.Age > pooledConnectionLifetime; |
| | | 919 | | } |
| | | 920 | | |
| | 0 | 921 | | return false; |
| | 0 | 922 | | } |
| | | 923 | | |
| | | 924 | | private bool CheckExpirationOnReturn(HttpConnectionBase connection) |
| | 0 | 925 | | { |
| | 0 | 926 | | if (connection.MarkedForEviction) |
| | 0 | 927 | | { |
| | 0 | 928 | | return true; |
| | | 929 | | } |
| | | 930 | | |
| | 0 | 931 | | TimeSpan lifetime = _poolManager.Settings._pooledConnectionLifetime; |
| | 0 | 932 | | if (lifetime != Timeout.InfiniteTimeSpan) |
| | 0 | 933 | | { |
| | 0 | 934 | | return lifetime == TimeSpan.Zero || connection.Age > lifetime; |
| | | 935 | | } |
| | | 936 | | |
| | 0 | 937 | | return false; |
| | 0 | 938 | | } |
| | | 939 | | |
| | | 940 | | /// <summary> |
| | | 941 | | /// Incremented at the start of each eviction evaluation pass. Connections record the generation at which they |
| | | 942 | | /// were last evaluated, so an HTTP/1.1 connection that was busy during a pass (and therefore not visible to it) |
| | | 943 | | /// can be re-evaluated in the background when it is returned to the pool. |
| | | 944 | | /// </summary> |
| | 0 | 945 | | internal int EvictionGeneration { get; private set; } |
| | | 946 | | |
| | | 947 | | /// <summary> |
| | | 948 | | /// Invokes the user-supplied <see cref="SocketsHttpHandler.ShouldEvictConnection"/> callback for each |
| | | 949 | | /// pooled connection and marks for eviction those the callback selects. List snapshots are taken under |
| | | 950 | | /// the pool lock; the per-connection checks are started outside the lock and intentionally not awaited. |
| | | 951 | | /// </summary> |
| | | 952 | | private void EvaluateConnectionsForEviction() |
| | 0 | 953 | | { |
| | 0 | 954 | | Debug.Assert(!HasSyncObjLock); |
| | | 955 | | |
| | 0 | 956 | | EvictionGeneration++; |
| | | 957 | | |
| | | 958 | | try |
| | 0 | 959 | | { |
| | | 960 | | // Each connection's eviction check is started but deliberately not awaited. The user callback may |
| | | 961 | | // block or take a long time, and awaiting the checks one by one would let a single slow callback |
| | | 962 | | // stall the evaluation of every other connection (and every subsequent eviction pass). Each |
| | | 963 | | // connection guards against overlapping runs of its own callback, so a connection whose callback is |
| | | 964 | | // still pending is simply skipped by later passes. A callback that completes synchronously still |
| | | 965 | | // completes inline here, so this only changes behavior when a callback does not complete promptly. |
| | | 966 | | |
| | | 967 | | // HTTP/1.1: the idle stack is lock-free and its enumerator returns a snapshot, so we can inspect |
| | | 968 | | // connections without removing them. Connections currently in use (and therefore not on the stack) |
| | | 969 | | // are evaluated by ReturnHttp11Connection when they are returned to the pool. |
| | 0 | 970 | | foreach (HttpConnection connection in _http11Connections) |
| | 0 | 971 | | { |
| | 0 | 972 | | _ = connection.EvaluateForEvictionAsync(); |
| | 0 | 973 | | } |
| | | 974 | | |
| | | 975 | | // HTTP/2: Get the list of available connections under the lock, then evaluate outside of it. |
| | 0 | 976 | | ReadOnlySpan<Http2Connection> http2Connections = default; |
| | 0 | 977 | | lock (SyncObj) |
| | 0 | 978 | | { |
| | 0 | 979 | | if (_availableHttp2Connections is { Count: > 0 } http2) |
| | 0 | 980 | | { |
| | 0 | 981 | | http2Connections = CollectionsMarshal.AsSpan(http2); |
| | 0 | 982 | | } |
| | 0 | 983 | | } |
| | | 984 | | |
| | 0 | 985 | | foreach (Http2Connection connection in http2Connections) |
| | 0 | 986 | | { |
| | | 987 | | // The span may be modified concurrently, so check for null connections |
| | 0 | 988 | | _ = connection?.EvaluateForEvictionAsync(); |
| | 0 | 989 | | } |
| | | 990 | | |
| | 0 | 991 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3) |
| | 0 | 992 | | { |
| | 0 | 993 | | ReadOnlySpan<Http3Connection> http3Connections = default; |
| | 0 | 994 | | lock (SyncObj) |
| | 0 | 995 | | { |
| | 0 | 996 | | if (_availableHttp3Connections is { Count: > 0 } http3) |
| | 0 | 997 | | { |
| | 0 | 998 | | http3Connections = CollectionsMarshal.AsSpan(http3); |
| | 0 | 999 | | } |
| | 0 | 1000 | | } |
| | | 1001 | | |
| | 0 | 1002 | | foreach (Http3Connection connection in http3Connections) |
| | 0 | 1003 | | { |
| | | 1004 | | // The span may be modified concurrently, so check for null connections |
| | 0 | 1005 | | _ = connection?.EvaluateForEvictionAsync(); |
| | 0 | 1006 | | } |
| | 0 | 1007 | | } |
| | 0 | 1008 | | } |
| | 0 | 1009 | | catch (Exception e) |
| | 0 | 1010 | | { |
| | 0 | 1011 | | Debug.Fail($"Unexpected exception while evaluating connections for eviction: {e}"); |
| | | 1012 | | |
| | | 1013 | | if (NetEventSource.Log.IsEnabled()) Trace($"Unexpected exception while evaluating connections for evicti |
| | | 1014 | | } |
| | 0 | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | /// <summary> |
| | | 1018 | | /// Disposes the connection pool. This is only needed when the pool currently contains |
| | | 1019 | | /// or has associated connections. |
| | | 1020 | | /// </summary> |
| | | 1021 | | public void Dispose() |
| | 0 | 1022 | | { |
| | 0 | 1023 | | List<HttpConnectionBase>? toDispose = null; |
| | | 1024 | | |
| | 0 | 1025 | | lock (SyncObj) |
| | 0 | 1026 | | { |
| | 0 | 1027 | | if (_disposed) |
| | 0 | 1028 | | { |
| | 0 | 1029 | | return; |
| | | 1030 | | } |
| | | 1031 | | |
| | 0 | 1032 | | _disposed = true; |
| | 0 | 1033 | | _http11RequestQueueIsEmptyAndNotDisposed = false; |
| | | 1034 | | |
| | 0 | 1035 | | if (NetEventSource.Log.IsEnabled()) Trace("Disposing the pool."); |
| | | 1036 | | |
| | 0 | 1037 | | if (_availableHttp2Connections is not null) |
| | 0 | 1038 | | { |
| | 0 | 1039 | | toDispose = [.. _availableHttp2Connections]; |
| | 0 | 1040 | | _associatedHttp2ConnectionCount -= _availableHttp2Connections.Count; |
| | 0 | 1041 | | _availableHttp2Connections.Clear(); |
| | 0 | 1042 | | } |
| | | 1043 | | |
| | 0 | 1044 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && _availableHttp3Connections is not null) |
| | 0 | 1045 | | { |
| | 0 | 1046 | | toDispose ??= new(); |
| | 0 | 1047 | | toDispose.AddRange(_availableHttp3Connections); |
| | 0 | 1048 | | _associatedHttp3ConnectionCount -= _availableHttp3Connections.Count; |
| | 0 | 1049 | | _availableHttp3Connections.Clear(); |
| | 0 | 1050 | | } |
| | | 1051 | | |
| | 0 | 1052 | | if (_authorityExpireTimer != null) |
| | 0 | 1053 | | { |
| | 0 | 1054 | | _authorityExpireTimer.Dispose(); |
| | 0 | 1055 | | _authorityExpireTimer = null; |
| | 0 | 1056 | | } |
| | | 1057 | | |
| | 0 | 1058 | | if (_altSvcBlocklistTimerCancellation != null) |
| | 0 | 1059 | | { |
| | 0 | 1060 | | _altSvcBlocklistTimerCancellation.Cancel(); |
| | 0 | 1061 | | _altSvcBlocklistTimerCancellation.Dispose(); |
| | 0 | 1062 | | _altSvcBlocklistTimerCancellation = null; |
| | 0 | 1063 | | } |
| | | 1064 | | |
| | 0 | 1065 | | Debug.Assert((_availableHttp2Connections?.Count ?? 0) == 0, $"Expected {nameof(_availableHttp2Connection |
| | 0 | 1066 | | } |
| | | 1067 | | |
| | | 1068 | | // Dispose connections outside the lock to avoid lock re-entrancy issues. |
| | | 1069 | | |
| | | 1070 | | // This will trigger the disposal of Http11 connections. |
| | | 1071 | | // Note: Http11 connections will decrement the _associatedHttp11ConnectionCount when disposed. |
| | | 1072 | | // Http2 connections will not, hence the difference in handing _associatedHttp2ConnectionCount. |
| | 0 | 1073 | | ProcessHttp11RequestQueue(null); |
| | | 1074 | | |
| | 0 | 1075 | | toDispose?.ForEach(c => c.Dispose()); |
| | 0 | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | /// <summary> |
| | | 1079 | | /// Removes any unusable connections from the pool, and if the pool |
| | | 1080 | | /// is then empty and stale, disposes of it. |
| | | 1081 | | /// </summary> |
| | | 1082 | | /// <returns> |
| | | 1083 | | /// true if the pool disposes of itself; otherwise, false. |
| | | 1084 | | /// </returns> |
| | | 1085 | | public bool CleanCacheAndDisposeIfUnused() |
| | 0 | 1086 | | { |
| | 0 | 1087 | | TimeSpan pooledConnectionLifetime = _poolManager.Settings._pooledConnectionLifetime; |
| | 0 | 1088 | | TimeSpan pooledConnectionIdleTimeout = _poolManager.Settings._pooledConnectionIdleTimeout; |
| | 0 | 1089 | | long nowTicks = Environment.TickCount64; |
| | | 1090 | | |
| | | 1091 | | // If the user supplied an eviction callback, give them a chance to mark pooled connections for |
| | | 1092 | | // eviction before we scavenge. The callback is asynchronous and may be slow (e.g. perform a DNS |
| | | 1093 | | // lookup), so it runs off the maintenance timer thread; connections it evicts are retired by a later |
| | | 1094 | | // scavenge pass or by the get/return paths. |
| | 0 | 1095 | | if (_poolManager.Settings._shouldEvictConnection is not null) |
| | 0 | 1096 | | { |
| | 0 | 1097 | | EvaluateConnectionsForEviction(); |
| | 0 | 1098 | | } |
| | | 1099 | | |
| | 0 | 1100 | | List<HttpConnectionBase>? toDispose = null; |
| | | 1101 | | |
| | 0 | 1102 | | lock (SyncObj) |
| | 0 | 1103 | | { |
| | | 1104 | | // If there are now no connections associated with this pool, we can dispose of it. We |
| | | 1105 | | // avoid aggressively cleaning up pools that have recently been used but currently aren't; |
| | | 1106 | | // if a pool was used since the last time we cleaned up, give it another chance. New pools |
| | | 1107 | | // start out saying they've recently been used, to give them a bit of breathing room and time |
| | | 1108 | | // for the initial collection to be added to it. |
| | 0 | 1109 | | if (!_usedSinceLastCleanup && _associatedHttp11ConnectionCount == 0 && _associatedHttp2ConnectionCount = |
| | 0 | 1110 | | // An HTTP/2 connection may still be draining requests (e.g. after a GOAWAY frame) and need heart be |
| | 0 | 1111 | | (_http2ConnectionsForHeartBeat?.Count ?? 0) == 0) |
| | 0 | 1112 | | { |
| | 0 | 1113 | | _disposed = true; |
| | 0 | 1114 | | return true; // Pool is disposed of. It should be removed. |
| | | 1115 | | } |
| | | 1116 | | |
| | | 1117 | | // Reset the cleanup flag. Any pools that are empty and not used since the last cleanup |
| | | 1118 | | // will be purged next time around. |
| | 0 | 1119 | | _usedSinceLastCleanup = false; |
| | | 1120 | | |
| | 0 | 1121 | | ScavengeHttp11ConnectionStack(this, _http11Connections, ref toDispose, nowTicks, pooledConnectionLifetim |
| | | 1122 | | |
| | 0 | 1123 | | if (_availableHttp2Connections is not null) |
| | 0 | 1124 | | { |
| | 0 | 1125 | | int removed = ScavengeHttp2ConnectionList(_availableHttp2Connections, ref toDispose, nowTicks, poole |
| | 0 | 1126 | | _associatedHttp2ConnectionCount -= removed; |
| | | 1127 | | |
| | | 1128 | | // Note: Http11 connections will decrement the _associatedHttp11ConnectionCount when disposed. |
| | | 1129 | | // Http2 connections will not, hence the difference in handing _associatedHttp2ConnectionCount. |
| | 0 | 1130 | | } |
| | 0 | 1131 | | if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && _availableHttp3Connections is not null) |
| | 0 | 1132 | | { |
| | 0 | 1133 | | int removed = ScavengeHttp3ConnectionList(_availableHttp3Connections, ref toDispose, nowTicks, poole |
| | 0 | 1134 | | _associatedHttp3ConnectionCount -= removed; |
| | | 1135 | | |
| | | 1136 | | // Note: Http11 connections will decrement the _associatedHttp11ConnectionCount when disposed. |
| | | 1137 | | // Http3 connections will not, hence the difference in handing _associatedHttp3ConnectionCount. |
| | 0 | 1138 | | } |
| | 0 | 1139 | | } |
| | | 1140 | | |
| | | 1141 | | // Dispose the stale connections outside the pool lock, to avoid holding the lock too long. |
| | | 1142 | | // Dispose them asynchronously to not to block the caller on closing the SslStream or NetworkStream. |
| | 0 | 1143 | | if (toDispose is not null) |
| | 0 | 1144 | | { |
| | 0 | 1145 | | Task.Factory.StartNew(static s => ((List<HttpConnectionBase>)s!).ForEach(c => c.Dispose()), toDispose, |
| | 0 | 1146 | | CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); |
| | 0 | 1147 | | } |
| | | 1148 | | |
| | | 1149 | | // Pool is active. Should not be removed. |
| | 0 | 1150 | | return false; |
| | 0 | 1151 | | } |
| | | 1152 | | |
| | | 1153 | | // For diagnostic purposes |
| | | 1154 | | public override string ToString() => |
| | 0 | 1155 | | $"{nameof(HttpConnectionPool)} " + |
| | 0 | 1156 | | (_proxyUri == null ? |
| | 0 | 1157 | | (_sslOptionsHttp11 == null ? |
| | 0 | 1158 | | $"http://{_originAuthority}" : |
| | 0 | 1159 | | $"https://{_originAuthority}" + (_sslOptionsHttp11.TargetHost != _originAuthority.IdnHost ? $", SSL |
| | 0 | 1160 | | (_sslOptionsHttp11 == null ? |
| | 0 | 1161 | | $"Proxy {_proxyUri}" : |
| | 0 | 1162 | | $"https://{_originAuthority}/ tunnelled via Proxy {_proxyUri}" + (_sslOptionsHttp11.TargetHost != _o |
| | | 1163 | | |
| | | 1164 | | public void Trace(string? message, [CallerMemberName] string? memberName = null) => |
| | 0 | 1165 | | NetEventSource.Log.HandlerMessage( |
| | 0 | 1166 | | GetHashCode(), // pool ID |
| | 0 | 1167 | | 0, // connection ID |
| | 0 | 1168 | | 0, // request ID |
| | 0 | 1169 | | memberName, // method name |
| | 0 | 1170 | | message); // message |
| | | 1171 | | } |
| | | 1172 | | } |
| | | 1173 | | |