Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / libs / MultiHttpClient.php
1 <?php
2 /**
3 * HTTP service client
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Class to handle concurrent HTTP requests
25 *
26 * HTTP request maps are arrays that use the following format:
27 * - method : GET/HEAD/PUT/POST/DELETE
28 * - url : HTTP/HTTPS URL
29 * - query : <query parameter field/value associative array> (uses RFC 3986)
30 * - headers : <header name/value associative array>
31 * - body : source to get the HTTP request body from;
32 * this can simply be a string (always), a resource for
33 * PUT requests, and a field/value array for POST request;
34 * array bodies are encoded as multipart/form-data and strings
35 * use application/x-www-form-urlencoded (headers sent automatically)
36 * - stream : resource to stream the HTTP response body to
37 * - proxy : HTTP proxy to use
38 * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.
39 *
40 * @author Aaron Schulz
41 * @since 1.23
42 */
43 class MultiHttpClient {
44 /** @var resource */
45 protected $multiHandle = null; // curl_multi handle
46 /** @var string|null SSL certificates path */
47 protected $caBundlePath;
48 /** @var integer */
49 protected $connTimeout = 10;
50 /** @var integer */
51 protected $reqTimeout = 300;
52 /** @var bool */
53 protected $usePipelining = false;
54 /** @var integer */
55 protected $maxConnsPerHost = 50;
56 /** @var string|null proxy */
57 protected $proxy;
58 /** @var string */
59 protected $userAgent = 'wikimedia/multi-http-client v1.0';
60
61 /**
62 * @param array $options
63 * - connTimeout : default connection timeout (seconds)
64 * - reqTimeout : default request timeout (seconds)
65 * - proxy : HTTP proxy to use
66 * - usePipelining : whether to use HTTP pipelining if possible (for all hosts)
67 * - maxConnsPerHost : maximum number of concurrent connections (per host)
68 * - userAgent : The User-Agent header value to send
69 * @throws Exception
70 */
71 public function __construct( array $options ) {
72 if ( isset( $options['caBundlePath'] ) ) {
73 $this->caBundlePath = $options['caBundlePath'];
74 if ( !file_exists( $this->caBundlePath ) ) {
75 throw new Exception( "Cannot find CA bundle: " . $this->caBundlePath );
76 }
77 }
78 static $opts = [
79 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost', 'proxy', 'userAgent'
80 ];
81 foreach ( $opts as $key ) {
82 if ( isset( $options[$key] ) ) {
83 $this->$key = $options[$key];
84 }
85 }
86 }
87
88 /**
89 * Execute an HTTP(S) request
90 *
91 * This method returns a response map of:
92 * - code : HTTP response code or 0 if there was a serious cURL error
93 * - reason : HTTP response reason (empty if there was a serious cURL error)
94 * - headers : <header name/value associative array>
95 * - body : HTTP response body or resource (if "stream" was set)
96 * - error : Any cURL error string
97 * The map also stores integer-indexed copies of these values. This lets callers do:
98 * @code
99 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $http->run( $req );
100 * @endcode
101 * @param array $req HTTP request array
102 * @param array $opts
103 * - connTimeout : connection timeout per request (seconds)
104 * - reqTimeout : post-connection timeout per request (seconds)
105 * @return array Response array for request
106 */
107 final public function run( array $req, array $opts = [] ) {
108 return $this->runMulti( [ $req ], $opts )[0]['response'];
109 }
110
111 /**
112 * Execute a set of HTTP(S) requests concurrently
113 *
114 * The maps are returned by this method with the 'response' field set to a map of:
115 * - code : HTTP response code or 0 if there was a serious cURL error
116 * - reason : HTTP response reason (empty if there was a serious cURL error)
117 * - headers : <header name/value associative array>
118 * - body : HTTP response body or resource (if "stream" was set)
119 * - error : Any cURL error string
120 * The map also stores integer-indexed copies of these values. This lets callers do:
121 * @code
122 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $req['response'];
123 * @endcode
124 * All headers in the 'headers' field are normalized to use lower case names.
125 * This is true for the request headers and the response headers. Integer-indexed
126 * method/URL entries will also be changed to use the corresponding string keys.
127 *
128 * @param array $reqs Map of HTTP request arrays
129 * @param array $opts
130 * - connTimeout : connection timeout per request (seconds)
131 * - reqTimeout : post-connection timeout per request (seconds)
132 * - usePipelining : whether to use HTTP pipelining if possible
133 * - maxConnsPerHost : maximum number of concurrent connections (per host)
134 * @return array $reqs With response array populated for each
135 * @throws Exception
136 */
137 public function runMulti( array $reqs, array $opts = [] ) {
138 $chm = $this->getCurlMulti();
139
140 // Normalize $reqs and add all of the required cURL handles...
141 $handles = [];
142 foreach ( $reqs as $index => &$req ) {
143 $req['response'] = [
144 'code' => 0,
145 'reason' => '',
146 'headers' => [],
147 'body' => '',
148 'error' => ''
149 ];
150 if ( isset( $req[0] ) ) {
151 $req['method'] = $req[0]; // short-form
152 unset( $req[0] );
153 }
154 if ( isset( $req[1] ) ) {
155 $req['url'] = $req[1]; // short-form
156 unset( $req[1] );
157 }
158 if ( !isset( $req['method'] ) ) {
159 throw new Exception( "Request has no 'method' field set." );
160 } elseif ( !isset( $req['url'] ) ) {
161 throw new Exception( "Request has no 'url' field set." );
162 }
163 $req['query'] = isset( $req['query'] ) ? $req['query'] : [];
164 $headers = []; // normalized headers
165 if ( isset( $req['headers'] ) ) {
166 foreach ( $req['headers'] as $name => $value ) {
167 $headers[strtolower( $name )] = $value;
168 }
169 }
170 $req['headers'] = $headers;
171 if ( !isset( $req['body'] ) ) {
172 $req['body'] = '';
173 $req['headers']['content-length'] = 0;
174 }
175 $handles[$index] = $this->getCurlHandle( $req, $opts );
176 if ( count( $reqs ) > 1 ) {
177 // https://github.com/guzzle/guzzle/issues/349
178 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE, true );
179 }
180 }
181 unset( $req ); // don't assign over this by accident
182
183 $indexes = array_keys( $reqs );
184 if ( function_exists( 'curl_multi_setopt' ) ) { // PHP 5.5
185 if ( isset( $opts['usePipelining'] ) ) {
186 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (int)$opts['usePipelining'] );
187 }
188 if ( isset( $opts['maxConnsPerHost'] ) ) {
189 // Keep these sockets around as they may be needed later in the request
190 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (int)$opts['maxConnsPerHost'] );
191 }
192 }
193
194 // @TODO: use a per-host rolling handle window (e.g. CURLMOPT_MAX_HOST_CONNECTIONS)
195 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
196 $infos = [];
197
198 foreach ( $batches as $batch ) {
199 // Attach all cURL handles for this batch
200 foreach ( $batch as $index ) {
201 curl_multi_add_handle( $chm, $handles[$index] );
202 }
203 // Execute the cURL handles concurrently...
204 $active = null; // handles still being processed
205 do {
206 // Do any available work...
207 do {
208 $mrc = curl_multi_exec( $chm, $active );
209 $info = curl_multi_info_read( $chm );
210 if ( $info !== false ) {
211 $infos[(int)$info['handle']] = $info;
212 }
213 } while ( $mrc == CURLM_CALL_MULTI_PERFORM );
214 // Wait (if possible) for available work...
215 if ( $active > 0 && $mrc == CURLM_OK ) {
216 if ( curl_multi_select( $chm, 10 ) == -1 ) {
217 // PHP bug 63411; http://curl.haxx.se/libcurl/c/curl_multi_fdset.html
218 usleep( 5000 ); // 5ms
219 }
220 }
221 } while ( $active > 0 && $mrc == CURLM_OK );
222 }
223
224 // Remove all of the added cURL handles and check for errors...
225 foreach ( $reqs as $index => &$req ) {
226 $ch = $handles[$index];
227 curl_multi_remove_handle( $chm, $ch );
228
229 if ( isset( $infos[(int)$ch] ) ) {
230 $info = $infos[(int)$ch];
231 $errno = $info['result'];
232 if ( $errno !== 0 ) {
233 $req['response']['error'] = "(curl error: $errno)";
234 if ( function_exists( 'curl_strerror' ) ) {
235 $req['response']['error'] .= " " . curl_strerror( $errno );
236 }
237 }
238 } else {
239 $req['response']['error'] = "(curl error: no status set)";
240 }
241
242 // For convenience with the list() operator
243 $req['response'][0] = $req['response']['code'];
244 $req['response'][1] = $req['response']['reason'];
245 $req['response'][2] = $req['response']['headers'];
246 $req['response'][3] = $req['response']['body'];
247 $req['response'][4] = $req['response']['error'];
248 curl_close( $ch );
249 // Close any string wrapper file handles
250 if ( isset( $req['_closeHandle'] ) ) {
251 fclose( $req['_closeHandle'] );
252 unset( $req['_closeHandle'] );
253 }
254 }
255 unset( $req ); // don't assign over this by accident
256
257 // Restore the default settings
258 if ( function_exists( 'curl_multi_setopt' ) ) { // PHP 5.5
259 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (int)$this->usePipelining );
260 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
261 }
262
263 return $reqs;
264 }
265
266 /**
267 * @param array $req HTTP request map
268 * @param array $opts
269 * - connTimeout : default connection timeout
270 * - reqTimeout : default request timeout
271 * @return resource
272 * @throws Exception
273 */
274 protected function getCurlHandle( array &$req, array $opts = [] ) {
275 $ch = curl_init();
276
277 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT,
278 isset( $opts['connTimeout'] ) ? $opts['connTimeout'] : $this->connTimeout );
279 curl_setopt( $ch, CURLOPT_PROXY, isset( $req['proxy'] ) ? $req['proxy'] : $this->proxy );
280 curl_setopt( $ch, CURLOPT_TIMEOUT,
281 isset( $opts['reqTimeout'] ) ? $opts['reqTimeout'] : $this->reqTimeout );
282 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
283 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
284 curl_setopt( $ch, CURLOPT_HEADER, 0 );
285 if ( !is_null( $this->caBundlePath ) ) {
286 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
287 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
288 }
289 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
290
291 $url = $req['url'];
292 // PHP_QUERY_RFC3986 is PHP 5.4+ only
293 $query = str_replace(
294 [ '+', '%7E' ],
295 [ '%20', '~' ],
296 http_build_query( $req['query'], '', '&' )
297 );
298 if ( $query != '' ) {
299 $url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
300 }
301 curl_setopt( $ch, CURLOPT_URL, $url );
302
303 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req['method'] );
304 if ( $req['method'] === 'HEAD' ) {
305 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
306 }
307
308 if ( $req['method'] === 'PUT' ) {
309 curl_setopt( $ch, CURLOPT_PUT, 1 );
310 if ( is_resource( $req['body'] ) ) {
311 curl_setopt( $ch, CURLOPT_INFILE, $req['body'] );
312 if ( isset( $req['headers']['content-length'] ) ) {
313 curl_setopt( $ch, CURLOPT_INFILESIZE, $req['headers']['content-length'] );
314 } elseif ( isset( $req['headers']['transfer-encoding'] ) &&
315 $req['headers']['transfer-encoding'] === 'chunks'
316 ) {
317 curl_setopt( $ch, CURLOPT_UPLOAD, true );
318 } else {
319 throw new Exception( "Missing 'Content-Length' or 'Transfer-Encoding' header." );
320 }
321 } elseif ( $req['body'] !== '' ) {
322 $fp = fopen( "php://temp", "wb+" );
323 fwrite( $fp, $req['body'], strlen( $req['body'] ) );
324 rewind( $fp );
325 curl_setopt( $ch, CURLOPT_INFILE, $fp );
326 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req['body'] ) );
327 $req['_closeHandle'] = $fp; // remember to close this later
328 } else {
329 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
330 }
331 curl_setopt( $ch, CURLOPT_READFUNCTION,
332 function ( $ch, $fd, $length ) {
333 $data = fread( $fd, $length );
334 $len = strlen( $data );
335 return $data;
336 }
337 );
338 } elseif ( $req['method'] === 'POST' ) {
339 curl_setopt( $ch, CURLOPT_POST, 1 );
340 // Don't interpret POST parameters starting with '@' as file uploads, because this
341 // makes it impossible to POST plain values starting with '@' (and causes security
342 // issues potentially exposing the contents of local files).
343 // The PHP manual says this option was introduced in PHP 5.5 defaults to true in PHP 5.6,
344 // but we support lower versions, and the option doesn't exist in HHVM 5.6.99.
345 if ( defined( 'CURLOPT_SAFE_UPLOAD' ) ) {
346 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD, true );
347 } elseif ( is_array( $req['body'] ) ) {
348 // In PHP 5.2 and later, '@' is interpreted as a file upload if POSTFIELDS
349 // is an array, but not if it's a string. So convert $req['body'] to a string
350 // for safety.
351 $req['body'] = wfArrayToCgi( $req['body'] );
352 }
353 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req['body'] );
354 } else {
355 if ( is_resource( $req['body'] ) || $req['body'] !== '' ) {
356 throw new Exception( "HTTP body specified for a non PUT/POST request." );
357 }
358 $req['headers']['content-length'] = 0;
359 }
360
361 if ( !isset( $req['headers']['user-agent'] ) ) {
362 $req['headers']['user-agent'] = $this->userAgent;
363 }
364
365 $headers = [];
366 foreach ( $req['headers'] as $name => $value ) {
367 if ( strpos( $name, ': ' ) ) {
368 throw new Exception( "Headers cannot have ':' in the name." );
369 }
370 $headers[] = $name . ': ' . trim( $value );
371 }
372 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
373
374 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
375 function ( $ch, $header ) use ( &$req ) {
376 $length = strlen( $header );
377 $matches = [];
378 if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {
379 $req['response']['code'] = (int)$matches[2];
380 $req['response']['reason'] = trim( $matches[3] );
381 return $length;
382 }
383 if ( strpos( $header, ":" ) === false ) {
384 return $length;
385 }
386 list( $name, $value ) = explode( ":", $header, 2 );
387 $req['response']['headers'][strtolower( $name )] = trim( $value );
388 return $length;
389 }
390 );
391
392 if ( isset( $req['stream'] ) ) {
393 // Don't just use CURLOPT_FILE as that might give:
394 // curl_setopt(): cannot represent a stream of type Output as a STDIO FILE*
395 // The callback here handles both normal files and php://temp handles.
396 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
397 function ( $ch, $data ) use ( &$req ) {
398 return fwrite( $req['stream'], $data );
399 }
400 );
401 } else {
402 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
403 function ( $ch, $data ) use ( &$req ) {
404 $req['response']['body'] .= $data;
405 return strlen( $data );
406 }
407 );
408 }
409
410 return $ch;
411 }
412
413 /**
414 * @return resource
415 */
416 protected function getCurlMulti() {
417 if ( !$this->multiHandle ) {
418 $cmh = curl_multi_init();
419 if ( function_exists( 'curl_multi_setopt' ) ) { // PHP 5.5
420 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (int)$this->usePipelining );
421 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
422 }
423 $this->multiHandle = $cmh;
424 }
425 return $this->multiHandle;
426 }
427
428 function __destruct() {
429 if ( $this->multiHandle ) {
430 curl_multi_close( $this->multiHandle );
431 }
432 }
433 }