Merge "build: Update wikimedia/ip-set from 2.0.1 to 2.1.0"
[lhc/web/wiklou.git] / includes / libs / http / 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 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerInterface;
25 use Psr\Log\NullLogger;
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Class to handle multiple HTTP requests
30 *
31 * If curl is available, requests will be made concurrently.
32 * Otherwise, they will be made serially.
33 *
34 * HTTP request maps are arrays that use the following format:
35 * - method : GET/HEAD/PUT/POST/DELETE
36 * - url : HTTP/HTTPS URL
37 * - query : <query parameter field/value associative array> (uses RFC 3986)
38 * - headers : <header name/value associative array>
39 * - body : source to get the HTTP request body from;
40 * this can simply be a string (always), a resource for
41 * PUT requests, and a field/value array for POST request;
42 * array bodies are encoded as multipart/form-data and strings
43 * use application/x-www-form-urlencoded (headers sent automatically)
44 * - stream : resource to stream the HTTP response body to
45 * - proxy : HTTP proxy to use
46 * - flags : map of boolean flags which supports:
47 * - relayResponseHeaders : write out header via header()
48 * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.
49 *
50 * @since 1.23
51 */
52 class MultiHttpClient implements LoggerAwareInterface {
53 /** @var resource */
54 protected $multiHandle = null; // curl_multi handle
55 /** @var string|null SSL certificates path */
56 protected $caBundlePath;
57 /** @var float */
58 protected $connTimeout = 10;
59 /** @var float */
60 protected $reqTimeout = 900;
61 /** @var bool */
62 protected $usePipelining = false;
63 /** @var int */
64 protected $maxConnsPerHost = 50;
65 /** @var string|null proxy */
66 protected $proxy;
67 /** @var string */
68 protected $userAgent = 'wikimedia/multi-http-client v1.0';
69 /** @var LoggerInterface */
70 protected $logger;
71
72 // In PHP 7 due to https://bugs.php.net/bug.php?id=76480 the request/connect
73 // timeouts are periodically polled instead of being accurately respected.
74 // The select timeout is set to the minimum timeout multiplied by this factor.
75 const TIMEOUT_ACCURACY_FACTOR = 0.1;
76
77 /**
78 * @param array $options
79 * - connTimeout : default connection timeout (seconds)
80 * - reqTimeout : default request timeout (seconds)
81 * - proxy : HTTP proxy to use
82 * - usePipelining : whether to use HTTP pipelining if possible (for all hosts)
83 * - maxConnsPerHost : maximum number of concurrent connections (per host)
84 * - userAgent : The User-Agent header value to send
85 * - logger : a \Psr\Log\LoggerInterface instance for debug logging
86 * - caBundlePath : path to specific Certificate Authority bundle (if any)
87 * @throws Exception
88 */
89 public function __construct( array $options ) {
90 if ( isset( $options['caBundlePath'] ) ) {
91 $this->caBundlePath = $options['caBundlePath'];
92 if ( !file_exists( $this->caBundlePath ) ) {
93 throw new Exception( "Cannot find CA bundle: " . $this->caBundlePath );
94 }
95 }
96 static $opts = [
97 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost',
98 'proxy', 'userAgent', 'logger'
99 ];
100 foreach ( $opts as $key ) {
101 if ( isset( $options[$key] ) ) {
102 $this->$key = $options[$key];
103 }
104 }
105 if ( $this->logger === null ) {
106 $this->logger = new NullLogger;
107 }
108 }
109
110 /**
111 * Execute an HTTP(S) request
112 *
113 * This method returns a response map of:
114 * - code : HTTP response code or 0 if there was a serious error
115 * - reason : HTTP response reason (empty if there was a serious error)
116 * - headers : <header name/value associative array>
117 * - body : HTTP response body or resource (if "stream" was set)
118 * - error : Any error string
119 * The map also stores integer-indexed copies of these values. This lets callers do:
120 * @code
121 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $http->run( $req );
122 * @endcode
123 * @param array $req HTTP request array
124 * @param array $opts
125 * - connTimeout : connection timeout per request (seconds)
126 * - reqTimeout : post-connection timeout per request (seconds)
127 * @return array Response array for request
128 */
129 public function run( array $req, array $opts = [] ) {
130 return $this->runMulti( [ $req ], $opts )[0]['response'];
131 }
132
133 /**
134 * Execute a set of HTTP(S) requests.
135 *
136 * If curl is available, requests will be made concurrently.
137 * Otherwise, they will be made serially.
138 *
139 * The maps are returned by this method with the 'response' field set to a map of:
140 * - code : HTTP response code or 0 if there was a serious error
141 * - reason : HTTP response reason (empty if there was a serious error)
142 * - headers : <header name/value associative array>
143 * - body : HTTP response body or resource (if "stream" was set)
144 * - error : Any error string
145 * The map also stores integer-indexed copies of these values. This lets callers do:
146 * @code
147 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $req['response'];
148 * @endcode
149 * All headers in the 'headers' field are normalized to use lower case names.
150 * This is true for the request headers and the response headers. Integer-indexed
151 * method/URL entries will also be changed to use the corresponding string keys.
152 *
153 * @param array[] $reqs Map of HTTP request arrays
154 * @param array $opts
155 * - connTimeout : connection timeout per request (seconds)
156 * - reqTimeout : post-connection timeout per request (seconds)
157 * - usePipelining : whether to use HTTP pipelining if possible
158 * - maxConnsPerHost : maximum number of concurrent connections (per host)
159 * @return array $reqs With response array populated for each
160 * @throws Exception
161 */
162 public function runMulti( array $reqs, array $opts = [] ) {
163 $this->normalizeRequests( $reqs );
164 if ( $this->isCurlEnabled() ) {
165 return $this->runMultiCurl( $reqs, $opts );
166 } else {
167 return $this->runMultiHttp( $reqs, $opts );
168 }
169 }
170
171 /**
172 * Determines if the curl extension is available
173 *
174 * @return bool true if curl is available, false otherwise.
175 */
176 protected function isCurlEnabled() {
177 return extension_loaded( 'curl' );
178 }
179
180 /**
181 * Execute a set of HTTP(S) requests concurrently
182 *
183 * @see MultiHttpClient::runMulti()
184 *
185 * @param array[] $reqs Map of HTTP request arrays
186 * @param array $opts
187 * - connTimeout : connection timeout per request (seconds)
188 * - reqTimeout : post-connection timeout per request (seconds)
189 * - usePipelining : whether to use HTTP pipelining if possible
190 * - maxConnsPerHost : maximum number of concurrent connections (per host)
191 * @codingStandardsIgnoreStart
192 * @phan-param array{connTimeout?:int,reqTimeout?:int,usePipelining?:bool,maxConnsPerHost?:int} $opts
193 * @codingStandardsIgnoreEnd
194 * @return array $reqs With response array populated for each
195 * @throws Exception
196 * @suppress PhanTypeInvalidDimOffset
197 */
198 private function runMultiCurl( array $reqs, array $opts = [] ) {
199 $chm = $this->getCurlMulti();
200
201 $selectTimeout = $this->getSelectTimeout( $opts );
202
203 // Add all of the required cURL handles...
204 $handles = [];
205 foreach ( $reqs as $index => &$req ) {
206 $handles[$index] = $this->getCurlHandle( $req, $opts );
207 if ( count( $reqs ) > 1 ) {
208 // https://github.com/guzzle/guzzle/issues/349
209 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE, true );
210 }
211 }
212 unset( $req ); // don't assign over this by accident
213
214 $indexes = array_keys( $reqs );
215 if ( isset( $opts['usePipelining'] ) ) {
216 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (int)$opts['usePipelining'] );
217 }
218 if ( isset( $opts['maxConnsPerHost'] ) ) {
219 // Keep these sockets around as they may be needed later in the request
220 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (int)$opts['maxConnsPerHost'] );
221 }
222
223 // @TODO: use a per-host rolling handle window (e.g. CURLMOPT_MAX_HOST_CONNECTIONS)
224 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
225 $infos = [];
226
227 foreach ( $batches as $batch ) {
228 // Attach all cURL handles for this batch
229 foreach ( $batch as $index ) {
230 curl_multi_add_handle( $chm, $handles[$index] );
231 }
232 // Execute the cURL handles concurrently...
233 $active = null; // handles still being processed
234 do {
235 // Do any available work...
236 do {
237 $mrc = curl_multi_exec( $chm, $active );
238 $info = curl_multi_info_read( $chm );
239 if ( $info !== false ) {
240 $infos[(int)$info['handle']] = $info;
241 }
242 } while ( $mrc == CURLM_CALL_MULTI_PERFORM );
243 // Wait (if possible) for available work...
244 if ( $active > 0 && $mrc == CURLM_OK && curl_multi_select( $chm, $selectTimeout ) == -1 ) {
245 // PHP bug 63411; https://curl.haxx.se/libcurl/c/curl_multi_fdset.html
246 usleep( 5000 ); // 5ms
247 }
248 } while ( $active > 0 && $mrc == CURLM_OK );
249 }
250
251 // Remove all of the added cURL handles and check for errors...
252 foreach ( $reqs as $index => &$req ) {
253 $ch = $handles[$index];
254 curl_multi_remove_handle( $chm, $ch );
255
256 if ( isset( $infos[(int)$ch] ) ) {
257 $info = $infos[(int)$ch];
258 $errno = $info['result'];
259 if ( $errno !== 0 ) {
260 $req['response']['error'] = "(curl error: $errno)";
261 if ( function_exists( 'curl_strerror' ) ) {
262 $req['response']['error'] .= " " . curl_strerror( $errno );
263 }
264 $this->logger->warning( "Error fetching URL \"{$req['url']}\": " .
265 $req['response']['error'] );
266 }
267 } else {
268 $req['response']['error'] = "(curl error: no status set)";
269 }
270
271 // For convenience with the list() operator
272 $req['response'][0] = $req['response']['code'];
273 $req['response'][1] = $req['response']['reason'];
274 $req['response'][2] = $req['response']['headers'];
275 $req['response'][3] = $req['response']['body'];
276 $req['response'][4] = $req['response']['error'];
277 curl_close( $ch );
278 // Close any string wrapper file handles
279 if ( isset( $req['_closeHandle'] ) ) {
280 fclose( $req['_closeHandle'] );
281 unset( $req['_closeHandle'] );
282 }
283 }
284 unset( $req ); // don't assign over this by accident
285
286 // Restore the default settings
287 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (int)$this->usePipelining );
288 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
289
290 return $reqs;
291 }
292
293 /**
294 * @param array &$req HTTP request map
295 * @param array $opts
296 * - connTimeout : default connection timeout
297 * - reqTimeout : default request timeout
298 * @return resource
299 * @throws Exception
300 * @suppress PhanTypeMismatchArgumentInternal
301 */
302 protected function getCurlHandle( array &$req, array $opts = [] ) {
303 $ch = curl_init();
304
305 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS,
306 ( $opts['connTimeout'] ?? $this->connTimeout ) * 1000 );
307 curl_setopt( $ch, CURLOPT_PROXY, $req['proxy'] ?? $this->proxy );
308 curl_setopt( $ch, CURLOPT_TIMEOUT_MS,
309 ( $opts['reqTimeout'] ?? $this->reqTimeout ) * 1000 );
310 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
311 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
312 curl_setopt( $ch, CURLOPT_HEADER, 0 );
313 if ( !is_null( $this->caBundlePath ) ) {
314 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
315 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
316 }
317 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
318
319 $url = $req['url'];
320 $query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
321 if ( $query != '' ) {
322 $url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
323 }
324 curl_setopt( $ch, CURLOPT_URL, $url );
325
326 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req['method'] );
327 if ( $req['method'] === 'HEAD' ) {
328 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
329 }
330
331 if ( $req['method'] === 'PUT' ) {
332 curl_setopt( $ch, CURLOPT_PUT, 1 );
333 if ( is_resource( $req['body'] ) ) {
334 curl_setopt( $ch, CURLOPT_INFILE, $req['body'] );
335 if ( isset( $req['headers']['content-length'] ) ) {
336 curl_setopt( $ch, CURLOPT_INFILESIZE, $req['headers']['content-length'] );
337 } elseif ( isset( $req['headers']['transfer-encoding'] ) &&
338 $req['headers']['transfer-encoding'] === 'chunks'
339 ) {
340 curl_setopt( $ch, CURLOPT_UPLOAD, true );
341 } else {
342 throw new Exception( "Missing 'Content-Length' or 'Transfer-Encoding' header." );
343 }
344 } elseif ( $req['body'] !== '' ) {
345 $fp = fopen( "php://temp", "wb+" );
346 fwrite( $fp, $req['body'], strlen( $req['body'] ) );
347 rewind( $fp );
348 curl_setopt( $ch, CURLOPT_INFILE, $fp );
349 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req['body'] ) );
350 $req['_closeHandle'] = $fp; // remember to close this later
351 } else {
352 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
353 }
354 curl_setopt( $ch, CURLOPT_READFUNCTION,
355 function ( $ch, $fd, $length ) {
356 return (string)fread( $fd, $length );
357 }
358 );
359 } elseif ( $req['method'] === 'POST' ) {
360 curl_setopt( $ch, CURLOPT_POST, 1 );
361 // Don't interpret POST parameters starting with '@' as file uploads, because this
362 // makes it impossible to POST plain values starting with '@' (and causes security
363 // issues potentially exposing the contents of local files).
364 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD, true );
365 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req['body'] );
366 } else {
367 if ( is_resource( $req['body'] ) || $req['body'] !== '' ) {
368 throw new Exception( "HTTP body specified for a non PUT/POST request." );
369 }
370 $req['headers']['content-length'] = 0;
371 }
372
373 if ( !isset( $req['headers']['user-agent'] ) ) {
374 $req['headers']['user-agent'] = $this->userAgent;
375 }
376
377 $headers = [];
378 foreach ( $req['headers'] as $name => $value ) {
379 if ( strpos( $name, ': ' ) ) {
380 throw new Exception( "Headers cannot have ':' in the name." );
381 }
382 $headers[] = $name . ': ' . trim( $value );
383 }
384 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
385
386 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
387 function ( $ch, $header ) use ( &$req ) {
388 if ( !empty( $req['flags']['relayResponseHeaders'] ) && trim( $header ) !== '' ) {
389 header( $header );
390 }
391 $length = strlen( $header );
392 $matches = [];
393 if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {
394 $req['response']['code'] = (int)$matches[2];
395 $req['response']['reason'] = trim( $matches[3] );
396 return $length;
397 }
398 if ( strpos( $header, ":" ) === false ) {
399 return $length;
400 }
401 list( $name, $value ) = explode( ":", $header, 2 );
402 $name = strtolower( $name );
403 $value = trim( $value );
404 if ( isset( $req['response']['headers'][$name] ) ) {
405 $req['response']['headers'][$name] .= ', ' . $value;
406 } else {
407 $req['response']['headers'][$name] = $value;
408 }
409 return $length;
410 }
411 );
412
413 if ( isset( $req['stream'] ) ) {
414 // Don't just use CURLOPT_FILE as that might give:
415 // curl_setopt(): cannot represent a stream of type Output as a STDIO FILE*
416 // The callback here handles both normal files and php://temp handles.
417 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
418 function ( $ch, $data ) use ( &$req ) {
419 return fwrite( $req['stream'], $data );
420 }
421 );
422 } else {
423 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
424 function ( $ch, $data ) use ( &$req ) {
425 $req['response']['body'] .= $data;
426 return strlen( $data );
427 }
428 );
429 }
430
431 return $ch;
432 }
433
434 /**
435 * @return resource
436 * @throws Exception
437 */
438 protected function getCurlMulti() {
439 if ( !$this->multiHandle ) {
440 if ( !function_exists( 'curl_multi_init' ) ) {
441 throw new Exception( "PHP cURL function curl_multi_init missing. " .
442 "Check https://www.mediawiki.org/wiki/Manual:CURL" );
443 }
444 $cmh = curl_multi_init();
445 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (int)$this->usePipelining );
446 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
447 $this->multiHandle = $cmh;
448 }
449 return $this->multiHandle;
450 }
451
452 /**
453 * Execute a set of HTTP(S) requests sequentially.
454 *
455 * @see MultiHttpClient::runMulti()
456 * @todo Remove dependency on MediaWikiServices: use a separate HTTP client
457 * library or copy code from PhpHttpRequest
458 * @param array $reqs Map of HTTP request arrays
459 * @param array $opts
460 * - connTimeout : connection timeout per request (seconds)
461 * - reqTimeout : post-connection timeout per request (seconds)
462 * @return array $reqs With response array populated for each
463 * @throws Exception
464 */
465 private function runMultiHttp( array $reqs, array $opts = [] ) {
466 $httpOptions = [
467 'timeout' => $opts['reqTimeout'] ?? $this->reqTimeout,
468 'connectTimeout' => $opts['connTimeout'] ?? $this->connTimeout,
469 'logger' => $this->logger,
470 'caInfo' => $this->caBundlePath,
471 ];
472 foreach ( $reqs as &$req ) {
473 $reqOptions = $httpOptions + [
474 'method' => $req['method'],
475 'proxy' => $req['proxy'] ?? $this->proxy,
476 'userAgent' => $req['headers']['user-agent'] ?? $this->userAgent,
477 'postData' => $req['body'],
478 ];
479
480 $url = $req['url'];
481 $query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
482 if ( $query != '' ) {
483 $url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
484 }
485
486 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
487 $url, $reqOptions );
488 $sv = $httpRequest->execute()->getStatusValue();
489
490 $respHeaders = array_map(
491 function ( $v ) {
492 return implode( ', ', $v );
493 },
494 $httpRequest->getResponseHeaders() );
495
496 $req['response'] = [
497 'code' => $httpRequest->getStatus(),
498 'reason' => '',
499 'headers' => $respHeaders,
500 'body' => $httpRequest->getContent(),
501 'error' => '',
502 ];
503
504 if ( !$sv->isOK() ) {
505 $svErrors = $sv->getErrors();
506 if ( isset( $svErrors[0] ) ) {
507 $req['response']['error'] = $svErrors[0]['message'];
508
509 // param values vary per failure type (ex. unknown host vs unknown page)
510 if ( isset( $svErrors[0]['params'][0] ) ) {
511 if ( is_numeric( $svErrors[0]['params'][0] ) ) {
512 if ( isset( $svErrors[0]['params'][1] ) ) {
513 // @phan-suppress-next-line PhanTypeInvalidDimOffset
514 $req['response']['reason'] = $svErrors[0]['params'][1];
515 }
516 } else {
517 $req['response']['reason'] = $svErrors[0]['params'][0];
518 }
519 }
520 }
521 }
522
523 $req['response'][0] = $req['response']['code'];
524 $req['response'][1] = $req['response']['reason'];
525 $req['response'][2] = $req['response']['headers'];
526 $req['response'][3] = $req['response']['body'];
527 $req['response'][4] = $req['response']['error'];
528 }
529
530 return $reqs;
531 }
532
533 /**
534 * Normalize request information
535 *
536 * @param array[] $reqs the requests to normalize
537 */
538 private function normalizeRequests( array &$reqs ) {
539 foreach ( $reqs as &$req ) {
540 $req['response'] = [
541 'code' => 0,
542 'reason' => '',
543 'headers' => [],
544 'body' => '',
545 'error' => ''
546 ];
547 if ( isset( $req[0] ) ) {
548 $req['method'] = $req[0]; // short-form
549 unset( $req[0] );
550 }
551 if ( isset( $req[1] ) ) {
552 $req['url'] = $req[1]; // short-form
553 unset( $req[1] );
554 }
555 if ( !isset( $req['method'] ) ) {
556 throw new Exception( "Request has no 'method' field set." );
557 } elseif ( !isset( $req['url'] ) ) {
558 throw new Exception( "Request has no 'url' field set." );
559 }
560 $this->logger->debug( "{$req['method']}: {$req['url']}" );
561 $req['query'] = $req['query'] ?? [];
562 $headers = []; // normalized headers
563 if ( isset( $req['headers'] ) ) {
564 foreach ( $req['headers'] as $name => $value ) {
565 $headers[strtolower( $name )] = $value;
566 }
567 }
568 $req['headers'] = $headers;
569 if ( !isset( $req['body'] ) ) {
570 $req['body'] = '';
571 $req['headers']['content-length'] = 0;
572 }
573 $req['flags'] = $req['flags'] ?? [];
574 }
575 }
576
577 /**
578 * Get a suitable select timeout for the given options.
579 *
580 * @param array $opts
581 * @return float
582 */
583 private function getSelectTimeout( $opts ) {
584 $connTimeout = $opts['connTimeout'] ?? $this->connTimeout;
585 $reqTimeout = $opts['reqTimeout'] ?? $this->reqTimeout;
586 $timeouts = array_filter( [ $connTimeout, $reqTimeout ] );
587 if ( count( $timeouts ) === 0 ) {
588 return 1;
589 }
590
591 $selectTimeout = min( $timeouts ) * self::TIMEOUT_ACCURACY_FACTOR;
592 // Minimum 10us for sanity
593 if ( $selectTimeout < 10e-6 ) {
594 $selectTimeout = 10e-6;
595 }
596 return $selectTimeout;
597 }
598
599 /**
600 * Register a logger
601 *
602 * @param LoggerInterface $logger
603 */
604 public function setLogger( LoggerInterface $logger ) {
605 $this->logger = $logger;
606 }
607
608 function __destruct() {
609 if ( $this->multiHandle ) {
610 curl_multi_close( $this->multiHandle );
611 }
612 }
613 }