gmail-smtp
/
google-api-php-client
/
vendor
/
guzzlehttp
/
guzzle
/
src
/
Handler
/
ProxyEnvironment.php
ProxyEnvironment.php in Gmail SMTP trunk, at google-api-php-client/vendor/guzzlehttp/guzzle/src/Handler/ProxyEnvironment.php
| 1 | <?php |
| 2 | |
| 3 | namespace GuzzleHttp\Handler; |
| 4 | |
| 5 | /** |
| 6 | * Resolves proxy configuration from the process environment with the same |
| 7 | * semantics libcurl applies, so the cURL handlers can pin CURLOPT_PROXY and |
| 8 | * CURLOPT_NOPROXY explicitly and libcurl never reads the environment itself. |
| 9 | * |
| 10 | * @internal |
| 11 | */ |
| 12 | final class ProxyEnvironment |
| 13 | { |
| 14 | private function __construct() |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Resolves the proxy to use for the given request scheme. |
| 20 | * |
| 21 | * The lookup mirrors libcurl for the http and https schemes the handlers |
| 22 | * accept: the lowercase scheme-specific variable first, its uppercase |
| 23 | * variant next (except for "http", where uppercase HTTP_PROXY is never |
| 24 | * read), then all_proxy/ALL_PROXY. |
| 25 | * |
| 26 | * @return string|null The proxy to use; null when the environment |
| 27 | * configures none. |
| 28 | */ |
| 29 | public static function getProxyForScheme(string $scheme): ?string |
| 30 | { |
| 31 | $scheme = \strtolower($scheme); |
| 32 | $candidates = [$scheme.'_proxy']; |
| 33 | if ($scheme !== 'http') { |
| 34 | // Uppercase HTTP_PROXY is deliberately never consulted: a CGI |
| 35 | // request header "Proxy:" becomes HTTP_PROXY in the environment. |
| 36 | // See https://httpoxy.org for more information. |
| 37 | $candidates[] = \strtoupper($scheme).'_PROXY'; |
| 38 | } |
| 39 | $candidates[] = 'all_proxy'; |
| 40 | $candidates[] = 'ALL_PROXY'; |
| 41 | |
| 42 | foreach ($candidates as $name) { |
| 43 | $value = self::getenv($name); |
| 44 | if ($value !== null) { |
| 45 | return $value; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return string|null The no-proxy list; null when nothing is set. |
| 54 | */ |
| 55 | public static function getNoProxy(): ?string |
| 56 | { |
| 57 | foreach (['no_proxy', 'NO_PROXY'] as $name) { |
| 58 | $value = self::getenv($name); |
| 59 | if ($value !== null) { |
| 60 | return $value; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Splits a no_proxy environment value into matchable entries. |
| 69 | * |
| 70 | * Mirrors libcurl's tokenization: entries may be separated by commas or |
| 71 | * blanks, and a single leading dot is ignored, so ".example.com" bypasses |
| 72 | * example.com and its subdomains exactly as a bare domain entry does. |
| 73 | * |
| 74 | * @return string[] |
| 75 | */ |
| 76 | public static function splitNoProxy(string $noProxy): array |
| 77 | { |
| 78 | $entries = []; |
| 79 | |
| 80 | foreach (\preg_split('/[\s,]+/', $noProxy) ?: [] as $entry) { |
| 81 | if ($entry !== '' && $entry[0] === '.') { |
| 82 | $entry = \substr($entry, 1); |
| 83 | } |
| 84 | |
| 85 | if ($entry !== '') { |
| 86 | $entries[] = $entry; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $entries; |
| 91 | } |
| 92 | |
| 93 | private static function getenv(string $name): ?string |
| 94 | { |
| 95 | // Windows environment variables are case-insensitive, so the |
| 96 | // lowercase-only httpoxy defence does not hold there. Outside the |
| 97 | // CLI SAPI on Windows, environment proxies are not resolved at all |
| 98 | // (a safe-side divergence from libcurl). |
| 99 | if (\PHP_OS_FAMILY === 'Windows' && \PHP_SAPI !== 'cli') { |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | // local_only: the OS environment and putenv() only - the same |
| 104 | // environ(7) libcurl reads. SAPI request environments such as |
| 105 | // fastcgi_param or SetEnv are deliberately excluded. |
| 106 | $value = \getenv($name, true); |
| 107 | |
| 108 | // libcurl's GetEnv (lib/getenv.c) treats variables set to an empty |
| 109 | // string as unset on every version, so the lookup falls through to |
| 110 | // the next candidate. |
| 111 | return $value === false || $value === '' ? null : $value; |
| 112 | } |
| 113 | } |
| 114 |