<commit_before><?php
namespace ManaPHP\Http;

use ManaPHP\Component;
use ManaPHP\Exception\NotSupportedException;
use ManaPHP\Http\Client\BadRequestException;
use ManaPHP\Http\Client\ContentTypeException;
use ManaPHP\Http\Client\ForbiddenException;
use ManaPHP\Http\Client\ServiceUnavailableException;
use ManaPHP\Http\Client\TooManyRequestsException;
use ManaPHP\Http\Client\UnauthorizedException;

/**
 * Class ManaPHP\Http\Client
 *
 * @package Curl
 */
abstract class Client extends Component implements ClientInterface
{
    const USER_AGENT_IE = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko';

    /**
     * @var string
     */
    protected $_proxy = '';

    /**
     * @var string
     */
    protected $_cafile;

    /**
     * @var int
     */
    protected $_timeout = 10;

    /**
     * @var bool
     */
    protected $_verify_peer = true;

    /**
     * Client constructor.
     *
     * @param array $options
     */
    public function __construct($options = [])
    {
        if (isset($options['proxy'])) {
            $this->_proxy = $options['proxy'];
        }

        if (isset($options['cafile'])) {
            $this->_cafile = $options['cafile'];
        }

        if (isset($options['timeout'])) {
            $this->_timeout = (int)$options['timeout'];
        }

        if (isset($options['verify_peer'])) {
            $this->_verify_peer = (bool)$options['verify_peer'];
        }
    }

    /**
     * @param \ManaPHP\Http\Client\Request $request
     *
     * @return \ManaPHP\Http\Client\Response
     */
    abstract public function do_request($request);

    /**
     * @param string          $method
     * @param string|array    $url
     * @param string|array    $body
     * @param array|string    $headers
     * @param array|int|float $options
     *
     * @return \ManaPHP\Http\Client\Response
     */
    public function request($method, $url, $body = null, $headers = [], $options = [])
    {
        if (is_array($url)) {
            if (count($url) > 1) {
                $uri = $url[0];
                unset($url[0]);
                $url = $uri . (strpos($uri, '?') !== false ? '&' : '?') . http_build_query($url);
            } else {
                $url = $url[0];
            }
        }

        $url = $this->alias->resolve($url);

        if (preg_match('#^http(s)?://#i', $url) !== 1) {
            throw new NotSupportedException(['only HTTP requests can be handled: `:url`', 'url' => $url]);
        }

        if (is_string($headers)) {
            $headers = [strpos($headers, '://') ? 'Referer' : 'User-Agent' => $headers];
        }

        if (isset($headers['-'])) {
            $headers['Referer'] = $headers['-'];
            unset($headers['-']);
        }

        if (isset($headers['Referer'])) {
            if (is_array($headers['Referer'])) {
                $referer = $headers['Referer'];
                if (isset($referer[0])) {
                    $str = $referer[0];
                    unset($referer[0]);
                } else {
                    $str = 'http://TRACK/';
                }
                $headers['Referer'] = $str . (strpos($str, '?') ? '&' : '?') . http_build_query($referer);
            } elseif (!strpos($headers['Referer'], '://')) {
                $headers['Referer'] = 'http://TRACE/' . $headers['Referer'];
            }
        }

        if (!isset($headers['User-Agent'])) {
            $headers['User-Agent'] = self::USER_AGENT_IE;
        }

        if (is_int($options) || is_float($options)) {
            $options = ['timeout' => $options];
        }

        if (!isset($options['timeout'])) {
            $options['timeout'] = $this->_timeout;
        }

        if ($this->_proxy && !isset($options['proxy'])) {
            $options['proxy'] = $this->_proxy;
        }

        if ($this->_cafile && !isset($options['cafile'])) {
            $options['cafile'] = $this->_cafile;
        }

        $options['verify_peer'] = $this->_verify_peer;

        $request = $this->_di->get('ManaPHP\Http\Client\Request');
        $request->method = $method;
        $request->url = $url;
        $request->headers = $headers;
        $request->body = $body;
        $request->options = $options;

        $this->eventsManager->fireEvent('httpClient:requesting', $this, $request);
        $response = $this->do_request($request);
        $response_text = $response->body;
        if (strpos($response->content_type, '/json') !== false && $json = json_parse($response->body)) {
            $response->body = $json;
        }
        $this->eventsManager->fireEvent('httpClient:requested', $this, $response);

        if ($response->http_code === 429) {
            throw new TooManyRequestsException($response->url, $response);
        } elseif ($response->http_code === 403) {
            throw new ForbiddenException($response->url, $response);
        } elseif ($response->http_code === 401) {
            throw new UnauthorizedException($response->url, $response);
        }

        if ($response->http_code >= 500) {<commit_msg>Modified   ManaPHP/Http/Client.php<commit_after><?php
namespace ManaPHP\Http;

use ManaPHP\Component;
use ManaPHP\Exception\NotSupportedException;
use ManaPHP\Http\Client\BadRequestException;
use ManaPHP\Http\Client\ContentTypeException;
use ManaPHP\Http\Client\ForbiddenException;
use ManaPHP\Http\Client\ServiceUnavailableException;
use ManaPHP\Http\Client\TooManyRequestsException;
use ManaPHP\Http\Client\UnauthorizedException;

/**
 * Class ManaPHP\Http\Client
 *
 * @package Curl
 */
abstract class Client extends Component implements ClientInterface
{
    const USER_AGENT_IE = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko';

    /**
     * @var string
     */
    protected $_proxy = '';

    /**
     * @var string
     */
    protected $_cafile;

    /**
     * @var int
     */
    protected $_timeout = 10;

    /**
     * @var bool
     */
    protected $_verify_peer = true;

    /**
     * Client constructor.
     *
     * @param array $options
     */
    public function __construct($options = [])
    {
        if (isset($options['proxy'])) {
            $this->_proxy = $options['proxy'];
        }

        if (isset($options['cafile'])) {
            $this->_cafile = $options['cafile'];
        }

        if (isset($options['timeout'])) {
            $this->_timeout = (int)$options['timeout'];
        }

        if (isset($options['verify_peer'])) {
            $this->_verify_peer = (bool)$options['verify_peer'];
        }
    }

    /**
     * @param \ManaPHP\Http\Client\Request $request
     *
     * @return \ManaPHP\Http\Client\Response
     */
    abstract public function do_request($request);

    /**
     * @param string          $method
     * @param string|array    $url
     * @param string|array    $body
     * @param array|string    $headers
     * @param array|int|float $options
     *
     * @return \ManaPHP\Http\Client\Response
     */
    public function request($method, $url, $body = null, $headers = [], $options = [])
    {
        if (is_array($url)) {
            if (count($url) > 1) {
                $uri = $url[0];
                unset($url[0]);
                $url = $uri . (strpos($uri, '?') !== false ? '&' : '?') . http_build_query($url);
            } else {
                $url = $url[0];
            }
        }

        $url = $this->alias->resolve($url);

        if (preg_match('#^http(s)?://#i', $url) !== 1) {
            throw new NotSupportedException(['only HTTP requests can be handled: `:url`', 'url' => $url]);
        }

        if (is_string($headers)) {
            $headers = [strpos($headers, '://') ? 'Referer' : 'User-Agent' => $headers];
        }

        if (isset($headers['-'])) {
            $headers['Referer'] = $headers['-'];
            unset($headers['-']);
        }

        if (isset($headers['Referer'])) {
            if (is_array($headers['Referer'])) {
                $referer = $headers['Referer'];
                if (isset($referer[0])) {
                    $str = $referer[0];
                    unset($referer[0]);
                } else {
                    $str = 'http://TRACK/';
                }
                $headers['Referer'] = $str . (strpos($str, '?') ? '&' : '?') . http_build_query($referer);
            } elseif (!strpos($headers['Referer'], '://')) {
                $headers['Referer'] = 'http://TRACE/' . $headers['Referer'];
            }
        }

        if (!isset($headers['User-Agent'])) {
            $headers['User-Agent'] = self::USER_AGENT_IE;
        }

        if (is_int($options) || is_float($options)) {
            $options = ['timeout' => $options];
        }

        if (!isset($options['timeout'])) {
            $options['timeout'] = $this->_timeout;
        }

        if ($this->_proxy && !isset($options['proxy'])) {
            $options['proxy'] = $this->_proxy;
        }

        if ($this->_cafile && !isset($options['cafile'])) {
            $options['cafile'] = $this->_cafile;
        }

        $options['verify_peer'] = $this->_verify_peer;

        $request = $this->_di->get('ManaPHP\Http\Client\Request');
        $request->method = $method;
        $request->url = $url;
        $request->headers = $headers;
        $request->body = $body;
        $request->options = $options;

        $this->eventsManager->fireEvent('httpClient:requesting', $this, $request);
        $response = $this->do_request($request);
        $response_text = $response->body;

        if ((isset($request->headers['Accept']) && strpos($request->headers['Accept'], '/json') !== false)
            || strpos($response->content_type, '/json') !== false) {
            $response->body = json_parse($response->body);
        }

        $this->eventsManager->fireEvent('httpClient:requested', $this, $response);

        if ($response->http_code === 429) {
            throw new TooManyRequestsException($response->url, $response);
        } elseif ($response->http_code === 403) {
            throw new ForbiddenException($response->url, $response);
        } elseif ($response->http_code === 401) {
            throw new UnauthorizedException($response->url, $response);
        }

        if ($response->http_code >= 500) {
{'fraction_non_alphanumeric': 0.17954456987154535, 'fraction_numerical': 0.006422732580770728, 'mean_word_length': 2.0631892697466467, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, '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': '6475261', 'n_tokens_mistral': 3150, 'n_tokens_neox': 3077, 'n_words': 533}