<commit_before><?php
/*
 * Author: <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Sonrisa\Component\RestfulClient;

use Sonrisa\Component\RestfulClient\Interfaces\ClientInterface as ClientInterface;

class CURLClient extends AbstractClient implements ClientInterface
{
    protected $curl;

    /**
     * Sets up CURL.
     */
    public function __construct()
    {
        //Set CURL basic fields.
        $this->curl = curl_init();

        $cookie = tempnam("/tmp", "CURLCOOKIE");
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookie);    //Allows cookies.
        curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($this->curl, CURLOPT_TIMEOUT, 30);            // Fail if takes longer than 30 seconds.
        curl_setopt($this->curl, CURLOPT_FAILONERROR, false);     // Do not fail on 40x codes.
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);   // Follow redirects
        curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->curl, CURLOPT_HEADER, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);   // return transfer instead of outputting .
        curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
    }

    /**
     * @param  string $methodName
     * @param  string $url
     * @param  array $params
     * @param  array $headers
     * @throws Exceptions\RestfulClientException
     * @return mixed
     */
    public function request($methodName, $url, array $params=array(), array $headers=array())
    {
        if (filter_var($url, FILTER_VALIDATE_URL, array('options' => array('flags' => FILTER_FLAG_PATH_REQUIRED)))) {

            //Prepare the request.
            if ($methodName == 'GET') {
                $url = $url . "?" . http_build_query($params);
                curl_setopt($this->curl, CURLOPT_URL, $url);
            } elseif ($methodName == 'POST') {
                curl_setopt($this->curl, CURLOPT_URL, $url);
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
                curl_setopt($this->curl, CURLOPT_POST, true);
            } elseif ($methodName == 'PUT') {
                curl_setopt($this->curl, CURLOPT_URL, $url);
            } else {
                curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $methodName);
                curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: {$methodName}"));
            }

            //Prepare headers and related config.
            if (!empty($headers['User-Agent'])) {
                curl_setopt($this->curl, CURLOPT_USERAGENT, $headers['User-Agent']);
                unset($headers['User-Agent']);
            }

            if (!empty($headers['Accept-Encoding'])) {
                curl_setopt($this->curl, CURLOPT_ENCODING, $headers['Accept-Encoding']);
                unset($headers['Accept-Encoding']);
            }
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);    //Sets the remaining headers.

            //Send request and retrieve the response.
            $data = curl_exec($this->curl);
            $data = explode("\r\n", $data);

            //Reads the response HTTP header and returns its contents in an array.
            $response = array(
                'Protocol'  => ((trim(substr($data[0], 0, 8))))? trim(substr($data[0], 0, 8)) : null,
                'Status'    => curl_getinfo($this->curl, CURLINFO_HTTP_CODE),
            );
            unset($data[0]);
            $info = array_pop($data);

            foreach ($data as $value) {
                $k = substr($value, 0, strpos($value, ':'));
                $v = trim(substr(strstr($value, ':'), 1));

                if (!empty($k) && !empty($v)) {
                    $k = str_replace(' ', '-', ucwords(str_replace('-', ' ', $k)));
                    $response[$k] = $v;
                }
            }
            $response['Request-Time'] = curl_getinfo($this->curl, CURLINFO_TOTAL_TIME).' seconds';
            array_filter($response);

            //Return data.
            return array(<commit_msg>Fix: Custom headers aren't set correctly for CURLClient.<commit_after><?php
/*
 * Author: <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Sonrisa\Component\RestfulClient;

use Sonrisa\Component\RestfulClient\Interfaces\ClientInterface as ClientInterface;

class CURLClient extends AbstractClient implements ClientInterface
{
    protected $curl;

    /**
     * Sets up CURL.
     */
    public function __construct()
    {
        //Set CURL basic fields.
        $this->curl = curl_init();

        $cookie = tempnam("/tmp", "CURLCOOKIE");
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookie);    //Allows cookies.
        curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($this->curl, CURLOPT_TIMEOUT, 30);            // Fail if takes longer than 30 seconds.
        curl_setopt($this->curl, CURLOPT_FAILONERROR, false);     // Do not fail on 40x codes.
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);   // Follow redirects
        curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->curl, CURLOPT_HEADER, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);   // return transfer instead of outputting .
        curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
    }

    /**
     * @param  string $methodName
     * @param  string $url
     * @param  array $params
     * @param  array $headers
     * @throws Exceptions\RestfulClientException
     * @return mixed
     */
    public function request($methodName, $url, array $params=array(), array $headers=array())
    {
        if (filter_var($url, FILTER_VALIDATE_URL, array('options' => array('flags' => FILTER_FLAG_PATH_REQUIRED)))) {

            //Prepare the request.
            if ($methodName == 'GET') {
                $url = $url . "?" . http_build_query($params);
                curl_setopt($this->curl, CURLOPT_URL, $url);
            } elseif ($methodName == 'POST') {
                curl_setopt($this->curl, CURLOPT_URL, $url);
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
                curl_setopt($this->curl, CURLOPT_POST, true);
            } elseif ($methodName == 'PUT') {
                curl_setopt($this->curl, CURLOPT_URL, $url);
            } else {
                curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $methodName);
                curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: {$methodName}"));
            }

            //Prepare headers and related config.
            if (!empty($headers['User-Agent'])) {
                curl_setopt($this->curl, CURLOPT_USERAGENT, $headers['User-Agent']);
                unset($headers['User-Agent']);
            }

            if (!empty($headers['Accept-Encoding'])) {
                curl_setopt($this->curl, CURLOPT_ENCODING, $headers['Accept-Encoding']);
                unset($headers['Accept-Encoding']);
            }

            // Prepare and set remaining header directly:
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, array_map(function($key, $value) {
                return $key . ': ' . $value;
            }, array_keys($headers), $headers));

            //Send request and retrieve the response.
            $data = curl_exec($this->curl);
            $data = explode("\r\n", $data);

            //Reads the response HTTP header and returns its contents in an array.
            $response = array(
                'Protocol'  => ((trim(substr($data[0], 0, 8))))? trim(substr($data[0], 0, 8)) : null,
                'Status'    => curl_getinfo($this->curl, CURLINFO_HTTP_CODE),
            );
            unset($data[0]);
            $info = array_pop($data);

            foreach ($data as $value) {
                $k = substr($value, 0, strpos($value, ':'));
                $v = trim(substr(strstr($value, ':'), 1));

                if (!empty($k) && !empty($v)) {
                    $k = str_replace(' ', '-', ucwords(str_replace('-', ' ', $k)));
                    $response[$k] = $v;
                }
            }
            $response['Request-Time'] = curl_getinfo($this->curl, CURLINFO_TOTAL_TIME).' seconds';
            array_filter($response);

            //Return data.
            return array(
{'fraction_non_alphanumeric': 0.14295540525048722, 'fraction_numerical': 0.0038977416026596354, 'mean_word_length': 2.25158404770779, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4075310', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2470, 'n_words': 542}