7
7
using System . Collections . Generic ;
8
8
using System . Net ;
9
9
using System . Net . Http ;
10
+ using System . Net . Sockets ;
10
11
using System . Security . Authentication ;
11
12
using System . Security . Cryptography . X509Certificates ;
12
13
using System . Threading ;
@@ -33,6 +34,7 @@ public class WebRequestSession : IDisposable
33
34
private bool _noProxy ;
34
35
private bool _disposed ;
35
36
private TimeSpan _connectionTimeout ;
37
+ private UnixDomainSocketEndPoint ? _unixSocket ;
36
38
37
39
/// <summary>
38
40
/// Contains true if an existing HttpClient had to be disposed and recreated since the WebSession was last used.
@@ -144,6 +146,8 @@ public WebRequestSession()
144
146
145
147
internal TimeSpan ConnectionTimeout { set => SetStructVar ( ref _connectionTimeout , value ) ; }
146
148
149
+ internal UnixDomainSocketEndPoint UnixSocket { set => SetClassVar ( ref _unixSocket , value ) ; }
150
+
147
151
internal bool NoProxy
148
152
{
149
153
set
@@ -195,7 +199,18 @@ internal HttpClient GetHttpClient(bool suppressHttpClientRedirects, out bool cli
195
199
196
200
private HttpClient CreateHttpClient ( )
197
201
{
198
- HttpClientHandler handler = new ( ) ;
202
+ SocketsHttpHandler handler = new ( ) ;
203
+
204
+ if ( _unixSocket is not null )
205
+ {
206
+ handler . ConnectCallback = async ( context , token ) =>
207
+ {
208
+ Socket socket = new ( AddressFamily . Unix , SocketType . Stream , ProtocolType . IP ) ;
209
+ await socket . ConnectAsync ( _unixSocket ) . ConfigureAwait ( false ) ;
210
+
211
+ return new NetworkStream ( socket , ownsSocket : false ) ;
212
+ } ;
213
+ }
199
214
200
215
handler . CookieContainer = Cookies ;
201
216
handler . AutomaticDecompression = DecompressionMethods . All ;
@@ -204,9 +219,9 @@ private HttpClient CreateHttpClient()
204
219
{
205
220
handler . Credentials = Credentials ;
206
221
}
207
- else
222
+ else if ( UseDefaultCredentials )
208
223
{
209
- handler . UseDefaultCredentials = UseDefaultCredentials ;
224
+ handler . Credentials = CredentialCache . DefaultCredentials ;
210
225
}
211
226
212
227
if ( _noProxy )
@@ -220,13 +235,12 @@ private HttpClient CreateHttpClient()
220
235
221
236
if ( Certificates is not null )
222
237
{
223
- handler . ClientCertificates . AddRange ( Certificates ) ;
238
+ handler . SslOptions . ClientCertificates = new X509CertificateCollection ( Certificates ) ;
224
239
}
225
240
226
241
if ( _skipCertificateCheck )
227
242
{
228
- handler . ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator ;
229
- handler . ClientCertificateOptions = ClientCertificateOption . Manual ;
243
+ handler . SslOptions . RemoteCertificateValidationCallback = delegate { return true ; } ;
230
244
}
231
245
232
246
handler . AllowAutoRedirect = _allowAutoRedirect ;
@@ -235,9 +249,9 @@ private HttpClient CreateHttpClient()
235
249
handler . MaxAutomaticRedirections = MaximumRedirection ;
236
250
}
237
251
238
- handler . SslProtocols = ( SslProtocols ) _sslProtocol ;
252
+ handler . SslOptions . EnabledSslProtocols = ( SslProtocols ) _sslProtocol ;
239
253
240
- // Check timeout setting (in seconds instead of milliseconds as in HttpWebRequest )
254
+ // Check timeout setting (in seconds)
241
255
return new HttpClient ( handler )
242
256
{
243
257
Timeout = _connectionTimeout
0 commit comments