You are here

protected function HttpClientDelegate::interpretResponse in Http Client 7.2

Same name and namespace in other branches
  1. 6.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 236

Class

HttpClientDelegate
Abstract base class for Http client delegates.

Code

protected function interpretResponse(HttpClient $client, $response) {
  $client->rawResponse = $response;
  if (preg_match('/\\nProxy-agent: .*\\r?\\n\\r?\\nHTTP/', $response)) {
    $split = preg_split('/\\r?\\n\\r?\\n/', $response, 3);
    if (!isset($split[2])) {
      throw new HttpClientException('Error interpreting response', 0, (object) array(
        'rawResponse' => $response,
      ));
    }
    $headers = $split[1];
    $body = $split[2];
  }
  else {
    $split = preg_split('/\\r?\\n\\r?\\n/', $response, 2);
    if (!isset($split[1])) {
      throw new HttpClientException('Error interpreting response', 0, (object) array(
        'rawResponse' => $response,
      ));
    }
    $headers = $split[0];
    $body = $split[1];
  }
  $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\\/[\\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;
}