< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 88
Coverable lines: 88
Total lines: 164
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()0%440%
CloneAndNormalize()0%12120%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs

#LineLine coverage
 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
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.Metrics;
 7using System.IO;
 8using System.Net.Http.Metrics;
 9using System.Net.Security;
 10using System.Runtime.Versioning;
 11using System.Security.Principal;
 12using System.Threading;
 13using System.Threading.Tasks;
 14
 15namespace System.Net.Http
 16{
 17    /// <summary>Provides a state bag of settings for configuring HTTP connections.</summary>
 18    internal sealed class HttpConnectionSettings
 19    {
 020        internal DecompressionMethods _automaticDecompression = HttpHandlerDefaults.DefaultAutomaticDecompression;
 21
 022        internal bool _useCookies = HttpHandlerDefaults.DefaultUseCookies;
 23        internal CookieContainer? _cookieContainer;
 24
 025        internal bool _useProxy = HttpHandlerDefaults.DefaultUseProxy;
 26        internal IWebProxy? _proxy;
 27        internal ICredentials? _defaultProxyCredentials;
 28        internal bool _defaultCredentialsUsedForProxy;
 29        internal bool _defaultCredentialsUsedForServer;
 30
 031        internal bool _preAuthenticate = HttpHandlerDefaults.DefaultPreAuthenticate;
 32        internal ICredentials? _credentials;
 033        internal TokenImpersonationLevel _impersonationLevel = HttpHandlerDefaults.DefaultImpersonationLevel;   // this 
 34
 035        internal bool _allowAutoRedirect = HttpHandlerDefaults.DefaultAutomaticRedirection;
 036        internal int _maxAutomaticRedirections = HttpHandlerDefaults.DefaultMaxAutomaticRedirections;
 37
 038        internal int _maxConnectionsPerServer = HttpHandlerDefaults.DefaultMaxConnectionsPerServer;
 039        internal int _maxResponseDrainSize = HttpHandlerDefaults.DefaultMaxResponseDrainSize;
 040        internal TimeSpan _maxResponseDrainTime = HttpHandlerDefaults.DefaultResponseDrainTimeout;
 041        internal int _maxResponseHeadersLength = HttpHandlerDefaults.DefaultMaxResponseHeadersLength;
 42        internal IMeterFactory? _meterFactory;
 43        internal SocketsHttpHandlerMetrics? _metrics;
 44
 045        internal TimeSpan _pooledConnectionLifetime = HttpHandlerDefaults.DefaultPooledConnectionLifetime;
 046        internal TimeSpan _pooledConnectionIdleTimeout = HttpHandlerDefaults.DefaultPooledConnectionIdleTimeout;
 047        internal TimeSpan _expect100ContinueTimeout = HttpHandlerDefaults.DefaultExpect100ContinueTimeout;
 048        internal TimeSpan _keepAlivePingTimeout = HttpHandlerDefaults.DefaultKeepAlivePingTimeout;
 049        internal TimeSpan _keepAlivePingDelay = HttpHandlerDefaults.DefaultKeepAlivePingDelay;
 050        internal HttpKeepAlivePingPolicy _keepAlivePingPolicy = HttpHandlerDefaults.DefaultKeepAlivePingPolicy;
 051        internal TimeSpan _connectTimeout = HttpHandlerDefaults.DefaultConnectTimeout;
 52
 53        internal HeaderEncodingSelector<HttpRequestMessage>? _requestHeaderEncodingSelector;
 54        internal HeaderEncodingSelector<HttpRequestMessage>? _responseHeaderEncodingSelector;
 55
 056        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 Func<SocketsHttpConnectionEvictionContext, CancellationToken, Task<bool>>? _shouldEvictConnection;
 70
 71        internal IDictionary<string, object?>? _properties;
 72
 73        // Http2 flow control settings:
 074        internal int _initialHttp2StreamWindowSize = HttpHandlerDefaults.DefaultInitialHttp2StreamWindowSize;
 75
 076        internal int _initialHttp2MaxConcurrentStreams = HttpHandlerDefaults.DefaultInitialHttp2MaxConcurrentStreams;
 77
 78        internal ClientCertificateOption _clientCertificateOptions;
 79
 080        public HttpConnectionSettings()
 081        {
 082            bool allowHttp2 = GlobalHttpSettings.SocketsHttpHandler.AllowHttp2;
 083            bool allowHttp3 = GlobalHttpSettings.SocketsHttpHandler.AllowHttp3;
 084            _maxHttpVersion =
 085                allowHttp3 && allowHttp2 ? HttpVersion.Version30 :
 086                allowHttp2 ? HttpVersion.Version20 :
 087                HttpVersion.Version11;
 88
 089            _clientCertificateOptions = ClientCertificateOption.Automatic;
 090        }
 91
 92        /// <summary>Creates a copy of the settings but with some values normalized to suit the implementation.</summary
 93        public HttpConnectionSettings CloneAndNormalize()
 094        {
 95            // Force creation of the cookie container if needed, so the original and clone share the same instance.
 096            if (_useCookies && _cookieContainer == null)
 097            {
 098                _cookieContainer = new CookieContainer();
 099            }
 100
 0101            var settings = new HttpConnectionSettings()
 0102            {
 0103                _allowAutoRedirect = _allowAutoRedirect,
 0104                _automaticDecompression = _automaticDecompression,
 0105                _cookieContainer = _cookieContainer,
 0106                _connectTimeout = _connectTimeout,
 0107                _credentials = _credentials,
 0108                _defaultProxyCredentials = _defaultProxyCredentials,
 0109                _expect100ContinueTimeout = _expect100ContinueTimeout,
 0110                _maxAutomaticRedirections = _maxAutomaticRedirections,
 0111                _maxConnectionsPerServer = _maxConnectionsPerServer,
 0112                _maxHttpVersion = _maxHttpVersion,
 0113                _maxResponseDrainSize = _maxResponseDrainSize,
 0114                _maxResponseDrainTime = _maxResponseDrainTime,
 0115                _maxResponseHeadersLength = _maxResponseHeadersLength,
 0116                _pooledConnectionLifetime = _pooledConnectionLifetime,
 0117                _pooledConnectionIdleTimeout = _pooledConnectionIdleTimeout,
 0118                _preAuthenticate = _preAuthenticate,
 0119                _properties = _properties,
 0120                _proxy = _proxy,
 0121                _sslOptions = _sslOptions?.ShallowClone(), // shallow clone the options for basic prevention of mutation
 0122                _useCookies = _useCookies,
 0123                _useProxy = _useProxy,
 0124                _keepAlivePingTimeout = _keepAlivePingTimeout,
 0125                _keepAlivePingDelay = _keepAlivePingDelay,
 0126                _keepAlivePingPolicy = _keepAlivePingPolicy,
 0127                _requestHeaderEncodingSelector = _requestHeaderEncodingSelector,
 0128                _responseHeaderEncodingSelector = _responseHeaderEncodingSelector,
 0129                _enableMultipleHttp2Connections = _enableMultipleHttp2Connections,
 0130                _enableMultipleHttp3Connections = _enableMultipleHttp3Connections,
 0131                _connectCallback = _connectCallback,
 0132                _plaintextStreamFilter = _plaintextStreamFilter,
 0133                _shouldEvictConnection = _shouldEvictConnection,
 0134                _initialHttp2StreamWindowSize = _initialHttp2StreamWindowSize,
 0135                _initialHttp2MaxConcurrentStreams = _initialHttp2MaxConcurrentStreams,
 0136                _activityHeadersPropagator = _activityHeadersPropagator,
 0137                _defaultCredentialsUsedForProxy = _proxy != null && (_proxy.Credentials == CredentialCache.DefaultCreden
 0138                _defaultCredentialsUsedForServer = _credentials == CredentialCache.DefaultCredentials,
 0139                _clientCertificateOptions = _clientCertificateOptions,
 0140                _impersonationLevel = _impersonationLevel,
 0141            };
 142
 0143            if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled)
 0144            {
 0145                settings._meterFactory = _meterFactory;
 0146                settings._metrics = _metrics;
 0147            }
 148
 0149            return settings;
 0150        }
 151
 0152        public int MaxResponseHeadersByteLength => (int)Math.Min(int.MaxValue, _maxResponseHeadersLength * 1024L);
 153
 0154        public bool EnableMultipleHttp2Connections => _enableMultipleHttp2Connections;
 155
 0156        public bool EnableMultipleHttp3Connections => _enableMultipleHttp3Connections;
 157
 158        [SupportedOSPlatform("windows")]
 159        [SupportedOSPlatform("linux")]
 160        [SupportedOSPlatform("macos")]
 0161        internal byte[] Http3SettingsFrame => field ??= Http3Connection.BuildSettingsFrame(this);
 162    }
 163}
 164