You are here

function httprl_build_url_self in HTTP Parallel Request & Threading Library 6

Same name and namespace in other branches
  1. 7 httprl.module \httprl_build_url_self()

Helper function to build an URL for asynchronous requests to self.

Parameters

string $path: Path to a URI excluding everything to the left and including the base path.

bool $detect_schema: If TRUE it will see if this request is https; if so, it will set the full url to be https as well.

bool $reset: If TRUE the $drupal_root static will be reset.

Return value

string The URL that points to this Drupal install.

4 calls to httprl_build_url_self()
httprl.examples.php in examples/httprl.examples.php
HTTP Parallel Request Library code examples.
httprl_install_http_test in ./httprl.install
Issue a HTTP request to admin/httprl-test, verifying that the server got it.
httprl_install_try_different_settings_checker in ./httprl.install
httprl_queue_background_callback in ./httprl.module
Run callback in the background.

File

./httprl.module, line 424
HTTP Parallel Request Library module.

Code

function httprl_build_url_self($path = '', $detect_schema = FALSE, $reset = FALSE) {
  static $drupal_root;
  if (!isset($drupal_root) || $reset) {
    $drupal_root = _httprl_build_drupal_root();

    // If ran from the command line, the drupal root might be in a subdir. Test to
    // make sure we have the right directory.
    if (is_callable('drupal_is_cli') && drupal_is_cli() || !isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)) {
      $level = 0;
      $found = FALSE;
      $mode = 0;
      while (!$found) {

        // Trick due to not knowing the subdir.
        // http://stackoverflow.com/questions/8361355/get-apache-document-root-from-command-line-execution-no-browser/8415235#8415235
        // Issue http request and get the headers.
        $override_function = httprl_variable_get('drupal_http_request_function', FALSE);
        $GLOBALS['conf']['drupal_http_request_function'] = FALSE;
        $test_request = drupal_http_request($drupal_root . 'httprl_async_function_callback');
        $GLOBALS['conf']['drupal_http_request_function'] = $override_function;

        // Look for the X-HTTPRL header.
        $headers = isset($test_request->headers) ? $test_request->headers : array();
        if (!empty($headers)) {
          foreach ($headers as $key => $value) {
            if (stripos($key, 'X-HTTPRL') !== FALSE || stripos($value, 'X-HTTPRL') !== FALSE) {
              $found = TRUE;
              break;
            }
          }
        }

        // If the header is not found, adjust sub dir and try again.
        if (!$found) {
          $level++;
          $new_drupal_root = _httprl_build_drupal_root($level, $mode);
          if ($new_drupal_root == $drupal_root) {

            // If the header is not found, adjust hostname and try again.
            $mode++;
            $level = 0;
            if ($mode > 4) {

              // Use no subdirectories if nothing worked.
              $drupal_root = _httprl_build_drupal_root();
              break;
            }
          }
          $drupal_root = $new_drupal_root;
        }
      }
    }
  }
  return $drupal_root . $path;
}