From a3bbcf9c7731930dc492527b27b6703268e4a19b Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 26 Jul 2018 17:31:40 +0200 Subject: [PATCH] Make RemoteFilesystem::getRemoteContents() report response headers also on exceptions --- src/Composer/Util/RemoteFilesystem.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 10db2203d..dc570ecf3 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -315,7 +315,7 @@ class RemoteFilesystem $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg); }); try { - list($http_response_header, $result) = $this->getRemoteContents($originUrl, $fileUrl, $ctx); + $result = $this->getRemoteContents($originUrl, $fileUrl, $ctx, $http_response_header); if (!empty($http_response_header[0])) { $statusCode = $this->findStatusCode($http_response_header); @@ -577,13 +577,24 @@ class RemoteFilesystem * @param string $fileUrl The file URL * @param resource $context The stream context * - * @return array The response headers and the contents + * @return string|false The response contents or false on failure */ - protected function getRemoteContents($originUrl, $fileUrl, $context) + protected function getRemoteContents($originUrl, $fileUrl, $context, array &$responseHeaders = null) { - $contents = file_get_contents($fileUrl, false, $context); + try { + $e = null; + $result = file_get_contents($fileUrl, false, $context); + } catch (\Throwable $e) { + } catch (\Exception $e) { + } - return array(isset($http_response_header) ? $http_response_header : null, $contents); + $responseHeaders = isset($http_response_header) ? $http_response_header : array(); + + if (null !== $e) { + throw $e; + } + + return $result; } /**