| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// A collection of proxies. |
| | | 11 | | /// </summary> |
| | | 12 | | internal struct MultiProxy |
| | | 13 | | { |
| | | 14 | | private readonly FailedProxyCache? _failedProxyCache; |
| | | 15 | | private readonly Uri[]? _uris; |
| | | 16 | | private readonly string? _proxyConfig; |
| | | 17 | | private readonly bool _secure; |
| | | 18 | | private int _currentIndex; |
| | | 19 | | private Uri? _currentUri; |
| | | 20 | | |
| | | 21 | | private MultiProxy(FailedProxyCache? failedProxyCache, Uri[] uris) |
| | 0 | 22 | | { |
| | 0 | 23 | | _failedProxyCache = failedProxyCache; |
| | 0 | 24 | | _uris = uris; |
| | 0 | 25 | | _proxyConfig = null; |
| | 0 | 26 | | _secure = default; |
| | 0 | 27 | | _currentIndex = 0; |
| | 0 | 28 | | _currentUri = null; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private MultiProxy(FailedProxyCache failedProxyCache, string proxyConfig, bool secure) |
| | 0 | 32 | | { |
| | 0 | 33 | | _failedProxyCache = failedProxyCache; |
| | 0 | 34 | | _uris = null; |
| | 0 | 35 | | _proxyConfig = proxyConfig; |
| | 0 | 36 | | _secure = secure; |
| | 0 | 37 | | _currentIndex = 0; |
| | 0 | 38 | | _currentUri = null; |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | public static MultiProxy Empty => new MultiProxy(null, Array.Empty<Uri>()); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Parses a WinHTTP proxy config into a MultiProxy instance. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="failedProxyCache">The cache of failed proxy requests to employ.</param> |
| | | 47 | | /// <param name="proxyConfig">The WinHTTP proxy config to parse.</param> |
| | | 48 | | /// <param name="secure">If true, return proxies suitable for use with a secure connection. If false, return pro |
| | | 49 | | public static MultiProxy ParseManualSettings(FailedProxyCache failedProxyCache, string? proxyConfig, bool secure |
| | 0 | 50 | | { |
| | 0 | 51 | | Debug.Assert(failedProxyCache != null); |
| | | 52 | | |
| | 0 | 53 | | Uri[] uris = Array.Empty<Uri>(); |
| | | 54 | | |
| | 0 | 55 | | ReadOnlySpan<char> span = proxyConfig; |
| | 0 | 56 | | while (TryParseProxyConfigPart(span, secure, manualSettingsUsed: true, out Uri? uri, out int charactersConsu |
| | 0 | 57 | | { |
| | 0 | 58 | | int idx = uris.Length; |
| | | 59 | | |
| | | 60 | | // Assume that we will typically not have more than 1...3 proxies, so just |
| | | 61 | | // grow by 1. This method is currently only used once per process, so the |
| | | 62 | | // case of an abnormally large config will not be much of a concern anyway. |
| | 0 | 63 | | Array.Resize(ref uris, idx + 1); |
| | 0 | 64 | | uris[idx] = uri; |
| | | 65 | | |
| | 0 | 66 | | span = span.Slice(charactersConsumed); |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return new MultiProxy(failedProxyCache, uris); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Initializes a MultiProxy instance that lazily parses a given WinHTTP configuration string. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="failedProxyCache">The cache of failed proxy requests to employ.</param> |
| | | 76 | | /// <param name="proxyConfig">The WinHTTP proxy config to parse.</param> |
| | | 77 | | /// <param name="secure">If true, return proxies suitable for use with a secure connection. If false, return pro |
| | | 78 | | public static MultiProxy CreateLazy(FailedProxyCache failedProxyCache, string proxyConfig, bool secure) |
| | 0 | 79 | | { |
| | 0 | 80 | | Debug.Assert(failedProxyCache != null); |
| | | 81 | | |
| | 0 | 82 | | return !string.IsNullOrEmpty(proxyConfig) ? |
| | 0 | 83 | | new MultiProxy(failedProxyCache, proxyConfig, secure) : |
| | 0 | 84 | | MultiProxy.Empty; |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Reads the next proxy URI from the MultiProxy. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="uri">The next proxy to use for the request.</param> |
| | | 91 | | /// <param name="isFinalProxy">If true, indicates there are no further proxies to read from the config.</param> |
| | | 92 | | /// <returns>If there is a proxy available, true. Otherwise, false.</returns> |
| | | 93 | | public bool ReadNext([NotNullWhen(true)] out Uri? uri, out bool isFinalProxy) |
| | 0 | 94 | | { |
| | | 95 | | // Enumerating indicates the previous proxy has failed; mark it as such. |
| | 0 | 96 | | if (_currentUri != null) |
| | 0 | 97 | | { |
| | 0 | 98 | | Debug.Assert(_failedProxyCache != null); |
| | 0 | 99 | | _failedProxyCache.SetProxyFailed(_currentUri); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | // If no more proxies to read, return out quickly. |
| | 0 | 103 | | if (!ReadNextHelper(out uri, out isFinalProxy)) |
| | 0 | 104 | | { |
| | 0 | 105 | | _currentUri = null; |
| | 0 | 106 | | return false; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // If this is the first ReadNext() and all proxies are marked as failed, return the proxy that is closest to |
| | 0 | 110 | | Uri? oldestFailedProxyUri = null; |
| | 0 | 111 | | long oldestFailedProxyTicks = long.MaxValue; |
| | | 112 | | |
| | | 113 | | do |
| | 0 | 114 | | { |
| | 0 | 115 | | Debug.Assert(_failedProxyCache != null); |
| | 0 | 116 | | long renewTicks = _failedProxyCache.GetProxyRenewTicks(uri); |
| | | 117 | | |
| | | 118 | | // Proxy hasn't failed recently, return for use. |
| | 0 | 119 | | if (renewTicks == FailedProxyCache.Immediate) |
| | 0 | 120 | | { |
| | 0 | 121 | | _currentUri = uri; |
| | 0 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (renewTicks < oldestFailedProxyTicks) |
| | 0 | 126 | | { |
| | 0 | 127 | | oldestFailedProxyUri = uri; |
| | 0 | 128 | | oldestFailedProxyTicks = renewTicks; |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | 0 | 131 | | while (ReadNextHelper(out uri, out isFinalProxy)); |
| | | 132 | | |
| | | 133 | | // All the proxies in the config have failed; in this case, return the proxy that is closest to renewal. |
| | 0 | 134 | | if (_currentUri == null) |
| | 0 | 135 | | { |
| | 0 | 136 | | uri = oldestFailedProxyUri; |
| | 0 | 137 | | _currentUri = oldestFailedProxyUri; |
| | | 138 | | |
| | 0 | 139 | | if (oldestFailedProxyUri != null) |
| | 0 | 140 | | { |
| | 0 | 141 | | Debug.Assert(uri != null); |
| | 0 | 142 | | _failedProxyCache.TryRenewProxy(uri, oldestFailedProxyTicks); |
| | 0 | 143 | | return true; |
| | | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | return false; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Reads the next proxy URI from the MultiProxy, either via parsing a config string or from an array. |
| | | 152 | | /// </summary> |
| | | 153 | | private bool ReadNextHelper([NotNullWhen(true)] out Uri? uri, out bool isFinalProxy) |
| | 0 | 154 | | { |
| | 0 | 155 | | Debug.Assert(_uris != null || _proxyConfig != null, $"{nameof(ReadNext)} must not be called on a default-ini |
| | | 156 | | |
| | 0 | 157 | | if (_uris != null) |
| | 0 | 158 | | { |
| | 0 | 159 | | if (_currentIndex == _uris.Length) |
| | 0 | 160 | | { |
| | 0 | 161 | | uri = default; |
| | 0 | 162 | | isFinalProxy = default; |
| | 0 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | uri = _uris[_currentIndex++]; |
| | 0 | 167 | | isFinalProxy = _currentIndex == _uris.Length; |
| | 0 | 168 | | return true; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | Debug.Assert(_proxyConfig != null); |
| | 0 | 172 | | if (_currentIndex < _proxyConfig.Length) |
| | 0 | 173 | | { |
| | 0 | 174 | | bool hasProxy = TryParseProxyConfigPart(_proxyConfig.AsSpan(_currentIndex), _secure, manualSettingsUsed: |
| | | 175 | | |
| | 0 | 176 | | _currentIndex += charactersConsumed; |
| | 0 | 177 | | Debug.Assert(_currentIndex <= _proxyConfig.Length); |
| | | 178 | | |
| | 0 | 179 | | isFinalProxy = _currentIndex == _proxyConfig.Length; |
| | | 180 | | |
| | 0 | 181 | | return hasProxy; |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | uri = default; |
| | 0 | 185 | | isFinalProxy = default; |
| | 0 | 186 | | return false; |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// This method is used to parse WinINet Proxy strings, a single proxy at a time. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <remarks> |
| | | 193 | | /// The strings are a semicolon or whitespace separated list, with each entry in the following format: |
| | | 194 | | /// ([<scheme>=][<scheme>"://"]<server>[":"<port>]) |
| | | 195 | | /// </remarks> |
| | | 196 | | private static bool TryParseProxyConfigPart(ReadOnlySpan<char> proxyString, bool secure, bool manualSettingsUsed |
| | 0 | 197 | | { |
| | | 198 | | const int SECURE_FLAG = 1; |
| | | 199 | | const int INSECURE_FLAG = 2; |
| | | 200 | | const string ProxyDelimiters = "; \n\r\t"; |
| | | 201 | | |
| | 0 | 202 | | int wantedFlag = secure ? SECURE_FLAG : INSECURE_FLAG; |
| | 0 | 203 | | int originalLength = proxyString.Length; |
| | | 204 | | |
| | 0 | 205 | | while (true) |
| | 0 | 206 | | { |
| | | 207 | | // Skip any delimiters. |
| | 0 | 208 | | int iter = 0; |
| | 0 | 209 | | while (iter < proxyString.Length && ProxyDelimiters.Contains(proxyString[iter])) |
| | 0 | 210 | | { |
| | 0 | 211 | | ++iter; |
| | 0 | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | if (iter == proxyString.Length) |
| | 0 | 215 | | { |
| | 0 | 216 | | break; |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | proxyString = proxyString.Slice(iter); |
| | | 220 | | |
| | | 221 | | // Determine which scheme this part is for. |
| | | 222 | | // If no schema is defined, use both. |
| | 0 | 223 | | int proxyType = SECURE_FLAG | INSECURE_FLAG; |
| | | 224 | | |
| | 0 | 225 | | if (proxyString.StartsWith("http=")) |
| | 0 | 226 | | { |
| | 0 | 227 | | proxyType = INSECURE_FLAG; |
| | 0 | 228 | | proxyString = proxyString.Slice("http=".Length); |
| | 0 | 229 | | } |
| | 0 | 230 | | else if (proxyString.StartsWith("https=")) |
| | 0 | 231 | | { |
| | 0 | 232 | | proxyType = SECURE_FLAG; |
| | 0 | 233 | | proxyString = proxyString.Slice("https=".Length); |
| | 0 | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | if (proxyString.StartsWith("http://")) |
| | 0 | 237 | | { |
| | 0 | 238 | | if (!manualSettingsUsed) |
| | 0 | 239 | | { |
| | 0 | 240 | | proxyType = INSECURE_FLAG; |
| | 0 | 241 | | } |
| | 0 | 242 | | proxyString = proxyString.Slice("http://".Length); |
| | 0 | 243 | | } |
| | 0 | 244 | | else if (proxyString.StartsWith("https://")) |
| | 0 | 245 | | { |
| | 0 | 246 | | if (!manualSettingsUsed) |
| | 0 | 247 | | { |
| | 0 | 248 | | proxyType = SECURE_FLAG; |
| | 0 | 249 | | } |
| | 0 | 250 | | proxyString = proxyString.Slice("https://".Length); |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | // Find the next delimiter, or end of string. |
| | 0 | 254 | | iter = proxyString.IndexOfAny(ProxyDelimiters); |
| | 0 | 255 | | if (iter < 0) |
| | 0 | 256 | | { |
| | 0 | 257 | | iter = proxyString.Length; |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | // Return URI if it's a match to what we want. |
| | 0 | 261 | | if ((proxyType & wantedFlag) != 0 && Uri.TryCreate(string.Concat("http://", proxyString.Slice(0, iter)), |
| | 0 | 262 | | { |
| | 0 | 263 | | charactersConsumed = originalLength - proxyString.Length + iter; |
| | 0 | 264 | | Debug.Assert(charactersConsumed > 0); |
| | | 265 | | |
| | 0 | 266 | | return true; |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | proxyString = proxyString.Slice(iter); |
| | 0 | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | uri = null; |
| | 0 | 273 | | charactersConsumed = originalLength; |
| | 0 | 274 | | return false; |
| | 0 | 275 | | } |
| | | 276 | | } |
| | | 277 | | } |