Properly handle float timeouts for all transports. Props mdawaffe. fixes #11468

git-svn-id: http://svn.automattic.com/wordpress/trunk@12424 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-12-17 16:13:58 +00:00
parent c0437cd3a6
commit 28d836c672
1 changed files with 25 additions and 12 deletions

View File

@ -678,7 +678,9 @@ class WP_Http_Fsockopen {
if ( false === $handle ) if ( false === $handle )
return new WP_Error('http_request_failed', $iError . ': ' . $strError); return new WP_Error('http_request_failed', $iError . ': ' . $strError);
stream_set_timeout($handle, $r['timeout'] ); $timeout = (int) floor( $r['timeout'] );
$utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
stream_set_timeout( $handle, $timeout, $utimeout );
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field.
$requestPath = $url; $requestPath = $url;
@ -834,7 +836,9 @@ class WP_Http_Fopen {
if (! $handle) if (! $handle)
return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url)); return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
stream_set_timeout($handle, $r['timeout'] ); $timeout = (int) floor( $r['timeout'] );
$utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
stream_set_timeout( $handle, $timeout, $utimeout );
if ( ! $r['blocking'] ) { if ( ! $r['blocking'] ) {
fclose($handle); fclose($handle);
@ -1007,9 +1011,9 @@ class WP_Http_Streams {
if ( ! $handle) if ( ! $handle)
return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url)); return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
// WordPress supports PHP 4.3, which has this function. Removed sanity checking for $timeout = (int) floor( $r['timeout'] );
// performance reasons. $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
stream_set_timeout($handle, $r['timeout'] ); stream_set_timeout( $handle, $timeout, $utimeout );
if ( ! $r['blocking'] ) { if ( ! $r['blocking'] ) {
stream_set_blocking($handle, 0); stream_set_blocking($handle, 0);
@ -1141,6 +1145,8 @@ class WP_Http_ExtHTTP {
elseif ( ! $is_local ) elseif ( ! $is_local )
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
$r['timeout'] = (int) ceil( $r['timeout'] );
$options = array( $options = array(
'timeout' => $r['timeout'], 'timeout' => $r['timeout'],
'connecttimeout' => $r['timeout'], 'connecttimeout' => $r['timeout'],
@ -1255,11 +1261,6 @@ class WP_Http_Curl {
// Construct Cookie: header if any cookies are set. // Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader( $r ); WP_Http::buildCookieHeader( $r );
// cURL extension will sometimes fail when the timeout is less than 1 as it may round down
// to 0, which gives it unlimited timeout.
if ( $r['timeout'] > 0 && $r['timeout'] < 1 )
$r['timeout'] = 1;
$handle = curl_init(); $handle = curl_init();
// cURL offers really easy proxy support. // cURL offers really easy proxy support.
@ -1292,13 +1293,25 @@ class WP_Http_Curl {
elseif ( ! $is_local ) elseif ( ! $is_local )
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
// CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since
// a value of 0 will allow an ulimited timeout.
// Use _MS if available.
if ( defined( 'CURLOPT_TIMEOUT_MS' ) ) {
$timeout_ms = (int) ceil( 1000 * $r['timeout'] );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT_MS, $timeout_ms );
curl_setopt( $handle, CURLOPT_TIMEOUT_MS, $timeout_ms );
} else {
$timeout = (int) ceil( $r['timeout'] );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
}
curl_setopt( $handle, CURLOPT_URL, $url); curl_setopt( $handle, CURLOPT_URL, $url);
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify ); curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] ); curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] ); curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
switch ( $r['method'] ) { switch ( $r['method'] ) {