From 62eff8e979a5adeffc2b5490f581d5ea5bbf008e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 24 Oct 2020 10:36:39 +0200 Subject: [PATCH] Tweaks to new proxying code, refs #9324 --- src/Composer/Util/Http/CurlDownloader.php | 2 +- src/Composer/Util/Http/ProxyManager.php | 22 +++++++++++++------ src/Composer/Util/Http/RequestProxy.php | 16 ++++++-------- src/Composer/Util/RemoteFilesystem.php | 2 +- .../Test/Util/Http/ProxyManagerTest.php | 5 +++-- .../Test/Util/Http/RequestProxyTest.php | 8 +++---- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Composer/Util/Http/CurlDownloader.php b/src/Composer/Util/Http/CurlDownloader.php index 9ab716e58..44f55764a 100644 --- a/src/Composer/Util/Http/CurlDownloader.php +++ b/src/Composer/Util/Http/CurlDownloader.php @@ -234,7 +234,7 @@ class CurlDownloader 'reject' => $reject, ); - $usingProxy = $proxy->getLastProxy(' using proxy (%s)'); + $usingProxy = $proxy->getFormattedUrl(' using proxy (%s)'); $ifModified = false !== stripos(implode(',', $options['http']['header']), 'if-modified-since:') ? ' if modified' : ''; if ($attributes['redirects'] === 0) { $this->io->writeError('Downloading ' . Url::sanitize($url) . $usingProxy . $ifModified, true, IOInterface::DEBUG); diff --git a/src/Composer/Util/Http/ProxyManager.php b/src/Composer/Util/Http/ProxyManager.php index be8983771..1963f8fc2 100644 --- a/src/Composer/Util/Http/ProxyManager.php +++ b/src/Composer/Util/Http/ProxyManager.php @@ -85,34 +85,42 @@ class ProxyManager $scheme = parse_url($requestUrl, PHP_URL_SCHEME) ?: 'http'; $proxyUrl = ''; $options = array(); - $lastProxy = ''; + $formattedProxyUrl = ''; if ($this->hasProxy && $this->fullProxy[$scheme]) { if ($this->noProxy($requestUrl)) { - $lastProxy = 'excluded by no_proxy'; + $formattedProxyUrl = 'excluded by no_proxy'; } else { $proxyUrl = $this->fullProxy[$scheme]; $options = $this->streams[$scheme]['options']; ProxyHelper::setRequestFullUri($requestUrl, $options); - $lastProxy = $this->safeProxy[$scheme]; + $formattedProxyUrl = $this->safeProxy[$scheme]; } } - return new RequestProxy($proxyUrl, $options, $lastProxy); + return new RequestProxy($proxyUrl, $options, $formattedProxyUrl); } /** * Returns true if a proxy is being used * - * @param string|null $message Set to safe proxy values * @return bool If false any error will be in $message */ - public function getStatus(&$message) + public function isProxying() { - $message = $this->hasProxy ? $this->info : $this->error; return $this->hasProxy; } + /** + * Returns proxy configuration info which can be shown to the user + * + * @return string|null Safe proxy URL or an error message if setting up proxy failed or null if no proxy was configured + */ + public function getFormattedProxy() + { + return $this->hasProxy ? $this->info : $this->error; + } + /** * Initializes proxy values from the environment */ diff --git a/src/Composer/Util/Http/RequestProxy.php b/src/Composer/Util/Http/RequestProxy.php index 44f0c27cc..abaa3e2ce 100644 --- a/src/Composer/Util/Http/RequestProxy.php +++ b/src/Composer/Util/Http/RequestProxy.php @@ -22,21 +22,19 @@ class RequestProxy { private $contextOptions; private $isSecure; - private $lastProxy; - private $safeUrl; + private $formattedUrl; private $url; /** * @param string $url * @param array $contextOptions - * @param string $lastProxy + * @param string $formattedUrl */ - public function __construct($url, array $contextOptions, $lastProxy) + public function __construct($url, array $contextOptions, $formattedUrl) { $this->url = $url; $this->contextOptions = $contextOptions; - $this->lastProxy = $lastProxy; - $this->safeUrl = Url::sanitize($url); + $this->formattedUrl = $formattedUrl; $this->isSecure = 0 === strpos($url, 'https://'); } @@ -56,12 +54,12 @@ class RequestProxy * @param string|null $format Output format specifier * @return string Safe proxy, no proxy or empty */ - public function getLastProxy($format = '') + public function getFormattedUrl($format = '') { $result = ''; - if ($this->lastProxy) { + if ($this->formattedUrl) { $format = $format ?: '%s'; - $result = sprintf($format, $this->lastProxy); + $result = sprintf($format, $this->formattedUrl); } return $result; diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index c5e5eba53..db5d5a0da 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -255,7 +255,7 @@ class RemoteFilesystem $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); $proxy = ProxyManager::getInstance()->getProxyForRequest($fileUrl); - $usingProxy = $proxy->getLastProxy(' using proxy (%s)'); + $usingProxy = $proxy->getFormattedUrl(' using proxy (%s)'); $this->io->writeError((strpos($origFileUrl, 'http') === 0 ? 'Downloading ' : 'Reading ') . Url::sanitize($origFileUrl) . $usingProxy, true, IOInterface::DEBUG); unset($origFileUrl, $proxy, $usingProxy); diff --git a/tests/Composer/Test/Util/Http/ProxyManagerTest.php b/tests/Composer/Test/Util/Http/ProxyManagerTest.php index e4eb54a46..492eed126 100644 --- a/tests/Composer/Test/Util/Http/ProxyManagerTest.php +++ b/tests/Composer/Test/Util/Http/ProxyManagerTest.php @@ -82,7 +82,7 @@ class ProxyManagerTest extends TestCase $this->assertSame($expectedOptions, $proxy->getContextOptions()); $this->assertSame($expectedSecure, $proxy->isSecure()); - $message = $proxy->getLastProxy(); + $message = $proxy->getFormattedUrl(); if ($expectedMessage) { $condition = stripos($message, $expectedMessage) !== false; @@ -134,7 +134,8 @@ class ProxyManagerTest extends TestCase { $_SERVER = array_merge($_SERVER, $server); $proxyManager = ProxyManager::getInstance(); - $status = $proxyManager->getStatus($message); + $status = $proxyManager->isProxying(); + $message = $proxyManager->getFormattedProxy(); $this->assertSame($expectedStatus, $status); diff --git a/tests/Composer/Test/Util/Http/RequestProxyTest.php b/tests/Composer/Test/Util/Http/RequestProxyTest.php index f87002515..6f571eb20 100644 --- a/tests/Composer/Test/Util/Http/RequestProxyTest.php +++ b/tests/Composer/Test/Util/Http/RequestProxyTest.php @@ -38,17 +38,17 @@ class RequestProxyTest extends TestCase } /** - * @dataProvider dataLastProxy + * @dataProvider dataProxyUrl */ - public function testGetLastProxyFormat($url, $format, $expected) + public function testGetFormattedUrlFormat($url, $format, $expected) { $proxy = new RequestProxy($url, array(), $url); - $message = $proxy->getLastProxy($format); + $message = $proxy->getFormattedUrl($format); $this->assertSame($expected, $message); } - public function dataLastProxy() + public function dataProxyUrl() { $format = 'proxy (%s)';