1
0
Fork 0

Merge pull request #9334 from johnstevenson/proxy-streams

Improve proxy error messages for streams
pull/9341/head
Jordi Boggiano 2020-10-25 14:13:40 +01:00 committed by GitHub
commit 0543b59e06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 15 deletions

View File

@ -13,6 +13,8 @@
namespace Composer\Exception; namespace Composer\Exception;
/** /**
* Specific exception for Composer\Util\HttpDownloader creation.
*
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class NoSslException extends \RuntimeException class NoSslException extends \RuntimeException

View File

@ -58,7 +58,7 @@ final class StreamContextFactory
/** /**
* @param string $url * @param string $url
* @param array $options * @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} * @psalm-return array{http:{header: string[], proxy?: string, request_fulluri: bool}, ssl: array}
* @return array formatted as a stream context array * @return array formatted as a stream context array
*/ */
@ -75,17 +75,20 @@ final class StreamContextFactory
// Add stream proxy options if there is a proxy // Add stream proxy options if there is a proxy
if (!$forCurl) { if (!$forCurl) {
$proxy = ProxyManager::getInstance()->getProxyForRequest($url); $proxy = ProxyManager::getInstance()->getProxyForRequest($url);
if ($proxyOptions = $proxy->getContextOptions()) {
$isHttpsRequest = 0 === strpos($url, 'https://');
if ($proxy->isSecure()) { if ($proxy->isSecure()) {
if (!extension_loaded('openssl')) { 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://')) { if ($isHttpsRequest) {
throw new TransportException('PHP does not support https requests to a secure proxy.'); 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();
// Header will be a Proxy-Authorization string or not set // Header will be a Proxy-Authorization string or not set
if (isset($proxyOptions['http']['header'])) { if (isset($proxyOptions['http']['header'])) {
$options['http']['header'][] = $proxyOptions['http']['header']; $options['http']['header'][] = $proxyOptions['http']['header'];
@ -93,6 +96,7 @@ final class StreamContextFactory
} }
$options = array_replace_recursive($options, $proxyOptions); $options = array_replace_recursive($options, $proxyOptions);
} }
}
if (defined('HHVM_VERSION')) { if (defined('HHVM_VERSION')) {
$phpVersion = 'HHVM ' . HHVM_VERSION; $phpVersion = 'HHVM ' . HHVM_VERSION;

View File

@ -226,6 +226,10 @@ class StreamContextFactoryTest extends TestCase
public function testInitOptionsForCurlDoesNotIncludeProxyAuthHeaders() public function testInitOptionsForCurlDoesNotIncludeProxyAuthHeaders()
{ {
if (!extension_loaded('curl')) {
$this->markTestSkipped('The curl is not available.');
}
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/'; $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
$options = array(); $options = array();