You are here

function _parse_message in Lockr 7.3

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.

@internal

Parameters

string $message HTTP request or response to parse.:

Return value

array

2 calls to _parse_message()
parse_request in vendor/guzzlehttp/psr7/src/functions.php
Parses a request message string into a request object.
parse_response in vendor/guzzlehttp/psr7/src/functions.php
Parses a response message string into a response object.

File

vendor/guzzlehttp/psr7/src/functions.php, line 761

Namespace

GuzzleHttp\Psr7

Code

function _parse_message($message) {
  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');
  }
  list($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');
  }
  list($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://tools.ietf.org/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,
  ];
}