Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.672 diff -u -p -r1.672 common.inc --- includes/common.inc 4 Jul 2007 22:47:28 -0000 1.672 +++ includes/common.inc 5 Jul 2007 11:32:03 -0000 @@ -392,11 +392,14 @@ function drupal_access_denied() { * @param $retry * An integer representing how many times to retry the request in case of a * redirect. + * @param $timeout + * An integer representing how long to wait for the socket to connect, + * and also how long to wait when writing data to the stream. * @return * An object containing the HTTP request headers, response code, headers, * data, and redirect status. */ -function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) { +function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3, $timeout = 15) { $result = new stdClass(); // Parse the URL, and make sure we can handle the schema. @@ -406,13 +409,13 @@ function drupal_http_request($url, $head case 'http': $port = isset($uri['port']) ? $uri['port'] : 80; $host = $uri['host'] . ($port != 80 ? ':'. $port : ''); - $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15); + $fp = @fsockopen($uri['host'], $port, $errno, $errstr, $timeout); break; case 'https': // Note: Only works for PHP 4.3 compiled with OpenSSL. $port = isset($uri['port']) ? $uri['port'] : 443; $host = $uri['host'] . ($port != 443 ? ':'. $port : ''); - $fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, 20); + $fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, $timeout); break; default: $result->error = 'invalid schema '. $uri['scheme']; @@ -456,6 +459,9 @@ function drupal_http_request($url, $head } $result->request = $request; + // Set a timeout on the stream + stream_set_timeout($fp, $timeout); + fwrite($fp, $request); // Fetch response. @@ -463,6 +469,14 @@ function drupal_http_request($url, $head while (!feof($fp) && $chunk = fread($fp, 1024)) { $response .= $chunk; } + + // Determine whether the response timed out during read + $response_metadata = stream_get_meta_data($fp); + if (array_key_exists('timed_out', $response_metadata) && $response_metadata['timed_out']) { + $result->error = 'Connection timed out while reading response'; + return $result; + } + fclose($fp); // Parse response.