Commit aac302ab authored by brice's avatar brice
Browse files

Fix cURL option management

parent 45166d01
......@@ -2,7 +2,6 @@
namespace Libcast\Client\Http;
use Libcast\Client\Exception\ConnectionException;
use Libcast\Client\Exception\ServerException;
use Psr\Log\LoggerInterface;
......@@ -11,7 +10,6 @@ use Psr\Log\LoggerAwareInterface;
class CurlClient implements LoggerAwareInterface
{
// <editor-fold desc="HTTP phrases" defaultstate="collapsed">
/**
* Associative array of HTTP status code / reason phrase.
*
......@@ -19,7 +17,6 @@ class CurlClient implements LoggerAwareInterface
* @link http://tools.ietf.org/html/rfc2616#section-10
*/
protected $phrases = array(
// 1xx: Informational - Request received, continuing process
100 => 'Continue',
101 => 'Switching Protocols',
......@@ -75,9 +72,9 @@ class CurlClient implements LoggerAwareInterface
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded',
);
// </editor-fold>
/**
*
* @var resource
*/
protected $handle;
......@@ -106,29 +103,34 @@ class CurlClient implements LoggerAwareInterface
/**
*
* @var int Request timeout in seconds
* @var array
*/
private $timeout;
private $curlOptions = array();
/**
*
* @var int Request timeout in seconds
*/
private $timeout;
public function __construct()
{
$this->handle = curl_init();
curl_setopt_array(
$this->handle,
array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_MAXREDIRS => 5,
)
);
$this->addCurlOption(CURLOPT_RETURNTRANSFER, true);
$this->addCurlOption(CURLOPT_HEADER, true);
$this->addCurlOption(CURLOPT_MAXREDIRS, 5);
$this->setTimeout(180);
}
public function __destruct()
{
if (is_resource($this->handle)) {
curl_close($this->handle);
}
}
/**
*
* @param LoggerInterface $logger
......@@ -138,15 +140,36 @@ class CurlClient implements LoggerAwareInterface
stream_wrapper_register('psrlog', __NAMESPACE__.'\LoggerWrapper');
$this->logger = $logger;
curl_setopt_array($this->handle, array(
// CURLOPT_STDERR => fopen('psrlog://foo', 'w+', false, stream_context_create(array(
// 'psr' => array(
// 'logger' => $this->logger,
// 'level' => LogLevel::DEBUG,
// )
// ))),
CURLOPT_VERBOSE => true,
));
$this->addCurlOption(CURLOPT_VERBOSE, true);
// $this->addCurlOption(CURLOPT_STDERR, fopen('psrlog://foo', 'w+', false, stream_context_create([
// 'psr' => [
// 'logger' => $this->logger,
// 'level' => LogLevel::DEBUG,
// ]
// ])));
}
/**
*
* @param $option
* @param $value
*/
public function addCurlOption($option, $value)
{
$this->curlOptions[$option] = $value;
}
/**
* @param array $extraOptions
* @return bool
*/
public function setCurlOptions(array $extraOptions = array())
{
curl_reset($this->handle);
return curl_setopt_array($this->handle, $extraOptions + $this->curlOptions);
}
/**
......@@ -155,8 +178,8 @@ class CurlClient implements LoggerAwareInterface
*/
public function setTimeout($seconds)
{
curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, 1000 * $seconds);
curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, 1000 * $seconds);
$this->addCurlOption(CURLOPT_TIMEOUT_MS, 1000 * $seconds);
$this->addCurlOption(CURLOPT_CONNECTTIMEOUT_MS, 1000 * $seconds);
$this->timeout = $seconds;
}
......@@ -177,16 +200,8 @@ class CurlClient implements LoggerAwareInterface
*/
public function authenticate($username, $password, $scheme = 'basic')
{
curl_setopt($this->handle, CURLOPT_HTTPAUTH, $this->schemes[$scheme]);
curl_setopt($this->handle, CURLOPT_USERPWD, "$username:$password");
}
public function __destruct()
{
if (is_resource($this->handle)) {
curl_close($this->handle);
}
$this->addCurlOption(CURLOPT_HTTPAUTH, $this->schemes[$scheme]);
$this->addCurlOption(CURLOPT_USERPWD, "$username:$password");
}
/**
......@@ -238,21 +253,24 @@ class CurlClient implements LoggerAwareInterface
*/
public function request($method, $url, $content = null, array $headers = array(), array $options = array())
{
curl_setopt($this->handle, CURLOPT_URL, $url);
curl_setopt($this->handle, CURLOPT_HTTPHEADER, $this->buildHeaders(array_merge($this->headers, $headers)));
$this->addCurlOption(CURLOPT_CUSTOMREQUEST, $method);
$this->addCurlOption(CURLOPT_URL, $url);
$this->addCurlOption(CURLOPT_HTTPHEADER, $this->buildHeaders(array_merge($this->headers, $headers)));
$payload = is_array($content) ? http_build_query($content) : $content;
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
switch ($method) {
case 'GET':
break;
case 'POST':
case 'PUT':
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $payload);
$this->addCurlOption(CURLOPT_POSTFIELDS, $payload);
break;
case 'DELETE':
break;
default:
throw new \Exception(sprintf('The HTTP method "%s" is unsupported.', $method));
}
......@@ -260,7 +278,7 @@ class CurlClient implements LoggerAwareInterface
$this->log("Curl request payload: $payload", LogLevel::DEBUG);
$this->log("Curl request: $method $url");
curl_setopt_array($this->handle, $options);
$this->setCurlOptions($options);
$response = curl_exec($this->handle);
$this->log(sprintf("Curl request duration: %ss", curl_getinfo($this->handle, CURLINFO_TOTAL_TIME)));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment