From: Aaron Schulz Date: Wed, 29 Jan 2014 22:44:54 +0000 (-0800) Subject: MultiHttpClient tweaks X-Git-Tag: 1.31.0-rc.0~16945 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=bc7f2962401aaa97c2cb0bc9e5a04ac6ced6e97c;p=lhc%2Fweb%2Fwiklou.git MultiHttpClient tweaks * Reference handling cleanup for loops in MultiHttpClient * Support a slightly more convenient request array format for MultiHttpClient * Added missing license header to MultiHttpClient Change-Id: I0a2eef02c06d9c900c4a1ae1b0eeb292755d6cd9 --- diff --git a/includes/libs/MultiHttpClient.php b/includes/libs/MultiHttpClient.php index 350790f214..00cd25792f 100644 --- a/includes/libs/MultiHttpClient.php +++ b/includes/libs/MultiHttpClient.php @@ -1,9 +1,29 @@ (uses RFC 3986) @@ -14,6 +34,7 @@ * array bodies are encoded as multipart/form-data and strings * use application/x-www-form-urlencoded (headers sent automatically) * - stream : resource to stream the HTTP response body to + * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'. * * @author Aaron Schulz * @since 1.23 @@ -73,13 +94,13 @@ class MultiHttpClient { * - reqTimeout : post-connection timeout per request * @return array Response array for request */ - public function run( array $req, array $opts = array() ) { + final public function run( array $req, array $opts = array() ) { $req = $this->runMulti( array( $req ), $opts ); return $req[0]['response']; } /** - * Execute a set of HTTP(S) request concurrently + * Execute a set of HTTP(S) requests concurrently * * The maps are returned by this method with the 'response' field set to a map of: * - code : HTTP response code or 0 if there was a serious cURL error @@ -92,7 +113,8 @@ class MultiHttpClient { * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $req; * * All headers in the 'headers' field are normalized to use lower case names. - * This is true for the request headers and the response headers. + * This is true for the request headers and the response headers. Integer-indexed + * method/URL entries will also be changed to use the corresponding string keys. * * @param array $req Map of HTTP request arrays * @param array $opts @@ -115,6 +137,14 @@ class MultiHttpClient { 'body' => '', 'error' => '' ); + if ( isset( $req[0] ) ) { + $req['method'] = $req[0]; // short-form + unset( $req[0] ); + } + if ( isset( $req[1] ) ) { + $req['url'] = $req[1]; // short-form + unset( $req[1] ); + } if ( !isset( $req['method'] ) ) { throw new Exception( "Request has no 'method' field set." ); } elseif ( !isset( $req['url'] ) ) { @@ -138,6 +168,7 @@ class MultiHttpClient { curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE, true ); } } + unset( $req ); // don't assign over this by accident $indexes = array_keys( $reqs ); if ( function_exists( 'curl_multi_setopt' ) ) { // PHP 5.5 @@ -195,6 +226,7 @@ class MultiHttpClient { unset( $req['_closeHandle'] ); } } + unset( $req ); // don't assign over this by accident // Restore the default settings if ( function_exists( 'curl_multi_setopt' ) ) { // PHP 5.5