Add all curl_getinfo into the response and exception for curl requests

main
Wissem Riahi 4 years ago
parent a5d79db0a7
commit bdecb4711d

@ -20,7 +20,7 @@ class TransportException extends \RuntimeException
protected $headers; protected $headers;
protected $response; protected $response;
protected $statusCode; protected $statusCode;
protected $totalResponseTime; protected $responseInfo;
public function setHeaders($headers) public function setHeaders($headers)
{ {
@ -52,13 +52,19 @@ class TransportException extends \RuntimeException
return $this->statusCode; return $this->statusCode;
} }
public function getTotalResponseTime() /**
* @return array
*/
public function getResponseInfo()
{ {
return $this->totalResponseTime; return $this->responseInfo;
} }
public function setTotalResponseTime($totalResponseTime) /**
* @param array $responseInfo
*/
public function setResponseInfo($responseInfo)
{ {
$this->totalResponseTime = $totalResponseTime; $this->responseInfo = $responseInfo;
} }
} }

@ -303,9 +303,8 @@ class CurlDownloader
if (!$error && function_exists('curl_strerror')) { if (!$error && function_exists('curl_strerror')) {
$error = curl_strerror($errno); $error = curl_strerror($errno);
} }
$exception = new TransportException('curl error '.$errno.' while downloading '.Url::sanitize($progress['url']).': '.$error); $exception = new TransportException('curl error '.$errno.' while downloading '.Url::sanitize($progress['url']).': '.$error);
$exception->setTotalResponseTime(isset($progress['total_time_us']) ? $progress['total_time_us'] : null); $exception->setResponseInfo($progress);
throw $exception; throw $exception;
} }
$statusCode = $progress['http_code']; $statusCode = $progress['http_code'];
@ -324,14 +323,12 @@ class CurlDownloader
rewind($job['bodyHandle']); rewind($job['bodyHandle']);
$contents = stream_get_contents($job['bodyHandle']); $contents = stream_get_contents($job['bodyHandle']);
} }
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents); $response = new CurlResponse(array('url' => $progress['url']), $statusCode, $headers, $contents, $progress);
$response->setTotalResponseTime(isset($progress['total_time_us']) ? $progress['total_time_us'] : null);
$this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.Url::sanitize($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']);
$response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents); $response = new CurlResponse(array('url' => $progress['url']), $statusCode, $headers, $contents, $progress);
$response->setTotalResponseTime(isset($progress['total_time_us']) ? $progress['total_time_us'] : null);
$this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG); $this->io->writeError('['.$statusCode.'] '.Url::sanitize($progress['url']), true, IOInterface::DEBUG);
} }
fclose($job['bodyHandle']); fclose($job['bodyHandle']);

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util\Http;
class CurlResponse extends Response
{
private $curlInfo;
public function __construct(array $request, $code, array $headers, $body, array $curlInfo)
{
parent::__construct($request, $code, $headers, $body);
$this->curlInfo = $curlInfo;
}
/**
* @return array
*/
public function getCurlInfo()
{
return $this->curlInfo;
}
}

@ -20,7 +20,6 @@ class Response
private $code; private $code;
private $headers; private $headers;
private $body; private $body;
private $totalResponseTime;
public function __construct(array $request, $code, array $headers, $body) public function __construct(array $request, $code, array $headers, $body)
{ {
@ -70,20 +69,6 @@ class Response
return $this->body; return $this->body;
} }
/**
* Total duration time it took for the response in micro seconds
* @return int|null
*/
public function getTotalResponseTime()
{
return $this->totalResponseTime;
}
public function setTotalResponseTime($totalResponseTime)
{
$this->totalResponseTime = $totalResponseTime;
}
public function decodeJson() public function decodeJson()
{ {
return JsonFile::parseJson($this->body, $this->request['url']); return JsonFile::parseJson($this->body, $this->request['url']);

Loading…
Cancel
Save