<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[12425] branches/2.9/wp-includes/http.php:
  Properly handle float timeouts for all transports.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12425">12425</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-12-17 16:14:34 +0000 (Thu, 17 Dec 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Properly handle float timeouts for all transports. Props mdawaffe. fixes <a href="http://trac.wordpress.org/ticket/11468">#11468</a> for 2.9</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches29wpincludeshttpphp">branches/2.9/wp-includes/http.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches29wpincludeshttpphp"></a>
<div class="modfile"><h4>Modified: branches/2.9/wp-includes/http.php (12424 => 12425)</h4>
<pre class="diff"><span>
<span class="info">--- branches/2.9/wp-includes/http.php        2009-12-17 16:13:58 UTC (rev 12424)
+++ branches/2.9/wp-includes/http.php        2009-12-17 16:14:34 UTC (rev 12425)
</span><span class="lines">@@ -678,7 +678,9 @@
</span><span class="cx">                 if ( false === $handle )
</span><span class="cx">                         return new WP_Error('http_request_failed', $iError . ': ' . $strError);
</span><span class="cx"> 
</span><del>-                stream_set_timeout($handle, $r['timeout'] );
</del><ins>+                $timeout = (int) floor( $r['timeout'] );
+                $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
+                stream_set_timeout( $handle, $timeout, $utimeout );
</ins><span class="cx"> 
</span><span class="cx">                 if ( $proxy-&gt;is_enabled() &amp;&amp; $proxy-&gt;send_through_proxy( $url ) ) //Some proxies require full URL in this field.
</span><span class="cx">                         $requestPath = $url;
</span><span class="lines">@@ -834,7 +836,9 @@
</span><span class="cx">                 if (! $handle)
</span><span class="cx">                         return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
</span><span class="cx"> 
</span><del>-                stream_set_timeout($handle, $r['timeout'] );
</del><ins>+                $timeout = (int) floor( $r['timeout'] );
+                $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
+                stream_set_timeout( $handle, $timeout, $utimeout );
</ins><span class="cx"> 
</span><span class="cx">                 if ( ! $r['blocking'] ) {
</span><span class="cx">                         fclose($handle);
</span><span class="lines">@@ -1007,9 +1011,9 @@
</span><span class="cx">                 if ( ! $handle)
</span><span class="cx">                         return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url));
</span><span class="cx"> 
</span><del>-                // WordPress supports PHP 4.3, which has this function. Removed sanity checking for
-                // performance reasons.
-                stream_set_timeout($handle, $r['timeout'] );
</del><ins>+                $timeout = (int) floor( $r['timeout'] );
+                $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
+                stream_set_timeout( $handle, $timeout, $utimeout );
</ins><span class="cx"> 
</span><span class="cx">                 if ( ! $r['blocking'] ) {
</span><span class="cx">                         stream_set_blocking($handle, 0);
</span><span class="lines">@@ -1141,6 +1145,8 @@
</span><span class="cx">                 elseif ( ! $is_local )
</span><span class="cx">                         $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
</span><span class="cx"> 
</span><ins>+                $r['timeout'] = (int) ceil( $r['timeout'] );
+
</ins><span class="cx">                 $options = array(
</span><span class="cx">                         'timeout' =&gt; $r['timeout'],
</span><span class="cx">                         'connecttimeout' =&gt; $r['timeout'],
</span><span class="lines">@@ -1255,11 +1261,6 @@
</span><span class="cx">                 // Construct Cookie: header if any cookies are set.
</span><span class="cx">                 WP_Http::buildCookieHeader( $r );
</span><span class="cx"> 
</span><del>-                // 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'] &gt; 0 &amp;&amp; $r['timeout'] &lt; 1 )
-                        $r['timeout'] = 1;
-
</del><span class="cx">                 $handle = curl_init();
</span><span class="cx"> 
</span><span class="cx">                 // cURL offers really easy proxy support.
</span><span class="lines">@@ -1292,13 +1293,25 @@
</span><span class="cx">                 elseif ( ! $is_local )
</span><span class="cx">                         $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
</span><span class="cx"> 
</span><ins>+
+                // 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 );
+                }
+
</ins><span class="cx">                 curl_setopt( $handle, CURLOPT_URL, $url);
</span><span class="cx">                 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
</span><span class="cx">                 curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
</span><span class="cx">                 curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
</span><span class="cx">                 curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
</span><del>-                curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] );
-                curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );
</del><span class="cx">                 curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
</span><span class="cx"> 
</span><span class="cx">                 switch ( $r['method'] ) {
</span></span></pre>
</div>
</div>

</body>
</html>