You are here

public static function EasyRdf_Http_Response::fromString in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Http/Response.php \EasyRdf_Http_Response::fromString()

Create an EasyRdf_Http_Response object from a HTTP response string

Parameters

string $responseStr:

Return value

EasyRdf_Http_Response

1 call to EasyRdf_Http_Response::fromString()
EasyRdf_Http_Client::request in vendor/easyrdf/easyrdf/lib/EasyRdf/Http/Client.php
Send the HTTP request and return an HTTP response object

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Http/Response.php, line 266

Class

EasyRdf_Http_Response
Class that represents an HTTP 1.0 / 1.1 response message.

Code

public static function fromString($responseStr) {

  // First, split body and headers
  $matches = preg_split('|(?:\\r?\\n){2}|m', $responseStr, 2);
  if ($matches and sizeof($matches) == 2) {
    list($headerLines, $body) = $matches;
  }
  else {
    throw new EasyRdf_Exception("Failed to parse HTTP response.");
  }

  // Split headers part to lines
  $headerLines = preg_split('|[\\r\\n]+|m', $headerLines);
  $status = array_shift($headerLines);
  if (preg_match("|^HTTP/([\\d\\.x]+) (\\d+) ([^\r\n]+)|", $status, $m)) {
    $version = $m[1];
    $status = $m[2];
    $message = $m[3];
  }
  else {
    throw new EasyRdf_Exception("Failed to parse HTTP response status line.");
  }

  // Process the rest of the header lines
  $headers = array();
  foreach ($headerLines as $line) {
    if (preg_match("|^([\\w-]+):\\s+(.+)\$|", $line, $m)) {
      $hName = ucwords(strtolower($m[1]));
      $hValue = $m[2];
      if (isset($headers[$hName])) {
        if (!is_array($headers[$hName])) {
          $headers[$hName] = array(
            $headers[$hName],
          );
        }
        $headers[$hName][] = $hValue;
      }
      else {
        $headers[$hName] = $hValue;
      }
    }
  }
  return new EasyRdf_Http_Response($status, $headers, $body, $version, $message);
}