diff --git a/includes/common.inc b/includes/common.inc index 5f7cdb8..97ed83b 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -779,8 +779,6 @@ function drupal_http_request($url, array $options = array()) { return $result; } - timer_start(__FUNCTION__); - // Merge the default options. $options += array( 'headers' => array(), @@ -790,10 +788,48 @@ function drupal_http_request($url, array $options = array()) { 'timeout' => 30.0, 'context' => NULL, ); + // Merge the default headers. + $options['headers'] += array( + 'User-Agent' => 'Drupal (+http://drupal.org/)', + ); // stream_socket_client() requires timeout to be a float. $options['timeout'] = (float) $options['timeout']; + // Proxy setup + $proxy_server = variable_get('proxy_server', ''); + // Use a proxy if one is defined and the host is not on the excluded list. + if ($proxy_server && _drupal_http_use_proxy($uri['host'])) { + // Since the URL is passed as the path, we won't use the parsed query. + unset($uri['query']); + $uri['path'] = $url; + // Set the scheme so we open a socket to the proxy server. + $uri['scheme'] = 'proxy'; + if ($proxy_username = variable_get('proxy_username', '')) { + $proxy_password = variable_get('proxy_password', ''); + $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')); + } + // Some proxies reject requests with any User-Agent headers, while others + // require a specific one. + $proxy_user_agent = variable_get('proxy_user_agent', ''); + // The default value matches neither condition. + if ($proxy_user_agent === NULL) { + unset($options['headers']['User-Agent']); + } + elseif ($proxy_user_agent) { + $options['headers']['User-Agent'] = $proxy_user_agent; + } + } + + timer_start(__FUNCTION__); + switch ($uri['scheme']) { + case 'proxy': + // Make the socket connection to a proxy server. + $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080); + // The Host header still needs to match the real request. + $options['headers']['Host'] = $uri['host']; + $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : ''; + break; case 'http': case 'feed': $port = isset($uri['port']) ? $uri['port'] : 80; @@ -845,11 +881,6 @@ function drupal_http_request($url, array $options = array()) { $path .= '?' . $uri['query']; } - // Merge the default headers. - $options['headers'] += array( - 'User-Agent' => 'Drupal (+http://drupal.org/)', - ); - // Only add Content-Length if we actually have any content or if it is a POST // or PUT request. Some non-standard servers get confused by Content-Length in // at least HEAD/GET requests, and Squid always requires Content-Length in @@ -1020,6 +1051,18 @@ function drupal_http_request($url, array $options = array()) { return $result; } + +/** + * Helper function for determining hosts excluded from needing a proxy. + * + * @return + * TRUE if a proxy should be used for this host. + */ +function _drupal_http_use_proxy($host) { + $proxy_exceptions = variable_get('proxy_exceptions', array('localhost', '127.0.0.1')); + return !in_array(strtolower($host), $proxy_exceptions, TRUE); +} + /** * @} End of "HTTP handling". */ diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php index a8fe52b..2b56fbd 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -479,3 +479,18 @@ $conf['404_fast_html'] = '