You are here

function cf_http_parse_response in Common Functionality 7.2

Same name and namespace in other branches
  1. 7 modules/cf_http/cf_http.module \cf_http_parse_response()

Accepts and processes provided http content.

This process checks for a valid http response, unchunks if needed, returns http content without headers on success, false on any errors.

Originally From: http://php.net/manual/en/function.fsockopen.php#85572

@see: cf_http_get_webpage()

Parameters

string $http_response: An http response string.

string $path: The file/path on the server to.

int $port: (optional) port number of the page to read (defaults to 80).

Return value

array An array containing the connection status and return http response. The array keys:

  • parsed: A boolean with TRUE representing that the http request string

was successfully parsed, FALSE otherwise.

  • headers: The http header from the httpd response.
  • document: The complete html document from the http response.

Related topics

1 call to cf_http_parse_response()
cf_http_get_webpage in modules/cf_http/cf_http.module
Reads and processes a website page at the given path.

File

modules/cf_http/cf_http.module, line 254
Common Functionality - HTTP module.

Code

function cf_http_parse_response($http_response) {
  $results = array(
    'parsed' => FALSE,
    'header' => '',
    'document' => '',
    'http_error' => array(
      'error_code' => 0,
      'key' => '',
      'value' => '',
    ),
  );
  if (cf_is_empty_or_non_string('http_response', $http_response, WATCHDOG_ERROR)) {
    return $results;
  }

  // split into array, headers and content.
  $hunks = explode("\r\n\r\n", trim($http_response));
  if (!is_array($hunks) || count($hunks) < 2) {
    return $results;
  }
  $header = $hunks[count($hunks) - 2];
  $document = $hunks[count($hunks) - 1];
  $headers = explode("\n", $header);
  $results['headers'] = $headers;
  $results['document'] = $document;
  unset($hunks);
  unset($header);
  unset($document);
  if (!cf_http_validate_response($results['headers'])) {
    $results['http_error'] = cf_http_headers_errors($results['headers']);
  }
  if (in_array('Transfer-Coding: chunked', $results['headers'])) {
    $result = cf_http_unchunk_response($results['document']);
    if ($result['unchunked']) {
      $results['document'] = $result['document'];
    }
  }
  $results['document'] = trim($results['document']);

  // remove some additional trash not removed by the original function
  $results['document'] = preg_replace("/^[[:alnum:]]+\r\n/i", '', $results['document']);
  $results['document'] = preg_replace("/\r\n0\$/i", '', $results['document']);
  if (!is_string($results['document'])) {
    $results['document'] = '';
    return $results;
  }
  $results['parsed'] = TRUE;
  return $results;
}