<commit_before><?php

/*
 * This file is part of the Orient package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * This class represents an HTTP client based on Curl.
 *
 * @package    Orient
 * @subpackage Http
 * @author     <NAME> <<EMAIL>>
 */

namespace Orient\Http\Client;

use Orient\Http\Response;
use Orient\Exception\Http\Response\Void as VoidResponse;
use Orient\Contract\Http\Client as HttpClient;

class Curl implements HttpClient
{
    protected $client;
    protected $credential;
    protected $authentication;

    /**
     * Creates a new Curl instance.
     *
     * @param String $location
     */
    public function __construct($location = null)
    {
        $this->client = curl_init($location);
    }

    /**
     * Executes a Curl.
     *
     * @param   String $method
     * @param   String $location
     * @return  Response
     * @throws  Inconsistent
     */
    public function execute($method, $location)
    {
        curl_setopt($this->client, CURLOPT_URL, $location);

        if ($this->authentication) {
            curl_setopt($this->client, CURLOPT_USERPWD, $this->authentication);
        }

        curl_setopt($this->client, CURLOPT_HEADER, 1);
        curl_setopt($this->client, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($this->client);
        $this->restart();

        if (!$response) {
            throw new VoidResponse(__CLASS__, $location);
        }

        return new Response($response);
    }

    /**
     * Executes a DELETE on a resource.
     *
     * @param  String $location
     * @return Response
     */
    public function delete($location)
    {
        curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, 'DELETE');

        return $this->execute('DELETE', $location);
    }

    /**
     * GETs a resource.
     *
     * @param   String $location
     * @return  Response
     */
    public function get($location)
    {
        return $this->execute('GET', $location);
    }

    /**
     * Executes a POST on a location.
     *
     * @param   String $location
     * @param   String $body
     * @return  Response
     */
    public function post($location, $body)
    {
        curl_setopt($this->client, CURLOPT_POST, 1);
        curl_setopt($this->client, CURLOPT_POSTFIELDS, $body);

        return $this->execute('POST', $location);
    }

    /**
     * PUTs a resource.
     *
     * @param   String $location
     * @param   String $body
     * @return  Response
     */
    public function put($location, $body)
    {
        curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($this->client, CURLOPT_POSTFIELDS, $body);

        return $this->execute('POST', $location);
    }

    /**
     * Sets the authentication string for the next HTTP requests.
     *
     * @param String $credential
     * @return String
     */
    public function setAuthentication($credential)
    {
        $this->authentication = $credential;

        return $this->authentication;
    }

    /**
     * Restarts the current cURL client
     */
    protected function restart()
    {
        curl_close($this->client);
        $this->client = curl_init();
    }
}<commit_msg>Reuse the underlying cURL handle by default.<commit_after><?php

/*
 * This file is part of the Orient package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * This class represents an HTTP client based on Curl.
 *
 * @package    Orient
 * @subpackage Http
 * @author     <NAME> <<EMAIL>>
 */

namespace Orient\Http\Client;

use Orient\Http\Response;
use Orient\Exception\Http\Response\Void as VoidResponse;
use Orient\Contract\Http\Client as HttpClient;

class Curl implements HttpClient
{
    protected $client;
    protected $reuseHandle;
    protected $authentication;

    /**
     * Creates a new Curl instance.
     */
    public function __construct()
    {
        $this->reuseHandle = true;
        $this->client = $this->createCurlHandle();
    }

    /**
     * Closes the underlying cURL handle.
     */
    public function __destruct()
    {
        curl_close($this->client);
    }

    /**
     * Create and initialize the underlying cURL handle.
     *
     * @return resource
     */
    protected function createCurlHandle()
    {
        $client = curl_init();

        $options = array(
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
        );
        if ($this->authentication) {
            $options[CURLOPT_USERPWD] = $this->authentication;
        }

        curl_setopt_array($client, $options);

        return $client;
    }

    /**
     * Executes a Curl.
     *
     * @param   String $method
     * @param   String $location
     * @return  Response
     * @throws  Inconsistent
     */
    public function execute($method, $location)
    {
        curl_setopt($this->client, CURLOPT_URL, $location);

        $response = curl_exec($this->client);

        if (!$this->reuseHandle) {
            $this->restart();
        }

        if (!$response) {
            throw new VoidResponse(__CLASS__, $location);
        }

        return new Response($response);
    }

    /**
     * Executes a DELETE on a resource.
     *
     * @param  String $location
     * @return Response
     */
    public function delete($location)
    {
        curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, 'DELETE');

        return $this->execute('DELETE', $location);
    }

    /**
     * GETs a resource.
     *
     * @param   String $location
     * @return  Response
     */
    public function get($location)
    {
        return $this->execute('GET', $location);
    }

    /**
     * Executes a POST on a location.
     *
     * @param   String $location
     * @param   String $body
     * @return  Response
     */
    public function post($location, $body)
    {
        curl_setopt($this->client, CURLOPT_POST, 1);
        curl_setopt($this->client, CURLOPT_POSTFIELDS, $body);

        return $this->execute('POST', $location);
    }

    /**
     * PUTs a resource.
     *
     * @param   String $location
     * @param   String $body
     * @return  Response
     */
    public function put($location, $body)
    {
        curl_setopt($this->client, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($this->client, CURLOPT_POSTFIELDS, $body);

        return $this->execute('POST', $location);
    }

    /**
     * Sets the authentication string for the next HTTP requests.
     *
     * @param String $credential
     * @return String
     */
    public function setAuthentication($credential)
    {
        $this->authentication = $credential;
        curl_setopt($this->client, CURLOPT_USERPWD, $this->authentication);

        return $this->authentication;
    }

    /**
     * Restarts the current cURL client
     */
    protected function restart()
    {
        curl_close($this->client);
        $this->client = $this->createCurlHandle();
    }

    /**
     * Sets whether to reuse the underlying cURL handle or use
     * a new one for each HTTP request.
     *
     * @param bool $value
     */
    public function reuseHandle($value)
    {
        $this->reuseHandle = $value;
    }
}
{'fraction_non_alphanumeric': 0.12214256729338563, 'fraction_numerical': 0.00040578926011091576, 'mean_word_length': 2.177481736140954, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 66, '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': '6232524', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2145, 'n_words': 546}