From 2a913c7a6894bdbd9097700f50a5d071bd5c2ce5 Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Sat, 24 Oct 2020 18:20:31 +0100 Subject: [PATCH 1/2] Improve proxy error messages for streams --- src/Composer/Util/StreamContextFactory.php | 12 ++++++++---- .../Composer/Test/Util/StreamContextFactoryTest.php | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php index e62bb7e52..781fda6ae 100644 --- a/src/Composer/Util/StreamContextFactory.php +++ b/src/Composer/Util/StreamContextFactory.php @@ -58,7 +58,7 @@ final class StreamContextFactory /** * @param string $url * @param array $options - * @param bool $forCurl + * @param bool $forCurl When true, will not add proxy values as these are handled separately * @psalm-return array{http:{header: string[], proxy?: string, request_fulluri: bool}, ssl: array} * @return array formatted as a stream context array */ @@ -75,13 +75,17 @@ final class StreamContextFactory // Add stream proxy options if there is a proxy if (!$forCurl) { $proxy = ProxyManager::getInstance()->getProxyForRequest($url); + $isHttpsRequest = 0 === strpos($url, 'https://'); + if ($proxy->isSecure()) { if (!extension_loaded('openssl')) { - throw new TransportException('You must enable the openssl extension to use a proxy over https.'); + throw new TransportException('You must enable the openssl extension to use a secure proxy.'); } - if (0 === strpos($url, 'https://')) { - throw new TransportException('PHP does not support https requests to a secure proxy.'); + if ($isHttpsRequest) { + throw new TransportException('You must enable the curl extension to make https requests through a secure proxy.'); } + } elseif ($isHttpsRequest && !extension_loaded('openssl')) { + throw new TransportException('You must enable the openssl extension to make https requests through a proxy.'); } $proxyOptions = $proxy->getContextOptions(); diff --git a/tests/Composer/Test/Util/StreamContextFactoryTest.php b/tests/Composer/Test/Util/StreamContextFactoryTest.php index 80423a825..c28b95b1a 100644 --- a/tests/Composer/Test/Util/StreamContextFactoryTest.php +++ b/tests/Composer/Test/Util/StreamContextFactoryTest.php @@ -226,6 +226,10 @@ class StreamContextFactoryTest extends TestCase public function testInitOptionsForCurlDoesNotIncludeProxyAuthHeaders() { + if (!extension_loaded('curl')) { + $this->markTestSkipped('The curl is not available.'); + } + $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/'; $options = array(); From c99e19db64bf279d7d3357f8289044875b3e0a65 Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Sun, 25 Oct 2020 12:59:01 +0000 Subject: [PATCH 2/2] Fix logic (again) --- src/Composer/Exception/NoSslException.php | 2 ++ src/Composer/Util/StreamContextFactory.php | 36 +++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Composer/Exception/NoSslException.php b/src/Composer/Exception/NoSslException.php index 017cac382..55c81fea9 100644 --- a/src/Composer/Exception/NoSslException.php +++ b/src/Composer/Exception/NoSslException.php @@ -13,6 +13,8 @@ namespace Composer\Exception; /** + * Specific exception for Composer\Util\HttpDownloader creation. + * * @author Jordi Boggiano */ class NoSslException extends \RuntimeException diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php index 781fda6ae..65ca3dd3e 100644 --- a/src/Composer/Util/StreamContextFactory.php +++ b/src/Composer/Util/StreamContextFactory.php @@ -75,27 +75,27 @@ final class StreamContextFactory // Add stream proxy options if there is a proxy if (!$forCurl) { $proxy = ProxyManager::getInstance()->getProxyForRequest($url); - $isHttpsRequest = 0 === strpos($url, 'https://'); - - if ($proxy->isSecure()) { - if (!extension_loaded('openssl')) { - throw new TransportException('You must enable the openssl extension to use a secure proxy.'); - } - if ($isHttpsRequest) { - throw new TransportException('You must enable the curl extension to make https requests through a secure proxy.'); + if ($proxyOptions = $proxy->getContextOptions()) { + $isHttpsRequest = 0 === strpos($url, 'https://'); + + if ($proxy->isSecure()) { + if (!extension_loaded('openssl')) { + throw new TransportException('You must enable the openssl extension to use a secure proxy.'); + } + if ($isHttpsRequest) { + throw new TransportException('You must enable the curl extension to make https requests through a secure proxy.'); + } + } elseif ($isHttpsRequest && !extension_loaded('openssl')) { + throw new TransportException('You must enable the openssl extension to make https requests through a proxy.'); } - } elseif ($isHttpsRequest && !extension_loaded('openssl')) { - throw new TransportException('You must enable the openssl extension to make https requests through a proxy.'); - } - $proxyOptions = $proxy->getContextOptions(); - - // Header will be a Proxy-Authorization string or not set - if (isset($proxyOptions['http']['header'])) { - $options['http']['header'][] = $proxyOptions['http']['header']; - unset($proxyOptions['http']['header']); + // Header will be a Proxy-Authorization string or not set + if (isset($proxyOptions['http']['header'])) { + $options['http']['header'][] = $proxyOptions['http']['header']; + unset($proxyOptions['http']['header']); + } + $options = array_replace_recursive($options, $proxyOptions); } - $options = array_replace_recursive($options, $proxyOptions); } if (defined('HHVM_VERSION')) {