MultiHttpClient tweaks
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 29 Jan 2014 22:44:54 +0000 (14:44 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 12 Feb 2014 00:53:10 +0000 (16:53 -0800)
* 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

includes/libs/MultiHttpClient.php

index 350790f..00cd257 100644 (file)
@@ -1,9 +1,29 @@
 <?php
+/**
+ * HTTP service client
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * Class to handle concurrent HTTP requests
  *
- * HTTP request maps use the following format:
+ * HTTP request maps are arrays that use the following format:
  *   - method   : GET/HEAD/PUT/POST/DELETE
  *   - url      : HTTP/HTTPS URL
  *   - query    : <query parameter field/value associative array> (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;
         *  </code>
         * 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