| | | 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 | | namespace System.Net.Http |
| | | 5 | | { |
| | | 6 | | internal sealed class HttpEnvironmentProxyCredentials : ICredentials |
| | | 7 | | { |
| | | 8 | | // Wrapper class for cases when http and https has different authentication. |
| | | 9 | | private readonly NetworkCredential? _httpCred; |
| | | 10 | | private readonly NetworkCredential? _httpsCred; |
| | | 11 | | private readonly Uri? _httpProxy; |
| | | 12 | | private readonly Uri? _httpsProxy; |
| | | 13 | | |
| | | 14 | | public HttpEnvironmentProxyCredentials(Uri? httpProxy, NetworkCredential? httpCred, |
| | | 15 | | Uri? httpsProxy, NetworkCredential? httpsCred) |
| | | 16 | | { |
| | | 17 | | _httpCred = httpCred; |
| | | 18 | | _httpsCred = httpsCred; |
| | | 19 | | _httpProxy = httpProxy; |
| | | 20 | | _httpsProxy = httpsProxy; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public NetworkCredential? GetCredential(Uri? uri, string authType) |
| | | 24 | | { |
| | | 25 | | if (uri == null) |
| | | 26 | | { |
| | | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | return uri.Equals(_httpProxy) ? _httpCred : |
| | | 30 | | uri.Equals(_httpsProxy) ? _httpsCred : null; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public static HttpEnvironmentProxyCredentials? TryCreate(Uri? httpProxy, Uri? httpsProxy) |
| | | 34 | | { |
| | | 35 | | NetworkCredential? httpCred = null; |
| | | 36 | | NetworkCredential? httpsCred = null; |
| | | 37 | | |
| | | 38 | | if (httpProxy != null) |
| | | 39 | | { |
| | | 40 | | httpCred = GetCredentialsFromString(httpProxy.UserInfo); |
| | | 41 | | } |
| | | 42 | | if (httpsProxy != null) |
| | | 43 | | { |
| | | 44 | | httpsCred = GetCredentialsFromString(httpsProxy.UserInfo); |
| | | 45 | | } |
| | | 46 | | if (httpCred == null && httpsCred == null) |
| | | 47 | | { |
| | | 48 | | return null; |
| | | 49 | | } |
| | | 50 | | return new HttpEnvironmentProxyCredentials(httpProxy, httpCred, httpsProxy, httpsCred); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Converts string containing user:password to NetworkCredential object |
| | | 55 | | /// </summary> |
| | | 56 | | private static NetworkCredential? GetCredentialsFromString(string? value) |
| | | 57 | | { |
| | | 58 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 59 | | { |
| | | 60 | | return null; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | if (value == ":") |
| | | 64 | | { |
| | | 65 | | return CredentialCache.DefaultNetworkCredentials; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | value = Uri.UnescapeDataString(value); |
| | | 69 | | |
| | | 70 | | string password = ""; |
| | | 71 | | string? domain = null; |
| | | 72 | | int idx = value.IndexOf(':'); |
| | | 73 | | if (idx != -1) |
| | | 74 | | { |
| | | 75 | | password = value.Substring(idx + 1); |
| | | 76 | | value = value.Substring(0, idx); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | idx = value.IndexOf('\\'); |
| | | 80 | | if (idx != -1) |
| | | 81 | | { |
| | | 82 | | domain = value.Substring(0, idx); |
| | | 83 | | value = value.Substring(idx + 1); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | return new NetworkCredential(value, password, domain); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal sealed partial class HttpEnvironmentProxy : IWebProxy |
| | | 91 | | { |
| | | 92 | | private const string EnvAllProxyUC = "ALL_PROXY"; |
| | | 93 | | private const string EnvHttpProxyUC = "HTTP_PROXY"; |
| | | 94 | | private const string EnvHttpsProxyUC = "HTTPS_PROXY"; |
| | | 95 | | private const string EnvNoProxyUC = "NO_PROXY"; |
| | | 96 | | private const string EnvCGI = "GATEWAY_INTERFACE"; // Running in a CGI environment. |
| | | 97 | | |
| | | 98 | | private readonly Uri? _httpProxyUri; // String URI for HTTP requests |
| | | 99 | | private readonly Uri? _httpsProxyUri; // String URI for HTTPS requests |
| | | 100 | | private readonly string[]? _bypass; // list of domains not to proxy |
| | | 101 | | private ICredentials? _credentials; |
| | | 102 | | |
| | 0 | 103 | | private HttpEnvironmentProxy(Uri? httpProxy, Uri? httpsProxy, string? bypassList) |
| | 0 | 104 | | { |
| | 0 | 105 | | _httpProxyUri = httpProxy; |
| | 0 | 106 | | _httpsProxyUri = httpsProxy; |
| | | 107 | | |
| | 0 | 108 | | _credentials = HttpEnvironmentProxyCredentials.TryCreate(httpProxy, httpsProxy); |
| | 0 | 109 | | _bypass = bypassList?.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Attempt to parse a partial Uri string into a Uri object. |
| | | 114 | | /// The string may contain the scheme, user info, host, and port. The host is the only required part. |
| | | 115 | | /// Example expected inputs: contoso.com, contoso.com:8080, http://contoso.com/, user@contoso.com. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <returns><see langword="null"/> if parsing fails.</returns> |
| | | 118 | | private static Uri? GetUriFromString(string? value) |
| | 0 | 119 | | { |
| | 0 | 120 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 121 | | { |
| | 0 | 122 | | return null; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | int hostIndex = 0; |
| | 0 | 126 | | string protocol = "http"; |
| | 0 | 127 | | ushort port = 80; |
| | | 128 | | |
| | 0 | 129 | | if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 130 | | { |
| | 0 | 131 | | hostIndex = 7; |
| | 0 | 132 | | } |
| | 0 | 133 | | else if (value.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 134 | | { |
| | 0 | 135 | | hostIndex = 8; |
| | 0 | 136 | | protocol = "https"; |
| | 0 | 137 | | port = 443; |
| | 0 | 138 | | } |
| | 0 | 139 | | else if (value.StartsWith("socks4://", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 140 | | { |
| | 0 | 141 | | hostIndex = 9; |
| | 0 | 142 | | protocol = "socks4"; |
| | 0 | 143 | | } |
| | 0 | 144 | | else if (value.StartsWith("socks5://", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 145 | | { |
| | 0 | 146 | | hostIndex = 9; |
| | 0 | 147 | | protocol = "socks5"; |
| | 0 | 148 | | } |
| | 0 | 149 | | else if (value.StartsWith("socks4a://", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 150 | | { |
| | 0 | 151 | | hostIndex = 10; |
| | 0 | 152 | | protocol = "socks4a"; |
| | 0 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | if (hostIndex > 0) |
| | 0 | 156 | | { |
| | 0 | 157 | | value = value.Substring(hostIndex); |
| | 0 | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | string? user = null; |
| | 0 | 161 | | string? password = null; |
| | | 162 | | string host; |
| | | 163 | | |
| | | 164 | | // Check if there is authentication part with user and possibly password. |
| | | 165 | | // Curl accepts raw text and that may break URI parser. |
| | 0 | 166 | | int separatorIndex = value.LastIndexOf('@'); |
| | 0 | 167 | | if (separatorIndex != -1) |
| | 0 | 168 | | { |
| | | 169 | | // The User and password may or may not be URL encoded. |
| | | 170 | | // Curl seems to accept both. To match that, we also decode the value. |
| | 0 | 171 | | string auth = Uri.UnescapeDataString(value.AsSpan(0, separatorIndex)); |
| | | 172 | | |
| | 0 | 173 | | value = value.Substring(separatorIndex + 1); |
| | 0 | 174 | | separatorIndex = auth.IndexOf(':'); |
| | 0 | 175 | | if (separatorIndex == -1) |
| | 0 | 176 | | { |
| | 0 | 177 | | user = auth; |
| | 0 | 178 | | } |
| | | 179 | | else |
| | 0 | 180 | | { |
| | 0 | 181 | | user = auth.Substring(0, separatorIndex); |
| | 0 | 182 | | password = auth.Substring(separatorIndex + 1); |
| | 0 | 183 | | } |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | // We expect inputs to not contain the path/query/fragment, but we do handle some simple cases like a traili |
| | | 187 | | // An arbitrary path can break this parsing logic, but we expect environment variables to be trusted and wel |
| | 0 | 188 | | int delimiterIndex = value.IndexOfAny('/', '?', '#'); |
| | 0 | 189 | | if (delimiterIndex >= 0) |
| | 0 | 190 | | { |
| | 0 | 191 | | value = value.Substring(0, delimiterIndex); |
| | 0 | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | int ipV6AddressEnd = value.IndexOf(']'); |
| | 0 | 195 | | separatorIndex = value.LastIndexOf(':'); |
| | | 196 | | // No ':' or it is part of IPv6 address. |
| | 0 | 197 | | if (separatorIndex == -1 || (ipV6AddressEnd != -1 && separatorIndex < ipV6AddressEnd)) |
| | 0 | 198 | | { |
| | 0 | 199 | | host = value; |
| | 0 | 200 | | } |
| | | 201 | | else |
| | 0 | 202 | | { |
| | 0 | 203 | | host = value.Substring(0, separatorIndex); |
| | | 204 | | |
| | 0 | 205 | | if (!ushort.TryParse(value.AsSpan(separatorIndex + 1), out port)) |
| | 0 | 206 | | { |
| | 0 | 207 | | return null; |
| | | 208 | | } |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | try |
| | 0 | 212 | | { |
| | 0 | 213 | | UriBuilder ub = new UriBuilder(protocol, host, port); |
| | 0 | 214 | | if (user != null) |
| | 0 | 215 | | { |
| | 0 | 216 | | ub.UserName = Uri.EscapeDataString(user); |
| | 0 | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | if (password != null) |
| | 0 | 220 | | { |
| | 0 | 221 | | ub.Password = Uri.EscapeDataString(password); |
| | 0 | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | Uri uri = ub.Uri; |
| | | 225 | | |
| | | 226 | | // if both user and password exist and are empty we should preserve that and use default credentials. |
| | | 227 | | // UriBuilder does not handle that now e.g. does not distinguish between empty and missing. |
| | 0 | 228 | | if (user == "" && password == "") |
| | 0 | 229 | | { |
| | 0 | 230 | | Span<Range> tokens = stackalloc Range[3]; |
| | 0 | 231 | | ReadOnlySpan<char> uriSpan = uri.ToString(); |
| | 0 | 232 | | if (uriSpan.Split(tokens, '/') == 3) |
| | 0 | 233 | | { |
| | 0 | 234 | | uri = new Uri($"{uriSpan[tokens[0]]}//:@{uriSpan[tokens[2]]}"); |
| | 0 | 235 | | } |
| | 0 | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | return uri; |
| | | 239 | | } |
| | 0 | 240 | | catch { }; |
| | 0 | 241 | | return null; |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// This function returns true if given Host match bypass list. |
| | | 246 | | /// Note, that the list is common for http and https. |
| | | 247 | | /// </summary> |
| | | 248 | | private bool IsMatchInBypassList(Uri input) |
| | 0 | 249 | | { |
| | 0 | 250 | | if (_bypass != null) |
| | 0 | 251 | | { |
| | 0 | 252 | | foreach (string s in _bypass) |
| | 0 | 253 | | { |
| | 0 | 254 | | if (s[0] == '.') |
| | 0 | 255 | | { |
| | | 256 | | // This should match either domain it self or any subdomain or host |
| | | 257 | | // .foo.com will match foo.com it self or *.foo.com |
| | 0 | 258 | | if (s.AsSpan(1).Equals(input.Host, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 259 | | { |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | 0 | 262 | | else if (input.Host.EndsWith(s, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 263 | | { |
| | 0 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | } |
| | | 268 | | else |
| | 0 | 269 | | { |
| | 0 | 270 | | if (string.Equals(s, input.Host, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 271 | | { |
| | 0 | 272 | | return true; |
| | | 273 | | } |
| | 0 | 274 | | } |
| | 0 | 275 | | } |
| | 0 | 276 | | } |
| | 0 | 277 | | return false; |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Gets the proxy URI. (iWebProxy interface) |
| | | 282 | | /// </summary> |
| | | 283 | | public Uri? GetProxy(Uri uri) |
| | 0 | 284 | | { |
| | 0 | 285 | | return HttpUtilities.IsSupportedNonSecureScheme(uri.Scheme) ? _httpProxyUri : _httpsProxyUri; |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Checks if URI is subject to proxy or not. |
| | | 290 | | /// </summary> |
| | | 291 | | public bool IsBypassed(Uri uri) |
| | 0 | 292 | | { |
| | 0 | 293 | | return GetProxy(uri) == null ? true : IsMatchInBypassList(uri); |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | public ICredentials? Credentials |
| | | 297 | | { |
| | | 298 | | get |
| | 0 | 299 | | { |
| | 0 | 300 | | return _credentials; |
| | 0 | 301 | | } |
| | | 302 | | set |
| | 0 | 303 | | { |
| | 0 | 304 | | _credentials = value; |
| | 0 | 305 | | } |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | } |