MediaWiki master
MultiHttpClient.php
Go to the documentation of this file.
1<?php
23namespace Wikimedia\Http;
24
25use InvalidArgumentException;
27use Psr\Log\LoggerAwareInterface;
28use Psr\Log\LoggerInterface;
29use Psr\Log\NullLogger;
30
58class MultiHttpClient implements LoggerAwareInterface {
60 private const SENSITIVE_HEADERS = '/(^|-|_)(authorization|auth|password|cookie)($|-|_)/';
65 protected $cmh = null;
67 protected $caBundlePath;
69 protected $connTimeout = 10;
71 protected $maxConnTimeout = INF;
73 protected $reqTimeout = 30;
75 protected $maxReqTimeout = INF;
77 protected $usePipelining = false;
79 protected $maxConnsPerHost = 50;
81 protected $proxy;
83 protected $localProxy = false;
85 protected $localVirtualHosts = [];
87 protected $userAgent = 'wikimedia/multi-http-client v1.1';
89 protected $logger;
90 protected array $headers = [];
91
92 // In PHP 7 due to https://bugs.php.net/bug.php?id=76480 the request/connect
93 // timeouts are periodically polled instead of being accurately respected.
94 // The select timeout is set to the minimum timeout multiplied by this factor.
95 private const TIMEOUT_ACCURACY_FACTOR = 0.1;
96
97 private ?TelemetryHeadersInterface $telemetry = null;
98
121 public function __construct( array $options ) {
122 if ( isset( $options['caBundlePath'] ) ) {
123 $this->caBundlePath = $options['caBundlePath'];
124 if ( !file_exists( $this->caBundlePath ) ) {
125 throw new InvalidArgumentException( "Cannot find CA bundle: " . $this->caBundlePath );
126 }
127 }
128 static $opts = [
129 'connTimeout', 'maxConnTimeout', 'reqTimeout', 'maxReqTimeout',
130 'usePipelining', 'maxConnsPerHost', 'proxy', 'userAgent', 'logger',
131 'localProxy', 'localVirtualHosts', 'headers', 'telemetry'
132 ];
133 foreach ( $opts as $key ) {
134 if ( isset( $options[$key] ) ) {
135 $this->$key = $options[$key];
136 }
137 }
138 $this->logger ??= new NullLogger;
139 }
140
164 public function run( array $req, array $opts = [] ) {
165 return $this->runMulti( [ $req ], $opts )[0]['response'];
166 }
167
199 public function runMulti( array $reqs, array $opts = [] ) {
200 $this->normalizeRequests( $reqs );
201 $opts += [ 'connTimeout' => $this->connTimeout, 'reqTimeout' => $this->reqTimeout ];
202
203 if ( $this->maxConnTimeout && $opts['connTimeout'] > $this->maxConnTimeout ) {
204 $opts['connTimeout'] = $this->maxConnTimeout;
205 }
206 if ( $this->maxReqTimeout && $opts['reqTimeout'] > $this->maxReqTimeout ) {
207 $opts['reqTimeout'] = $this->maxReqTimeout;
208 }
209
210 if ( $this->isCurlEnabled() ) {
211 switch ( $opts['httpVersion'] ?? null ) {
212 case 'v1.0':
213 $opts['httpVersion'] = CURL_HTTP_VERSION_1_0;
214 break;
215 case 'v1.1':
216 $opts['httpVersion'] = CURL_HTTP_VERSION_1_1;
217 break;
218 case 'v2':
219 case 'v2.0':
220 $opts['httpVersion'] = CURL_HTTP_VERSION_2_0;
221 break;
222 default:
223 $opts['httpVersion'] = CURL_HTTP_VERSION_NONE;
224 }
225 return $this->runMultiCurl( $reqs, $opts );
226 } else {
227 # TODO: Add handling for httpVersion option
228 return $this->runMultiHttp( $reqs, $opts );
229 }
230 }
231
237 protected function isCurlEnabled() {
238 // Explicitly test if curl_multi* is blocked, as some users' hosts provide
239 // them with a modified curl with the multi-threaded parts removed(!)
240 return extension_loaded( 'curl' ) && function_exists( 'curl_multi_init' );
241 }
242
260 private function runMultiCurl( array $reqs, array $opts ) {
261 $chm = $this->getCurlMulti( $opts );
262
263 $selectTimeout = $this->getSelectTimeout( $opts );
264
265 // Add all of the required cURL handles...
266 $handles = [];
267 foreach ( $reqs as $index => &$req ) {
268 $handles[$index] = $this->getCurlHandle( $req, $opts );
269 curl_multi_add_handle( $chm, $handles[$index] );
270 }
271 unset( $req ); // don't assign over this by accident
272
273 $infos = [];
274 // Execute the cURL handles concurrently...
275 $active = null; // handles still being processed
276 do {
277 // Do any available work...
278 do {
279 $mrc = curl_multi_exec( $chm, $active );
280 $info = curl_multi_info_read( $chm );
281 if ( $info !== false ) {
282 // Note: cast to integer even works on PHP 8.0+ despite the
283 // handle being an object not a resource, because CurlHandle
284 // has a backwards-compatible cast_object handler.
285 $infos[(int)$info['handle']] = $info;
286 }
287 } while ( $mrc == CURLM_CALL_MULTI_PERFORM );
288 // Wait (if possible) for available work...
289 if ( $active > 0 && $mrc == CURLM_OK && curl_multi_select( $chm, $selectTimeout ) == -1 ) {
290 // PHP bug 63411; https://curl.haxx.se/libcurl/c/curl_multi_fdset.html
291 usleep( 5000 ); // 5ms
292 }
293 } while ( $active > 0 && $mrc == CURLM_OK );
294
295 // Remove all of the added cURL handles and check for errors...
296 foreach ( $reqs as $index => &$req ) {
297 $ch = $handles[$index];
298 curl_multi_remove_handle( $chm, $ch );
299
300 if ( isset( $infos[(int)$ch] ) ) {
301 $info = $infos[(int)$ch];
302 $errno = $info['result'];
303 if ( $errno !== 0 ) {
304 $req['response']['error'] = "(curl error: $errno)";
305 if ( function_exists( 'curl_strerror' ) ) {
306 $req['response']['error'] .= " " . curl_strerror( $errno );
307 }
308 $this->logger->warning( "Error fetching URL \"{$req['url']}\": " .
309 $req['response']['error'] );
310 } else {
311 $this->logger->debug(
312 "HTTP complete: {method} {url} code={response_code} size={size} " .
313 "total={total_time} connect={connect_time}",
314 [
315 'method' => $req['method'],
316 'url' => $req['url'],
317 'response_code' => $req['response']['code'],
318 'size' => curl_getinfo( $ch, CURLINFO_SIZE_DOWNLOAD ),
319 'total_time' => $this->getCurlTime(
320 $ch, CURLINFO_TOTAL_TIME, 'CURLINFO_TOTAL_TIME_T'
321 ),
322 'connect_time' => $this->getCurlTime(
323 $ch, CURLINFO_CONNECT_TIME, 'CURLINFO_CONNECT_TIME_T'
324 ),
325 ]
326 );
327 }
328 } else {
329 $req['response']['error'] = "(curl error: no status set)";
330 }
331
332 // For convenience with array destructuring
333 $req['response'][0] = $req['response']['code'];
334 $req['response'][1] = $req['response']['reason'];
335 $req['response'][2] = $req['response']['headers'];
336 $req['response'][3] = $req['response']['body'];
337 $req['response'][4] = $req['response']['error'];
338 curl_close( $ch );
339 // Close any string wrapper file handles
340 if ( isset( $req['_closeHandle'] ) ) {
341 fclose( $req['_closeHandle'] );
342 unset( $req['_closeHandle'] );
343 }
344 }
345 unset( $req ); // don't assign over this by accident
346
347 return $reqs;
348 }
349
362 protected function getCurlHandle( array &$req, array $opts ) {
363 $ch = curl_init();
364
365 curl_setopt( $ch, CURLOPT_PROXY, $req['proxy'] ?? $this->proxy );
366 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS, intval( $opts['connTimeout'] * 1e3 ) );
367 curl_setopt( $ch, CURLOPT_TIMEOUT_MS, intval( $opts['reqTimeout'] * 1e3 ) );
368 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
369 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
370 curl_setopt( $ch, CURLOPT_HEADER, 0 );
371 if ( $this->caBundlePath !== null ) {
372 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
373 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
374 }
375 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
376
377 $url = $req['url'];
378 $query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
379 if ( $query != '' ) {
380 $url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
381 }
382 curl_setopt( $ch, CURLOPT_URL, $url );
383 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req['method'] );
384 curl_setopt( $ch, CURLOPT_NOBODY, ( $req['method'] === 'HEAD' ) );
385 curl_setopt( $ch, CURLOPT_HTTP_VERSION, $opts['httpVersion'] ?? CURL_HTTP_VERSION_NONE );
386
387 if ( $req['method'] === 'PUT' ) {
388 curl_setopt( $ch, CURLOPT_PUT, 1 );
389 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
390 if ( is_resource( $req['body'] ) ) {
391 curl_setopt( $ch, CURLOPT_INFILE, $req['body'] );
392 if ( isset( $req['headers']['content-length'] ) ) {
393 curl_setopt( $ch, CURLOPT_INFILESIZE, $req['headers']['content-length'] );
394 } elseif ( isset( $req['headers']['transfer-encoding'] ) &&
395 $req['headers']['transfer-encoding'] === 'chunks'
396 ) {
397 curl_setopt( $ch, CURLOPT_UPLOAD, true );
398 } else {
399 throw new InvalidArgumentException( "Missing 'Content-Length' or 'Transfer-Encoding' header." );
400 }
401 } elseif ( $req['body'] !== '' ) {
402 $fp = fopen( "php://temp", "wb+" );
403 fwrite( $fp, $req['body'], strlen( $req['body'] ) );
404 rewind( $fp );
405 curl_setopt( $ch, CURLOPT_INFILE, $fp );
406 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req['body'] ) );
407 $req['_closeHandle'] = $fp; // remember to close this later
408 } else {
409 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
410 }
411 curl_setopt( $ch, CURLOPT_READFUNCTION,
412 static function ( $ch, $fd, $length ) {
413 return (string)fread( $fd, $length );
414 }
415 );
416 } elseif ( $req['method'] === 'POST' ) {
417 curl_setopt( $ch, CURLOPT_POST, 1 );
418 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req['body'] );
419 } else {
420 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
421 if ( is_resource( $req['body'] ) || $req['body'] !== '' ) {
422 throw new InvalidArgumentException( "HTTP body specified for a non PUT/POST request." );
423 }
424 $req['headers']['content-length'] = 0;
425 }
426
427 if ( !isset( $req['headers']['user-agent'] ) ) {
428 $req['headers']['user-agent'] = $this->userAgent;
429 }
430
431 $headers = [];
432 foreach ( $req['headers'] as $name => $value ) {
433 if ( strpos( $name, ':' ) !== false ) {
434 throw new InvalidArgumentException( "Header name must not contain colon-space." );
435 }
436 $headers[] = $name . ': ' . trim( $value );
437 }
438 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
439
440 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
441 static function ( $ch, $header ) use ( &$req ) {
442 if ( !empty( $req['flags']['relayResponseHeaders'] ) && trim( $header ) !== '' ) {
443 header( $header );
444 }
445 $length = strlen( $header );
446 $matches = [];
447 if ( preg_match( "/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/", $header, $matches ) ) {
448 $req['response']['code'] = (int)$matches[2];
449 $req['response']['reason'] = trim( $matches[3] );
450 // After a redirect we will receive this again, but we already stored headers
451 // that belonged to a redirect response. Start over.
452 $req['response']['headers'] = [];
453 return $length;
454 }
455 if ( strpos( $header, ":" ) === false ) {
456 return $length;
457 }
458 [ $name, $value ] = explode( ":", $header, 2 );
459 $name = strtolower( $name );
460 $value = trim( $value );
461 if ( isset( $req['response']['headers'][$name] ) ) {
462 $req['response']['headers'][$name] .= ', ' . $value;
463 } else {
464 $req['response']['headers'][$name] = $value;
465 }
466 return $length;
467 }
468 );
469
470 // This works with both file and php://temp handles (unlike CURLOPT_FILE)
471 $hasOutputStream = isset( $req['stream'] );
472 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
473 static function ( $ch, $data ) use ( &$req, $hasOutputStream ) {
474 if ( $hasOutputStream ) {
475 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
476 return fwrite( $req['stream'], $data );
477 } else {
478 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
479 $req['response']['body'] .= $data;
480
481 return strlen( $data );
482 }
483 }
484 );
485
486 return $ch;
487 }
488
495 protected function getCurlMulti( array $opts ) {
496 if ( !$this->cmh ) {
497 $cmh = curl_multi_init();
498 // Limit the size of the idle connection cache such that consecutive parallel
499 // request batches to the same host can avoid having to keep making connections
500 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
501 $this->cmh = $cmh;
502 }
503
504 $curlVersion = curl_version()['version'];
505
506 // CURLMOPT_MAX_HOST_CONNECTIONS is available since PHP 7.0.7 and cURL 7.30.0
507 if ( version_compare( $curlVersion, '7.30.0', '>=' ) ) {
508 // Limit the number of in-flight requests for any given host
509 $maxHostConns = $opts['maxConnsPerHost'] ?? $this->maxConnsPerHost;
510 curl_multi_setopt( $this->cmh, CURLMOPT_MAX_HOST_CONNECTIONS, (int)$maxHostConns );
511 }
512
513 if ( $opts['usePipelining'] ?? $this->usePipelining ) {
514 if ( version_compare( $curlVersion, '7.43', '<' ) ) {
515 // The option is a boolean
516 $pipelining = 1;
517 } elseif ( version_compare( $curlVersion, '7.62', '<' ) ) {
518 // The option is a bitfield and HTTP/1.x pipelining is supported
519 $pipelining = CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX;
520 } else {
521 // The option is a bitfield but HTTP/1.x pipelining has been removed
522 $pipelining = CURLPIPE_MULTIPLEX;
523 }
524 // Suppress deprecation, we know already (T264735)
525 // phpcs:ignore Generic.PHP.NoSilencedErrors
526 @curl_multi_setopt( $this->cmh, CURLMOPT_PIPELINING, $pipelining );
527 }
528
529 return $this->cmh;
530 }
531
542 private function getCurlTime( $ch, $oldOption, $newConstName ): string {
543 if ( defined( $newConstName ) ) {
544 return sprintf( "%.6F", curl_getinfo( $ch, constant( $newConstName ) ) / 1e6 );
545 } else {
546 return (string)curl_getinfo( $ch, $oldOption );
547 }
548 }
549
565 private function runMultiHttp( array $reqs, array $opts = [] ) {
566 $httpOptions = [
567 'timeout' => $opts['reqTimeout'] ?? $this->reqTimeout,
568 'connectTimeout' => $opts['connTimeout'] ?? $this->connTimeout,
569 'logger' => $this->logger,
570 'caInfo' => $this->caBundlePath,
571 ];
572 foreach ( $reqs as &$req ) {
573 $reqOptions = $httpOptions + [
574 'method' => $req['method'],
575 'proxy' => $req['proxy'] ?? $this->proxy,
576 'userAgent' => $req['headers']['user-agent'] ?? $this->userAgent,
577 'postData' => $req['body'],
578 ];
579
580 $url = $req['url'];
581 $query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
582 if ( $query != '' ) {
583 $url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
584 }
585
586 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
587 $url, $reqOptions, __METHOD__ );
588 $httpRequest->setLogger( $this->logger );
589 foreach ( $req['headers'] as $header => $value ) {
590 $httpRequest->setHeader( $header, $value );
591 }
592 $sv = $httpRequest->execute()->getStatusValue();
593
594 $respHeaders = array_map(
595 static function ( $v ) {
596 return implode( ', ', $v );
597 },
598 $httpRequest->getResponseHeaders() );
599
600 $req['response'] = [
601 'code' => $httpRequest->getStatus(),
602 'reason' => '',
603 'headers' => $respHeaders,
604 'body' => $httpRequest->getContent(),
605 'error' => '',
606 ];
607
608 if ( !$sv->isOK() ) {
609 $svErrors = $sv->getErrors();
610 if ( isset( $svErrors[0] ) ) {
611 $req['response']['error'] = $svErrors[0]['message'];
612
613 // param values vary per failure type (ex. unknown host vs unknown page)
614 if ( isset( $svErrors[0]['params'][0] ) ) {
615 if ( is_numeric( $svErrors[0]['params'][0] ) ) {
616 if ( isset( $svErrors[0]['params'][1] ) ) {
617 // @phan-suppress-next-line PhanTypeInvalidDimOffset
618 $req['response']['reason'] = $svErrors[0]['params'][1];
619 }
620 } else {
621 $req['response']['reason'] = $svErrors[0]['params'][0];
622 }
623 }
624 }
625 }
626
627 $req['response'][0] = $req['response']['code'];
628 $req['response'][1] = $req['response']['reason'];
629 $req['response'][2] = $req['response']['headers'];
630 $req['response'][3] = $req['response']['body'];
631 $req['response'][4] = $req['response']['error'];
632 }
633
634 return $reqs;
635 }
636
642 private function normalizeHeaders( array $headers ): array {
643 $normalized = [];
644 foreach ( $headers as $name => $value ) {
645 $normalized[strtolower( $name )] = $value;
646 }
647 return $normalized;
648 }
649
655 private function normalizeRequests( array &$reqs ) {
656 foreach ( $reqs as &$req ) {
657 $req['response'] = [
658 'code' => 0,
659 'reason' => '',
660 'headers' => [],
661 'body' => '',
662 'error' => ''
663 ];
664 if ( isset( $req[0] ) ) {
665 $req['method'] = $req[0]; // short-form
666 unset( $req[0] );
667 }
668 if ( isset( $req[1] ) ) {
669 $req['url'] = $req[1]; // short-form
670 unset( $req[1] );
671 }
672 if ( !isset( $req['method'] ) ) {
673 throw new InvalidArgumentException( "Request has no 'method' field set." );
674 } elseif ( !isset( $req['url'] ) ) {
675 throw new InvalidArgumentException( "Request has no 'url' field set." );
676 }
677 if ( $this->localProxy !== false && $this->isLocalURL( $req['url'] ) ) {
678 $this->useReverseProxy( $req, $this->localProxy );
679 }
680 $req['query'] ??= [];
681 $req['headers'] = $this->normalizeHeaders(
682 array_merge(
683 $this->headers,
684 $this->telemetry ? $this->telemetry->getRequestHeaders() : [],
685 $req['headers'] ?? []
686 )
687 );
688
689 if ( !isset( $req['body'] ) ) {
690 $req['body'] = '';
691 $req['headers']['content-length'] = 0;
692 }
693 // Redact some headers we know to have tokens before logging them
694 $logHeaders = $req['headers'];
695 foreach ( $logHeaders as $header => $value ) {
696 if ( preg_match( self::SENSITIVE_HEADERS, $header ) === 1 ) {
697 $logHeaders[$header] = '[redacted]';
698 }
699 }
700 $this->logger->debug( "HTTP start: {method} {url}",
701 [
702 'method' => $req['method'],
703 'url' => $req['url'],
704 'headers' => $logHeaders,
705 ]
706 );
707 $req['flags'] ??= [];
708 }
709 }
710
711 private function useReverseProxy( array &$req, $proxy ) {
712 $parsedProxy = parse_url( $proxy );
713 if ( $parsedProxy === false ) {
714 throw new InvalidArgumentException( "Invalid reverseProxy configured: $proxy" );
715 }
716 $parsedUrl = parse_url( $req['url'] );
717 if ( $parsedUrl === false ) {
718 throw new InvalidArgumentException( "Invalid url specified: {$req['url']}" );
719 }
720 // Set the current host in the Host header
721 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
722 $req['headers']['Host'] = $parsedUrl['host'];
723 // Replace scheme, host and port in the request
724 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
725 $parsedUrl['scheme'] = $parsedProxy['scheme'];
726 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
727 $parsedUrl['host'] = $parsedProxy['host'];
728 if ( isset( $parsedProxy['port'] ) ) {
729 $parsedUrl['port'] = $parsedProxy['port'];
730 } else {
731 unset( $parsedUrl['port'] );
732 }
733 $req['url'] = self::assembleUrl( $parsedUrl );
734 // Explicitly disable use of another proxy by setting to false,
735 // since null will fallback to $this->proxy
736 $req['proxy'] = false;
737 }
738
749 private static function assembleUrl( array $urlParts ): string {
750 $result = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] . '://' : '';
751
752 if ( isset( $urlParts['host'] ) ) {
753 if ( isset( $urlParts['user'] ) ) {
754 $result .= $urlParts['user'];
755 if ( isset( $urlParts['pass'] ) ) {
756 $result .= ':' . $urlParts['pass'];
757 }
758 $result .= '@';
759 }
760
761 $result .= $urlParts['host'];
762
763 if ( isset( $urlParts['port'] ) ) {
764 $result .= ':' . $urlParts['port'];
765 }
766 }
767
768 if ( isset( $urlParts['path'] ) ) {
769 $result .= $urlParts['path'];
770 }
771
772 if ( isset( $urlParts['query'] ) && $urlParts['query'] !== '' ) {
773 $result .= '?' . $urlParts['query'];
774 }
775
776 if ( isset( $urlParts['fragment'] ) ) {
777 $result .= '#' . $urlParts['fragment'];
778 }
779
780 return $result;
781 }
782
790 private function isLocalURL( $url ) {
791 if ( !$this->localVirtualHosts ) {
792 // Shortcut
793 return false;
794 }
795
796 // Extract host part
797 $matches = [];
798 if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
799 $host = $matches[1];
800 // Split up dotwise
801 $domainParts = explode( '.', $host );
802 // Check if this domain or any superdomain is listed as a local virtual host
803 $domainParts = array_reverse( $domainParts );
804
805 $domain = '';
806 $countParts = count( $domainParts );
807 for ( $i = 0; $i < $countParts; $i++ ) {
808 $domainPart = $domainParts[$i];
809 if ( $i == 0 ) {
810 $domain = $domainPart;
811 } else {
812 $domain = $domainPart . '.' . $domain;
813 }
814
815 if ( in_array( $domain, $this->localVirtualHosts ) ) {
816 return true;
817 }
818 }
819 }
820
821 return false;
822 }
823
830 private function getSelectTimeout( $opts ) {
831 $connTimeout = $opts['connTimeout'] ?? $this->connTimeout;
832 $reqTimeout = $opts['reqTimeout'] ?? $this->reqTimeout;
833 $timeouts = array_filter( [ $connTimeout, $reqTimeout ] );
834 if ( count( $timeouts ) === 0 ) {
835 return 1;
836 }
837
838 $selectTimeout = min( $timeouts ) * self::TIMEOUT_ACCURACY_FACTOR;
839 // Minimum 10us
840 if ( $selectTimeout < 10e-6 ) {
841 $selectTimeout = 10e-6;
842 }
843 return $selectTimeout;
844 }
845
849 public function setLogger( LoggerInterface $logger ) {
850 $this->logger = $logger;
851 }
852
853 public function __destruct() {
854 if ( $this->cmh ) {
855 curl_multi_close( $this->cmh );
856 $this->cmh = null;
857 }
858 }
859
860}
862class_alias( MultiHttpClient::class, 'MultiHttpClient' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Service locator for MediaWiki core services.
Class to handle multiple HTTP requests.
runMulti(array $reqs, array $opts=[])
Execute a set of HTTP(S) requests.
resource object null $cmh
@phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.ObjectTypeHintVar curl_multi_init() handle,...
__construct(array $options)
Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get a client object with ap...
run(array $req, array $opts=[])
Execute an HTTP(S) request.
string null $caBundlePath
SSL certificates path.
getCurlHandle(array &$req, array $opts)
setLogger(LoggerInterface $logger)
Register a logger.
isCurlEnabled()
Determines if the curl extension is available.
Provide Request Telemetry information.
Utility for parsing a HTTP Accept header value into a weight map.
$header