You are here

function background_process_http_request in Background Process 7

Same name and namespace in other branches
  1. 8 background_process.module \background_process_http_request()
  2. 6 background_process.module \background_process_http_request()
  3. 7.2 background_process.http.inc \background_process_http_request()

Perform an http request.

See also

drupal_http_request()

3 calls to background_process_http_request()
BackgroundProcess::dispatch in ./BackgroundProcess.class.php
background_process_determine_default_service_host in ./background_process.module
Determine host for current installation.
background_process_http_request_get_response in ./background_process.module
Get response for an http request.

File

./background_process.module, line 1052

Code

function background_process_http_request($url, array $options = array()) {

  // Parse the URL and make sure we can handle the schema.
  $result = new stdClass();
  $result->url = $url;
  $result->options = $options;
  $result->code = NULL;
  $uri = @parse_url($url);
  $result->uri = $uri;
  if ($uri == FALSE) {
    $result->error = 'unable to parse URL';
    $result->code = -1001;
    return _background_process_http_request_result($result);
  }
  if (!isset($uri['scheme'])) {
    $result->error = 'missing schema';
    $result->code = -1002;
    return _background_process_http_request_result($result);
  }

  // Set default context to enable/disable SSL verification.
  $default_context = stream_context_create(array(
    'ssl' => array(
      'verify_peer' => variable_get('background_process_ssl_verification', TRUE),
      'verify_peer_name' => variable_get('background_process_ssl_verification', TRUE),
    ),
  ));

  // Merge the default options.
  $options += array(
    'headers' => array(),
    'method' => 'GET',
    'data' => NULL,
    'max_redirects' => 3,
    'timeout' => variable_get('background_process_connection_timeout', BACKGROUND_PROCESS_CONNECTION_TIMEOUT),
    'context' => $default_context,
    'blocking' => FALSE,
    'postpone' => FALSE,
  );

  // stream_socket_client() requires timeout to be a float.
  $options['timeout'] = (double) $options['timeout'];
  $host = NULL;
  switch ($uri['scheme']) {
    case 'http':
    case 'feed':
      $port = isset($uri['port']) ? $uri['port'] : 80;
      $socket = 'tcp://' . $uri['host'] . ':' . $port;

      // RFC 2616: "non-standard ports MUST, default ports MAY be included".
      // We don't add the standard port to prevent from breaking rewrite rules
      // checking the host that do not take into account the port number.
      $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
      break;
    case 'https':

      // Note: Only works when PHP is compiled with OpenSSL support.
      $port = isset($uri['port']) ? $uri['port'] : 443;
      $socket = 'ssl://' . $uri['host'] . ':' . $port;
      $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
      break;
    default:
      $result->error = 'invalid schema ' . $uri['scheme'];
      $result->code = -1003;
      return _background_process_http_request_result($result);
  }
  if (!empty($host) && empty($options['headers']['Host'])) {
    $options['headers']['Host'] = $host;
  }
  $result->options = $options;
  $result->socket = $socket;
  $result->postponed = $options['postpone'];
  if ($result->postponed) {
    return $result;
  }
  else {
    return background_process_http_request_initiate($result);
  }
}