1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 
<?php
namespace Manticoresearch\Transport;

use Manticoresearch\Exceptions\ConnectionException;
use Manticoresearch\Exceptions\ResponseException;
use Manticoresearch\Request;
use Manticoresearch\Response;

/**
 * Class Http
 * @package Manticoresearch\Transport
 */
class Http extends \Manticoresearch\Transport implements TransportInterface
{

    /**
     * @var string
     */
    protected $_scheme = 'http';

    /**
     * @var object
     */
    protected static $_curl;

    /**
     * @param Request $request
     * @param array $params
     * @return Response
     */
    public function execute(Request $request, $params=[])
    {
        $connection = $this->getConnection();
        $conn = $this->_getCurlConnection($connection->getConfig('persistent'));
        $url = $this->_scheme.'://'.$connection->getHost().':'.$connection->getPort();
        $endpoint = $request->getPath();
        $url .= $endpoint;
        $url = $this->setupURI($url, $request->getQuery());

        curl_setopt($conn, CURLOPT_URL, $url);
        curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout());
        curl_setopt($conn, CURLOPT_ENCODING, '');
        curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
        $data = $request->getBody();
        $method = $request->getMethod();
        $headers = $connection->getHeaders();
        $headers[] = sprintf('Content-Type: %s', $request->getContentType());
        if (!empty($data)) {
            if (is_array($data)) {
                $content = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            } else {
                $content = $data;
            }
            curl_setopt($conn, CURLOPT_POSTFIELDS, $content);
        } else {
            curl_setopt($conn, CURLOPT_POSTFIELDS, '');
        }
        curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);

        if ($connection->getConnectTimeout()>0) {
            curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout());
        }

        if ($connection->getConfig('username') !== null && $connection->getConfig('password') !== null) {
            curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($conn, CURLOPT_USERPWD, $connection->getConfig('username').":".$connection->getConfig('password'));
        }
        if ($connection->getConfig('proxy') !== null) {
            curl_setopt($conn, CURLOPT_PROXY, $connection->getConfig('proxy'));
        }
        if (!empty($connection->getConfig('curl'))) {
            foreach ($connection->getConfig('curl') as $k=>$v) {
                curl_setopt($conn, $k, $v);
            }
        }
        $start = microtime(true);
        ob_start();
        curl_exec($conn);
        $responseString = \ob_get_clean();
        $end = microtime(true);
        $errorno = curl_errno($conn);
        $status = curl_getinfo($conn, CURLINFO_HTTP_CODE);
        if (isset($params['responseClass'])) {
            $responseClass = $params['responseClass'];
            $response = new $responseClass($responseString, $status);
        } else {
            $response = new Response($responseString, $status);
        }

        $time = $end-$start;
        $response->setTime($time);
        $response->setTransportInfo([
                'url' => $url,
                'headers' => $headers,
                'body' => $request->getBody()
            ]);
        //hard error
        if ($errorno>0) {
            $error = curl_error($conn);
            self::$_curl = false;
            throw new ConnectionException($error, $request);
        }


        $this->_logger->debug('Request body:', [
                'connection' => $connection->getConfig(),
                'payload'=> $request->getBody()
            ]);
        $this->_logger->info(
            'Request:',
            [
                    'url' => $url,
                    'status' => $status,
                    'time' => $time
                ]
        );
        $this->_logger->debug('Response body:', [json_decode($responseString, true)]);
        //soft error
        if ($response->hasError()) {
            $this->_logger->error('Response error:', [$response->getError()]);
            throw new ResponseException($request, $response);
        }
        return $response;
    }

    /**
     * @param bool $persistent
     * @return false|resource
     */
    protected function _getCurlConnection(bool $persistent=true)
    {
        if (!$persistent || !self::$_curl) {
            self::$_curl = curl_init();
        }
        return self::$_curl;
    }
}