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