24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
75 protected $userAgent =
'wikimedia/multi-http-client v1.0';
103 if ( isset( $options[
'caBundlePath'] ) ) {
104 $this->caBundlePath = $options[
'caBundlePath'];
105 if ( !file_exists( $this->caBundlePath ) ) {
106 throw new Exception(
"Cannot find CA bundle: " . $this->caBundlePath );
110 'connTimeout',
'maxConnTimeout',
'reqTimeout',
'maxReqTimeout',
111 'usePipelining',
'maxConnsPerHost',
'proxy',
'userAgent',
'logger'
113 foreach ( $opts as $key ) {
114 if ( isset( $options[$key] ) ) {
115 $this->$key = $options[$key];
118 if ( $this->logger ===
null ) {
119 $this->logger =
new NullLogger;
144 public function run( array $req, array $opts = [] ) {
145 return $this->
runMulti( [ $req ], $opts )[0][
'response'];
177 public function runMulti( array $reqs, array $opts = [] ) {
181 if ( $opts[
'connTimeout'] > $this->maxConnTimeout ) {
184 if ( $opts[
'reqTimeout'] > $this->maxReqTimeout ) {
203 return extension_loaded(
'curl' ) && function_exists(
'curl_multi_init' );
231 foreach ( $reqs as $index => &$req ) {
233 curl_multi_add_handle( $chm, $handles[$index] );
243 $mrc = curl_multi_exec( $chm, $active );
244 $info = curl_multi_info_read( $chm );
245 if ( $info !==
false ) {
246 $infos[(int)$info[
'handle']] = $info;
248 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
250 if ( $active > 0 && $mrc == CURLM_OK && curl_multi_select( $chm, $selectTimeout ) == -1 ) {
254 }
while ( $active > 0 && $mrc == CURLM_OK );
257 foreach ( $reqs as $index => &$req ) {
258 $ch = $handles[$index];
259 curl_multi_remove_handle( $chm, $ch );
261 if ( isset( $infos[(
int)$ch] ) ) {
262 $info = $infos[(int)$ch];
263 $errno = $info[
'result'];
264 if ( $errno !== 0 ) {
265 $req[
'response'][
'error'] =
"(curl error: $errno)";
266 if ( function_exists(
'curl_strerror' ) ) {
267 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
269 $this->logger->warning(
"Error fetching URL \"{$req['url']}\": " .
270 $req[
'response'][
'error'] );
273 $req[
'response'][
'error'] =
"(curl error: no status set)";
277 $req[
'response'][0] = $req[
'response'][
'code'];
278 $req[
'response'][1] = $req[
'response'][
'reason'];
279 $req[
'response'][2] = $req[
'response'][
'headers'];
280 $req[
'response'][3] = $req[
'response'][
'body'];
281 $req[
'response'][4] = $req[
'response'][
'error'];
284 if ( isset( $req[
'_closeHandle'] ) ) {
285 fclose( $req[
'_closeHandle'] );
286 unset( $req[
'_closeHandle'] );
308 curl_setopt( $ch, CURLOPT_PROXY, $req[
'proxy'] ?? $this->proxy );
309 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS, intval( $opts[
'connTimeout'] * 1e3 ) );
310 curl_setopt( $ch, CURLOPT_TIMEOUT_MS, intval( $opts[
'reqTimeout'] * 1e3 ) );
311 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
312 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
313 curl_setopt( $ch, CURLOPT_HEADER, 0 );
314 if ( $this->caBundlePath !==
null ) {
315 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
316 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
318 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
321 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
322 if ( $query !=
'' ) {
323 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
325 curl_setopt( $ch, CURLOPT_URL, $url );
326 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req[
'method'] );
327 curl_setopt( $ch, CURLOPT_NOBODY, ( $req[
'method'] ===
'HEAD' ) );
329 if ( $req[
'method'] ===
'PUT' ) {
330 curl_setopt( $ch, CURLOPT_PUT, 1 );
331 if ( is_resource( $req[
'body'] ) ) {
332 curl_setopt( $ch, CURLOPT_INFILE, $req[
'body'] );
333 if ( isset( $req[
'headers'][
'content-length'] ) ) {
334 curl_setopt( $ch, CURLOPT_INFILESIZE, $req[
'headers'][
'content-length'] );
335 } elseif ( isset( $req[
'headers'][
'transfer-encoding'] ) &&
336 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
338 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
340 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
342 } elseif ( $req[
'body'] !==
'' ) {
343 $fp = fopen(
"php://temp",
"wb+" );
344 fwrite( $fp, $req[
'body'], strlen( $req[
'body'] ) );
346 curl_setopt( $ch, CURLOPT_INFILE, $fp );
347 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req[
'body'] ) );
348 $req[
'_closeHandle'] = $fp;
350 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
352 curl_setopt( $ch, CURLOPT_READFUNCTION,
353 function ( $ch, $fd, $length ) {
354 return (
string)fread( $fd, $length );
357 } elseif ( $req[
'method'] ===
'POST' ) {
358 curl_setopt( $ch, CURLOPT_POST, 1 );
359 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req[
'body'] );
361 if ( is_resource( $req[
'body'] ) || $req[
'body'] !==
'' ) {
362 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
364 $req[
'headers'][
'content-length'] = 0;
367 if ( !isset( $req[
'headers'][
'user-agent'] ) ) {
372 foreach ( $req[
'headers'] as $name => $value ) {
373 if ( strpos( $name,
': ' ) ) {
374 throw new Exception(
"Headers cannot have ':' in the name." );
376 $headers[] = $name .
': ' . trim( $value );
378 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
380 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
381 function ( $ch,
$header ) use ( &$req ) {
382 if ( !empty( $req[
'flags'][
'relayResponseHeaders'] ) && trim(
$header ) !==
'' ) {
387 if ( preg_match(
"/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/",
$header,
$matches ) ) {
388 $req[
'response'][
'code'] = (int)
$matches[2];
389 $req[
'response'][
'reason'] = trim(
$matches[3] );
392 $req[
'response'][
'headers'] = [];
395 if ( strpos(
$header,
":" ) ===
false ) {
398 list( $name, $value ) = explode(
":",
$header, 2 );
399 $name = strtolower( $name );
400 $value = trim( $value );
401 if ( isset( $req[
'response'][
'headers'][$name] ) ) {
402 $req[
'response'][
'headers'][$name] .=
', ' . $value;
404 $req[
'response'][
'headers'][$name] = $value;
411 $hasOutputStream = isset( $req[
'stream'] );
412 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
413 function ( $ch, $data ) use ( &$req, $hasOutputStream ) {
414 if ( $hasOutputStream ) {
415 return fwrite( $req[
'stream'], $data );
418 $req[
'response'][
'body'] .= $data;
420 return strlen( $data );
435 $cmh = curl_multi_init();
438 curl_multi_setopt(
$cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
443 if ( version_compare( curl_version()[
'version'],
'7.30.0',
'>=' ) ) {
446 curl_multi_setopt( $this->cmh, CURLMOPT_MAX_HOST_CONNECTIONS, (
int)$maxHostConns );
451 curl_multi_setopt( $this->cmh, CURLMOPT_PIPELINING, $pipelining ? 3 : 0 );
480 foreach ( $reqs as &$req ) {
481 $reqOptions = $httpOptions + [
482 'method' => $req[
'method'],
485 'postData' => $req[
'body'],
489 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
490 if ( $query !=
'' ) {
491 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
494 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
495 $url, $reqOptions, __METHOD__ );
496 $sv = $httpRequest->execute()->getStatusValue();
498 $respHeaders = array_map(
500 return implode(
', ', $v );
502 $httpRequest->getResponseHeaders() );
505 'code' => $httpRequest->getStatus(),
507 'headers' => $respHeaders,
508 'body' => $httpRequest->getContent(),
512 if ( !$sv->isOK() ) {
513 $svErrors = $sv->getErrors();
514 if ( isset( $svErrors[0] ) ) {
515 $req[
'response'][
'error'] = $svErrors[0][
'message'];
518 if ( isset( $svErrors[0][
'params'][0] ) ) {
519 if ( is_numeric( $svErrors[0][
'params'][0] ) ) {
520 if ( isset( $svErrors[0][
'params'][1] ) ) {
522 $req[
'response'][
'reason'] = $svErrors[0][
'params'][1];
525 $req[
'response'][
'reason'] = $svErrors[0][
'params'][0];
531 $req[
'response'][0] = $req[
'response'][
'code'];
532 $req[
'response'][1] = $req[
'response'][
'reason'];
533 $req[
'response'][2] = $req[
'response'][
'headers'];
534 $req[
'response'][3] = $req[
'response'][
'body'];
535 $req[
'response'][4] = $req[
'response'][
'error'];
547 foreach ( $reqs as &$req ) {
555 if ( isset( $req[0] ) ) {
556 $req[
'method'] = $req[0];
559 if ( isset( $req[1] ) ) {
560 $req[
'url'] = $req[1];
563 if ( !isset( $req[
'method'] ) ) {
564 throw new Exception(
"Request has no 'method' field set." );
565 } elseif ( !isset( $req[
'url'] ) ) {
566 throw new Exception(
"Request has no 'url' field set." );
568 $this->logger->debug(
"{$req['method']}: {$req['url']}" );
569 $req[
'query'] = $req[
'query'] ?? [];
571 if ( isset( $req[
'headers'] ) ) {
572 foreach ( $req[
'headers'] as $name => $value ) {
573 $headers[strtolower( $name )] = $value;
576 $req[
'headers'] = $headers;
577 if ( !isset( $req[
'body'] ) ) {
579 $req[
'headers'][
'content-length'] = 0;
581 $req[
'flags'] = $req[
'flags'] ?? [];
595 if ( count( $timeouts ) === 0 ) {
601 if ( $selectTimeout < 10e-6 ) {
602 $selectTimeout = 10e-6;
604 return $selectTimeout;
618 curl_multi_close( $this->cmh );