Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HttpConnectionPoolManager.cs
Go to the documentation of this file.
8
9namespace System.Net.Http;
10
12{
13 private sealed class NetworkChangeCleanup : IDisposable
14 {
16
18 {
19 _handler = handler;
20 }
21
23 {
24 NetworkChange.NetworkAddressChanged -= _handler;
25 }
26
27 public void Dispose()
28 {
29 NetworkChange.NetworkAddressChanged -= _handler;
30 GC.SuppressFinalize(this);
31 }
32 }
33
34 internal readonly struct HttpConnectionKey : IEquatable<HttpConnectionKey>
35 {
36 public readonly HttpConnectionKind Kind;
37
38 public readonly string Host;
39
40 public readonly int Port;
41
42 public readonly string SslHostName;
43
44 public readonly Uri ProxyUri;
45
46 public readonly string Identity;
47
48 public HttpConnectionKey(HttpConnectionKind kind, string host, int port, string sslHostName, Uri proxyUri, string identity)
49 {
50 Kind = kind;
51 Host = host;
52 Port = port;
55 Identity = identity;
56 }
57
58 public override int GetHashCode()
59 {
60 if (!(SslHostName == Host))
61 {
62 return HashCode.Combine(Kind, Host, Port, SslHostName, ProxyUri, Identity);
63 }
64 return HashCode.Combine(Kind, Host, Port, ProxyUri, Identity);
65 }
66
67 public override bool Equals([NotNullWhen(true)] object obj)
68 {
70 {
71 return Equals(other);
72 }
73 return false;
74 }
75
77 {
78 if (Kind == other.Kind && Host == other.Host && Port == other.Port && ProxyUri == other.ProxyUri && SslHostName == other.SslHostName)
79 {
80 return Identity == other.Identity;
81 }
82 return false;
83 }
84 }
85
86 private readonly TimeSpan _cleanPoolTimeout;
87
89
90 private readonly Timer _cleaningTimer;
91
92 private readonly Timer _heartBeatTimer;
93
95
96 private readonly IWebProxy _proxy;
97
99
101
102 private bool _timerIsRunning;
103
104 private object SyncObj => _pools;
105
107
109
111 {
112 _settings = settings;
114 if (settings._maxConnectionsPerServer != int.MaxValue || (!(settings._pooledConnectionIdleTimeout == TimeSpan.Zero) && !(settings._pooledConnectionLifetime == TimeSpan.Zero)))
115 {
117 {
119 }
120 else
121 {
122 TimeSpan timeSpan = settings._pooledConnectionIdleTimeout / 4.0;
123 _cleanPoolTimeout = ((timeSpan.TotalSeconds >= 1.0) ? timeSpan : TimeSpan.FromSeconds(1.0));
124 }
125 bool flag = false;
126 try
127 {
129 {
131 flag = true;
132 }
134 _cleaningTimer = new Timer(delegate(object s)
135 {
137 if (weakReference2.TryGetTarget(out var target2))
138 {
139 target2.RemoveStalePools();
140 }
141 }, state2, -1, -1);
143 {
145 _heartBeatTimer = new Timer(delegate(object state)
146 {
148 if (weakReference.TryGetTarget(out var target))
149 {
150 target.HeartBeat();
151 }
152 }, state2, num, num);
153 }
154 }
155 finally
156 {
157 if (flag)
158 {
160 }
161 }
162 }
163 if (settings._useProxy)
164 {
165 _proxy = settings._proxy ?? HttpClient.DefaultProxy;
166 if (_proxy != null)
167 {
168 _proxyCredentials = _proxy.Credentials ?? settings._defaultProxyCredentials;
169 }
170 }
171 }
172
174 {
175 if (_networkChangeCleanup != null)
176 {
177 return;
178 }
181 {
182 if (poolsRef.TryGetTarget(out var target))
183 {
184 foreach (HttpConnectionPool value in target.Values)
185 {
186 value.OnNetworkChanged();
187 }
188 }
189 };
192 {
194 return;
195 }
197 {
199 {
200 NetworkChange.NetworkAddressChanged += networkAddressChangedEventHandler;
201 return;
202 }
203 }
204 NetworkChange.NetworkAddressChanged += networkAddressChangedEventHandler;
205 }
206
207 private static string ParseHostNameFromHeader(string hostHeader)
208 {
209 int num = hostHeader.IndexOf(':');
210 if (num >= 0)
211 {
212 int num2 = hostHeader.IndexOf(']');
213 if (num2 == -1)
214 {
215 return hostHeader.Substring(0, num);
216 }
217 num = hostHeader.LastIndexOf(':');
218 if (num > num2)
219 {
220 return hostHeader.Substring(0, num);
221 }
222 }
223 return hostHeader;
224 }
225
227 {
228 Uri requestUri = request.RequestUri;
229 if (isProxyConnect)
230 {
232 }
233 string text = null;
235 {
236 string host = request.Headers.Host;
237 text = ((host == null) ? requestUri.IdnHost : ParseHostNameFromHeader(host));
238 }
240 if (proxyUri != null)
241 {
243 {
244 if (text != null)
245 {
247 }
249 }
250 if (text == null)
251 {
253 {
255 }
257 }
259 }
260 if (text != null)
261 {
263 }
265 }
266
293
298
300 {
301 if (_proxy == null)
302 {
304 }
305 Uri uri = null;
306 try
307 {
308 if (!_proxy.IsBypassed(request.RequestUri))
309 {
311 {
312 MultiProxy multiProxy = multiWebProxy.GetMultiProxy(request.RequestUri);
313 if (multiProxy.ReadNext(out uri, out var isFinalProxy) && !isFinalProxy)
314 {
316 }
317 }
318 else
319 {
320 uri = _proxy.GetProxy(request.RequestUri);
321 }
322 }
323 }
324 catch (Exception ex)
325 {
326 if (System.Net.NetEventSource.Log.IsEnabled())
327 {
328 System.Net.NetEventSource.Error(this, $"Exception from {_proxy.GetType().Name}.GetProxy({request.RequestUri}): {ex}", "SendAsync");
329 }
330 }
331 if (uri != null && !HttpUtilities.IsSupportedProxyScheme(uri.Scheme))
332 {
334 }
336 }
337
357
358 public void Dispose()
359 {
363 {
364 pool.Value.Dispose();
365 }
367 }
368
370 {
371 try
372 {
375 }
377 {
378 }
379 }
380
381 private void RemoveStalePools()
382 {
384 {
385 if (pool.Value.CleanCacheAndDisposeIfUnused())
386 {
387 _pools.TryRemove(pool.Key, out var _);
388 }
389 }
390 lock (SyncObj)
391 {
392 if (_pools.IsEmpty)
393 {
395 }
396 }
397 }
398
399 private void HeartBeat()
400 {
402 {
403 pool.Value.HeartBeat();
404 }
405 }
406
408 {
410 {
411 return string.Empty;
412 }
414 }
415}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool TryAdd(TKey key, TValue value)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static IWebProxy DefaultProxy
Definition HttpClient.cs:40
ValueTask< HttpResponseMessage > SendAsyncCore(HttpRequestMessage request, Uri proxyUri, bool async, bool doRequestAuth, bool isProxyConnect, CancellationToken cancellationToken)
static string ParseHostNameFromHeader(string hostHeader)
HttpConnectionKey GetConnectionKey(HttpRequestMessage request, Uri proxyUri, bool isProxyConnect)
async ValueTask< HttpResponseMessage > SendAsyncMultiProxy(HttpRequestMessage request, bool async, bool doRequestAuth, MultiProxy multiProxy, Uri firstProxy, CancellationToken cancellationToken)
readonly ConcurrentDictionary< HttpConnectionKey, HttpConnectionPool > _pools
HttpConnectionPoolManager(HttpConnectionSettings settings)
ValueTask< HttpResponseMessage > SendProxyConnectAsync(HttpRequestMessage request, Uri proxyUri, bool async, CancellationToken cancellationToken)
ValueTask< HttpResponseMessage > SendAsync(HttpRequestMessage request, bool async, bool doRequestAuth, CancellationToken cancellationToken)
static string GetIdentityIfDefaultCredentialsUsed(bool defaultCredentialsUsed)
static bool IsSupportedSecureScheme(string scheme)
static bool IsSupportedProxyScheme(string scheme)
static bool IsSocksScheme(string scheme)
static bool IsNonSecureWebSocketScheme(string scheme)
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static string net_http_invalid_proxy_scheme
Definition SR.cs:130
Definition SR.cs:7
static AsyncFlowControl SuppressFlow()
static int CompareExchange(ref int location1, int value, int comparand)
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5
bool Dispose(WaitHandle notifyObject)
Definition Timer.cs:176
bool Change(int dueTime, int period)
Definition Timer.cs:131
string Scheme
Definition Uri.cs:505
Uri? GetProxy(Uri destination)
bool IsBypassed(Uri host)
delegate void NetworkAddressChangedEventHandler(object? sender, EventArgs e)
HttpConnectionKey(HttpConnectionKind kind, string host, int port, string sslHostName, Uri proxyUri, string identity)
double TotalMilliseconds
Definition TimeSpan.cs:46
static TimeSpan FromSeconds(double value)
Definition TimeSpan.cs:247
static readonly TimeSpan Zero
Definition TimeSpan.cs:21