You are here

function httprl_install_http_test in HTTP Parallel Request & Threading Library 7

Same name and namespace in other branches
  1. 6 httprl.install \httprl_install_http_test()

Issue a HTTP request to admin/httprl-test, verifying that the server got it.

Parameters

int $mode: 1: use drupal_http_request() 2: use httprl_request()

bool $blocking: (Optional) HTTPRL blocking mode.

Return value

array (bool, msg).

3 calls to httprl_install_http_test()
httprl_install_fclose_delay_check in ./httprl.install
httprl_install_try_different_settings_checker in ./httprl.install
httprl_requirements in ./httprl.install
Implements hook_requirements().

File

./httprl.install, line 173
Handle HTTP Parallel Request Library installation and upgrade tasks.

Code

function httprl_install_http_test($mode, $blocking = FALSE, $depth = 0) {
  $t = get_t();
  $depth++;
  set_time_limit(0);

  // 512 bits = 64 bytes.
  if (function_exists('drupal_random_bytes')) {
    $id = 'httprl_' . hash('sha512', drupal_random_bytes(64));
  }
  elseif (function_exists('openssl_random_pseudo_bytes')) {
    $id = 'httprl_' . hash('sha512', openssl_random_pseudo_bytes(64));
  }
  else {
    $id = 'httprl_' . hash('sha512', mt_rand() . microtime(TRUE) . serialize($_SERVER));
  }
  $msg = '';
  $hostname = httprl_get_hostname();

  // Set the headers to point to this hostname.
  $headers = array(
    'Host' => $hostname,
    'Connection' => 'closed',
  );

  // Setup the arguments for releasing the lock.
  $timing = array(
    httprl_variable_get('httprl_install_lock_time', 7),
    httprl_variable_get('httprl_install_connection_time', 5),
  );
  $args = array(
    array(
      'function' => 'httprl_lock_release',
      // Setup options array.
      'options' => array(
        'blocking' => $blocking,
        'timeout' => $timing[0],
        'max_redirects' => 0,
        'headers' => $headers,
      ),
    ),
    $id,
  );

  // Get a lock & start the timer.
  lock_acquire($id, $args[0]['options']['timeout']);
  timer_start($id);
  if ($mode == 2) {

    // Queue up the request.
    if ($blocking) {
      $args[0]['return'] = '';
      $args[0]['printed'] = '';
    }

    // Enable background callbacks even if disabled.
    $old_var = httprl_variable_get('httprl_background_callback', HTTPRL_BACKGROUND_CALLBACK);
    $GLOBALS['conf']['httprl_background_callback'] = HTTPRL_BACKGROUND_CALLBACK;
    $url = httprl_queue_background_callback($args);
    if (empty($url)) {
      return array(
        FALSE,
        $t('The background callbacks setting is disabled.'),
      );
    }
    else {
      $url = array_keys($url);
      $url = array_pop($url);

      // Execute request.
      $output = httprl_send_request();
    }

    // Restore the background callbacks setting.
    $GLOBALS['conf']['httprl_background_callback'] = $old_var;
  }
  else {

    // Get options.
    $callback_options = array_shift($args);

    // Build URL to point to httprl_async_function_callback on this server.
    $url = httprl_build_url_self('httprl_async_function_callback?count=0', TRUE);

    // Create lock name for this run.
    $available = FALSE;
    $lock_counter = 0;
    while (!$available && $lock_counter < 20) {

      // 512 bits = 64 bytes.
      if (function_exists('drupal_random_bytes')) {
        $name = 'httprl_' . hash('sha512', drupal_random_bytes(64));
      }
      elseif (function_exists('openssl_random_pseudo_bytes')) {
        $name = 'httprl_' . hash('sha512', openssl_random_pseudo_bytes(64));
      }
      else {
        $name = 'httprl_' . hash('sha512', mt_rand() . microtime(TRUE) . serialize($_SERVER));
      }
      $available = lock_may_be_available($name);
      $lock_counter++;
    }
    $callback_options['options']['lock_name'] = $name;
    lock_acquire($name, $callback_options['options']['timeout']);

    // Create data array and options for request.
    $options = array(
      'data' => array(
        'master_key' => hash('sha512', httprl_drupal_get_private_key()),
        'temp_key' => $name,
        'mode' => TRUE,
        'php_timeout' => $callback_options['options']['timeout'],
        'function' => $callback_options['function'],
        // Follow rfc4648 for base64url
        // @see http://tools.ietf.org/html/rfc4648#page-7
        'args' => strtr(base64_encode(serialize($args)), array(
          '+' => '-',
          '/' => '_',
        )),
      ),
      'method' => 'POST',
      'headers' => $headers,
      'timeout' => $callback_options['options']['timeout'],
      'max_redirects' => $callback_options['options']['max_redirects'],
    );
    httprl_handle_data($options);

    // Execute the request using core.
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      $output = drupal_http_request($url, $options);
    }
    else {
      $output = drupal_http_request($url, $options['headers'], $options['method'], $options['data'], $options['max_redirects'], $options['timeout']);
    }
  }

  // Wait for the lock and stop the timer.
  while (lock_wait($id)) {
    usleep(25000);
  }
  $time = timer_stop($id);

  // Add in debugging info.
  $time['mode'] = $mode;
  $time['blocking'] = $blocking;
  $time['url'] = $url;
  $time['request'] = $output;
  $GLOBALS['_httprl']['install']['debug'][] = $time;

  // See if the request came back in under 5 seconds, or if it timed out.
  if ($time['time'] / 1000 > $timing[1]) {
    if ($depth <= 1) {
      list($success, $msg) = httprl_install_try_different_settings_checker($mode, $blocking, $depth);
      if ($success) {
        return array(
          $success,
          $msg,
        );
      }
    }
  }
  else {
    $hostname = httprl_get_hostname();

    // Check if the httprl_server_hostname needs to be set to HTTP_HOST or
    // SERVER_NAME.
    if (!empty($hostname) && $hostname !== 'default' && ip2long($hostname) === FALSE && httprl_variable_get('httprl_server_hostname', FALSE) != $hostname) {
      $msg = $t('The "Host name of the server to send all self server requests to" setting needs to be changed to @hostname on the <a href="@url">configuration page</a>,', array(
        '@url' => url('admin/config/development/httprl'),
        '@hostname' => $hostname,
      ));
    }
    return array(
      TRUE,
      $msg,
    );
  }
}