From f7bf63544045b9fc286bd222cd7ba2745922f558 Mon Sep 17 00:00:00 2001 From: Wissem Riahi Date: Fri, 8 Jan 2021 11:28:35 +0100 Subject: [PATCH] Add response info for 3xx/4xx/5xx http responses --- src/Composer/Util/Http/CurlDownloader.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Composer/Util/Http/CurlDownloader.php b/src/Composer/Util/Http/CurlDownloader.php index 6f6d69352..de4cd3d11 100644 --- a/src/Composer/Util/Http/CurlDownloader.php +++ b/src/Composer/Util/Http/CurlDownloader.php @@ -337,7 +337,7 @@ class CurlDownloader HttpDownloader::outputWarnings($this->io, $job['origin'], json_decode($response->getBody(), true)); } - $result = $this->isAuthenticatedRetryNeeded($job, $response); + $result = $this->isAuthenticatedRetryNeeded($job, $response, $progress); if ($result['retry']) { $this->restartJob($job, $job['url'], array('storeAuth' => $result['storeAuth'])); continue; @@ -345,7 +345,7 @@ class CurlDownloader // handle 3xx redirects, 304 Not Modified is excluded if ($statusCode >= 300 && $statusCode <= 399 && $statusCode !== 304 && $job['attributes']['redirects'] < $this->maxRedirects) { - $location = $this->handleRedirect($job, $response); + $location = $this->handleRedirect($job, $response, $progress); if ($location) { $this->restartJob($job, $location, array('redirects' => $job['attributes']['redirects'] + 1)); continue; @@ -354,7 +354,7 @@ class CurlDownloader // fail 4xx and 5xx responses and capture the response if ($statusCode >= 400 && $statusCode <= 599) { - throw $this->failResponse($job, $response, $response->getStatusMessage()); + throw $this->failResponse($job, $response, $response->getStatusMessage(), $progress); } if ($job['attributes']['storeAuth']) { @@ -417,7 +417,7 @@ class CurlDownloader } } - private function handleRedirect(array $job, Response $response) + private function handleRedirect(array $job, Response $response, array $responseInfo) { if ($locationHeader = $response->getHeader('location')) { if (parse_url($locationHeader, PHP_URL_SCHEME)) { @@ -445,10 +445,12 @@ class CurlDownloader return $targetUrl; } - throw new TransportException('The "'.$job['url'].'" file could not be downloaded, got redirect without Location ('.$response->getStatusMessage().')'); + $exception = new TransportException('The "'.$job['url'].'" file could not be downloaded, got redirect without Location ('.$response->getStatusMessage().')'); + $exception->setResponseInfo($responseInfo); + throw $exception; } - private function isAuthenticatedRetryNeeded(array $job, Response $response) + private function isAuthenticatedRetryNeeded(array $job, Response $response, array $responseInfo) { if (in_array($response->getStatusCode(), array(401, 403)) && $job['attributes']['retryAuthFailure']) { $result = $this->authHelper->promptAuthIfNeeded($job['url'], $job['origin'], $response->getStatusCode(), $response->getStatusMessage(), $response->getHeaders()); @@ -489,7 +491,7 @@ class CurlDownloader } } - throw $this->failResponse($job, $response, $needsAuthRetry); + throw $this->failResponse($job, $response, $needsAuthRetry, $responseInfo); } return array('retry' => false, 'storeAuth' => false); @@ -507,13 +509,15 @@ class CurlDownloader $this->initDownload($job['resolve'], $job['reject'], $origin, $url, $job['options'], $job['filename'], $attributes); } - private function failResponse(array $job, Response $response, $errorMessage) + private function failResponse(array $job, Response $response, $errorMessage, array $responseInfo) { if ($job['filename']) { @unlink($job['filename'].'~'); } - return new TransportException('The "'.$job['url'].'" file could not be downloaded ('.$errorMessage.')', $response->getStatusCode()); + $exception = new TransportException('The "'.$job['url'].'" file could not be downloaded ('.$errorMessage.')', $response->getStatusCode()); + $exception->setResponseInfo($responseInfo); + return $exception; } private function checkCurlResult($code)