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