Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d742b54

Browse files
committed
bug #45678 [HttpClient] Fix reading proxy settings from dotenv when curl is used (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Fix reading proxy settings from dotenv when curl is used | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- 9e5305e [HttpClient] Fix reading proxy settings from dotenv when curl is used
2 parents bea4df7 + 9e5305e commit d742b54

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public function request(string $method, string $url, array $options = []): Respo
9292
$scheme = $url['scheme'];
9393
$authority = $url['authority'];
9494
$host = parse_url($authority, \PHP_URL_HOST);
95+
$proxy = $options['proxy']
96+
?? ('https:' === $url['scheme'] ? $_SERVER['https_proxy'] ?? $_SERVER['HTTPS_PROXY'] ?? null : null)
97+
// Ignore HTTP_PROXY except on the CLI to work around httpoxy set of vulnerabilities
98+
?? $_SERVER['http_proxy'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? $_SERVER['HTTP_PROXY'] ?? null : null) ?? $_SERVER['all_proxy'] ?? $_SERVER['ALL_PROXY'] ?? null;
9599
$url = implode('', $url);
96100

97101
if (!isset($options['normalized_headers']['user-agent'])) {
@@ -107,7 +111,7 @@ public function request(string $method, string $url, array $options = []): Respo
107111
\CURLOPT_MAXREDIRS => 0 < $options['max_redirects'] ? $options['max_redirects'] : 0,
108112
\CURLOPT_COOKIEFILE => '', // Keep track of cookies during redirects
109113
\CURLOPT_TIMEOUT => 0,
110-
\CURLOPT_PROXY => $options['proxy'],
114+
\CURLOPT_PROXY => $proxy,
111115
\CURLOPT_NOPROXY => $options['no_proxy'] ?? $_SERVER['no_proxy'] ?? $_SERVER['NO_PROXY'] ?? '',
112116
\CURLOPT_SSL_VERIFYPEER => $options['verify_peer'],
113117
\CURLOPT_SSL_VERIFYHOST => $options['verify_host'] ? 2 : 0,
@@ -404,8 +408,15 @@ private static function createRedirectResolver(array $options, string $host): \C
404408
}
405409

406410
$url = self::parseUrl(curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL));
411+
$url = self::resolveUrl($location, $url);
407412

408-
return implode('', self::resolveUrl($location, $url));
413+
curl_setopt($ch, \CURLOPT_PROXY, $options['proxy']
414+
?? ('https:' === $url['scheme'] ? $_SERVER['https_proxy'] ?? $_SERVER['HTTPS_PROXY'] ?? null : null)
415+
// Ignore HTTP_PROXY except on the CLI to work around httpoxy set of vulnerabilities
416+
?? $_SERVER['http_proxy'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? $_SERVER['HTTP_PROXY'] ?? null : null) ?? $_SERVER['all_proxy'] ?? $_SERVER['ALL_PROXY'] ?? null
417+
);
418+
419+
return implode('', $url);
409420
};
410421
}
411422
}

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,16 @@ public function testProxy()
929929

930930
$body = $response->toArray();
931931
$this->assertSame('Basic Zm9vOmI9YXI=', $body['HTTP_PROXY_AUTHORIZATION']);
932+
933+
$_SERVER['http_proxy'] = 'http://localhost:8057';
934+
try {
935+
$response = $client->request('GET', 'http://localhost:8057/');
936+
$body = $response->toArray();
937+
$this->assertSame('localhost:8057', $body['HTTP_HOST']);
938+
$this->assertMatchesRegularExpression('#^http://(localhost|127\.0\.0\.1):8057/$#', $body['REQUEST_URI']);
939+
} finally {
940+
unset($_SERVER['http_proxy']);
941+
}
932942
}
933943

934944
public function testNoProxy()

0 commit comments

Comments
 (0)