/// <inheritdoc/> public HttpMessageInvoker CreateClient(ProxyHttpClientContext context) { if (CanReuseOldClient(context)) { Log.ProxyClientReused(_logger, context.ClusterId); return(context.OldClient); } var newClientOptions = context.NewOptions; var handler = new SocketsHttpHandler { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false // NOTE: MaxResponseHeadersLength = 64, which means up to 64 KB of headers are allowed by default as of .NET Core 3.1. }; if (newClientOptions.SslProtocols.HasValue) { handler.SslOptions.EnabledSslProtocols = newClientOptions.SslProtocols.Value; } if (newClientOptions.ClientCertificate != null) { handler.SslOptions.ClientCertificates = new X509CertificateCollection { newClientOptions.ClientCertificate }; } if (newClientOptions.MaxConnectionsPerServer != null) { handler.MaxConnectionsPerServer = newClientOptions.MaxConnectionsPerServer.Value; } if (newClientOptions.DangerousAcceptAnyServerCertificate ?? false) { handler.SslOptions.RemoteCertificateValidationCallback = delegate { return(true); }; } #if NET if (newClientOptions.EnableMultipleHttp2Connections.HasValue) { handler.EnableMultipleHttp2Connections = newClientOptions.EnableMultipleHttp2Connections.Value; } if (newClientOptions.RequestHeaderEncoding != null) { handler.RequestHeaderEncodingSelector = (_, _) => newClientOptions.RequestHeaderEncoding; } #endif Log.ProxyClientCreated(_logger, context.ClusterId); var activityContextHeaders = newClientOptions.ActivityContextHeaders.GetValueOrDefault(ActivityContextHeaders.BaggageAndCorrelationContext); if (activityContextHeaders != ActivityContextHeaders.None) { return(new HttpMessageInvoker(new ActivityPropagationHandler(activityContextHeaders, handler), disposeHandler: true)); } return(new HttpMessageInvoker(handler, disposeHandler: true)); }
/// <inheritdoc/> public HttpMessageInvoker CreateClient(ProxyHttpClientContext context) { if (CanReuseOldClient(context)) { Log.ProxyClientReused(_logger, context.ClusterId); return(context.OldClient); } var newClientOptions = context.NewOptions; var handler = new SocketsHttpHandler { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false // NOTE: MaxResponseHeadersLength = 64, which means up to 64 KB of headers are allowed by default as of .NET Core 3.1. }; if (newClientOptions.SslProtocols.HasValue) { handler.SslOptions.EnabledSslProtocols = newClientOptions.SslProtocols.Value; } if (newClientOptions.ClientCertificate != null) { handler.SslOptions.ClientCertificates = new X509CertificateCollection { newClientOptions.ClientCertificate }; } if (newClientOptions.MaxConnectionsPerServer != null) { handler.MaxConnectionsPerServer = newClientOptions.MaxConnectionsPerServer.Value; } if (newClientOptions.DangerousAcceptAnyServerCertificate) { handler.SslOptions.RemoteCertificateValidationCallback = delegate { return(true); }; } Log.ProxyClientCreated(_logger, context.ClusterId); return(new HttpMessageInvoker(handler, disposeHandler: true)); }
private bool CanReuseOldClient(ProxyHttpClientContext context) { return(context.OldClient != null && context.NewOptions == context.OldOptions); }