229 foreach ( $reqs as $index => &$req ) {
231 curl_multi_add_handle( $chm, $handles[$index] );
241 $mrc = curl_multi_exec( $chm, $active );
242 $info = curl_multi_info_read( $chm );
243 if ( $info !==
false ) {
244 $infos[(int)$info[
'handle']] = $info;
246 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
248 if ( $active > 0 && $mrc == CURLM_OK && curl_multi_select( $chm, $selectTimeout ) == -1 ) {
252 }
while ( $active > 0 && $mrc == CURLM_OK );
255 foreach ( $reqs as $index => &$req ) {
256 $ch = $handles[$index];
257 curl_multi_remove_handle( $chm, $ch );
259 if ( isset( $infos[(
int)$ch] ) ) {
260 $info = $infos[(int)$ch];
261 $errno = $info[
'result'];
262 if ( $errno !== 0 ) {
263 $req[
'response'][
'error'] =
"(curl error: $errno)";
264 if ( function_exists(
'curl_strerror' ) ) {
265 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
267 $this->logger->warning(
"Error fetching URL \"{$req['url']}\": " .
268 $req[
'response'][
'error'] );
270 $this->logger->debug(
271 "HTTP complete: {method} {url} code={response_code} size={size} " .
272 "total={total_time} connect={connect_time}",
274 'method' => $req[
'method'],
275 'url' => $req[
'url'],
276 'response_code' => $req[
'response'][
'code'],
277 'size' => curl_getinfo( $ch, CURLINFO_SIZE_DOWNLOAD ),
279 $ch, CURLINFO_TOTAL_TIME,
'CURLINFO_TOTAL_TIME_T'
282 $ch, CURLINFO_CONNECT_TIME,
'CURLINFO_CONNECT_TIME_T'
288 $req[
'response'][
'error'] =
"(curl error: no status set)";
292 $req[
'response'][0] = $req[
'response'][
'code'];
293 $req[
'response'][1] = $req[
'response'][
'reason'];
294 $req[
'response'][2] = $req[
'response'][
'headers'];
295 $req[
'response'][3] = $req[
'response'][
'body'];
296 $req[
'response'][4] = $req[
'response'][
'error'];
299 if ( isset( $req[
'_closeHandle'] ) ) {
300 fclose( $req[
'_closeHandle'] );
301 unset( $req[
'_closeHandle'] );
322 curl_setopt( $ch, CURLOPT_PROXY, $req[
'proxy'] ?? $this->proxy );
323 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS, intval( $opts[
'connTimeout'] * 1e3 ) );
324 curl_setopt( $ch, CURLOPT_TIMEOUT_MS, intval( $opts[
'reqTimeout'] * 1e3 ) );
325 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
326 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
327 curl_setopt( $ch, CURLOPT_HEADER, 0 );
328 if ( $this->caBundlePath !==
null ) {
329 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
330 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
332 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
335 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
336 if ( $query !=
'' ) {
337 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
339 curl_setopt( $ch, CURLOPT_URL, $url );
340 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req[
'method'] );
341 curl_setopt( $ch, CURLOPT_NOBODY, ( $req[
'method'] ===
'HEAD' ) );
343 if ( $req[
'method'] ===
'PUT' ) {
344 curl_setopt( $ch, CURLOPT_PUT, 1 );
346 if ( is_resource( $req[
'body'] ) ) {
347 curl_setopt( $ch, CURLOPT_INFILE, $req[
'body'] );
348 if ( isset( $req[
'headers'][
'content-length'] ) ) {
349 curl_setopt( $ch, CURLOPT_INFILESIZE, $req[
'headers'][
'content-length'] );
350 } elseif ( isset( $req[
'headers'][
'transfer-encoding'] ) &&
351 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
353 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
355 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
357 } elseif ( $req[
'body'] !==
'' ) {
358 $fp = fopen(
"php://temp",
"wb+" );
359 fwrite( $fp, $req[
'body'], strlen( $req[
'body'] ) );
361 curl_setopt( $ch, CURLOPT_INFILE, $fp );
362 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req[
'body'] ) );
363 $req[
'_closeHandle'] = $fp;
365 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
367 curl_setopt( $ch, CURLOPT_READFUNCTION,
368 static function ( $ch, $fd, $length ) {
369 return (
string)fread( $fd, $length );
372 } elseif ( $req[
'method'] ===
'POST' ) {
373 curl_setopt( $ch, CURLOPT_POST, 1 );
374 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req[
'body'] );
377 if ( is_resource( $req[
'body'] ) || $req[
'body'] !==
'' ) {
378 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
380 $req[
'headers'][
'content-length'] = 0;
383 if ( !isset( $req[
'headers'][
'user-agent'] ) ) {
384 $req[
'headers'][
'user-agent'] = $this->userAgent;
388 foreach ( $req[
'headers'] as $name => $value ) {
389 if ( strpos( $name,
': ' ) ) {
390 throw new Exception(
"Headers cannot have ':' in the name." );
392 $headers[] = $name .
': ' . trim( $value );
394 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
396 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
397 static function ( $ch,
$header ) use ( &$req ) {
398 if ( !empty( $req[
'flags'][
'relayResponseHeaders'] ) && trim(
$header ) !==
'' ) {
403 if ( preg_match(
"/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/",
$header,
$matches ) ) {
404 $req[
'response'][
'code'] = (int)
$matches[2];
405 $req[
'response'][
'reason'] = trim(
$matches[3] );
408 $req[
'response'][
'headers'] = [];
411 if ( strpos(
$header,
":" ) ===
false ) {
414 list( $name, $value ) = explode(
":",
$header, 2 );
415 $name = strtolower( $name );
416 $value = trim( $value );
417 if ( isset( $req[
'response'][
'headers'][$name] ) ) {
418 $req[
'response'][
'headers'][$name] .=
', ' . $value;
420 $req[
'response'][
'headers'][$name] = $value;
427 $hasOutputStream = isset( $req[
'stream'] );
428 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
429 static function ( $ch, $data ) use ( &$req, $hasOutputStream ) {
430 if ( $hasOutputStream ) {
431 return fwrite( $req[
'stream'], $data );
434 $req[
'response'][
'body'] .= $data;
436 return strlen( $data );
451 $cmh = curl_multi_init();
454 curl_multi_setopt(
$cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
458 $curlVersion = curl_version()[
'version'];
461 if ( version_compare( $curlVersion,
'7.30.0',
'>=' ) ) {
463 $maxHostConns = $opts[
'maxConnsPerHost'] ?? $this->maxConnsPerHost;
464 curl_multi_setopt( $this->cmh, CURLMOPT_MAX_HOST_CONNECTIONS, (
int)$maxHostConns );
467 if ( $opts[
'usePipelining'] ?? $this->usePipelining ) {
468 if ( version_compare( $curlVersion,
'7.43',
'<' ) ) {
471 } elseif ( version_compare( $curlVersion,
'7.62',
'<' ) ) {
473 $pipelining = CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX;
476 $pipelining = CURLPIPE_MULTIPLEX;
480 @curl_multi_setopt( $this->cmh, CURLMOPT_PIPELINING, $pipelining );
521 'timeout' => $opts[
'reqTimeout'] ?? $this->reqTimeout,
522 'connectTimeout' => $opts[
'connTimeout'] ?? $this->connTimeout,
523 'logger' => $this->logger,
524 'caInfo' => $this->caBundlePath,
526 foreach ( $reqs as &$req ) {
527 $reqOptions = $httpOptions + [
528 'method' => $req[
'method'],
529 'proxy' => $req[
'proxy'] ?? $this->proxy,
530 'userAgent' => $req[
'headers'][
'user-agent'] ?? $this->userAgent,
531 'postData' => $req[
'body'],
535 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
536 if ( $query !=
'' ) {
537 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
540 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
541 $url, $reqOptions, __METHOD__ );
542 $httpRequest->setLogger( $this->logger );
543 $sv = $httpRequest->execute()->getStatusValue();
545 $respHeaders = array_map(
546 static function ( $v ) {
547 return implode(
', ', $v );
549 $httpRequest->getResponseHeaders() );
552 'code' => $httpRequest->getStatus(),
554 'headers' => $respHeaders,
555 'body' => $httpRequest->getContent(),
559 if ( !$sv->isOK() ) {
560 $svErrors = $sv->getErrors();
561 if ( isset( $svErrors[0] ) ) {
562 $req[
'response'][
'error'] = $svErrors[0][
'message'];
565 if ( isset( $svErrors[0][
'params'][0] ) ) {
566 if ( is_numeric( $svErrors[0][
'params'][0] ) ) {
567 if ( isset( $svErrors[0][
'params'][1] ) ) {
569 $req[
'response'][
'reason'] = $svErrors[0][
'params'][1];
572 $req[
'response'][
'reason'] = $svErrors[0][
'params'][0];
578 $req[
'response'][0] = $req[
'response'][
'code'];
579 $req[
'response'][1] = $req[
'response'][
'reason'];
580 $req[
'response'][2] = $req[
'response'][
'headers'];
581 $req[
'response'][3] = $req[
'response'][
'body'];
582 $req[
'response'][4] = $req[
'response'][
'error'];