From 625bcee63a58e17cd6e2cc981e76541ea526040e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 1 Apr 2019 17:56:03 +0200 Subject: [PATCH] Fix handling of warnings to incl all 4xx responses --- .../Repository/ComposerRepository.php | 24 +---------- src/Composer/Util/RemoteFilesystem.php | 40 +++++++++++++------ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index b9de0d7ea..38b865103 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -700,7 +700,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } $data = JsonFile::parseJson($json, $filename); - $this->outputWarnings($data); + RemoteFilesystem::outputWarnings($this->io, $this->url, $data); if ($cacheKey) { if ($storeLastModifiedTime) { @@ -765,7 +765,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } $data = JsonFile::parseJson($json, $filename); - $this->outputWarnings($data); + RemoteFilesystem::outputWarnings($this->io, $this->url, $data); $lastModifiedDate = $rfs->findHeaderValue($rfs->getLastHeaders(), 'last-modified'); if ($lastModifiedDate) { @@ -826,24 +826,4 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito // wipe rootData as it is fully consumed at this point and this saves some memory $this->rootData = true; } - - private function outputWarnings($data) - { - foreach (array('warning', 'info') as $type) { - if (empty($data[$type])) { - continue; - } - - if (!empty($data[$type . '-versions'])) { - $versionParser = new VersionParser(); - $constraint = $versionParser->parseConstraints($data[$type . '-versions']); - $composer = new Constraint('==', $versionParser->normalize(Composer::getVersion())); - if (!$constraint->matches($composer)) { - continue; - } - } - - $this->io->writeError('<'.$type.'>'.ucfirst($type).' from '.$this->url.': '.$data[$type].''); - } - } } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index ea18a9e30..e1a93fcf9 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -13,6 +13,9 @@ namespace Composer\Util; use Composer\Config; +use Composer\Composer; +use Composer\Semver\Constraint\Constraint; +use Composer\Package\Version\VersionParser; use Composer\IO\IOInterface; use Composer\Downloader\TransportException; use Composer\CaBundle\CaBundle; @@ -324,15 +327,12 @@ class RemoteFilesystem if (!empty($http_response_header[0])) { $statusCode = $this->findStatusCode($http_response_header); + if ($statusCode >= 400 && $this->findHeaderValue($http_response_header, 'content-type') === 'application/json') { + self::outputWarnings($this->io, $originUrl, json_decode($result, true)); + } + if (in_array($statusCode, array(401, 403)) && $this->retryAuthFailure) { - $warning = null; - if ($this->findHeaderValue($http_response_header, 'content-type') === 'application/json') { - $data = json_decode($result, true); - if (!empty($data['warning'])) { - $warning = $data['warning']; - } - } - $this->promptAuthAndRetry($statusCode, $this->findStatusMessage($http_response_header), $warning, $http_response_header); + $this->promptAuthAndRetry($statusCode, $this->findStatusMessage($http_response_header), null, $http_response_header); } } @@ -741,10 +741,6 @@ class RemoteFilesystem throw new TransportException("Invalid credentials for '" . $this->fileUrl . "', aborting.", $httpStatus); } - $this->io->overwriteError(''); - if ($warning) { - $this->io->writeError(' '.$warning.''); - } $this->io->writeError(' Authentication required ('.parse_url($this->fileUrl, PHP_URL_HOST).'):'); $username = $this->io->ask(' Username: '); $password = $this->io->askAndHideAnswer(' Password: '); @@ -1090,4 +1086,24 @@ class RemoteFilesystem return count($pathParts) >= 4 && $pathParts[3] == 'downloads'; } + + public static function outputWarnings(IOInterface $io, $url, $data) + { + foreach (array('warning', 'info') as $type) { + if (empty($data[$type])) { + continue; + } + + if (!empty($data[$type . '-versions'])) { + $versionParser = new VersionParser(); + $constraint = $versionParser->parseConstraints($data[$type . '-versions']); + $composer = new Constraint('==', $versionParser->normalize(Composer::getVersion())); + if (!$constraint->matches($composer)) { + continue; + } + } + + $io->writeError('<'.$type.'>'.ucfirst($type).' from '.$url.': '.$data[$type].''); + } + } }