Merge "Improve serialization of Message, Title"
[lhc/web/wiklou.git] / includes / libs / MultiHttpClient.php
index 16e0d4f..aa0cc46 100644 (file)
@@ -84,12 +84,12 @@ class MultiHttpClient {
         * Execute an HTTP(S) request
         *
         * This method returns a response map of:
-        *   - code    : HTTP response code or 0 if there was a serious cURL error
-        *   - reason  : HTTP response reason (empty if there was a serious cURL error)
-        *   - headers : <header name/value associative array>
-        *   - body    : HTTP response body or resource (if "stream" was set)
+        *   - code    : HTTP response code or 0 if there was a serious cURL error
+        *   - reason  : HTTP response reason (empty if there was a serious cURL error)
+        *   - headers : <header name/value associative array>
+        *   - body    : HTTP response body or resource (if "stream" was set)
         *   - error     : Any cURL error string
-        * The map also stores integer-indexed copies of these values. This lets callers do:
+        * The map also stores integer-indexed copies of these values. This lets callers do:
         * @code
         *              list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $http->run( $req );
         * @endcode
@@ -189,6 +189,7 @@ class MultiHttpClient {
 
                // @TODO: use a per-host rolling handle window (e.g. CURLMOPT_MAX_HOST_CONNECTIONS)
                $batches = array_chunk( $indexes, $this->maxConnsPerHost );
+               $infos = array();
 
                foreach ( $batches as $batch ) {
                        // Attach all cURL handles for this batch
@@ -201,6 +202,10 @@ class MultiHttpClient {
                                // Do any available work...
                                do {
                                        $mrc = curl_multi_exec( $chm, $active );
+                                       $info = curl_multi_info_read( $chm );
+                                       if ( $info !== false ) {
+                                               $infos[(int)$info['handle']] = $info;
+                                       }
                                } while ( $mrc == CURLM_CALL_MULTI_PERFORM );
                                // Wait (if possible) for available work...
                                if ( $active > 0 && $mrc == CURLM_OK ) {
@@ -216,10 +221,18 @@ class MultiHttpClient {
                foreach ( $reqs as $index => &$req ) {
                        $ch = $handles[$index];
                        curl_multi_remove_handle( $chm, $ch );
-                       if ( curl_errno( $ch ) !== 0 ) {
-                               $req['response']['error'] = "(curl error: " .
-                                       curl_errno( $ch ) . ") " . curl_error( $ch );
+
+                       $info = $infos[(int)$ch];
+
+                       $errno = $info['result'];
+                       if ( $errno !== 0 ) {
+                               $req['response']['error'] = "(curl error: $errno)";
+
+                               if ( version_compare( PHP_VERSION, '5.5.0' ) >= 0 ) {
+                                       $req['response']['error'] .= " " . curl_strerror( $errno );
+                               }
                        }
+
                        // For convenience with the list() operator
                        $req['response'][0] = $req['response']['code'];
                        $req['response'][1] = $req['response']['reason'];