209 foreach ( $reqs as $index => &$req ) {
211 if ( count( $reqs ) > 1 ) {
213 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
218 $indexes = array_keys( $reqs );
219 if ( isset( $opts[
'usePipelining'] ) ) {
220 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
222 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
224 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
228 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
231 foreach ( $batches as $batch ) {
233 foreach ( $batch as $index ) {
234 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 );
256 foreach ( $reqs as $index => &$req ) {
257 $ch = $handles[$index];
258 curl_multi_remove_handle( $chm, $ch );
260 if ( isset( $infos[(
int)$ch] ) ) {
261 $info = $infos[(int)$ch];
262 $errno = $info[
'result'];
263 if ( $errno !== 0 ) {
264 $req[
'response'][
'error'] =
"(curl error: $errno)";
265 if ( function_exists(
'curl_strerror' ) ) {
266 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
268 $this->logger->warning(
"Error fetching URL \"{$req['url']}\": " .
269 $req[
'response'][
'error'] );
272 $req[
'response'][
'error'] =
"(curl error: no status set)";
276 $req[
'response'][0] = $req[
'response'][
'code'];
277 $req[
'response'][1] = $req[
'response'][
'reason'];
278 $req[
'response'][2] = $req[
'response'][
'headers'];
279 $req[
'response'][3] = $req[
'response'][
'body'];
280 $req[
'response'][4] = $req[
'response'][
'error'];
283 if ( isset( $req[
'_closeHandle'] ) ) {
284 fclose( $req[
'_closeHandle'] );
285 unset( $req[
'_closeHandle'] );
291 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
292 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
311 curl_setopt( $ch, CURLOPT_PROXY, $req[
'proxy'] ?? $this->proxy );
312 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS, intval( $opts[
'connTimeout'] * 1e3 ) );
313 curl_setopt( $ch, CURLOPT_TIMEOUT_MS, intval( $opts[
'reqTimeout'] * 1e3 ) );
314 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
315 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
316 curl_setopt( $ch, CURLOPT_HEADER, 0 );
317 if ( !is_null( $this->caBundlePath ) ) {
318 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
319 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
321 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
324 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
325 if ( $query !=
'' ) {
326 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
328 curl_setopt( $ch, CURLOPT_URL, $url );
329 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req[
'method'] );
330 curl_setopt( $ch, CURLOPT_NOBODY, ( $req[
'method'] ===
'HEAD' ) );
332 if ( $req[
'method'] ===
'PUT' ) {
333 curl_setopt( $ch, CURLOPT_PUT, 1 );
334 if ( is_resource( $req[
'body'] ) ) {
335 curl_setopt( $ch, CURLOPT_INFILE, $req[
'body'] );
336 if ( isset( $req[
'headers'][
'content-length'] ) ) {
337 curl_setopt( $ch, CURLOPT_INFILESIZE, $req[
'headers'][
'content-length'] );
338 } elseif ( isset( $req[
'headers'][
'transfer-encoding'] ) &&
339 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
341 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
343 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
345 } elseif ( $req[
'body'] !==
'' ) {
346 $fp = fopen(
"php://temp",
"wb+" );
347 fwrite( $fp, $req[
'body'], strlen( $req[
'body'] ) );
349 curl_setopt( $ch, CURLOPT_INFILE, $fp );
350 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req[
'body'] ) );
351 $req[
'_closeHandle'] = $fp;
353 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
355 curl_setopt( $ch, CURLOPT_READFUNCTION,
356 function ( $ch, $fd, $length ) {
357 return (
string)fread( $fd, $length );
360 } elseif ( $req[
'method'] ===
'POST' ) {
361 curl_setopt( $ch, CURLOPT_POST, 1 );
362 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req[
'body'] );
364 if ( is_resource( $req[
'body'] ) || $req[
'body'] !==
'' ) {
365 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
367 $req[
'headers'][
'content-length'] = 0;
370 if ( !isset( $req[
'headers'][
'user-agent'] ) ) {
375 foreach ( $req[
'headers'] as $name => $value ) {
376 if ( strpos( $name,
': ' ) ) {
377 throw new Exception(
"Headers cannot have ':' in the name." );
379 $headers[] = $name .
': ' . trim( $value );
381 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
383 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
384 function ( $ch,
$header ) use ( &$req ) {
385 if ( !empty( $req[
'flags'][
'relayResponseHeaders'] ) && trim(
$header ) !==
'' ) {
390 if ( preg_match(
"/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/",
$header,
$matches ) ) {
391 $req[
'response'][
'code'] = (int)
$matches[2];
392 $req[
'response'][
'reason'] = trim(
$matches[3] );
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 );
417 $req[
'response'][
'body'] .= $data;
419 return strlen( $data );
465 foreach ( $reqs as &$req ) {
466 $reqOptions = $httpOptions + [
467 'method' => $req[
'method'],
470 'postData' => $req[
'body'],
474 $query = http_build_query( $req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
475 if ( $query !=
'' ) {
476 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
479 $httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
481 $sv = $httpRequest->execute()->getStatusValue();
483 $respHeaders = array_map(
485 return implode(
', ', $v );
487 $httpRequest->getResponseHeaders() );
490 'code' => $httpRequest->getStatus(),
492 'headers' => $respHeaders,
493 'body' => $httpRequest->getContent(),
497 if ( !$sv->isOK() ) {
498 $svErrors = $sv->getErrors();
499 if ( isset( $svErrors[0] ) ) {
500 $req[
'response'][
'error'] = $svErrors[0][
'message'];
503 if ( isset( $svErrors[0][
'params'][0] ) ) {
504 if ( is_numeric( $svErrors[0][
'params'][0] ) ) {
505 if ( isset( $svErrors[0][
'params'][1] ) ) {
507 $req[
'response'][
'reason'] = $svErrors[0][
'params'][1];
510 $req[
'response'][
'reason'] = $svErrors[0][
'params'][0];
516 $req[
'response'][0] = $req[
'response'][
'code'];
517 $req[
'response'][1] = $req[
'response'][
'reason'];
518 $req[
'response'][2] = $req[
'response'][
'headers'];
519 $req[
'response'][3] = $req[
'response'][
'body'];
520 $req[
'response'][4] = $req[
'response'][
'error'];