61 protected $userAgent =
'wikimedia/multi-http-client v1.0';
74 if ( isset(
$options[
'caBundlePath'] ) ) {
75 $this->caBundlePath =
$options[
'caBundlePath'];
76 if ( !file_exists( $this->caBundlePath ) ) {
77 throw new Exception(
"Cannot find CA bundle: " . $this->caBundlePath );
81 'connTimeout',
'reqTimeout',
'usePipelining',
'maxConnsPerHost',
'proxy',
'userAgent'
83 foreach ( $opts
as $key ) {
110 return $this->
runMulti( [ $req ], $opts )[0][
'response'];
144 foreach ( $reqs
as $index => &
$req ) {
152 if ( isset(
$req[0] ) ) {
156 if ( isset(
$req[1] ) ) {
160 if ( !isset(
$req[
'method'] ) ) {
161 throw new Exception(
"Request has no 'method' field set." );
162 } elseif ( !isset(
$req[
'url'] ) ) {
163 throw new Exception(
"Request has no 'url' field set." );
165 $req[
'query'] = isset(
$req[
'query'] ) ?
$req[
'query'] : [];
167 if ( isset(
$req[
'headers'] ) ) {
172 $req[
'headers'] = $headers;
173 if ( !isset(
$req[
'body'] ) ) {
175 $req[
'headers'][
'content-length'] = 0;
177 $req[
'flags'] = isset(
$req[
'flags'] ) ?
$req[
'flags'] : [];
179 if (
count( $reqs ) > 1 ) {
181 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
186 $indexes = array_keys( $reqs );
187 if ( isset( $opts[
'usePipelining'] ) ) {
188 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
190 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
192 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
196 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
202 curl_multi_add_handle( $chm, $handles[$index] );
209 $mrc = curl_multi_exec( $chm, $active );
210 $info = curl_multi_info_read( $chm );
211 if ( $info !==
false ) {
212 $infos[(int)$info[
'handle']] = $info;
214 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
216 if ( $active > 0 && $mrc == CURLM_OK ) {
217 if ( curl_multi_select( $chm, 10 ) == -1 ) {
222 }
while ( $active > 0 && $mrc == CURLM_OK );
226 foreach ( $reqs
as $index => &
$req ) {
227 $ch = $handles[$index];
228 curl_multi_remove_handle( $chm, $ch );
230 if ( isset( $infos[(
int)$ch] ) ) {
231 $info = $infos[(int)$ch];
232 $errno = $info[
'result'];
233 if ( $errno !== 0 ) {
234 $req[
'response'][
'error'] =
"(curl error: $errno)";
235 if ( function_exists(
'curl_strerror' ) ) {
236 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
240 $req[
'response'][
'error'] =
"(curl error: no status set)";
244 $req[
'response'][0] =
$req[
'response'][
'code'];
245 $req[
'response'][1] =
$req[
'response'][
'reason'];
246 $req[
'response'][2] =
$req[
'response'][
'headers'];
247 $req[
'response'][3] =
$req[
'response'][
'body'];
248 $req[
'response'][4] =
$req[
'response'][
'error'];
251 if ( isset(
$req[
'_closeHandle'] ) ) {
252 fclose(
$req[
'_closeHandle'] );
253 unset(
$req[
'_closeHandle'] );
259 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
260 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
276 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT,
277 isset( $opts[
'connTimeout'] ) ? $opts[
'connTimeout'] : $this->connTimeout );
278 curl_setopt( $ch, CURLOPT_PROXY, isset(
$req[
'proxy'] ) ?
$req[
'proxy'] : $this->proxy );
279 curl_setopt( $ch, CURLOPT_TIMEOUT,
280 isset( $opts[
'reqTimeout'] ) ? $opts[
'reqTimeout'] : $this->reqTimeout );
281 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
282 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
283 curl_setopt( $ch, CURLOPT_HEADER, 0 );
284 if ( !is_null( $this->caBundlePath ) ) {
285 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
286 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
288 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
291 $query = http_build_query(
$req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
293 $url .= strpos(
$req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
295 curl_setopt( $ch, CURLOPT_URL, $url );
297 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST,
$req[
'method'] );
298 if (
$req[
'method'] ===
'HEAD' ) {
299 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
302 if (
$req[
'method'] ===
'PUT' ) {
303 curl_setopt( $ch, CURLOPT_PUT, 1 );
304 if ( is_resource(
$req[
'body'] ) ) {
305 curl_setopt( $ch, CURLOPT_INFILE,
$req[
'body'] );
306 if ( isset(
$req[
'headers'][
'content-length'] ) ) {
307 curl_setopt( $ch, CURLOPT_INFILESIZE,
$req[
'headers'][
'content-length'] );
308 } elseif ( isset(
$req[
'headers'][
'transfer-encoding'] ) &&
309 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
311 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
313 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
315 } elseif (
$req[
'body'] !==
'' ) {
316 $fp = fopen(
"php://temp",
"wb+" );
317 fwrite( $fp,
$req[
'body'], strlen(
$req[
'body'] ) );
319 curl_setopt( $ch, CURLOPT_INFILE, $fp );
320 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen(
$req[
'body'] ) );
321 $req[
'_closeHandle'] = $fp;
323 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
325 curl_setopt( $ch, CURLOPT_READFUNCTION,
326 function ( $ch, $fd, $length ) {
327 $data = fread( $fd, $length );
328 $len = strlen( $data );
332 } elseif (
$req[
'method'] ===
'POST' ) {
333 curl_setopt( $ch, CURLOPT_POST, 1 );
339 if ( defined(
'CURLOPT_SAFE_UPLOAD' ) ) {
340 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD,
true );
341 } elseif ( is_array(
$req[
'body'] ) ) {
345 $req[
'body'] = http_build_query(
$req[
'body'] );
347 curl_setopt( $ch, CURLOPT_POSTFIELDS,
$req[
'body'] );
349 if ( is_resource(
$req[
'body'] ) ||
$req[
'body'] !==
'' ) {
350 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
352 $req[
'headers'][
'content-length'] = 0;
355 if ( !isset(
$req[
'headers'][
'user-agent'] ) ) {
361 if ( strpos(
$name,
': ' ) ) {
362 throw new Exception(
"Headers cannot have ':' in the name." );
366 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
368 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
370 if ( !empty(
$req[
'flags'][
'relayResponseHeaders'] ) ) {
375 if ( preg_match(
"/^(HTTP\/1\.[01]) (\d{3}) (.*)/",
$header,
$matches ) ) {
380 if ( strpos(
$header,
":" ) ===
false ) {
389 if ( isset(
$req[
'stream'] ) ) {
393 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
394 function ( $ch, $data )
use ( &
$req ) {
395 return fwrite(
$req[
'stream'], $data );
399 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
400 function ( $ch, $data )
use ( &
$req ) {
401 $req[
'response'][
'body'] .= $data;
402 return strlen( $data );
414 if ( !$this->multiHandle ) {
415 $cmh = curl_multi_init();
416 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
417 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
418 $this->multiHandle = $cmh;
424 if ( $this->multiHandle ) {
425 curl_multi_close( $this->multiHandle );