| | | 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.Metrics; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Net.Http.Metrics; |
| | | 9 | | using System.Net.Security; |
| | | 10 | | using System.Runtime.Versioning; |
| | | 11 | | using System.Security.Principal; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | |
| | | 15 | | namespace System.Net.Http |
| | | 16 | | { |
| | | 17 | | /// <summary>Provides a state bag of settings for configuring HTTP connections.</summary> |
| | | 18 | | internal sealed class HttpConnectionSettings |
| | | 19 | | { |
| | 0 | 20 | | internal DecompressionMethods _automaticDecompression = HttpHandlerDefaults.DefaultAutomaticDecompression; |
| | | 21 | | |
| | 0 | 22 | | internal bool _useCookies = HttpHandlerDefaults.DefaultUseCookies; |
| | | 23 | | internal CookieContainer? _cookieContainer; |
| | | 24 | | |
| | 0 | 25 | | internal bool _useProxy = HttpHandlerDefaults.DefaultUseProxy; |
| | | 26 | | internal IWebProxy? _proxy; |
| | | 27 | | internal ICredentials? _defaultProxyCredentials; |
| | | 28 | | internal bool _defaultCredentialsUsedForProxy; |
| | | 29 | | internal bool _defaultCredentialsUsedForServer; |
| | | 30 | | |
| | 0 | 31 | | internal bool _preAuthenticate = HttpHandlerDefaults.DefaultPreAuthenticate; |
| | | 32 | | internal ICredentials? _credentials; |
| | 0 | 33 | | internal TokenImpersonationLevel _impersonationLevel = HttpHandlerDefaults.DefaultImpersonationLevel; // this |
| | | 34 | | |
| | 0 | 35 | | internal bool _allowAutoRedirect = HttpHandlerDefaults.DefaultAutomaticRedirection; |
| | 0 | 36 | | internal int _maxAutomaticRedirections = HttpHandlerDefaults.DefaultMaxAutomaticRedirections; |
| | | 37 | | |
| | 0 | 38 | | internal int _maxConnectionsPerServer = HttpHandlerDefaults.DefaultMaxConnectionsPerServer; |
| | 0 | 39 | | internal int _maxResponseDrainSize = HttpHandlerDefaults.DefaultMaxResponseDrainSize; |
| | 0 | 40 | | internal TimeSpan _maxResponseDrainTime = HttpHandlerDefaults.DefaultResponseDrainTimeout; |
| | 0 | 41 | | internal int _maxResponseHeadersLength = HttpHandlerDefaults.DefaultMaxResponseHeadersLength; |
| | | 42 | | internal IMeterFactory? _meterFactory; |
| | | 43 | | internal SocketsHttpHandlerMetrics? _metrics; |
| | | 44 | | |
| | 0 | 45 | | internal TimeSpan _pooledConnectionLifetime = HttpHandlerDefaults.DefaultPooledConnectionLifetime; |
| | 0 | 46 | | internal TimeSpan _pooledConnectionIdleTimeout = HttpHandlerDefaults.DefaultPooledConnectionIdleTimeout; |
| | 0 | 47 | | internal TimeSpan _expect100ContinueTimeout = HttpHandlerDefaults.DefaultExpect100ContinueTimeout; |
| | 0 | 48 | | internal TimeSpan _keepAlivePingTimeout = HttpHandlerDefaults.DefaultKeepAlivePingTimeout; |
| | 0 | 49 | | internal TimeSpan _keepAlivePingDelay = HttpHandlerDefaults.DefaultKeepAlivePingDelay; |
| | 0 | 50 | | internal HttpKeepAlivePingPolicy _keepAlivePingPolicy = HttpHandlerDefaults.DefaultKeepAlivePingPolicy; |
| | 0 | 51 | | internal TimeSpan _connectTimeout = HttpHandlerDefaults.DefaultConnectTimeout; |
| | | 52 | | |
| | | 53 | | internal HeaderEncodingSelector<HttpRequestMessage>? _requestHeaderEncodingSelector; |
| | | 54 | | internal HeaderEncodingSelector<HttpRequestMessage>? _responseHeaderEncodingSelector; |
| | | 55 | | |
| | 0 | 56 | | internal DistributedContextPropagator? _activityHeadersPropagator = DistributedContextPropagator.Current; |
| | | 57 | | |
| | | 58 | | internal Version _maxHttpVersion; |
| | | 59 | | |
| | | 60 | | internal SslClientAuthenticationOptions? _sslOptions; |
| | | 61 | | |
| | | 62 | | internal bool _enableMultipleHttp2Connections; |
| | | 63 | | |
| | | 64 | | internal bool _enableMultipleHttp3Connections; |
| | | 65 | | |
| | | 66 | | internal Func<SocketsHttpConnectionContext, CancellationToken, ValueTask<Stream>>? _connectCallback; |
| | | 67 | | internal Func<SocketsHttpPlaintextStreamFilterContext, CancellationToken, ValueTask<Stream>>? _plaintextStreamFi |
| | | 68 | | |
| | | 69 | | internal IDictionary<string, object?>? _properties; |
| | | 70 | | |
| | | 71 | | // Http2 flow control settings: |
| | 0 | 72 | | internal int _initialHttp2StreamWindowSize = HttpHandlerDefaults.DefaultInitialHttp2StreamWindowSize; |
| | | 73 | | |
| | | 74 | | internal ClientCertificateOption _clientCertificateOptions; |
| | | 75 | | |
| | 0 | 76 | | public HttpConnectionSettings() |
| | 0 | 77 | | { |
| | 0 | 78 | | bool allowHttp2 = GlobalHttpSettings.SocketsHttpHandler.AllowHttp2; |
| | 0 | 79 | | bool allowHttp3 = GlobalHttpSettings.SocketsHttpHandler.AllowHttp3; |
| | 0 | 80 | | _maxHttpVersion = |
| | 0 | 81 | | allowHttp3 && allowHttp2 ? HttpVersion.Version30 : |
| | 0 | 82 | | allowHttp2 ? HttpVersion.Version20 : |
| | 0 | 83 | | HttpVersion.Version11; |
| | | 84 | | |
| | 0 | 85 | | _clientCertificateOptions = ClientCertificateOption.Automatic; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary>Creates a copy of the settings but with some values normalized to suit the implementation.</summary |
| | | 89 | | public HttpConnectionSettings CloneAndNormalize() |
| | 0 | 90 | | { |
| | | 91 | | // Force creation of the cookie container if needed, so the original and clone share the same instance. |
| | 0 | 92 | | if (_useCookies && _cookieContainer == null) |
| | 0 | 93 | | { |
| | 0 | 94 | | _cookieContainer = new CookieContainer(); |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | var settings = new HttpConnectionSettings() |
| | 0 | 98 | | { |
| | 0 | 99 | | _allowAutoRedirect = _allowAutoRedirect, |
| | 0 | 100 | | _automaticDecompression = _automaticDecompression, |
| | 0 | 101 | | _cookieContainer = _cookieContainer, |
| | 0 | 102 | | _connectTimeout = _connectTimeout, |
| | 0 | 103 | | _credentials = _credentials, |
| | 0 | 104 | | _defaultProxyCredentials = _defaultProxyCredentials, |
| | 0 | 105 | | _expect100ContinueTimeout = _expect100ContinueTimeout, |
| | 0 | 106 | | _maxAutomaticRedirections = _maxAutomaticRedirections, |
| | 0 | 107 | | _maxConnectionsPerServer = _maxConnectionsPerServer, |
| | 0 | 108 | | _maxHttpVersion = _maxHttpVersion, |
| | 0 | 109 | | _maxResponseDrainSize = _maxResponseDrainSize, |
| | 0 | 110 | | _maxResponseDrainTime = _maxResponseDrainTime, |
| | 0 | 111 | | _maxResponseHeadersLength = _maxResponseHeadersLength, |
| | 0 | 112 | | _pooledConnectionLifetime = _pooledConnectionLifetime, |
| | 0 | 113 | | _pooledConnectionIdleTimeout = _pooledConnectionIdleTimeout, |
| | 0 | 114 | | _preAuthenticate = _preAuthenticate, |
| | 0 | 115 | | _properties = _properties, |
| | 0 | 116 | | _proxy = _proxy, |
| | 0 | 117 | | _sslOptions = _sslOptions?.ShallowClone(), // shallow clone the options for basic prevention of mutation |
| | 0 | 118 | | _useCookies = _useCookies, |
| | 0 | 119 | | _useProxy = _useProxy, |
| | 0 | 120 | | _keepAlivePingTimeout = _keepAlivePingTimeout, |
| | 0 | 121 | | _keepAlivePingDelay = _keepAlivePingDelay, |
| | 0 | 122 | | _keepAlivePingPolicy = _keepAlivePingPolicy, |
| | 0 | 123 | | _requestHeaderEncodingSelector = _requestHeaderEncodingSelector, |
| | 0 | 124 | | _responseHeaderEncodingSelector = _responseHeaderEncodingSelector, |
| | 0 | 125 | | _enableMultipleHttp2Connections = _enableMultipleHttp2Connections, |
| | 0 | 126 | | _enableMultipleHttp3Connections = _enableMultipleHttp3Connections, |
| | 0 | 127 | | _connectCallback = _connectCallback, |
| | 0 | 128 | | _plaintextStreamFilter = _plaintextStreamFilter, |
| | 0 | 129 | | _initialHttp2StreamWindowSize = _initialHttp2StreamWindowSize, |
| | 0 | 130 | | _activityHeadersPropagator = _activityHeadersPropagator, |
| | 0 | 131 | | _defaultCredentialsUsedForProxy = _proxy != null && (_proxy.Credentials == CredentialCache.DefaultCreden |
| | 0 | 132 | | _defaultCredentialsUsedForServer = _credentials == CredentialCache.DefaultCredentials, |
| | 0 | 133 | | _clientCertificateOptions = _clientCertificateOptions, |
| | 0 | 134 | | _impersonationLevel = _impersonationLevel, |
| | 0 | 135 | | }; |
| | | 136 | | |
| | 0 | 137 | | if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled) |
| | 0 | 138 | | { |
| | 0 | 139 | | settings._meterFactory = _meterFactory; |
| | 0 | 140 | | settings._metrics = _metrics; |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | return settings; |
| | 0 | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | public int MaxResponseHeadersByteLength => (int)Math.Min(int.MaxValue, _maxResponseHeadersLength * 1024L); |
| | | 147 | | |
| | 0 | 148 | | public bool EnableMultipleHttp2Connections => _enableMultipleHttp2Connections; |
| | | 149 | | |
| | 0 | 150 | | public bool EnableMultipleHttp3Connections => _enableMultipleHttp3Connections; |
| | | 151 | | |
| | | 152 | | [SupportedOSPlatform("windows")] |
| | | 153 | | [SupportedOSPlatform("linux")] |
| | | 154 | | [SupportedOSPlatform("macos")] |
| | 0 | 155 | | internal byte[] Http3SettingsFrame => field ??= Http3Connection.BuildSettingsFrame(this); |
| | | 156 | | } |
| | | 157 | | } |