Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables customization of HTTPClient #599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ancailliau
Copy link
Contributor

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.

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
Copy link
Contributor Author

Update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant