Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Message.php

function Message::parseMessage

Parses an HTTP message into an associative array.

The array contains the "start-line" key containing the start line of the message, "headers" key containing an associative array of header array values, and a "body" key containing the body of the message.

Parameters

string $message HTTP request or response to parse.:

2 calls to Message::parseMessage()
Message::parseRequest in vendor/guzzlehttp/psr7/src/Message.php
Parses a request message string into a request object.
Message::parseResponse in vendor/guzzlehttp/psr7/src/Message.php
Parses a response message string into a response object.

File

vendor/guzzlehttp/psr7/src/Message.php, line 115

Class

Message

Namespace

GuzzleHttp\Psr7

Code

public static function parseMessage(string $message) : array {
    if (!$message) {
        throw new \InvalidArgumentException('Invalid message');
    }
    $message = ltrim($message, "\r\n");
    $messageParts = preg_split("/\r?\n\r?\n/", $message, 2);
    if ($messageParts === false || count($messageParts) !== 2) {
        throw new \InvalidArgumentException('Invalid message: Missing header delimiter');
    }
    [
        $rawHeaders,
        $body,
    ] = $messageParts;
    $rawHeaders .= "\r\n";
    // Put back the delimiter we split previously
    $headerParts = preg_split("/\r?\n/", $rawHeaders, 2);
    if ($headerParts === false || count($headerParts) !== 2) {
        throw new \InvalidArgumentException('Invalid message: Missing status line');
    }
    [
        $startLine,
        $rawHeaders,
    ] = $headerParts;
    if (preg_match("/(?:^HTTP\\/|^[A-Z]+ \\S+ HTTP\\/)(\\d+(?:\\.\\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') {
        // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0
        $rawHeaders = preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders);
    }
    
    /** @var array[] $headerLines */
    $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, 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://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
        if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) {
            throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding');
        }
        throw new \InvalidArgumentException('Invalid header syntax');
    }
    $headers = [];
    foreach ($headerLines as $headerLine) {
        $headers[$headerLine[1]][] = $headerLine[2];
    }
    return [
        'start-line' => $startLine,
        'headers' => $headers,
        'body' => $body,
    ];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal