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 );
332 if ( is_resource( $req[
'body'] ) ) {
333 curl_setopt( $ch, CURLOPT_INFILE, $req[
'body'] );
334 if ( isset( $req[
'headers'][
'content-length'] ) ) {
335 curl_setopt( $ch, CURLOPT_INFILESIZE, $req[
'headers'][
'content-length'] );
336 } elseif ( isset( $req[
'headers'][
'transfer-encoding'] ) &&
337 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
339 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
341 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
343 } elseif ( $req[
'body'] !==
'' ) {
344 $fp = fopen(
"php://temp",
"wb+" );
345 fwrite( $fp, $req[
'body'], strlen( $req[
'body'] ) );
347 curl_setopt( $ch, CURLOPT_INFILE, $fp );
348 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req[
'body'] ) );
349 $req[
'_closeHandle'] = $fp;
351 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
353 curl_setopt( $ch, CURLOPT_READFUNCTION,
354 function ( $ch, $fd, $length ) {
355 return (
string)fread( $fd, $length );
358 } elseif ( $req[
'method'] ===
'POST' ) {
359 curl_setopt( $ch, CURLOPT_POST, 1 );
360 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req[
'body'] );
363 if ( is_resource( $req[
'body'] ) || $req[
'body'] !==
'' ) {
364 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
366 $req[
'headers'][
'content-length'] = 0;
369 if ( !isset( $req[
'headers'][
'user-agent'] ) ) {
370 $req[
'headers'][
'user-agent'] = $this->userAgent;
374 foreach ( $req[
'headers'] as $name => $value ) {
375 if ( strpos( $name,
': ' ) ) {
376 throw new Exception(
"Headers cannot have ':' in the name." );
378 $headers[] = $name .
': ' . trim( $value );
380 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
382 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
383 function ( $ch,
$header ) use ( &$req ) {
384 if ( !empty( $req[
'flags'][
'relayResponseHeaders'] ) && trim(
$header ) !==
'' ) {
389 if ( preg_match(
"/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/",
$header,
$matches ) ) {
390 $req[
'response'][
'code'] = (int)
$matches[2];
391 $req[
'response'][
'reason'] = trim(
$matches[3] );
394 $req[
'response'][
'headers'] = [];
397 if ( strpos(
$header,
":" ) ===
false ) {
400 list( $name, $value ) = explode(
":",
$header, 2 );
401 $name = strtolower( $name );
402 $value = trim( $value );
403 if ( isset( $req[
'response'][
'headers'][$name] ) ) {
404 $req[
'response'][
'headers'][$name] .=
', ' . $value;
406 $req[
'response'][
'headers'][$name] = $value;
413 $hasOutputStream = isset( $req[
'stream'] );
414 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
415 function ( $ch, $data ) use ( &$req, $hasOutputStream ) {
416 if ( $hasOutputStream ) {
417 return fwrite( $req[
'stream'], $data );
420 $req[
'response'][
'body'] .= $data;
422 return strlen( $data );
437 if ( !function_exists(
'curl_multi_init' ) ) {
438 throw new Exception(
"PHP cURL function curl_multi_init missing. " .
439 "Check https://www.mediawiki.org/wiki/Manual:CURL" );
441 $cmh = curl_multi_init();
444 curl_multi_setopt(
$cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
448 $curlVersion = curl_version()[
'version'];
451 if ( version_compare( $curlVersion,
'7.30.0',
'>=' ) ) {
453 $maxHostConns = $opts[
'maxConnsPerHost'] ?? $this->maxConnsPerHost;
454 curl_multi_setopt( $this->cmh, CURLMOPT_MAX_HOST_CONNECTIONS, (
int)$maxHostConns );
457 if ( $opts[
'usePipelining'] ?? $this->usePipelining ) {
458 if ( version_compare( $curlVersion,
'7.43',
'<' ) ) {
461 } elseif ( version_compare( $curlVersion,
'7.62',
'<' ) ) {
463 $pipelining = CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX;
466 $pipelining = CURLPIPE_MULTIPLEX;
470 @curl_multi_setopt( $this->cmh, CURLMOPT_PIPELINING, $pipelining );
495 'timeout' => $opts[
'reqTimeout'] ?? $this->reqTimeout,
496 'connectTimeout' => $opts[
'connTimeout'] ?? $this->connTimeout,
497 'logger' => $this->logger,
498 'caInfo' => $this->caBundlePath,
500 foreach ( $reqs as &$req ) {
501 $reqOptions = $httpOptions + [
502 'method' => $req[
'method'],
503 'proxy' => $req[
'proxy'] ?? $this->proxy,
504 'userAgent' => $req[
'headers'][
'user-agent'] ?? $this->userAgent,
505 'postData' => $req[
'body'],
509 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
510 if ( $query !=
'' ) {
511 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
514 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
515 $url, $reqOptions, __METHOD__ );
516 $sv = $httpRequest->execute()->getStatusValue();
518 $respHeaders = array_map(
520 return implode(
', ', $v );
522 $httpRequest->getResponseHeaders() );
525 'code' => $httpRequest->getStatus(),
527 'headers' => $respHeaders,
528 'body' => $httpRequest->getContent(),
532 if ( !$sv->isOK() ) {
533 $svErrors = $sv->getErrors();
534 if ( isset( $svErrors[0] ) ) {
535 $req[
'response'][
'error'] = $svErrors[0][
'message'];
538 if ( isset( $svErrors[0][
'params'][0] ) ) {
539 if ( is_numeric( $svErrors[0][
'params'][0] ) ) {
540 if ( isset( $svErrors[0][
'params'][1] ) ) {
542 $req[
'response'][
'reason'] = $svErrors[0][
'params'][1];
545 $req[
'response'][
'reason'] = $svErrors[0][
'params'][0];
551 $req[
'response'][0] = $req[
'response'][
'code'];
552 $req[
'response'][1] = $req[
'response'][
'reason'];
553 $req[
'response'][2] = $req[
'response'][
'headers'];
554 $req[
'response'][3] = $req[
'response'][
'body'];
555 $req[
'response'][4] = $req[
'response'][
'error'];