<commit_before><?php

namespace VCR\Util;

use VCR\Response;

class HttpUtil
{
    /**
     * Returns key value pairs of response headers.
     *
     * @param array $headers List of headers. Example: ['Content-Type: text/html', '...']
     * @return array Key/value pairs of headers, e.g. ['Content-Type' => 'text/html']
     */
    public static function parseHeaders(array $headers)
    {
        // Collect matching headers into groups
        foreach ($headers as $i => $line) {
            list($key, $value) = explode(': ', $line, 2);
            if (isset($headers[$key])) {
                if (is_array($headers[$key])) {
                    $headers[$key][] = $value;
                } else {
                    $headers[$key] = array($headers[$key], $value);
                }
            } else {
                $headers[$key] = $value;
            }
            unset($headers[$i]);
        }
        
        return $headers;
    }

    /**
     * Returns http_version, code and message from a HTTP status line.
     *
     * @param string $status HTTP status line, e.g. HTTP/1.1 200 OK
     * @return array Parsed 'http_version', 'code' and 'message'.
     */
    public static function parseStatus($status)
    {
        Assertion::startsWith(
            $status,
            'HTTP/',
            "Invalid HTTP status '$status', expected format like: 'HTTP/1.1 200 OK'."
        );

        $part = explode(' ', $status, 3);

        return array(
            'http_version' => substr(strrchr($part[0], '/'), 1),
            'code' => $part[1],
            'message' => isset($part[2]) ? $part[2] : ''
        );
    }

    /**
     * Returns status, headers and body from a HTTP response string.
     *
     * @param string $response Response including header and body.
     * @return array Status, headers and body as strings.
     */
    public static function parseResponse($response)
    {
        $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response);
        
        list($rawHeader, $rawBody) = explode("\r\n\r\n", $response, 2);

        // Parse headers and status.
        $headers = self::parseRawHeader($rawHeader);
        $status = array_shift($headers);

        return array($status, $headers, $rawBody);
    }

    /**
     * Returns an array of arrays for specified raw header string.
     *
     * @param string $header
     * @return array
     */
    public static function parseRawHeader($rawHeader)
    {
        return explode("\r\n", trim($rawHeader));
    }

    /**
     * Returns a list of headers from a key/value paired array.
     *
     * @param array $headers Headers as key/value pairs.
     * @return array List of headers ['Content-Type: text/html', '...'].
     */
    public static function formatHeadersForCurl(array $headers)
    {
        $curlHeaders = array();

        foreach ($headers as $key => $values) {
            if (is_array($values)) {
                foreach ($values as $value) {
                    $curlHeaders[] = $key . ': ' . $value;
                }
            } else {
                $curlHeaders[] = $key.': '.$values;
            }
        }

        return $curlHeaders;
    }

    /**
     * Returns a HTTP status line from specified response.
     *
     * @param Response $response
     * @return string HTTP status line.<commit_msg>Style fix<commit_after><?php

namespace VCR\Util;

use VCR\Response;

class HttpUtil
{
    /**
     * Returns key value pairs of response headers.
     *
     * @param array $headers List of headers. Example: ['Content-Type: text/html', '...']
     * @return array Key/value pairs of headers, e.g. ['Content-Type' => 'text/html']
     */
    public static function parseHeaders(array $headers)
    {
        // Collect matching headers into groups
        foreach ($headers as $i => $line) {
            list($key, $value) = explode(': ', $line, 2);
            if (isset($headers[$key])) {
                if (is_array($headers[$key])) {
                    $headers[$key][] = $value;
                } else {
                    $headers[$key] = array($headers[$key], $value);
                }
            } else {
                $headers[$key] = $value;
            }
            unset($headers[$i]);
        }
        
        return $headers;
    }

    /**
     * Returns http_version, code and message from a HTTP status line.
     *
     * @param string $status HTTP status line, e.g. HTTP/1.1 200 OK
     * @return array Parsed 'http_version', 'code' and 'message'.
     */
    public static function parseStatus($status)
    {
        Assertion::startsWith(
            $status,
            'HTTP/',
            "Invalid HTTP status '$status', expected format like: 'HTTP/1.1 200 OK'."
        );

        $part = explode(' ', $status, 3);

        return array(
            'http_version' => substr(strrchr($part[0], '/'), 1),
            'code' => $part[1],
            'message' => isset($part[2]) ? $part[2] : ''
        );
    }

    /**
     * Returns status, headers and body from a HTTP response string.
     *
     * @param string $response Response including header and body.
     * @return array Status, headers and body as strings.
     */
    public static function parseResponse($response)
    {
        $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response);
        
        list($rawHeader, $rawBody) = explode("\r\n\r\n", $response, 2);

        // Parse headers and status.
        $headers = self::parseRawHeader($rawHeader);
        $status = array_shift($headers);

        return array($status, $headers, $rawBody);
    }

    /**
     * Returns an array of arrays for specified raw header string.
     *
     * @param string $header
     * @return array
     */
    public static function parseRawHeader($rawHeader)
    {
        return explode("\r\n", trim($rawHeader));
    }

    /**
     * Returns a list of headers from a key/value paired array.
     *
     * @param array $headers Headers as key/value pairs.
     * @return array List of headers ['Content-Type: text/html', '...'].
     */
    public static function formatHeadersForCurl(array $headers)
    {
        $curlHeaders = array();

        foreach ($headers as $key => $values) {
            if (is_array($values)) {
                foreach ($values as $value) {
                    $curlHeaders[] = $key . ': ' . $value;
                }
            } else {
                $curlHeaders[] = $key . ': ' . $values;
            }
        }

        return $curlHeaders;
    }

    /**
     * Returns a HTTP status line from specified response.
     *
     * @param Response $response
     * @return string HTTP status line.
{'fraction_non_alphanumeric': 0.1402511961722488, 'fraction_numerical': 0.00687799043062201, 'mean_word_length': 1.7824459234608985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '7557010', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1851, 'n_words': 534}