Skip to content

Commit 93eda58

Browse files
committed
Allow for modifying HttpClientHandler on .NET Core
Closes #2198
1 parent 1c60b59 commit 93eda58

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

src/Elasticsearch.Net/Connection/HttpConnection-CoreFx.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Net;
77
using System.Net.Http;
88
using System.Net.Http.Headers;
9+
using System.Net.Security;
10+
using System.Security.Cryptography.X509Certificates;
911
using System.Text;
1012
using System.Threading.Tasks;
1113
using static System.Net.DecompressionMethods;
@@ -43,22 +45,7 @@ private HttpClient GetClient(RequestData requestData)
4345
{
4446
if (this._clients.TryGetValue(hashCode, out client)) return client;
4547

46-
var handler = new HttpClientHandler
47-
{
48-
AutomaticDecompression = requestData.HttpCompression ? GZip | Deflate : None
49-
};
50-
51-
if (!requestData.ProxyAddress.IsNullOrEmpty())
52-
{
53-
var uri = new Uri(requestData.ProxyAddress);
54-
var proxy = new WebProxy(uri);
55-
var credentials = new NetworkCredential(requestData.ProxyUsername, requestData.ProxyPassword);
56-
proxy.Credentials = credentials;
57-
handler.Proxy = proxy;
58-
}
59-
60-
if (requestData.DisableAutomaticProxyDetection)
61-
handler.Proxy = null;
48+
var handler = CreateHttpClientHandler(requestData);
6249

6350
client = new HttpClient(handler, false)
6451
{
@@ -67,8 +54,6 @@ private HttpClient GetClient(RequestData requestData)
6754

6855
client.DefaultRequestHeaders.ExpectContinue = false;
6956

70-
//TODO add headers
71-
//client.DefaultRequestHeaders =
7257
this._clients.TryAdd(hashCode, client);
7358
return client;
7459
}
@@ -119,6 +104,28 @@ public virtual async Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(
119104
return await builder.ToResponseAsync().ConfigureAwait(false);
120105
}
121106

107+
protected virtual HttpClientHandler CreateHttpClientHandler(RequestData requestData)
108+
{
109+
var handler = new HttpClientHandler
110+
{
111+
AutomaticDecompression = requestData.HttpCompression ? GZip | Deflate : None
112+
};
113+
114+
if (!requestData.ProxyAddress.IsNullOrEmpty())
115+
{
116+
var uri = new Uri(requestData.ProxyAddress);
117+
var proxy = new WebProxy(uri);
118+
var credentials = new NetworkCredential(requestData.ProxyUsername, requestData.ProxyPassword);
119+
proxy.Credentials = credentials;
120+
handler.Proxy = proxy;
121+
}
122+
123+
if (requestData.DisableAutomaticProxyDetection)
124+
handler.Proxy = null;
125+
126+
return handler;
127+
}
128+
122129
protected virtual HttpRequestMessage CreateHttpRequestMessage(RequestData requestData)
123130
{
124131
var request = this.CreateRequestMessage(requestData);

src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Tests.Framework;
1212
using Tests.Framework.MockData;
1313
using Xunit;
14+
using System.Net.Http;
1415

1516
namespace Tests.ClientConcepts.LowLevel
1617
{
@@ -248,25 +249,42 @@ public void ConfiguringSSL()
248249
/**
249250
* [[configuring-ssl]]
250251
* === Configuring SSL
251-
* SSL must be configured outside of the client using .NET's
252-
* http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager%28v=vs.110%29.aspx[ServicePointManager]
253-
* class and setting the http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback.aspx[ServerCertificateValidationCallback]
254-
* property.
252+
* SSL can be configured via the `ServerCertificateValidationCallback` property on either `ServerPointManager` or `HttpClientHandler`
253+
* depending on which version of the .NET framework is in use.
254+
*
255+
* On the full .NET Framework, this must be done outside of the client using .NET's built-in
256+
* http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager%28v=vs.110%29.aspx[ServicePointManager] class:
255257
*
256-
* The bare minimum to make .NET accept self-signed SSL certs that are not in the Windows CA store would be to have the callback simply return `true`:
257258
*/
258259

259260
#if !DOTNETCORE
260261
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;
261262
#endif
262263
/**
264+
* The bare minimum to make .NET accept self-signed SSL certs that are not in the Windows CA store would be to have the callback simply return `true`.
265+
*
263266
* However, this will accept **all** requests from the AppDomain to untrusted SSL sites,
264267
* therefore **we recommend doing some minimal introspection on the passed in certificate.**
265-
*
266-
* IMPORTANT: Using `ServicePointManager` does not work on **Core CLR** as the request does not go through `ServicePointManager`; please file an {github}/issues[issue] if you need support for certificate validation on Core CLR.
267268
*/
268269
}
269270

271+
/*
272+
* If running on Core CLR, then a custom connection type must be created by deriving from `HttpConnection` and
273+
* overriding the `CreateHttpClientHandler` method in order to set the `ServerCertificateCustomValidationCallback` property:
274+
*/
275+
276+
#if DOTNETCORE
277+
public class SecureHttpConnection : HttpConnection
278+
{
279+
protected override HttpClientHandler CreateHttpClientHandler(RequestData requestData)
280+
{
281+
var handler = base.CreateHttpClientHandler(requestData);
282+
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, errors) => true;
283+
return handler;
284+
}
285+
}
286+
#endif
287+
270288
/**=== Overriding default Json.NET behavior
271289
*
272290
* Overriding the default Json.NET behaviour in NEST is an expert behavior but if you need to get to the nitty gritty, this can be really useful.

0 commit comments

Comments
 (0)