Exemplo n.º 1
0
        private HttpClient GetHttpClient(ServiceDiscoveryConfig config)
        {
            lock (HttpClientLock)
            {
                bool   useHttps     = config.UseHttpsOverride ?? UseHttpsDefault;
                string securityRole = config.SecurityRole;
                var    httpKey      = Tuple.Create(useHttps, securityRole, config.RequestTimeout);

                if (LastHttpClient != null && LastHttpClientKey.Equals(httpKey))
                {
                    return(LastHttpClient);
                }

                if (useHttps)
                {
                    InitHttps(securityRole);
                }

                LastHttpClientKey = httpKey;
                LastHttpClient    = new HttpClient(HttpMessageHandler);
                TimeSpan?timeout = Timeout ?? config.RequestTimeout;
                if (timeout.HasValue)
                {
                    LastHttpClient.Timeout = timeout.Value;
                }
                return(LastHttpClient);
            }
        }
Exemplo n.º 2
0
        private (HttpClient httpClient, bool isHttps, bool isNewCreated) GetHttpClient(ServiceDiscoveryConfig serviceConfig, DiscoveryConfig defaultConfig, bool tryHttps, string hostname, int basePort)
        {
            var    forceHttps       = serviceConfig.UseHttpsOverride ?? (ServiceInterfaceRequiresHttps || defaultConfig.UseHttpsOverride);
            var    useHttps         = tryHttps || forceHttps;
            string securityRole     = serviceConfig.SecurityRole;
            var    verificationMode = serviceConfig.ServerCertificateVerification ??
                                      defaultConfig.ServerCertificateVerification;
            var supplyClientCertificate = (serviceConfig.ClientCertificateVerification ?? defaultConfig.ClientCertificateVerification)
                                          == ClientCertificateVerificationMode.VerifyIdenticalRootCertificate;
            var httpKey = new HttpClientConfiguration(useHttps, securityRole, serviceConfig.RequestTimeout, verificationMode, supplyClientCertificate);

            lock (HttpClientLock)
            {
                if (LastHttpClient != null && LastHttpClientKey.Equals(httpKey))
                {
                    return(httpClient : LastHttpClient, isHttps : useHttps, isNewCreated : false);
                }

                // In case we're trying HTTPs and the previous request on this instance was HTTP (or if this is the first request)
                if (Not(forceHttps) && httpKey.UseHttps && Not(LastHttpClientKey?.UseHttps ?? false))
                {
                    var now = DateTime.Now;
                    if (now - _lastHttpsTestTime > _httpsTestInterval)
                    {
                        _lastHttpsTestTime = now;
                        RunHttpsAvailabilityTest(httpKey, hostname, basePort);
                    }

                    httpKey = new HttpClientConfiguration(
                        useHttps: false,
                        securityRole: null,
                        timeout: httpKey.Timeout,
                        verificationMode: httpKey.VerificationMode,
                        supplyClientCertificate: httpKey.SupplyClientCertificate);
                }


                var isNewCreated = false;
                if (!(LastHttpClientKey?.Equals(httpKey) ?? false))
                {
                    isNewCreated = true;
                    var equalState = (LastHttpClientKey?.Equals(httpKey))?.ToString() ?? "FirstTime";
                    Log.Info(msg => msg("GetHttpClient created new HttpClient", unencryptedTags: new Tags
                    {
                        { "debug.delay.newHttpClient", equalState },
                        { "debug.delay.method", "GetHttpClient" }
                    })); // we expect this not to be equal ever

                    var messageHandler = _httpMessageHandlerFactory(httpKey);
                    var httpClient     = CreateHttpClient(messageHandler, httpKey.Timeout);

                    LastHttpClient      = httpClient;
                    LastHttpClientKey   = httpKey;
                    _httpMessageHandler = messageHandler;
                }

                return(httpClient : LastHttpClient, isHttps : httpKey.UseHttps, isNewCreated : isNewCreated);
            }
        }
Exemplo n.º 3
0
        private (HttpClient httpClient, bool isHttps) GetHttpClient(ServiceDiscoveryConfig serviceConfig, DiscoveryConfig defaultConfig, bool tryHttps, string hostname, int basePort)
        {
            var    forceHttps       = serviceConfig.UseHttpsOverride ?? (ServiceInterfaceRequiresHttps || defaultConfig.UseHttpsOverride);
            var    useHttps         = tryHttps || forceHttps;
            string securityRole     = serviceConfig.SecurityRole;
            var    verificationMode = serviceConfig.ServerCertificateVerification ??
                                      defaultConfig.ServerCertificateVerification;
            var supplyClientCertificate = (serviceConfig.ClientCertificateVerification ?? defaultConfig.ClientCertificateVerification)
                                          == ClientCertificateVerificationMode.VerifyIdenticalRootCertificate;
            var httpKey = new HttpClientConfiguration(useHttps, securityRole, serviceConfig.RequestTimeout, verificationMode, supplyClientCertificate);

            lock (HttpClientLock)
            {
                if (LastHttpClient != null && LastHttpClientKey.Equals(httpKey))
                {
                    return(httpClient : LastHttpClient, isHttps : useHttps);
                }

                // In case we're trying HTTPs and the previous request on this instance was HTTP (or if this is the first request)
                if (Not(forceHttps) && httpKey.UseHttps && Not(LastHttpClientKey?.UseHttps ?? false))
                {
                    var now = DateTime.Now;
                    if (now - _lastHttpsTestTime > _httpsTestInterval)
                    {
                        _lastHttpsTestTime = now;
                        RunHttpsAvailabilityTest(httpKey, hostname, basePort);
                    }

                    httpKey = new HttpClientConfiguration(
                        useHttps: false,
                        securityRole: null,
                        timeout: httpKey.Timeout,
                        verificationMode: httpKey.VerificationMode,
                        supplyClientCertificate: httpKey.SupplyClientCertificate);
                }

                if (!(LastHttpClientKey?.Equals(httpKey) ?? false))
                {
                    var messageHandler = _httpMessageHandlerFactory(httpKey);
                    var httpClient     = CreateHttpClient(messageHandler, httpKey.Timeout);

                    LastHttpClient      = httpClient;
                    LastHttpClientKey   = httpKey;
                    _httpMessageHandler = messageHandler;
                }

                return(httpClient : LastHttpClient, isHttps : httpKey.UseHttps);
            }
        }