| | | 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.Globalization; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Runtime.Versioning; |
| | | 8 | | using System.Security.Authentication; |
| | | 9 | | using System.Security.Cryptography.X509Certificates; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Diagnostics.Metrics; |
| | | 13 | | #if TARGET_WASI |
| | | 14 | | using System.Diagnostics; |
| | | 15 | | using System.Net.Http.Metrics; |
| | | 16 | | using HttpHandlerType = System.Net.Http.WasiHttpHandler; |
| | | 17 | | #elif TARGET_BROWSER |
| | | 18 | | using System.Diagnostics; |
| | | 19 | | using System.Net.Http.Metrics; |
| | | 20 | | using HttpHandlerType = System.Net.Http.BrowserHttpHandler; |
| | | 21 | | #else |
| | | 22 | | using HttpHandlerType = System.Net.Http.SocketsHttpHandler; |
| | | 23 | | #endif |
| | | 24 | | |
| | | 25 | | namespace System.Net.Http |
| | | 26 | | { |
| | | 27 | | public partial class HttpClientHandler : HttpMessageHandler |
| | | 28 | | { |
| | | 29 | | private readonly HttpHandlerType _underlyingHandler; |
| | | 30 | | |
| | | 31 | | #if TARGET_BROWSER || TARGET_WASI |
| | | 32 | | private IMeterFactory? _meterFactory; |
| | | 33 | | private HttpMessageHandler? _firstHandler; // DiagnosticsHandler or MetricsHandler, depending on global configur |
| | | 34 | | |
| | | 35 | | private HttpMessageHandler Handler |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | | 39 | | if (_firstHandler != null) |
| | | 40 | | { |
| | | 41 | | return _firstHandler; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | HttpMessageHandler handler = _underlyingHandler; |
| | | 45 | | |
| | | 46 | | // MetricsHandler should be descendant of DiagnosticsHandler in the handler chain to make sure the 'http |
| | | 47 | | // metric is recorded before stopping the request Activity. This is needed to make sure that our telemet |
| | | 48 | | // Since HttpClientHandler.Proxy is unsupported on most platforms, don't bother passing it to telemetry |
| | | 49 | | if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled) |
| | | 50 | | { |
| | | 51 | | handler = new MetricsHandler(handler, _meterFactory, proxy: null, out _); |
| | | 52 | | } |
| | | 53 | | if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation) |
| | | 54 | | { |
| | | 55 | | handler = new DiagnosticsHandler(handler, DistributedContextPropagator.Current, proxy: null); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Ensure a single handler is used for all requests. |
| | | 59 | | if (Interlocked.CompareExchange(ref _firstHandler, handler, null) != null) |
| | | 60 | | { |
| | | 61 | | handler.Dispose(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | return _firstHandler; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | #else |
| | 0 | 68 | | private HttpHandlerType Handler => _underlyingHandler; |
| | | 69 | | #endif |
| | | 70 | | |
| | | 71 | | private volatile bool _disposed; |
| | | 72 | | |
| | 0 | 73 | | public HttpClientHandler() |
| | 0 | 74 | | { |
| | 0 | 75 | | _underlyingHandler = new HttpHandlerType(); |
| | | 76 | | |
| | 0 | 77 | | ClientCertificateOptions = ClientCertificateOption.Manual; |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | protected override void Dispose(bool disposing) |
| | 0 | 81 | | { |
| | 0 | 82 | | if (disposing && !_disposed) |
| | 0 | 83 | | { |
| | 0 | 84 | | _disposed = true; |
| | 0 | 85 | | _underlyingHandler.Dispose(); |
| | 0 | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | base.Dispose(disposing); |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | public virtual bool SupportsAutomaticDecompression => HttpHandlerType.SupportsAutomaticDecompression; |
| | 0 | 92 | | public virtual bool SupportsProxy => HttpHandlerType.SupportsProxy; |
| | 0 | 93 | | public virtual bool SupportsRedirectConfiguration => HttpHandlerType.SupportsRedirectConfiguration; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets or sets the <see cref="IMeterFactory"/> to create a custom <see cref="Meter"/> for the <see cref="HttpC |
| | | 97 | | /// </summary> |
| | | 98 | | /// <remarks> |
| | | 99 | | /// When <see cref="MeterFactory"/> is set to a non-<see langword="null"/> value, all metrics emitted by the <se |
| | | 100 | | /// will be recorded using the <see cref="Meter"/> provided by the <see cref="IMeterFactory"/>. |
| | | 101 | | /// </remarks> |
| | | 102 | | [CLSCompliant(false)] |
| | | 103 | | public IMeterFactory? MeterFactory |
| | | 104 | | { |
| | | 105 | | #if TARGET_BROWSER || TARGET_WASI |
| | | 106 | | get => _meterFactory; |
| | | 107 | | set |
| | | 108 | | { |
| | | 109 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 110 | | if (_firstHandler != null) |
| | | 111 | | { |
| | | 112 | | throw new InvalidOperationException(SR.net_http_operation_started); |
| | | 113 | | } |
| | | 114 | | _meterFactory = value; |
| | | 115 | | } |
| | | 116 | | #else |
| | 0 | 117 | | get => _underlyingHandler.MeterFactory; |
| | 0 | 118 | | set => _underlyingHandler.MeterFactory = value; |
| | | 119 | | #endif |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | [UnsupportedOSPlatform("browser")] |
| | | 123 | | public bool UseCookies |
| | | 124 | | { |
| | 0 | 125 | | get => _underlyingHandler.UseCookies; |
| | 0 | 126 | | set => _underlyingHandler.UseCookies = value; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | [UnsupportedOSPlatform("browser")] |
| | | 130 | | public CookieContainer CookieContainer |
| | | 131 | | { |
| | 0 | 132 | | get => _underlyingHandler.CookieContainer; |
| | | 133 | | set |
| | 0 | 134 | | { |
| | 0 | 135 | | ArgumentNullException.ThrowIfNull(value); |
| | | 136 | | |
| | 0 | 137 | | _underlyingHandler.CookieContainer = value; |
| | 0 | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | [UnsupportedOSPlatform("browser")] |
| | | 142 | | public DecompressionMethods AutomaticDecompression |
| | | 143 | | { |
| | 0 | 144 | | get => _underlyingHandler.AutomaticDecompression; |
| | 0 | 145 | | set => _underlyingHandler.AutomaticDecompression = value; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | [UnsupportedOSPlatform("browser")] |
| | | 149 | | public bool UseProxy |
| | | 150 | | { |
| | 0 | 151 | | get => _underlyingHandler.UseProxy; |
| | 0 | 152 | | set => _underlyingHandler.UseProxy = value; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | [UnsupportedOSPlatform("browser")] |
| | | 156 | | [UnsupportedOSPlatform("ios")] |
| | | 157 | | [UnsupportedOSPlatform("tvos")] |
| | | 158 | | public IWebProxy? Proxy |
| | | 159 | | { |
| | 0 | 160 | | get => _underlyingHandler.Proxy; |
| | 0 | 161 | | set => _underlyingHandler.Proxy = value; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | [UnsupportedOSPlatform("browser")] |
| | | 165 | | public ICredentials? DefaultProxyCredentials |
| | | 166 | | { |
| | 0 | 167 | | get => _underlyingHandler.DefaultProxyCredentials; |
| | 0 | 168 | | set => _underlyingHandler.DefaultProxyCredentials = value; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | [UnsupportedOSPlatform("browser")] |
| | | 172 | | public bool PreAuthenticate |
| | | 173 | | { |
| | 0 | 174 | | get => _underlyingHandler.PreAuthenticate; |
| | 0 | 175 | | set => _underlyingHandler.PreAuthenticate = value; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | [UnsupportedOSPlatform("browser")] |
| | | 179 | | public bool UseDefaultCredentials |
| | | 180 | | { |
| | | 181 | | // SocketsHttpHandler doesn't have a separate UseDefaultCredentials property. There |
| | | 182 | | // is just a Credentials property. So, we need to map the behavior. |
| | 0 | 183 | | get => _underlyingHandler.Credentials == CredentialCache.DefaultCredentials; |
| | | 184 | | set |
| | 0 | 185 | | { |
| | 0 | 186 | | if (value) |
| | 0 | 187 | | { |
| | 0 | 188 | | _underlyingHandler.Credentials = CredentialCache.DefaultCredentials; |
| | 0 | 189 | | } |
| | | 190 | | else |
| | 0 | 191 | | { |
| | 0 | 192 | | if (_underlyingHandler.Credentials == CredentialCache.DefaultCredentials) |
| | 0 | 193 | | { |
| | | 194 | | // Only clear out the Credentials property if it was a DefaultCredentials. |
| | 0 | 195 | | _underlyingHandler.Credentials = null; |
| | 0 | 196 | | } |
| | 0 | 197 | | } |
| | 0 | 198 | | } |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | [UnsupportedOSPlatform("browser")] |
| | | 202 | | public ICredentials? Credentials |
| | | 203 | | { |
| | 0 | 204 | | get => _underlyingHandler.Credentials; |
| | 0 | 205 | | set => _underlyingHandler.Credentials = value; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | public bool AllowAutoRedirect |
| | | 209 | | { |
| | 0 | 210 | | get => _underlyingHandler.AllowAutoRedirect; |
| | 0 | 211 | | set => _underlyingHandler.AllowAutoRedirect = value; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | [UnsupportedOSPlatform("browser")] |
| | | 215 | | public int MaxAutomaticRedirections |
| | | 216 | | { |
| | 0 | 217 | | get => _underlyingHandler.MaxAutomaticRedirections; |
| | 0 | 218 | | set => _underlyingHandler.MaxAutomaticRedirections = value; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | [UnsupportedOSPlatform("browser")] |
| | | 222 | | public int MaxConnectionsPerServer |
| | | 223 | | { |
| | 0 | 224 | | get => _underlyingHandler.MaxConnectionsPerServer; |
| | 0 | 225 | | set => _underlyingHandler.MaxConnectionsPerServer = value; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public long MaxRequestContentBufferSize |
| | | 229 | | { |
| | | 230 | | // This property is not supported. In the .NET Framework it was only used when the handler needed to |
| | | 231 | | // automatically buffer the request content. That only happened if neither 'Content-Length' nor |
| | | 232 | | // 'Transfer-Encoding: chunked' request headers were specified. So, the handler thus needed to buffer |
| | | 233 | | // in the request content to determine its length and then would choose 'Content-Length' semantics when |
| | | 234 | | // POST'ing. In .NET Core, the handler will resolve the ambiguity by always choosing |
| | | 235 | | // 'Transfer-Encoding: chunked'. The handler will never automatically buffer in the request content. |
| | | 236 | | get |
| | 0 | 237 | | { |
| | 0 | 238 | | return 0; // Returning zero is appropriate since in .NET Framework it means no limit. |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | set |
| | 0 | 242 | | { |
| | 0 | 243 | | ArgumentOutOfRangeException.ThrowIfNegative(value); |
| | | 244 | | |
| | 0 | 245 | | if (value > HttpContent.MaxBufferSize) |
| | 0 | 246 | | { |
| | 0 | 247 | | throw new ArgumentOutOfRangeException(nameof(value), value, |
| | 0 | 248 | | SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_limit, |
| | 0 | 249 | | HttpContent.MaxBufferSize)); |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 253 | | |
| | | 254 | | // No-op on property setter. |
| | 0 | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | [UnsupportedOSPlatform("browser")] |
| | | 259 | | public int MaxResponseHeadersLength |
| | | 260 | | { |
| | 0 | 261 | | get => _underlyingHandler.MaxResponseHeadersLength; |
| | 0 | 262 | | set => _underlyingHandler.MaxResponseHeadersLength = value; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | public ClientCertificateOption ClientCertificateOptions |
| | | 266 | | { |
| | 0 | 267 | | get => _underlyingHandler.ClientCertificateOptions; |
| | | 268 | | set |
| | 0 | 269 | | { |
| | 0 | 270 | | switch (value) |
| | | 271 | | { |
| | | 272 | | case ClientCertificateOption.Manual: |
| | | 273 | | #if !(TARGET_BROWSER || TARGET_WASI) |
| | 0 | 274 | | ThrowForModifiedManagedSslOptionsIfStarted(); |
| | 0 | 275 | | _underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCert |
| | | 276 | | #endif |
| | 0 | 277 | | break; |
| | | 278 | | |
| | | 279 | | case ClientCertificateOption.Automatic: |
| | | 280 | | #if !(TARGET_BROWSER || TARGET_WASI) |
| | 0 | 281 | | ThrowForModifiedManagedSslOptionsIfStarted(); |
| | 0 | 282 | | _underlyingHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCert |
| | | 283 | | #endif |
| | 0 | 284 | | break; |
| | | 285 | | |
| | | 286 | | default: |
| | 0 | 287 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 288 | | } |
| | 0 | 289 | | _underlyingHandler.ClientCertificateOptions = value; |
| | 0 | 290 | | } |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | [UnsupportedOSPlatform("browser")] |
| | | 294 | | public X509CertificateCollection ClientCertificates |
| | | 295 | | { |
| | | 296 | | get |
| | 0 | 297 | | { |
| | 0 | 298 | | if (ClientCertificateOptions != ClientCertificateOption.Manual) |
| | 0 | 299 | | { |
| | 0 | 300 | | throw new InvalidOperationException(SR.Format(SR.net_http_invalid_enable_first, nameof(ClientCertifi |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | return _underlyingHandler.SslOptions.ClientCertificates ?? |
| | 0 | 304 | | (_underlyingHandler.SslOptions.ClientCertificates = new X509CertificateCollection()); |
| | 0 | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | [UnsupportedOSPlatform("browser")] |
| | | 309 | | public Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool>? ServerCertificateCustomVa |
| | | 310 | | { |
| | | 311 | | #if TARGET_BROWSER || TARGET_WASI |
| | | 312 | | get => throw new PlatformNotSupportedException(); |
| | | 313 | | set => throw new PlatformNotSupportedException(); |
| | | 314 | | #else |
| | 0 | 315 | | get => (_underlyingHandler.SslOptions.RemoteCertificateValidationCallback?.Target as ConnectHelper.Certifica |
| | | 316 | | set |
| | 0 | 317 | | { |
| | 0 | 318 | | ThrowForModifiedManagedSslOptionsIfStarted(); |
| | 0 | 319 | | _underlyingHandler.SslOptions.RemoteCertificateValidationCallback = value != null ? |
| | 0 | 320 | | new ConnectHelper.CertificateCallbackMapper(value).ForSocketsHttpHandler : |
| | 0 | 321 | | null; |
| | 0 | 322 | | } |
| | | 323 | | #endif |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | [UnsupportedOSPlatform("browser")] |
| | | 327 | | public bool CheckCertificateRevocationList |
| | | 328 | | { |
| | 0 | 329 | | get => _underlyingHandler.SslOptions.CertificateRevocationCheckMode == X509RevocationMode.Online; |
| | | 330 | | set |
| | 0 | 331 | | { |
| | 0 | 332 | | ThrowForModifiedManagedSslOptionsIfStarted(); |
| | 0 | 333 | | _underlyingHandler.SslOptions.CertificateRevocationCheckMode = value ? X509RevocationMode.Online : X509R |
| | 0 | 334 | | } |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | [UnsupportedOSPlatform("browser")] |
| | | 338 | | public SslProtocols SslProtocols |
| | | 339 | | { |
| | 0 | 340 | | get => _underlyingHandler.SslOptions.EnabledSslProtocols; |
| | | 341 | | set |
| | 0 | 342 | | { |
| | 0 | 343 | | ThrowForModifiedManagedSslOptionsIfStarted(); |
| | 0 | 344 | | _underlyingHandler.SslOptions.EnabledSslProtocols = value; |
| | 0 | 345 | | } |
| | | 346 | | } |
| | | 347 | | |
| | 0 | 348 | | public IDictionary<string, object?> Properties => _underlyingHandler.Properties; |
| | | 349 | | |
| | | 350 | | // |
| | | 351 | | // Attributes are commented out due to https://github.com/dotnet/arcade/issues/7585 |
| | | 352 | | // API compat will fail until this is fixed |
| | | 353 | | // |
| | | 354 | | [UnsupportedOSPlatform("android")] |
| | | 355 | | [UnsupportedOSPlatform("browser")] |
| | | 356 | | [UnsupportedOSPlatform("ios")] |
| | | 357 | | [UnsupportedOSPlatform("tvos")] |
| | | 358 | | protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationT |
| | 0 | 359 | | { |
| | | 360 | | #if TARGET_BROWSER || TARGET_WASI |
| | | 361 | | throw new PlatformNotSupportedException(); |
| | | 362 | | #else |
| | 0 | 363 | | ArgumentNullException.ThrowIfNull(request); |
| | 0 | 364 | | return Handler.Send(request, cancellationToken); |
| | | 365 | | #endif |
| | 0 | 366 | | } |
| | | 367 | | |
| | | 368 | | protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ca |
| | 0 | 369 | | { |
| | 0 | 370 | | ArgumentNullException.ThrowIfNull(request); |
| | 0 | 371 | | return Handler.SendAsync(request, cancellationToken); |
| | 0 | 372 | | } |
| | | 373 | | |
| | | 374 | | // lazy-load the validator func so it can be trimmed by the ILLinker if it isn't used. |
| | | 375 | | [UnsupportedOSPlatform("browser")] |
| | | 376 | | public static Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> DangerousAcceptAnyS |
| | 0 | 377 | | field ?? |
| | 0 | 378 | | Interlocked.CompareExchange(ref field, delegate { return true; }, null) ?? |
| | 0 | 379 | | field; |
| | | 380 | | |
| | | 381 | | private void ThrowForModifiedManagedSslOptionsIfStarted() |
| | 0 | 382 | | { |
| | | 383 | | // Hack to trigger an InvalidOperationException if a property that's stored on |
| | | 384 | | // SslOptions is changed, since SslOptions itself does not do any such checks. |
| | 0 | 385 | | _underlyingHandler.SslOptions = _underlyingHandler.SslOptions; |
| | 0 | 386 | | } |
| | | 387 | | } |
| | | 388 | | } |