1
0
Fork 0

Clean up temp file on curl request failure and make sure the response body is avaiable on 3xx/4xx/5xx responses

pull/8102/head
Jordi Boggiano 2019-02-21 15:28:50 +01:00
parent d37642d9f2
commit f77285916a
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 15 additions and 7 deletions

View File

@ -251,16 +251,20 @@ class CurlDownloader
// prepare response object // prepare response object
if ($job['filename']) { if ($job['filename']) {
fclose($job['bodyHandle']); $contents = $job['filename'].'~';
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $job['filename'].'~'); if ($statusCode >= 300) {
rewind($job['bodyHandle']);
$contents = stream_get_contents($job['bodyHandle']);
}
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents);
$this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG);
} else { } else {
rewind($job['bodyHandle']); rewind($job['bodyHandle']);
$contents = stream_get_contents($job['bodyHandle']); $contents = stream_get_contents($job['bodyHandle']);
fclose($job['bodyHandle']);
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents); $response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents);
$this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG);
} }
fclose($job['bodyHandle']);
if ($response->getStatusCode() >= 400 && $response->getHeader('content-type') === 'application/json') { if ($response->getStatusCode() >= 400 && $response->getHeader('content-type') === 'application/json') {
HttpDownloader::outputWarnings($this->io, $job['origin'], json_decode($response->getBody(), true)); HttpDownloader::outputWarnings($this->io, $job['origin'], json_decode($response->getBody(), true));
@ -268,10 +272,6 @@ class CurlDownloader
$result = $this->isAuthenticatedRetryNeeded($job, $response); $result = $this->isAuthenticatedRetryNeeded($job, $response);
if ($result['retry']) { if ($result['retry']) {
if ($job['filename']) {
@unlink($job['filename'].'~');
}
$this->restartJob($job, $job['url'], array('storeAuth' => $result['storeAuth'])); $this->restartJob($job, $job['url'], array('storeAuth' => $result['storeAuth']));
continue; continue;
} }
@ -422,6 +422,10 @@ class CurlDownloader
private function restartJob(array $job, $url, array $attributes = array()) private function restartJob(array $job, $url, array $attributes = array())
{ {
if ($job['filename']) {
@unlink($job['filename'].'~');
}
$attributes = array_merge($job['attributes'], $attributes); $attributes = array_merge($job['attributes'], $attributes);
$origin = Url::getOrigin($this->config, $url); $origin = Url::getOrigin($this->config, $url);
@ -430,6 +434,10 @@ class CurlDownloader
private function failResponse(array $job, Response $response, $errorMessage) private function failResponse(array $job, Response $response, $errorMessage)
{ {
if ($job['filename']) {
@unlink($job['filename'].'~');
}
return new TransportException('The "'.$job['url'].'" file could not be downloaded ('.$errorMessage.')', $response->getStatusCode()); return new TransportException('The "'.$job['url'].'" file could not be downloaded ('.$errorMessage.')', $response->getStatusCode());
} }