| 1 |
<?php |
| 2 |
|
| 3 |
namespace YoastSEO_Vendor\GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 6 |
/** |
| 7 |
* Resolves proxy configuration from the process environment with the same |
| 8 |
* semantics libcurl applies, so the cURL handlers can pin CURLOPT_PROXY and |
| 9 |
* CURLOPT_NOPROXY explicitly and libcurl never reads the environment itself. |
| 10 |
* |
| 11 |
* @internal |
| 12 |
*/ |
| 13 |
final class ProxyEnvironment |
| 14 |
{ |
| 15 |
private function __construct() |
| 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 = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToLower($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[] = \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::asciiToUpper($scheme) . '_PROXY'; |
| 38 |
} |
| 39 |
$candidates[] = 'all_proxy'; |
| 40 |
$candidates[] = 'ALL_PROXY'; |
| 41 |
foreach ($candidates as $name) { |
| 42 |
$value = self::getenv($name); |
| 43 |
if ($value !== null) { |
| 44 |
return $value; |
| 45 |
} |
| 46 |
} |
| 47 |
return null; |
| 48 |
} |
| 49 |
/** |
| 50 |
* @return string|null The no-proxy list; null when nothing is set. |
| 51 |
*/ |
| 52 |
public static function getNoProxy() : ?string |
| 53 |
{ |
| 54 |
foreach (['no_proxy', 'NO_PROXY'] as $name) { |
| 55 |
$value = self::getenv($name); |
| 56 |
if ($value !== null) { |
| 57 |
return $value; |
| 58 |
} |
| 59 |
} |
| 60 |
return null; |
| 61 |
} |
| 62 |
/** |
| 63 |
* Splits a no_proxy environment value into matchable entries. |
| 64 |
* |
| 65 |
* Mirrors libcurl's tokenization: entries may be separated by commas or |
| 66 |
* blanks, and a single leading dot is ignored, so ".example.com" bypasses |
| 67 |
* example.com and its subdomains exactly as a bare domain entry does. |
| 68 |
* |
| 69 |
* @return string[] |
| 70 |
*/ |
| 71 |
public static function splitNoProxy(string $noProxy) : array |
| 72 |
{ |
| 73 |
$entries = []; |
| 74 |
$split = \preg_split('/[\\s,]+/', $noProxy); |
| 75 |
if ($split === \false) { |
| 76 |
throw new \RuntimeException('Unable to split the no_proxy value: ' . \preg_last_error_msg()); |
| 77 |
} |
| 78 |
foreach ($split as $entry) { |
| 79 |
if ($entry !== '' && $entry[0] === '.') { |
| 80 |
$entry = \substr($entry, 1); |
| 81 |
} |
| 82 |
if ($entry !== '') { |
| 83 |
$entries[] = $entry; |
| 84 |
} |
| 85 |
} |
| 86 |
return $entries; |
| 87 |
} |
| 88 |
private static function getenv(string $name) : ?string |
| 89 |
{ |
| 90 |
// Windows environment variables are case-insensitive, so the |
| 91 |
// lowercase-only httpoxy defence does not hold there. Outside the |
| 92 |
// CLI SAPI on Windows, environment proxies are not resolved at all |
| 93 |
// (a safe-side divergence from libcurl). |
| 94 |
if (\PHP_OS_FAMILY === 'Windows' && \PHP_SAPI !== 'cli') { |
| 95 |
return null; |
| 96 |
} |
| 97 |
// local_only: the OS environment and putenv() only - the same |
| 98 |
// environ(7) libcurl reads. SAPI request environments such as |
| 99 |
// fastcgi_param or SetEnv are deliberately excluded. |
| 100 |
$value = \getenv($name, \true); |
| 101 |
// libcurl's GetEnv (lib/getenv.c) treats variables set to an empty |
| 102 |
// string as unset on every version, so the lookup falls through to |
| 103 |
// the next candidate. |
| 104 |
return $value === \false || $value === '' ? null : $value; |
| 105 |
} |
| 106 |
} |
| 107 |
|