You are here

function httprl_setup_proxy in HTTP Parallel Request & Threading Library 6

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

If server uses a proxy, change the request to utilize said proxy.

Parameters

array &$uri: Array from parse_url().

array &$options: Array containing options.

string $url: String containing the URL.

Return value

string String containing the proxy servers host name if one is to be used.

1 call to httprl_setup_proxy()
httprl_request in ./httprl.module
Queue up a HTTP request in httprl_send_request.

File

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

Code

function httprl_setup_proxy(&$uri, &$options, $url) {

  // Proxy setup.
  $proxy_server = httprl_variable_get('proxy_server', '');

  // Use a proxy if one is defined and the host is not on the excluded list.
  if ($proxy_server && _httprl_use_proxy($uri['host'])) {

    // Set the scheme so we open a socket to the proxy server.
    $uri['scheme'] = 'proxy';

    // Set the path to be the full URL.
    $uri['path'] = $url;

    // Since the full URL is passed as the path, we won't use the parsed query.
    unset($uri['query']);

    // Add in username and password to Proxy-Authorization header if needed.
    if ($proxy_username = httprl_variable_get('proxy_username', '')) {
      $proxy_password = httprl_variable_get('proxy_password', '');
      $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . ':' . $proxy_password);
    }

    // Some proxies reject requests with any User-Agent headers, while others
    // require a specific one.
    $proxy_user_agent = httprl_variable_get('proxy_user_agent', '');

    // The default value matches neither condition.
    if (is_null($proxy_user_agent)) {
      unset($options['headers']['User-Agent']);
    }
    elseif ($proxy_user_agent) {
      $options['headers']['User-Agent'] = $proxy_user_agent;
    }
  }
  return $proxy_server;
}