SolrNet / SolrNet

Solr client for .Net
https://github.com/SolrNet/SolrNet?tab=readme-ov-file#documentation-index
Apache License 2.0
931 stars 797 forks source link

Enables customization of HTTPClient #599

Open ancailliau opened 2 years ago

ancailliau commented 2 years ago

In some environement, specific control to the proxy configuration and SSL certificate verification is required. This patch allow the user of the library to use custom HTTP client configuration.

For example, this code allow the user to disable SSL certificate verification for dev. environement, where self-signed certificates are commonly used:

var handler = new HttpClientHandler()
{
    AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate),
    ClientCertificateOptions = ClientCertificateOption.Manual,
    ServerCertificateCustomValidationCallback = (_, _, _, _) => true,
};
var client = new HttpClient(handler);

serviceCollection.AddSolrNet($"https://myinstance.local/solr",
    null,
    (url) => new AutoSolrConnection(url, client, new InsecureHttpWebRequestFactory()));

with the following WebRequestFactory

public class InsecureHttpWebRequestFactory : IHttpWebRequestFactory
{
    public IHttpWebRequest Create(Uri url)
    {
        var req = (HttpWebRequest)WebRequest.Create(url);
        req.ServerCertificateValidationCallback += (_, _, _, _) => true;
        return new HttpWebRequestAdapter(req);
    }
}

or to enable specific proxy to be specified with

var handler = new HttpClientHandler()
{
    AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate),
    Proxy = new WebProxy()
    {
        Address = new Uri("http://myproxy.local:3128/"),
        BypassList = new[] { "localhost, 127.0.0.1, .local" }
    }
};

The code has been fully tested in a larger scale application.

⚠️ Why SolRNet requires both an HttpClient and a WebRequestFactory should probably be checked. In addition, WebRequest are obsolete and should be replaced by HttpClient. I did not want to mix these two changes, the latter having much more impact.

ancailliau commented 1 year ago

Update?