Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ConnectHelper.cs
Go to the documentation of this file.
1using System.IO;
9
10namespace System.Net.Http;
11
12internal static class ConnectHelper
13{
14 internal sealed class CertificateCallbackMapper
15 {
16 public readonly Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> FromHttpClientHandler;
17
18 public readonly RemoteCertificateValidationCallback ForSocketsHttpHandler;
19
20 public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> fromHttpClientHandler)
21 {
22 FromHttpClientHandler = fromHttpClientHandler;
23 ForSocketsHttpHandler = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => FromHttpClientHandler((HttpRequestMessage)sender, certificate as X509Certificate2, chain, sslPolicyErrors);
24 }
25 }
26
28 {
29 RemoteCertificateValidationCallback remoteCertificateValidationCallback = sslOptions.RemoteCertificateValidationCallback;
30 if (remoteCertificateValidationCallback != null && remoteCertificateValidationCallback.Target is CertificateCallbackMapper certificateCallbackMapper)
31 {
32 sslOptions = sslOptions.ShallowClone();
33 Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> localFromHttpClientHandler = certificateCallbackMapper.FromHttpClientHandler;
34 HttpRequestMessage localRequest = request;
35 sslOptions.RemoteCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
36 {
37 bool result = localFromHttpClientHandler(localRequest, certificate as X509Certificate2, chain, sslPolicyErrors);
38 localRequest = null;
39 return result;
40 };
41 }
42 return sslOptions;
43 }
44
46 {
47 sslOptions = SetUpRemoteCertificateValidationCallback(sslOptions, request);
48 SslStream sslStream = new SslStream(stream);
49 try
50 {
51 if (async)
52 {
53 await sslStream.AuthenticateAsClientAsync(sslOptions, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
54 }
55 else
56 {
57 using (cancellationToken.UnsafeRegister(delegate(object s)
58 {
59 ((Stream)s).Dispose();
60 }, stream))
61 {
62 sslStream.AuthenticateAsClient(sslOptions);
63 }
64 }
65 }
66 catch (Exception ex)
67 {
68 sslStream.Dispose();
70 {
71 throw;
72 }
74 {
76 }
78 }
79 if (cancellationToken.IsCancellationRequested)
80 {
81 sslStream.Dispose();
83 }
84 return sslStream;
85 }
86
87 [SupportedOSPlatform("windows")]
88 [SupportedOSPlatform("linux")]
89 [SupportedOSPlatform("macos")]
91 {
92 clientAuthenticationOptions = SetUpRemoteCertificateValidationCallback(clientAuthenticationOptions, request);
93 QuicConnection con = new QuicConnection(quicImplementationProvider, endPoint, clientAuthenticationOptions);
94 try
95 {
96 await con.ConnectAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
97 return con;
98 }
99 catch (Exception error)
100 {
101 con.Dispose();
102 throw CreateWrappedException(error, endPoint.Host, endPoint.Port, cancellationToken);
103 }
104 }
105
106 internal static Exception CreateWrappedException(Exception error, string host, int port, CancellationToken cancellationToken)
107 {
109 {
110 return new HttpRequestException($"{error.Message} ({host}:{port})", error, RequestRetryType.RetryOnNextProxy);
111 }
113 }
114}
static bool ShouldWrapInOperationCanceledException(Exception exception, CancellationToken cancellationToken)
static Exception CreateOperationCanceledException(Exception innerException, CancellationToken cancellationToken)
CertificateCallbackMapper(Func< HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool > fromHttpClientHandler)
readonly Func< HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool > FromHttpClientHandler
readonly RemoteCertificateValidationCallback ForSocketsHttpHandler
static Exception CreateWrappedException(Exception error, string host, int port, CancellationToken cancellationToken)
static SslClientAuthenticationOptions SetUpRemoteCertificateValidationCallback(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request)
static async ValueTask< SslStream > EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, bool async, Stream stream, CancellationToken cancellationToken)
static async ValueTask< QuicConnection > ConnectQuicAsync(HttpRequestMessage request, QuicImplementationProvider quicImplementationProvider, DnsEndPoint endPoint, SslClientAuthenticationOptions clientAuthenticationOptions, CancellationToken cancellationToken)
ValueTask ConnectAsync(CancellationToken cancellationToken=default(CancellationToken))
RemoteCertificateValidationCallback? RemoteCertificateValidationCallback
override void Dispose(bool disposing)
Definition SslStream.cs:725
virtual Task AuthenticateAsClientAsync(string targetHost)
Definition SslStream.cs:554
virtual void AuthenticateAsClient(string targetHost)
Definition SslStream.cs:485
static string net_http_ssl_connection_failed
Definition SR.cs:134
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312