You are here

protected function HttpClientDelegate::interpretResponse in Http Client 6.2

Same name and namespace in other branches
  1. 7.2 includes/HttpClient.inc \HttpClientDelegate::interpretResponse()

This function interprets a raw http response.

Parameters

HttpClient $client:

string $response:

Return value

object The interpreted response.

1 call to HttpClientDelegate::interpretResponse()
HttpClientCurlDelegate::execute in includes/HttpClientCurlDelegate.inc
Executes a request for the HttpClient.

File

includes/HttpClient.inc, line 233

Class

HttpClientDelegate
Abstract base class for Http client delegates.

Code

protected function interpretResponse(HttpClient $client, $response) {
  $client->rawResponse = $response;
  $split = preg_split('/\\r\\n\\r\\n/', $response, 2);
  if (!isset($split[1])) {
    throw new HttpClientException('Error interpreting response', 0, (object) array(
      'rawResponse' => $response,
    ));
  }
  list($headers, $body) = $split;
  $obj = (object) array(
    'headers' => $headers,
    'body' => $body,
  );

  // Drupal sends errors are via X-Drupal-Assertion-* headers,
  // generated by _drupal_log_error(). Read them to ease debugging.
  if (preg_match_all('/X-Drupal-Assertion-[0-9]+: (.*)\\n/', $headers, $matches)) {
    foreach ($matches[1] as $key => $match) {
      $obj->drupalErrors[] = print_r(unserialize(urldecode($match)), 1);
    }
  }
  $matches = array();
  if (preg_match('/HTTP\\/1.\\d (\\d{3}) (.*)/', $headers, $matches)) {
    $obj->responseCode = intVal(trim($matches[1]), 10);
    $obj->responseMessage = trim($matches[2]);

    // Handle HTTP/1.1 100 Continue
    if ($obj->responseCode == 100) {
      return $this
        ->interpretResponse($client, $body);
    }
  }
  return $obj;
}