Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ClickHouse.Driver/Http/DefaultPoolHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace ClickHouse.Driver.Http;

internal class DefaultPoolHttpClientFactory : IHttpClientFactory, IDisposable
{
private readonly DefaultHttpClientHandler handler;
private readonly HttpMessageHandler handler;

public TimeSpan Timeout { get; init; }

public DefaultPoolHttpClientFactory(bool skipServerCertificateValidation)
{
handler = new(skipServerCertificateValidation);
handler = HttpHandlerProvider.CreateHandler(skipServerCertificateValidation);
}

public HttpClient CreateClient(string name) => new(handler, false) { Timeout = Timeout };
Expand Down
41 changes: 41 additions & 0 deletions ClickHouse.Driver/Http/HttpHandlerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Security;

namespace ClickHouse.Driver.Http;

internal static class HttpHandlerProvider
{
public static HttpMessageHandler CreateHandler(bool skipServerCertificateValidation, int? maxConnectionsPerServer = null)
{
#if NETCOREAPP2_1_OR_GREATER
var socketsHandler = new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
PooledConnectionIdleTimeout = TimeSpan.FromSeconds(5),
};

if (maxConnectionsPerServer.HasValue)
{
socketsHandler.MaxConnectionsPerServer = maxConnectionsPerServer.Value;
}

if (skipServerCertificateValidation)
{
socketsHandler.SslOptions = new SslClientAuthenticationOptions
{
RemoteCertificateValidationCallback = (_, _, _, _) => true,

Check warning on line 28 in ClickHouse.Driver/Http/HttpHandlerProvider.cs

View workflow job for this annotation

GitHub Actions / Build & Run Examples

The ServerCertificateValidationCallback is set to a function that accepts any server certificate, by always returning true. Ensure that server certificates are validated to verify the identity of the server receiving requests. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca5359)
};
}
return socketsHandler;
#else
var defaultHandler = new DefaultHttpClientHandler(skipServerCertificateValidation);
if (maxConnectionsPerServer.HasValue)
{
defaultHandler.MaxConnectionsPerServer = maxConnectionsPerServer.Value;
}
return defaultHandler;
#endif
}
}
4 changes: 2 additions & 2 deletions ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace ClickHouse.Driver.Http;

internal class SingleConnectionHttpClientFactory : IHttpClientFactory, IDisposable
{
private readonly DefaultHttpClientHandler handler;
private readonly HttpMessageHandler handler;

public TimeSpan Timeout { get; init; }

public SingleConnectionHttpClientFactory(bool skipServerCertificateValidation)
{
handler = new(skipServerCertificateValidation) { MaxConnectionsPerServer = 1 };
handler = HttpHandlerProvider.CreateHandler(skipServerCertificateValidation, maxConnectionsPerServer: 1);
}

public HttpClient CreateClient(string name) => new(handler, false) { Timeout = Timeout };
Expand Down
Loading