<commit_before><?php

namespace Phpoaipmh\HttpAdapter;

use Phpoaipmh\Exception\HttpException;

/**
 * CurlAdapter HttpAdapter HttpAdapterInterface Adapter
 *
 * @package Phpoaipmh\HttpAdapter
 */
class CurlAdapter implements HttpAdapterInterface
{
    /**
     * @var int  The maximum number of redirects
     */
    protected $maxRedirects = 3;

    /**
     * @var int  Connection timeout
     */
    protected $connectTimeout = 10;

    /**
     * @var int  DNS lookup timeout
     */
    protected $dnsCacheTimeout = 10;

    /**
     * @var int  Response timeout
     */
    protected $responseTimeout = 60;

    // -------------------------------------------------------------------------

    /**
     * Constructor
     *
     * Checks for CURL libraries
     */
    public function __construct()
    {
        if (! is_callable('curl_exec')) {
            throw new \Exception("OAI-PMH CurlAdapter HTTP HttpAdapterInterface requires the CURL PHP Extension");
        }
    }

    // -------------------------------------------------------------------------

    /**
     * Do CURL Request
     *
     * @param  string $url The full URL
     * @return string The response body
     */
    public function request($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, $this->dnsCacheTimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->responseTimeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects);
        curl_setopt($ch, CURLOPT_USERAGENT, 'PHP OAI-PMH Library');
        $resp = curl_exec($ch);
        $info = (object) curl_getinfo($ch);
        curl_close($ch);

        //Check response
        $httpCode = (string) $info->http_code;
        if ($httpCode{0} != '2') {
            $msg = sprintf('HTTP Request Failed (code %s): %s', $info->http_code, $resp);
            throw new HttpException($resp, $msg, $httpCode);
        } elseif (strlen(trim($resp)) == 0) {
            throw new HttpException($resp, 'HTTP Response Empty');
        }

        return $resp;
    }
}
<commit_msg>Added ability to set runtime directives when using cURL cient<commit_after><?php

namespace Phpoaipmh\HttpAdapter;

use Phpoaipmh\Exception\HttpException;

/**
 * CurlAdapter HttpAdapter HttpAdapterInterface Adapter
 *
 * @package Phpoaipmh\HttpAdapter
 */
class CurlAdapter implements HttpAdapterInterface
{
    /**
     * @var array  CURL Options
     */
    private $curlOpts = [
        CURLOPT_RETURNTRANSFER    => true,
        CURLOPT_CONNECTTIMEOUT    => 10,
        CURLOPT_DNS_CACHE_TIMEOUT => 10,
        CURLOPT_TIMEOUT           => 60,
        CURLOPT_FOLLOWLOCATION    => true,
        CURLOPT_MAXREDIRS         => 3,
        CURLOPT_USERAGENT         => 'PHP OAI-PMH Library',
    ];

    // -------------------------------------------------------------------------

    /**
     * Constructor
     *
     * Checks for CURL libraries
     *
     * @param array $curlOpts Array of CURL directives and values (e.g. [CURLOPT_TIMEOUT => 50])
     * @throws \Exception  If CURL not installed.
     */
    public function __construct(array $curlOpts = [])
    {
        if (! is_callable('curl_exec')) {
            throw new \Exception("OAI-PMH CurlAdapter HTTP HttpAdapterInterface requires the CURL PHP Extension");
        }

        $this->curlOpts = array_replace($this->curlOpts, $curlOpts);
    }

    // -------------------------------------------------------------------------

    /**
     * Do CURL Request
     *
     * @param  string $url The full URL
     * @return string The response body
     */
    public function request($url)
    {
        $curlOpts = array_replace($this->curlOpts, [CURLOPT_URL => $url]);

        $ch = curl_init();
        foreach ($curlOpts as $opt => $optVal) {
            curl_setopt($ch, $opt, $optVal);
        }

        $resp = curl_exec($ch);
        $info = (object) curl_getinfo($ch);
        curl_close($ch);

        //Check response
        $httpCode = (string) $info->http_code;
        if ($httpCode{0} != '2') {
            $msg = sprintf('HTTP Request Failed (code %s): %s', $info->http_code, $resp);
            throw new HttpException($resp, $msg, $httpCode);
        } elseif (strlen(trim($resp)) == 0) {
            throw new HttpException($resp, 'HTTP Response Empty');
        }

        return $resp;
    }
}

{'fraction_non_alphanumeric': 0.17838427947598254, 'fraction_numerical': 0.00480349344978166, 'mean_word_length': 1.8739021329987453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '7475038', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1321, 'n_words': 311}