You are here

function _parse_message in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/psr7/src/functions.php \GuzzleHttp\Psr7\_parse_message()

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 729

Namespace

GuzzleHttp\Psr7

Code

function _parse_message($message) {
  if (!$message) {
    throw new \InvalidArgumentException('Invalid message');
  }

  // Iterate over each line in the message, accounting for line endings
  $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
  $result = [
    'start-line' => array_shift($lines),
    'headers' => [],
    'body' => '',
  ];
  array_shift($lines);
  for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
    $line = $lines[$i];

    // If two line breaks were encountered, then this is the end of body
    if (empty($line)) {
      if ($i < $totalLines - 1) {
        $result['body'] = implode('', array_slice($lines, $i + 2));
      }
      break;
    }
    if (strpos($line, ':')) {
      $parts = explode(':', $line, 2);
      $key = trim($parts[0]);
      $value = isset($parts[1]) ? trim($parts[1]) : '';
      $result['headers'][$key][] = $value;
    }
  }
  return $result;
}