Skip to content

Commit b1a64f3

Browse files
committed
fix: 兼容数组类型的env_http_proxy
1 parent 7b8bb0a commit b1a64f3

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

src/Client.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function parse_url;
3030
use function str_contains;
3131
use function strtolower;
32+
use function is_array;
3233

3334
/**
3435
*
@@ -68,14 +69,7 @@ public function request(RequestInterface $request, array $option = []): Response
6869
$port = $scheme === 'https' ? 443 : 80;
6970
}
7071

71-
if (!isset($option['proxy'])) {
72-
if ($scheme === 'http' && $httpProxy = getenv('http_proxy')) {
73-
$option['proxy'] = $httpProxy;
74-
} elseif ($scheme === 'https' && $httpsProxy = getenv('https_proxy')) {
75-
$option['proxy'] = $httpsProxy;
76-
}
77-
}
78-
72+
$proxy = $this->parseProxy($scheme, $option['proxy'] ?? null);
7973
$capture = $option['capture'] ?? null;
8074

8175
try {
@@ -84,7 +78,7 @@ public function request(RequestInterface $request, array $option = []): Response
8478
$port,
8579
$scheme === 'https',
8680
$option['timeout'] ?? 0,
87-
$option['proxy'] ?? null
81+
$proxy
8882
);
8983
} catch (Throwable $exception) {
9084
if ($capture instanceof Capture) {
@@ -186,4 +180,29 @@ public function getConnectionPool(): ConnectionPool|null
186180
{
187181
return $this->connectionPool;
188182
}
183+
184+
/**
185+
* @param string $scheme
186+
* @param mixed|null $proxy
187+
* @return mixed
188+
*/
189+
private function parseProxy(string $scheme, mixed $proxy = null): mixed
190+
{
191+
if (is_array($proxy)) {
192+
return $proxy[$scheme] ?? null;
193+
}
194+
195+
if ($proxy) {
196+
return $proxy;
197+
}
198+
199+
if ($scheme === 'http' && $httpProxy = getenv('http_proxy')) {
200+
return $httpProxy;
201+
}
202+
if ($scheme === 'https' && $httpsProxy = getenv('https_proxy')) {
203+
return $httpsProxy;
204+
}
205+
206+
return null;
207+
}
189208
}

0 commit comments

Comments
 (0)