public static function parseHeaders(string $rawHeaders): array
    {
        // Ensure that the last line also ends with a newline, this is important.
        \assert(\substr($rawHeaders, -2) === "\r\n", "Argument 1 must end with CRLF");

        /** @var array[] $matches */
        $count = \preg_match_all(self::HEADER_REGEX, $rawHeaders, $matches, \PREG_SET_ORDER);

        // If these aren't the same, then one line didn't match and there's an invalid header.
        if ($count !== \substr_count($rawHeaders, "\n")) {
            // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
            if (\preg_match(self::HEADER_FOLD_REGEX, $rawHeaders)) {
                throw new InvalidHeaderException("Invalid header syntax: Obsolete line folding");
            }

            throw new InvalidHeaderException("Invalid header syntax");
        }

        $headers = [];

        foreach ($matches as $match) {
            // We avoid a call to \trim() here due to the regex.
            // Unfortunately, we can't avoid the \strtolower() calls due to \array_change_key_case() behavior
            // when equal headers are present with different casing, e.g. 'set-cookie' and 'Set-Cookie'.
            // Accessing matches directly instead of using foreach (... as list(...)) is slightly faster.
            $headers[\strtolower($match[1])][] = $match[2];
        }

        return $headers;
    }
public static function parseHeaders(string $rawHeaders): array
    {
        // Ensure that the last line also ends with a newline, this is important.
        \assert(\substr($rawHeaders, -2) === "\r\n", "Argument 1 must end with CRLF");

        /** @var array[] $matches */
        $count = \preg_match_all(self::HEADER_REGEX, $rawHeaders, $matches, \PREG_SET_ORDER);

        // If these aren't the same, then one line didn't match and there's an invalid header.
        if ($count !== \substr_count($rawHeaders, "\n")) {
            // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
            if (\preg_match(self::HEADER_FOLD_REGEX, $rawHeaders)) {
                throw new InvalidHeaderException("Invalid header syntax: Obsolete line folding");
            }

            throw new InvalidHeaderException("Invalid header syntax");
        }

        $headers = [];

        foreach ($matches as $match) {
            // We avoid a call to \trim() here due to the regex.
            // Unfortunately, we can't avoid the \strtolower() calls due to \array_change_key_case() behavior
            // when equal headers are present with different casing, e.g. 'set-cookie' and 'Set-Cookie'.
            // Accessing matches directly instead of using foreach (... as list(...)) is slightly faster.
            $headers[\strtolower($match[1])][] = $match[2];
        }

        return $headers;
    }
public static function parseHeaders ( string $ rawHeaders ) : array { // Ensure that the last line also ends with a newline, this is important. \ assert ( \ substr ( $ rawHeaders , - 2 ) === "\r\n" , "Argument 1 must end with CRLF" ) ; /** @var array[] $matches */ $ count = \ preg_match_all ( self :: HEADER_REGEX , $ rawHeaders , $ matches , \ PREG_SET_ORDER ) ; // If these aren't the same, then one line didn't match and there's an invalid header. if ( $ count !== \ substr_count ( $ rawHeaders , "\n" ) ) { // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4 if ( \ preg_match ( self :: HEADER_FOLD_REGEX , $ rawHeaders ) ) { throw new InvalidHeaderException ( "Invalid header syntax: Obsolete line folding" ) ; } throw new InvalidHeaderException ( "Invalid header syntax" ) ; } $ headers = [ ] ; foreach ( $ matches as $ match ) { // We avoid a call to \trim() here due to the regex. // Unfortunately, we can't avoid the \strtolower() calls due to \array_change_key_case() behavior // when equal headers are present with different casing, e.g. 'set-cookie' and 'Set-Cookie'. // Accessing matches directly instead of using foreach (... as list(...)) is slightly faster. $ headers [ \ strtolower ( $ match [ 1 ] ) ] [ ] = $ match [ 2 ] ; } return $ headers ; }
Parses headers according to RFC 7230 and 2616.

Allows empty header values, as HTTP/1.0 allows that.

@param string $rawHeaders

@return array Associative array mapping header names to arrays of values.

@throws InvalidHeaderException If invalid headers have been passed.
Parses headers according to RFC 7230 and 2616 .