TrackManiaControl/libs/curl-easy/cURL/Request.php
Sebastian Büttner b0732bee96 Implemented strict http request serialization for APIs that forbid a session to be used in concurrent requests (e.g. dedimania).
Updated all API requests in DedimaniaPlugin to be serialized by this mechanism. Closes #139.
2017-05-22 22:32:15 +02:00

184 lines
4.1 KiB
PHP

<?php
namespace cURL;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Request extends EventDispatcher implements RequestInterface
{
/**
* @var resource cURL handler
*/
protected $ch;
/**
* @var RequestsQueue Queue instance when requesting async
*/
protected $queue;
/**
* @var Options Object containing options for current request
*/
protected $options = null;
/**
* @var bool Whether requests to the target host should be serialized or not.
*/
protected $serializeRequests = false;
/**
* Create new cURL handle
*
* @param string $url The URL to fetch.
*/
public function __construct($url = null)
{
if ($url !== null) {
$this->getOptions()->set(CURLOPT_URL, $url);
}
$this->ch = curl_init();
}
/**
* Get the cURL\Options instance
* Creates empty one if does not exist
*
* @return Options
*/
public function getOptions()
{
if (!isset($this->options)) {
$this->options = new Options();
}
return $this->options;
}
/**
* Sets Options
*
* @param Options $options Options
* @return void
*/
public function setOptions(Options $options)
{
$this->options = $options;
}
/**
* Closes cURL resource and frees the memory.
* It is neccessary when you make a lot of requests
* and you want to avoid fill up the memory.
*/
public function __destruct() {
if (isset($this->ch)) {
curl_close($this->ch);
}
}
/**
* Returns cURL raw resource
*
* @return resource cURL handle
*/
public function getHandle()
{
return $this->ch;
}
/**
* Get unique id of cURL handle
* Useful for debugging or logging.
*
* @return int
*/
public function getUID()
{
return (int)$this->ch;
}
/**
* Perform a cURL session.
* Equivalent to curl_exec().
* This function should be called after initializing a cURL
* session and all the options for the session are set.
*
* Warning: it doesn't fire 'complete' event.
*
* @return Response
*/
public function send()
{
if ($this->options instanceof Options) {
$this->options->applyTo($this);
}
$content = curl_exec($this->ch);
$response = new Response($this, $content);
$errorCode = curl_errno($this->ch);
if ($errorCode !== CURLE_OK) {
$response->setError(new Error(curl_error($this->ch), $errorCode));
}
return $response;
}
/**
* Binds the request to a given RequestQueue.
*
* @param \cURL\RequestsQueue $requestsQueue
* @throws \cURL\Exception
*/
public function attachTo(RequestsQueue $requestsQueue)
{
if (isset($this->queue)) {
throw new Exception('Already bound to a RequestQueue.');
}
$this->queue = $requestsQueue;
$this->queue->attach($this);
}
/**
* Whether to serialize requests to the same host or not.
*
* @param bool $serialize
*/
public function setSerialize($serialize = true)
{
$this->serializeRequests = $serialize;
}
/**
* @return bool
*/
public function getSerialize()
{
return $this->serializeRequests;
}
/**
* Creates new RequestsQueue with single Request attached to it
* and calls RequestsQueue::socketPerform() method.
*
* @see RequestsQueue::socketPerform()
*/
public function socketPerform()
{
if (!isset($this->queue)) {
$this->queue = new RequestsQueue();
$this->queue->attach($this);
}
return $this->queue->socketPerform();
}
/**
* Calls socketSelect() on previously created RequestsQueue
*
* @see RequestsQueue::socketSelect()
*/
public function socketSelect($timeout = 1)
{
if (!isset($this->queue)) {
throw new Exception('You need to call socketPerform() before.');
}
return $this->queue->socketSelect($timeout);
}
}