<commit_before><?php

namespace perf\Http\Client;

use perf\Http\Curl\CurlClient;
use perf\Http\Curl\CurlExecutionException;
use perf\Http\Curl\CurlExecutionResult;

/**
 *
 *
 */
class HttpClient
{

    /**
     *
     *
     * @var HttpRequestFactory
     */
    private $requestFactory;

    /**
     *
     *
     * @var HttpResponseFactory
     */
    private $responseFactory;

    /**
     *
     *
     * @var CurlClient
     */
    private $curlClient;

    /**
     *
     *
     * @return HttpClient
     */
    public static function createDefault()
    {
        return self::createBuilder()->build();
    }

    /**
     *
     *
     * @return HttpClientBuilder
     */
    public static function createBuilder()
    {
        return new HttpClientBuilder();
    }

    /**
     * Constructor.
     *
     * @param HttpRequestFactory $requestFactory
     * @param HttpResponseFactory $responseFactory
     * @param CurlClient $curlClient
     * @return void
     */
    public function __construct(
        HttpRequestFactory $requestFactory,
        HttpResponseFactory $responseFactory,
        CurlClient $curlClient
    ) {
        $this->requestFactory  = $requestFactory;
        $this->responseFactory = $responseFactory;
        $this->curlClient      = $curlClient;
    }

    /**
     *
     *
     * @return HttpRequest
     */
    public function createRequest()
    {
        return $this->requestFactory->create();
    }

    /**
     *
     *
     * @param string $filename
     * @param string $mimeType
     * @param string $postFilename
     * @return \CURLFile
     */
    public function createFile($filename, $mimeType = '', $postFilename = '')
    {
        return $this->curlClient->createFile($filename, $mimeType, $postFilename);
    }

    /**
     *
     *
     * @param HttpRequest $request
     * @return HttpResponse
     * @throws \RuntimeException
     */
    public function execute(HttpRequest $request)
    {
        $result         = $this->getResult($request);
        $httpStatusCode = $result->getInfo('http_code');
        $options        = $request->getOptions();
        $headers        = array();
        $bodyContent    = '';

        if (!$this->isDownload($request)) {
            $withHeader = (array_key_exists(\CURLOPT_HEADER, $options) && $options[\CURLOPT_HEADER]);
            $withBody   = !(array_key_exists(\CURLOPT_NOBODY, $options) && $options[\CURLOPT_NOBODY]);

            if ($withHeader && $withBody) {
                $responseContent = $result->getResponseContent();
                $position = strpos($responseContent, "\r\n\r\n", 0);
                
                if (false === $position) {
                    throw new \RuntimeException('Failed to find end of response headers.');
                }
                
                $headerContent = substr($responseContent, 0, $position);
                $headers       = explode("\r\n", $headerContent);
                $bodyContent   = substr($responseContent, $position + 4);
            } elseif ($withHeader) {
                $headerContent = $result->getResponseContent();
                $headers       = explode("\r\n", $headerContent);
            } elseif ($withBody) {
                $bodyContent = $result->getResponseContent();
            }
        }

        return $this->responseFactory->create($httpStatusCode, $headers, $bodyContent, $result->getInfos());
    }

    /**
     *
     *
     * @param HttpRequest $request
     * @return CurlExecutionResult
     * @throws \RuntimeException
     */
    private function getResult(HttpRequest $request)
    {
        $options = $request->getOptions();

        if ($this->isDownload($request)) {
            $options[\CURLOPT_HEADER] = false;
            $options[\CURLOPT_NOBODY] = false;
        } else {
            $options[\CURLOPT_RETURNTRANSFER] = true;
        }<commit_msg>Fixed issue with HTTP 100 Continue statuses.<commit_after><?php

namespace perf\Http\Client;

use perf\Http\Curl\CurlClient;
use perf\Http\Curl\CurlExecutionException;
use perf\Http\Curl\CurlExecutionResult;

/**
 *
 *
 */
class HttpClient
{

    /**
     *
     *
     * @var HttpRequestFactory
     */
    private $requestFactory;

    /**
     *
     *
     * @var HttpResponseFactory
     */
    private $responseFactory;

    /**
     *
     *
     * @var CurlClient
     */
    private $curlClient;

    /**
     *
     *
     * @return HttpClient
     */
    public static function createDefault()
    {
        return self::createBuilder()->build();
    }

    /**
     *
     *
     * @return HttpClientBuilder
     */
    public static function createBuilder()
    {
        return new HttpClientBuilder();
    }

    /**
     * Constructor.
     *
     * @param HttpRequestFactory $requestFactory
     * @param HttpResponseFactory $responseFactory
     * @param CurlClient $curlClient
     * @return void
     */
    public function __construct(
        HttpRequestFactory $requestFactory,
        HttpResponseFactory $responseFactory,
        CurlClient $curlClient
    ) {
        $this->requestFactory  = $requestFactory;
        $this->responseFactory = $responseFactory;
        $this->curlClient      = $curlClient;
    }

    /**
     *
     *
     * @return HttpRequest
     */
    public function createRequest()
    {
        return $this->requestFactory->create();
    }

    /**
     *
     *
     * @param string $filename
     * @param string $mimeType
     * @param string $postFilename
     * @return \CURLFile
     */
    public function createFile($filename, $mimeType = '', $postFilename = '')
    {
        return $this->curlClient->createFile($filename, $mimeType, $postFilename);
    }

    /**
     *
     *
     * @param HttpRequest $request
     * @return HttpResponse
     * @throws \RuntimeException
     */
    public function execute(HttpRequest $request)
    {
        $result         = $this->getResult($request);
        $httpStatusCode = $result->getInfo('http_code');
        $options        = $request->getOptions();
        $headers        = array();
        $bodyContent    = '';

        if (!$this->isDownload($request)) {
            $withHeader = (array_key_exists(\CURLOPT_HEADER, $options) && $options[\CURLOPT_HEADER]);
            $withBody   = !(array_key_exists(\CURLOPT_NOBODY, $options) && $options[\CURLOPT_NOBODY]);

            if ($withHeader && $withBody) {
                $responseContent = $result->getResponseContent();
                $position        = $result->getInfo('header_size');
                $headerContent   = substr($responseContent, 0, $position);
                $headers         = preg_split('|\\r\\n|', $headerContent, -1, \PREG_SPLIT_NO_EMPTY);
                $bodyContent     = substr($responseContent, $position);
            } elseif ($withHeader) {
                $headerContent = $result->getResponseContent();
                $headers       = explode("\r\n", $headerContent);
            } elseif ($withBody) {
                $bodyContent = $result->getResponseContent();
            }
        }

        return $this->responseFactory->create($httpStatusCode, $headers, $bodyContent, $result->getInfos());
    }

    /**
     *
     *
     * @param HttpRequest $request
     * @return CurlExecutionResult
     * @throws \RuntimeException
     */
    private function getResult(HttpRequest $request)
    {
        $options = $request->getOptions();

        if ($this->isDownload($request)) {
            $options[\CURLOPT_HEADER] = false;
            $options[\CURLOPT_NOBODY] = false;
        } else {
            $options[\CURLOPT_RETURNTRANSFER] = true;
        }
{'fraction_non_alphanumeric': 0.129576359832636, 'fraction_numerical': 0.0010460251046025104, 'mean_word_length': 1.9475915221579962, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5090602', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2114, 'n_words': 381}