201 foreach ( $reqs
as $index => &
$req ) {
203 if ( count( $reqs ) > 1 ) {
205 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
210 $indexes = array_keys( $reqs );
211 if ( isset( $opts[
'usePipelining'] ) ) {
212 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
214 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
216 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
220 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
226 curl_multi_add_handle( $chm, $handles[$index] );
233 $mrc = curl_multi_exec( $chm, $active );
234 $info = curl_multi_info_read( $chm );
235 if ( $info !==
false ) {
236 $infos[(int)$info[
'handle']] = $info;
238 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
240 if ( $active > 0 && $mrc == CURLM_OK ) {
241 if ( curl_multi_select( $chm, $selectTimeout ) == -1 ) {
246 }
while ( $active > 0 && $mrc == CURLM_OK );
250 foreach ( $reqs
as $index => &
$req ) {
251 $ch = $handles[$index];
252 curl_multi_remove_handle( $chm, $ch );
254 if ( isset( $infos[(
int)$ch] ) ) {
255 $info = $infos[(int)$ch];
256 $errno = $info[
'result'];
257 if ( $errno !== 0 ) {
258 $req[
'response'][
'error'] =
"(curl error: $errno)";
259 if ( function_exists(
'curl_strerror' ) ) {
260 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
262 $this->logger->warning(
"Error fetching URL \"{$req['url']}\": " .
263 $req[
'response'][
'error'] );
266 $req[
'response'][
'error'] =
"(curl error: no status set)";
270 $req[
'response'][0] =
$req[
'response'][
'code'];
271 $req[
'response'][1] =
$req[
'response'][
'reason'];
272 $req[
'response'][2] =
$req[
'response'][
'headers'];
273 $req[
'response'][3] =
$req[
'response'][
'body'];
274 $req[
'response'][4] =
$req[
'response'][
'error'];
277 if ( isset(
$req[
'_closeHandle'] ) ) {
278 fclose(
$req[
'_closeHandle'] );
279 unset(
$req[
'_closeHandle'] );
285 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
286 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
302 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS,
303 ( $opts[
'connTimeout'] ?? $this->connTimeout ) * 1000 );
304 curl_setopt( $ch, CURLOPT_PROXY,
$req[
'proxy'] ?? $this->proxy );
305 curl_setopt( $ch, CURLOPT_TIMEOUT_MS,
306 ( $opts[
'reqTimeout'] ?? $this->reqTimeout ) * 1000 );
307 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
308 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
309 curl_setopt( $ch, CURLOPT_HEADER, 0 );
310 if ( !is_null( $this->caBundlePath ) ) {
311 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
312 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
314 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
317 $query = http_build_query(
$req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
319 $url .= strpos(
$req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
321 curl_setopt( $ch, CURLOPT_URL, $url );
323 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST,
$req[
'method'] );
324 if (
$req[
'method'] ===
'HEAD' ) {
325 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
328 if (
$req[
'method'] ===
'PUT' ) {
329 curl_setopt( $ch, CURLOPT_PUT, 1 );
330 if ( is_resource(
$req[
'body'] ) ) {
331 curl_setopt( $ch, CURLOPT_INFILE,
$req[
'body'] );
332 if ( isset(
$req[
'headers'][
'content-length'] ) ) {
333 curl_setopt( $ch, CURLOPT_INFILESIZE,
$req[
'headers'][
'content-length'] );
334 } elseif ( isset(
$req[
'headers'][
'transfer-encoding'] ) &&
335 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
337 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
339 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
341 } elseif (
$req[
'body'] !==
'' ) {
342 $fp = fopen(
"php://temp",
"wb+" );
343 fwrite( $fp,
$req[
'body'], strlen(
$req[
'body'] ) );
345 curl_setopt( $ch, CURLOPT_INFILE, $fp );
346 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen(
$req[
'body'] ) );
347 $req[
'_closeHandle'] = $fp;
349 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
351 curl_setopt( $ch, CURLOPT_READFUNCTION,
352 function ( $ch, $fd, $length ) {
353 $data = fread( $fd, $length );
354 $len = strlen( $data );
358 } elseif (
$req[
'method'] ===
'POST' ) {
359 curl_setopt( $ch, CURLOPT_POST, 1 );
363 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD,
true );
364 curl_setopt( $ch, CURLOPT_POSTFIELDS,
$req[
'body'] );
366 if ( is_resource(
$req[
'body'] ) ||
$req[
'body'] !==
'' ) {
367 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
369 $req[
'headers'][
'content-length'] = 0;
372 if ( !isset(
$req[
'headers'][
'user-agent'] ) ) {
378 if ( strpos(
$name,
': ' ) ) {
379 throw new Exception(
"Headers cannot have ':' in the name." );
383 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
385 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
387 if ( !empty(
$req[
'flags'][
'relayResponseHeaders'] ) ) {
392 if ( preg_match(
"/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/",
$header,
$matches ) ) {
397 if ( strpos(
$header,
":" ) ===
false ) {
403 if ( isset(
$req[
'response'][
'headers'][
$name] ) ) {
412 if ( isset(
$req[
'stream'] ) ) {
416 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
417 function ( $ch, $data )
use ( &
$req ) {
418 return fwrite(
$req[
'stream'], $data );
422 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
423 function ( $ch, $data )
use ( &
$req ) {
424 $req[
'response'][
'body'] .= $data;
425 return strlen( $data );
438 if ( !$this->multiHandle ) {
439 if ( !function_exists(
'curl_multi_init' ) ) {
440 throw new Exception(
"PHP cURL function curl_multi_init missing. " .
441 "Check https://www.mediawiki.org/wiki/Manual:CURL" );
443 $cmh = curl_multi_init();
444 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
445 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
446 $this->multiHandle = $cmh;
471 foreach ( $reqs
as &
$req ) {
472 $reqOptions = $httpOptions + [
473 'method' =>
$req[
'method'],
476 'postData' =>
$req[
'body'],
480 $query = http_build_query(
$req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
482 $url .= strpos(
$req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
485 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
487 $sv = $httpRequest->execute()->getStatusValue();
489 $respHeaders = array_map(
491 return implode(
', ', $v );
493 $httpRequest->getResponseHeaders() );
496 'code' => $httpRequest->getStatus(),
498 'headers' => $respHeaders,
499 'body' => $httpRequest->getContent(),
503 if ( !$sv->isOk() ) {
504 $svErrors = $sv->getErrors();
505 if ( isset( $svErrors[0] ) ) {
506 $req[
'response'][
'error'] = $svErrors[0][
'message'];
509 if ( isset( $svErrors[0][
'params'][0] ) ) {
510 if ( is_numeric( $svErrors[0][
'params'][0] ) ) {
511 if ( isset( $svErrors[0][
'params'][1] ) ) {
512 $req[
'response'][
'reason'] = $svErrors[0][
'params'][1];
515 $req[
'response'][
'reason'] = $svErrors[0][
'params'][0];
521 $req[
'response'][0] =
$req[
'response'][
'code'];
522 $req[
'response'][1] =
$req[
'response'][
'reason'];
523 $req[
'response'][2] =
$req[
'response'][
'headers'];
524 $req[
'response'][3] =
$req[
'response'][
'body'];
525 $req[
'response'][4] =
$req[
'response'][
'error'];