| | | 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.Diagnostics.Metrics; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Net.Http.Metrics; |
| | | 10 | | using System.Net.Security; |
| | | 11 | | using System.Runtime.ExceptionServices; |
| | | 12 | | using System.Runtime.Versioning; |
| | | 13 | | using System.Text; |
| | | 14 | | using System.Threading; |
| | | 15 | | using System.Threading.Tasks; |
| | | 16 | | |
| | | 17 | | namespace System.Net.Http |
| | | 18 | | { |
| | | 19 | | [UnsupportedOSPlatform("browser")] |
| | | 20 | | public sealed class SocketsHttpHandler : HttpMessageHandler |
| | | 21 | | { |
| | 0 | 22 | | private readonly HttpConnectionSettings _settings = new HttpConnectionSettings(); |
| | | 23 | | private HttpMessageHandlerStage? _handler; |
| | | 24 | | private Task<HttpMessageHandlerStage>? _handlerChainSetupTask; |
| | | 25 | | private Func<HttpConnectionSettings, HttpMessageHandlerStage, HttpMessageHandlerStage>? _decompressionHandlerFac |
| | | 26 | | private bool _disposed; |
| | | 27 | | |
| | | 28 | | // Accessed via UnsafeAccessor from HttpWebRequest. |
| | 0 | 29 | | internal HttpConnectionSettings Settings => _settings; |
| | | 30 | | |
| | | 31 | | private void CheckDisposedOrStarted() |
| | 0 | 32 | | { |
| | 0 | 33 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 34 | | if (_handler != null) |
| | 0 | 35 | | { |
| | 0 | 36 | | throw new InvalidOperationException(SR.net_http_operation_started); |
| | | 37 | | } |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets a value that indicates whether the handler is supported on the current platform. |
| | | 42 | | /// </summary> |
| | | 43 | | [UnsupportedOSPlatformGuard("browser")] |
| | 0 | 44 | | public static bool IsSupported => !OperatingSystem.IsBrowser() && !OperatingSystem.IsWasi(); |
| | | 45 | | |
| | | 46 | | public bool UseCookies |
| | | 47 | | { |
| | 0 | 48 | | get => _settings._useCookies; |
| | | 49 | | set |
| | 0 | 50 | | { |
| | 0 | 51 | | CheckDisposedOrStarted(); |
| | 0 | 52 | | _settings._useCookies = value; |
| | 0 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | [AllowNull] |
| | | 57 | | public CookieContainer CookieContainer |
| | | 58 | | { |
| | 0 | 59 | | get => _settings._cookieContainer ??= new CookieContainer(); |
| | | 60 | | set |
| | 0 | 61 | | { |
| | 0 | 62 | | CheckDisposedOrStarted(); |
| | 0 | 63 | | _settings._cookieContainer = value; |
| | 0 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public DecompressionMethods AutomaticDecompression |
| | | 68 | | { |
| | 0 | 69 | | get => _settings._automaticDecompression; |
| | | 70 | | set |
| | 0 | 71 | | { |
| | 0 | 72 | | CheckDisposedOrStarted(); |
| | 0 | 73 | | EnsureDecompressionHandlerFactory(); |
| | 0 | 74 | | _settings._automaticDecompression = value; |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public bool UseProxy |
| | | 79 | | { |
| | 0 | 80 | | get => _settings._useProxy; |
| | | 81 | | set |
| | 0 | 82 | | { |
| | 0 | 83 | | CheckDisposedOrStarted(); |
| | 0 | 84 | | _settings._useProxy = value; |
| | 0 | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public IWebProxy? Proxy |
| | | 89 | | { |
| | 0 | 90 | | get => _settings._proxy; |
| | | 91 | | set |
| | 0 | 92 | | { |
| | 0 | 93 | | CheckDisposedOrStarted(); |
| | 0 | 94 | | _settings._proxy = value; |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public ICredentials? DefaultProxyCredentials |
| | | 99 | | { |
| | 0 | 100 | | get => _settings._defaultProxyCredentials; |
| | | 101 | | set |
| | 0 | 102 | | { |
| | 0 | 103 | | CheckDisposedOrStarted(); |
| | 0 | 104 | | _settings._defaultProxyCredentials = value; |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public bool PreAuthenticate |
| | | 109 | | { |
| | 0 | 110 | | get => _settings._preAuthenticate; |
| | | 111 | | set |
| | 0 | 112 | | { |
| | 0 | 113 | | CheckDisposedOrStarted(); |
| | 0 | 114 | | _settings._preAuthenticate = value; |
| | 0 | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public ICredentials? Credentials |
| | | 119 | | { |
| | 0 | 120 | | get => _settings._credentials; |
| | | 121 | | set |
| | 0 | 122 | | { |
| | 0 | 123 | | CheckDisposedOrStarted(); |
| | 0 | 124 | | _settings._credentials = value; |
| | 0 | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public bool AllowAutoRedirect |
| | | 129 | | { |
| | 0 | 130 | | get => _settings._allowAutoRedirect; |
| | | 131 | | set |
| | 0 | 132 | | { |
| | 0 | 133 | | CheckDisposedOrStarted(); |
| | 0 | 134 | | _settings._allowAutoRedirect = value; |
| | 0 | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public int MaxAutomaticRedirections |
| | | 139 | | { |
| | 0 | 140 | | get => _settings._maxAutomaticRedirections; |
| | | 141 | | set |
| | 0 | 142 | | { |
| | 0 | 143 | | ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); |
| | | 144 | | |
| | 0 | 145 | | CheckDisposedOrStarted(); |
| | 0 | 146 | | _settings._maxAutomaticRedirections = value; |
| | 0 | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | public int MaxConnectionsPerServer |
| | | 151 | | { |
| | 0 | 152 | | get => _settings._maxConnectionsPerServer; |
| | | 153 | | set |
| | 0 | 154 | | { |
| | 0 | 155 | | ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); |
| | | 156 | | |
| | 0 | 157 | | CheckDisposedOrStarted(); |
| | 0 | 158 | | _settings._maxConnectionsPerServer = value; |
| | 0 | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public int MaxResponseDrainSize |
| | | 163 | | { |
| | 0 | 164 | | get => _settings._maxResponseDrainSize; |
| | | 165 | | set |
| | 0 | 166 | | { |
| | 0 | 167 | | ArgumentOutOfRangeException.ThrowIfNegative(value); |
| | | 168 | | |
| | 0 | 169 | | CheckDisposedOrStarted(); |
| | 0 | 170 | | _settings._maxResponseDrainSize = value; |
| | 0 | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | public TimeSpan ResponseDrainTimeout |
| | | 175 | | { |
| | 0 | 176 | | get => _settings._maxResponseDrainTime; |
| | | 177 | | set |
| | 0 | 178 | | { |
| | 0 | 179 | | if ((value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) || |
| | 0 | 180 | | (value.TotalMilliseconds > int.MaxValue)) |
| | 0 | 181 | | { |
| | 0 | 182 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | CheckDisposedOrStarted(); |
| | 0 | 186 | | _settings._maxResponseDrainTime = value; |
| | 0 | 187 | | } |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | public int MaxResponseHeadersLength |
| | | 191 | | { |
| | 0 | 192 | | get => _settings._maxResponseHeadersLength; |
| | | 193 | | set |
| | 0 | 194 | | { |
| | 0 | 195 | | ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); |
| | | 196 | | |
| | 0 | 197 | | CheckDisposedOrStarted(); |
| | 0 | 198 | | _settings._maxResponseHeadersLength = value; |
| | 0 | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | [AllowNull] |
| | | 203 | | public SslClientAuthenticationOptions SslOptions |
| | | 204 | | { |
| | 0 | 205 | | get => _settings._sslOptions ??= new SslClientAuthenticationOptions(); |
| | | 206 | | set |
| | 0 | 207 | | { |
| | 0 | 208 | | CheckDisposedOrStarted(); |
| | 0 | 209 | | _settings._sslOptions = value; |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | public TimeSpan PooledConnectionLifetime |
| | | 214 | | { |
| | 0 | 215 | | get => _settings._pooledConnectionLifetime; |
| | | 216 | | set |
| | 0 | 217 | | { |
| | 0 | 218 | | if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) |
| | 0 | 219 | | { |
| | 0 | 220 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | CheckDisposedOrStarted(); |
| | 0 | 224 | | _settings._pooledConnectionLifetime = value; |
| | 0 | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public TimeSpan PooledConnectionIdleTimeout |
| | | 229 | | { |
| | 0 | 230 | | get => _settings._pooledConnectionIdleTimeout; |
| | | 231 | | set |
| | 0 | 232 | | { |
| | 0 | 233 | | if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) |
| | 0 | 234 | | { |
| | 0 | 235 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | CheckDisposedOrStarted(); |
| | 0 | 239 | | _settings._pooledConnectionIdleTimeout = value; |
| | 0 | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | public TimeSpan ConnectTimeout |
| | | 244 | | { |
| | 0 | 245 | | get => _settings._connectTimeout; |
| | | 246 | | set |
| | 0 | 247 | | { |
| | 0 | 248 | | if ((value <= TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) || |
| | 0 | 249 | | (value.TotalMilliseconds > int.MaxValue)) |
| | 0 | 250 | | { |
| | 0 | 251 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | CheckDisposedOrStarted(); |
| | 0 | 255 | | _settings._connectTimeout = value; |
| | 0 | 256 | | } |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | public TimeSpan Expect100ContinueTimeout |
| | | 260 | | { |
| | 0 | 261 | | get => _settings._expect100ContinueTimeout; |
| | | 262 | | set |
| | 0 | 263 | | { |
| | 0 | 264 | | if ((value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) || |
| | 0 | 265 | | (value.TotalMilliseconds > int.MaxValue)) |
| | 0 | 266 | | { |
| | 0 | 267 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | CheckDisposedOrStarted(); |
| | 0 | 271 | | _settings._expect100ContinueTimeout = value; |
| | 0 | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Defines the initial HTTP2 stream receive window size for all connections opened by the this <see cref="Socke |
| | | 277 | | /// </summary> |
| | | 278 | | /// <remarks> |
| | | 279 | | /// Larger the values may lead to faster download speed, but potentially higher memory footprint. |
| | | 280 | | /// The property must be set to a value between 65535 and the configured maximum window size, which is 16777216 |
| | | 281 | | /// </remarks> |
| | | 282 | | public int InitialHttp2StreamWindowSize |
| | | 283 | | { |
| | 0 | 284 | | get => _settings._initialHttp2StreamWindowSize; |
| | | 285 | | set |
| | 0 | 286 | | { |
| | 0 | 287 | | if (value < HttpHandlerDefaults.DefaultInitialHttp2StreamWindowSize || value > GlobalHttpSettings.Socket |
| | 0 | 288 | | { |
| | 0 | 289 | | string message = SR.Format( |
| | 0 | 290 | | SR.net_http_http2_invalidinitialstreamwindowsize, |
| | 0 | 291 | | HttpHandlerDefaults.DefaultInitialHttp2StreamWindowSize, |
| | 0 | 292 | | GlobalHttpSettings.SocketsHttpHandler.MaxHttp2StreamWindowSize); |
| | | 293 | | |
| | 0 | 294 | | throw new ArgumentOutOfRangeException(nameof(InitialHttp2StreamWindowSize), message); |
| | | 295 | | } |
| | 0 | 296 | | CheckDisposedOrStarted(); |
| | 0 | 297 | | _settings._initialHttp2StreamWindowSize = value; |
| | 0 | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Gets or sets the keep alive ping delay. The client will send a keep alive ping to the server if it |
| | | 303 | | /// doesn't receive any frames on a connection for this period of time. This property is used together with |
| | | 304 | | /// <see cref="SocketsHttpHandler.KeepAlivePingTimeout"/> to close broken connections. |
| | | 305 | | /// <para> |
| | | 306 | | /// Delay value must be greater than or equal to 1 second. Set to <see cref="Timeout.InfiniteTimeSpan"/> to |
| | | 307 | | /// disable the keep alive ping. |
| | | 308 | | /// Defaults to <see cref="Timeout.InfiniteTimeSpan"/>. |
| | | 309 | | /// </para> |
| | | 310 | | /// </summary> |
| | | 311 | | public TimeSpan KeepAlivePingDelay |
| | | 312 | | { |
| | 0 | 313 | | get => _settings._keepAlivePingDelay; |
| | | 314 | | set |
| | 0 | 315 | | { |
| | 0 | 316 | | if (value.Ticks < TimeSpan.TicksPerSecond && value != Timeout.InfiniteTimeSpan) |
| | 0 | 317 | | { |
| | 0 | 318 | | throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(SR.net_http_value_must_be_grea |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | CheckDisposedOrStarted(); |
| | 0 | 322 | | _settings._keepAlivePingDelay = value; |
| | 0 | 323 | | } |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Gets or sets the keep alive ping timeout. Keep alive pings are sent when a period of inactivity exceeds |
| | | 328 | | /// the configured <see cref="KeepAlivePingDelay"/> value. The client will close the connection if it |
| | | 329 | | /// doesn't receive any frames within the timeout. |
| | | 330 | | /// <para> |
| | | 331 | | /// Timeout must be greater than or equal to 1 second. Set to <see cref="Timeout.InfiniteTimeSpan"/> to |
| | | 332 | | /// disable the keep alive ping timeout. |
| | | 333 | | /// Defaults to 20 seconds. |
| | | 334 | | /// </para> |
| | | 335 | | /// </summary> |
| | | 336 | | public TimeSpan KeepAlivePingTimeout |
| | | 337 | | { |
| | 0 | 338 | | get => _settings._keepAlivePingTimeout; |
| | | 339 | | set |
| | 0 | 340 | | { |
| | 0 | 341 | | if (value.Ticks < TimeSpan.TicksPerSecond && value != Timeout.InfiniteTimeSpan) |
| | 0 | 342 | | { |
| | 0 | 343 | | throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(SR.net_http_value_must_be_grea |
| | | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | CheckDisposedOrStarted(); |
| | 0 | 347 | | _settings._keepAlivePingTimeout = value; |
| | 0 | 348 | | } |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Gets or sets the keep alive ping behaviour. Keep alive pings are sent when a period of inactivity exceeds |
| | | 353 | | /// the configured <see cref="KeepAlivePingDelay"/> value. |
| | | 354 | | /// </summary> |
| | | 355 | | public HttpKeepAlivePingPolicy KeepAlivePingPolicy |
| | | 356 | | { |
| | 0 | 357 | | get => _settings._keepAlivePingPolicy; |
| | | 358 | | set |
| | 0 | 359 | | { |
| | 0 | 360 | | CheckDisposedOrStarted(); |
| | 0 | 361 | | _settings._keepAlivePingPolicy = value; |
| | 0 | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | /// <summary> |
| | | 366 | | /// Gets or sets a value that indicates whether additional HTTP/2 connections can be established to the same ser |
| | | 367 | | /// </summary> |
| | | 368 | | /// <remarks> |
| | | 369 | | /// Enabling multiple connections to the same server explicitly goes against <see href="https://www.rfc-editor.o |
| | | 370 | | /// </remarks> |
| | | 371 | | public bool EnableMultipleHttp2Connections |
| | | 372 | | { |
| | 0 | 373 | | get => _settings._enableMultipleHttp2Connections; |
| | | 374 | | set |
| | 0 | 375 | | { |
| | 0 | 376 | | CheckDisposedOrStarted(); |
| | | 377 | | |
| | 0 | 378 | | _settings._enableMultipleHttp2Connections = value; |
| | 0 | 379 | | } |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// Gets or sets a value that indicates whether additional HTTP/3 connections can be established to the same ser |
| | | 384 | | /// </summary> |
| | | 385 | | /// <remarks> |
| | | 386 | | /// Enabling multiple connections to the same server explicitly goes against <see href="https://www.rfc-editor.o |
| | | 387 | | /// </remarks> |
| | | 388 | | public bool EnableMultipleHttp3Connections |
| | | 389 | | { |
| | 0 | 390 | | get => _settings._enableMultipleHttp3Connections; |
| | | 391 | | set |
| | 0 | 392 | | { |
| | 0 | 393 | | CheckDisposedOrStarted(); |
| | | 394 | | |
| | 0 | 395 | | _settings._enableMultipleHttp3Connections = value; |
| | 0 | 396 | | } |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | internal const bool SupportsAutomaticDecompression = true; |
| | | 400 | | internal const bool SupportsProxy = true; |
| | | 401 | | internal const bool SupportsRedirectConfiguration = true; |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// When non-null, a custom callback used to open new connections. |
| | | 405 | | /// </summary> |
| | | 406 | | public Func<SocketsHttpConnectionContext, CancellationToken, ValueTask<Stream>>? ConnectCallback |
| | | 407 | | { |
| | 0 | 408 | | get => _settings._connectCallback; |
| | | 409 | | set |
| | 0 | 410 | | { |
| | 0 | 411 | | CheckDisposedOrStarted(); |
| | 0 | 412 | | _settings._connectCallback = value; |
| | 0 | 413 | | } |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Gets or sets a custom callback that provides access to the plaintext HTTP protocol stream. |
| | | 418 | | /// </summary> |
| | | 419 | | public Func<SocketsHttpPlaintextStreamFilterContext, CancellationToken, ValueTask<Stream>>? PlaintextStreamFilte |
| | | 420 | | { |
| | 0 | 421 | | get => _settings._plaintextStreamFilter; |
| | | 422 | | set |
| | 0 | 423 | | { |
| | 0 | 424 | | CheckDisposedOrStarted(); |
| | 0 | 425 | | _settings._plaintextStreamFilter = value; |
| | 0 | 426 | | } |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Gets a writable dictionary (that is, a map) of custom properties for the HttpClient requests. The dictionary |
| | | 431 | | /// </summary> |
| | | 432 | | public IDictionary<string, object?> Properties => |
| | 0 | 433 | | _settings._properties ??= new Dictionary<string, object?>(); |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Gets or sets a callback that returns the <see cref="Encoding"/> to encode the value for the specified reques |
| | | 437 | | /// or <see langword="null"/> to use the default behavior. |
| | | 438 | | /// </summary> |
| | | 439 | | public HeaderEncodingSelector<HttpRequestMessage>? RequestHeaderEncodingSelector |
| | | 440 | | { |
| | 0 | 441 | | get => _settings._requestHeaderEncodingSelector; |
| | | 442 | | set |
| | 0 | 443 | | { |
| | 0 | 444 | | CheckDisposedOrStarted(); |
| | 0 | 445 | | _settings._requestHeaderEncodingSelector = value; |
| | 0 | 446 | | } |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | /// <summary> |
| | | 450 | | /// Gets or sets a callback that returns the <see cref="Encoding"/> to decode the value for the specified respon |
| | | 451 | | /// or <see langword="null"/> to use the default behavior. |
| | | 452 | | /// </summary> |
| | | 453 | | public HeaderEncodingSelector<HttpRequestMessage>? ResponseHeaderEncodingSelector |
| | | 454 | | { |
| | 0 | 455 | | get => _settings._responseHeaderEncodingSelector; |
| | | 456 | | set |
| | 0 | 457 | | { |
| | 0 | 458 | | CheckDisposedOrStarted(); |
| | 0 | 459 | | _settings._responseHeaderEncodingSelector = value; |
| | 0 | 460 | | } |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | /// <summary> |
| | | 464 | | /// Gets or sets the <see cref="DistributedContextPropagator"/> to use when propagating the distributed trace an |
| | | 465 | | /// Use <see langword="null"/> to disable propagation. |
| | | 466 | | /// Defaults to <see cref="DistributedContextPropagator.Current"/>. |
| | | 467 | | /// </summary> |
| | | 468 | | [CLSCompliant(false)] |
| | | 469 | | public DistributedContextPropagator? ActivityHeadersPropagator |
| | | 470 | | { |
| | 0 | 471 | | get => _settings._activityHeadersPropagator; |
| | | 472 | | set |
| | 0 | 473 | | { |
| | 0 | 474 | | CheckDisposedOrStarted(); |
| | 0 | 475 | | _settings._activityHeadersPropagator = value; |
| | 0 | 476 | | } |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary> |
| | | 480 | | /// Gets or sets the <see cref="IMeterFactory"/> to create a custom <see cref="Meter"/> for the <see cref="Socke |
| | | 481 | | /// </summary> |
| | | 482 | | /// <remarks> |
| | | 483 | | /// When <see cref="MeterFactory"/> is set to a non-<see langword="null"/> value, all metrics emitted by the <se |
| | | 484 | | /// will be recorded using the <see cref="Meter"/> provided by the <see cref="IMeterFactory"/>. |
| | | 485 | | /// </remarks> |
| | | 486 | | [CLSCompliant(false)] |
| | | 487 | | public IMeterFactory? MeterFactory |
| | | 488 | | { |
| | 0 | 489 | | get => _settings._meterFactory; |
| | | 490 | | set |
| | 0 | 491 | | { |
| | 0 | 492 | | CheckDisposedOrStarted(); |
| | 0 | 493 | | _settings._meterFactory = value; |
| | 0 | 494 | | } |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | internal ClientCertificateOption ClientCertificateOptions |
| | | 498 | | { |
| | 0 | 499 | | get => _settings._clientCertificateOptions; |
| | | 500 | | set |
| | 0 | 501 | | { |
| | 0 | 502 | | CheckDisposedOrStarted(); |
| | 0 | 503 | | _settings._clientCertificateOptions = value; |
| | 0 | 504 | | } |
| | | 505 | | } |
| | | 506 | | |
| | | 507 | | protected override void Dispose(bool disposing) |
| | 0 | 508 | | { |
| | 0 | 509 | | if (disposing && !_disposed) |
| | 0 | 510 | | { |
| | 0 | 511 | | _disposed = true; |
| | 0 | 512 | | _handler?.Dispose(); |
| | 0 | 513 | | } |
| | | 514 | | |
| | 0 | 515 | | base.Dispose(disposing); |
| | 0 | 516 | | } |
| | | 517 | | |
| | | 518 | | private HttpMessageHandlerStage SetupHandlerChain() |
| | 0 | 519 | | { |
| | | 520 | | // Clone the settings to get a relatively consistent view that won't change after this point. |
| | | 521 | | // (This isn't entirely complete, as some of the collections it contains aren't currently deeply cloned.) |
| | 0 | 522 | | HttpConnectionSettings settings = _settings.CloneAndNormalize(); |
| | | 523 | | |
| | 0 | 524 | | HttpConnectionPoolManager poolManager = new HttpConnectionPoolManager(settings); |
| | 0 | 525 | | HttpMessageHandlerStage handler = new HttpConnectionHandler(poolManager, doRequestAuth: settings._credential |
| | | 526 | | |
| | | 527 | | // MetricsHandler should be descendant of DiagnosticsHandler in the handler chain to make sure the 'http.req |
| | | 528 | | // metric is recorded before stopping the request Activity. This is needed to make sure that our telemetry s |
| | 0 | 529 | | if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled) |
| | 0 | 530 | | { |
| | 0 | 531 | | handler = new MetricsHandler(handler, settings._meterFactory, settings._proxy, out Meter meter); |
| | 0 | 532 | | settings._metrics = new SocketsHttpHandlerMetrics(meter); |
| | 0 | 533 | | } |
| | | 534 | | |
| | | 535 | | // DiagnosticsHandler is inserted before RedirectHandler so that trace propagation is done on redirects as w |
| | 0 | 536 | | if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation && settings._activityHeadersPropagator i |
| | 0 | 537 | | { |
| | 0 | 538 | | handler = new DiagnosticsHandler(handler, propagator, settings._proxy, settings._allowAutoRedirect); |
| | 0 | 539 | | } |
| | | 540 | | |
| | 0 | 541 | | if (settings._allowAutoRedirect) |
| | 0 | 542 | | { |
| | | 543 | | // Just as with WinHttpHandler, for security reasons, we do not support authentication on redirects |
| | | 544 | | // if the credential is anything other than a CredentialCache. |
| | | 545 | | // We allow credentials in a CredentialCache since they are specifically tied to URIs. |
| | 0 | 546 | | handler = new RedirectHandler(settings._maxAutomaticRedirections, handler, disableAuthOnRedirect: settin |
| | 0 | 547 | | } |
| | | 548 | | |
| | 0 | 549 | | if ((settings._automaticDecompression & SupportedDecompressionMethods) != DecompressionMethods.None) |
| | 0 | 550 | | { |
| | 0 | 551 | | Debug.Assert(_decompressionHandlerFactory is not null); |
| | 0 | 552 | | handler = _decompressionHandlerFactory(settings, handler); |
| | 0 | 553 | | } |
| | | 554 | | |
| | | 555 | | // Ensure a single handler is used for all requests. |
| | 0 | 556 | | if (Interlocked.CompareExchange(ref _handler, handler, null) != null) |
| | 0 | 557 | | { |
| | 0 | 558 | | handler.Dispose(); |
| | 0 | 559 | | } |
| | | 560 | | |
| | 0 | 561 | | return _handler; |
| | 0 | 562 | | } |
| | | 563 | | |
| | | 564 | | // Allows for DecompressionHandler (and its compression dependencies) to be trimmed when |
| | | 565 | | // AutomaticDecompression is not being used. |
| | | 566 | | private void EnsureDecompressionHandlerFactory() |
| | 0 | 567 | | { |
| | 0 | 568 | | _decompressionHandlerFactory ??= (settings, handler) => new DecompressionHandler(settings._automaticDecompre |
| | 0 | 569 | | } |
| | | 570 | | |
| | | 571 | | // Not stored as a constant on the DecompressionHandler to allow it to get trimmed. |
| | | 572 | | private const DecompressionMethods SupportedDecompressionMethods = DecompressionMethods.GZip | DecompressionMeth |
| | | 573 | | |
| | | 574 | | protected internal override HttpResponseMessage Send(HttpRequestMessage request, |
| | | 575 | | CancellationToken cancellationToken) |
| | 0 | 576 | | { |
| | 0 | 577 | | ArgumentNullException.ThrowIfNull(request); |
| | | 578 | | |
| | 0 | 579 | | if (request.Version.Major >= 2) |
| | 0 | 580 | | { |
| | 0 | 581 | | throw new NotSupportedException(SR.Format(SR.net_http_http2_sync_not_supported, GetType())); |
| | | 582 | | } |
| | | 583 | | |
| | | 584 | | // Do not allow upgrades for synchronous requests, that might lead to asynchronous code-paths. |
| | 0 | 585 | | if (request.VersionPolicy == HttpVersionPolicy.RequestVersionOrHigher) |
| | 0 | 586 | | { |
| | 0 | 587 | | throw new NotSupportedException(SR.Format(SR.net_http_upgrade_not_enabled_sync, nameof(Send), request.Ve |
| | | 588 | | } |
| | | 589 | | |
| | 0 | 590 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 591 | | |
| | 0 | 592 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 593 | | |
| | 0 | 594 | | Exception? error = ValidateAndNormalizeRequest(request); |
| | 0 | 595 | | if (error != null) |
| | 0 | 596 | | { |
| | 0 | 597 | | throw error; |
| | | 598 | | } |
| | | 599 | | |
| | 0 | 600 | | HttpMessageHandlerStage handler = _handler ?? SetupHandlerChain(); |
| | | 601 | | |
| | 0 | 602 | | return handler.Send(request, cancellationToken); |
| | 0 | 603 | | } |
| | | 604 | | |
| | | 605 | | protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ca |
| | 0 | 606 | | { |
| | 0 | 607 | | ArgumentNullException.ThrowIfNull(request); |
| | | 608 | | |
| | 0 | 609 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 610 | | |
| | 0 | 611 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 612 | | { |
| | 0 | 613 | | return Task.FromCanceled<HttpResponseMessage>(cancellationToken); |
| | | 614 | | } |
| | | 615 | | |
| | 0 | 616 | | Exception? error = ValidateAndNormalizeRequest(request); |
| | 0 | 617 | | if (error != null) |
| | 0 | 618 | | { |
| | 0 | 619 | | return Task.FromException<HttpResponseMessage>(error); |
| | | 620 | | } |
| | | 621 | | |
| | 0 | 622 | | return _handler is { } handler |
| | 0 | 623 | | ? handler.SendAsync(request, cancellationToken) |
| | 0 | 624 | | : CreateHandlerAndSendAsync(request, cancellationToken); |
| | | 625 | | |
| | | 626 | | // SetupHandlerChain may block for a few seconds in some environments. |
| | | 627 | | // E.g. during the first access of HttpClient.DefaultProxy - https://github.com/dotnet/runtime/issues/115301 |
| | | 628 | | // The setup procedure is enqueued to thread pool to prevent the caller from blocking. |
| | | 629 | | async Task<HttpResponseMessage> CreateHandlerAndSendAsync(HttpRequestMessage request, CancellationToken canc |
| | 0 | 630 | | { |
| | 0 | 631 | | _handlerChainSetupTask ??= Task.Run(SetupHandlerChain); |
| | 0 | 632 | | HttpMessageHandlerStage handler = await _handlerChainSetupTask.ConfigureAwait(false); |
| | 0 | 633 | | return await handler.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| | 0 | 634 | | } |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | private static Exception? ValidateAndNormalizeRequest(HttpRequestMessage request) |
| | 0 | 638 | | { |
| | 0 | 639 | | if (request.Version != HttpVersion.Version10 && request.Version != HttpVersion.Version11 && request.Version |
| | 0 | 640 | | { |
| | 0 | 641 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_vers |
| | | 642 | | } |
| | | 643 | | |
| | | 644 | | // Add headers to define content transfer, if not present |
| | 0 | 645 | | if (request.HasHeaders && request.Headers.TransferEncodingChunked.GetValueOrDefault()) |
| | 0 | 646 | | { |
| | 0 | 647 | | if (request.Content == null) |
| | 0 | 648 | | { |
| | 0 | 649 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_client_execut |
| | 0 | 650 | | ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_chunked_not |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | // Since the user explicitly set TransferEncodingChunked to true, we need to remove |
| | | 654 | | // the Content-Length header if present, as sending both is invalid. |
| | 0 | 655 | | request.Content.Headers.ContentLength = null; |
| | 0 | 656 | | } |
| | 0 | 657 | | else if (request.Content != null && request.Content.Headers.ContentLength == null) |
| | 0 | 658 | | { |
| | | 659 | | // We have content, but neither Transfer-Encoding nor Content-Length is set. |
| | 0 | 660 | | request.Headers.TransferEncodingChunked = true; |
| | 0 | 661 | | } |
| | | 662 | | |
| | 0 | 663 | | if (request.Version.Minor == 0 && request.Version.Major == 1 && request.HasHeaders) |
| | 0 | 664 | | { |
| | | 665 | | // HTTP 1.0 does not support chunking |
| | 0 | 666 | | if (request.Headers.TransferEncodingChunked == true) |
| | 0 | 667 | | { |
| | 0 | 668 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_ |
| | | 669 | | } |
| | | 670 | | |
| | | 671 | | // HTTP 1.0 does not support Expect: 100-continue; just disable it. |
| | 0 | 672 | | if (request.Headers.ExpectContinue == true) |
| | 0 | 673 | | { |
| | 0 | 674 | | request.Headers.ExpectContinue = false; |
| | 0 | 675 | | } |
| | 0 | 676 | | } |
| | | 677 | | |
| | 0 | 678 | | Uri? requestUri = request.RequestUri; |
| | 0 | 679 | | if (requestUri is null || !requestUri.IsAbsoluteUri) |
| | 0 | 680 | | { |
| | 0 | 681 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_client_inval |
| | | 682 | | } |
| | | 683 | | |
| | 0 | 684 | | if (!HttpUtilities.IsSupportedScheme(requestUri.Scheme)) |
| | 0 | 685 | | { |
| | 0 | 686 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.Format(SR.net_http_unsupp |
| | | 687 | | } |
| | | 688 | | |
| | 0 | 689 | | return null; |
| | 0 | 690 | | } |
| | | 691 | | } |
| | | 692 | | } |