@@ -150,7 +150,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
150150 // Schedule the request in a non-blocking way
151151 $ multi ->lastTimeout = null ;
152152 $ multi ->openHandles [$ id ] = [$ ch , $ options ];
153- curl_multi_add_handle ($ multi ->handles [ 0 ] , $ ch );
153+ curl_multi_add_handle ($ multi ->handle , $ ch );
154154
155155 $ this ->canary = new Canary (static function () use ($ ch , $ multi , $ id ) {
156156 unset($ multi ->openHandles [$ id ], $ multi ->handlesActivity [$ id ]);
@@ -160,9 +160,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
160160 return ;
161161 }
162162
163- foreach ($ multi ->handles as $ mh ) {
164- curl_multi_remove_handle ($ mh , $ ch );
165- }
163+ curl_multi_remove_handle ($ multi ->handle , $ ch );
166164 curl_setopt_array ($ ch , [
167165 \CURLOPT_NOPROGRESS => true ,
168166 \CURLOPT_PROGRESSFUNCTION => null ,
@@ -244,7 +242,7 @@ public function __destruct()
244242 */
245243 private static function schedule (self $ response , array &$ runningResponses ): void
246244 {
247- if (isset ($ runningResponses [$ i = (int ) $ response ->multi ->handles [ 0 ] ])) {
245+ if (isset ($ runningResponses [$ i = (int ) $ response ->multi ->handle ])) {
248246 $ runningResponses [$ i ][1 ][$ response ->id ] = $ response ;
249247 } else {
2
D966
50248 $ runningResponses [$ i ] = [$ response ->multi , [$ response ->id => $ response ]];
@@ -276,47 +274,39 @@ private static function perform(ClientState $multi, array &$responses = null): v
276274
277275 try {
278276 self ::$ performing = true ;
277+ $ active = 0 ;
278+ while (\CURLM_CALL_MULTI_PERFORM === ($ err = curl_multi_exec ($ multi ->handle , $ active ))) {
279+ }
279280
280- foreach ($ multi ->handles as $ i => $ mh ) {
281- $ active = 0 ;
282- while (\CURLM_CALL_MULTI_PERFORM === ($ err = curl_multi_exec ($ mh , $ active ))) {
283- }
281+ if (\CURLM_OK !== $ err ) {
282+ throw new TransportException (curl_multi_strerror ($ err ));
283+ }
284284
285- if (\CURLM_OK !== $ err ) {
286- throw new TransportException (curl_multi_strerror ($ err ));
285+ while ($ info = curl_multi_info_read ($ multi ->handle )) {
286+ if (\CURLMSG_DONE !== $ info ['msg ' ]) {
287+ continue ;
287288 }
289+ $ result = $ info ['result ' ];
290+ $ id = (int ) $ ch = $ info ['handle ' ];
291+ $ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
288292
289- while ($ info = curl_multi_info_read ($ mh )) {
290- if (\CURLMSG_DONE !== $ info ['msg ' ]) {
291- continue ;
292- }
293- $ result = $ info ['result ' ];
294- $ id = (int ) $ ch = $ info ['handle ' ];
295- $ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
296-
297- if (\in_array ($ result , [\CURLE_SEND_ERROR , \CURLE_RECV_ERROR , /*CURLE_HTTP2*/ 16 , /*CURLE_HTTP2_STREAM*/ 92 ], true ) && $ waitFor [1 ] && 'C ' !== $ waitFor [0 ]) {
298- curl_multi_remove_handle ($ mh , $ ch );
299- $ waitFor [1 ] = (string ) ((int ) $ waitFor [1 ] - 1 ); // decrement the retry counter
300- curl_setopt ($ ch , \CURLOPT_PRIVATE , $ waitFor );
301- curl_setopt ($ ch , \CURLOPT_FORBID_REUSE , true );
302-
303- if (0 === curl_multi_add_handle ($ mh , $ ch )) {
304- continue ;
305- }
306- }
293+ if (\in_array ($ result , [\CURLE_SEND_ERROR , \CURLE_RECV_ERROR , /*CURLE_HTTP2*/ 16 , /*CURLE_HTTP2_STREAM*/ 92 ], true ) && $ waitFor [1 ] && 'C ' !== $ waitFor [0 ]) {
294+ curl_multi_remove_handle ($ multi ->handle , $ ch );
295+ $ waitFor [1 ] = (string ) ((int ) $ waitFor [1 ] - 1 ); // decrement the retry counter
296+ curl_setopt ($ ch , \CURLOPT_PRIVATE , $ waitFor );
297+ curl_setopt ($ ch , \CURLOPT_FORBID_REUSE , true );
307298
308- if (\ CURLE_RECV_ERROR === $ result && ' H ' === $ waitFor [ 0 ] && 400 <= ( $ responses [( int ) $ ch ]-> info [ ' http_code ' ] ?? 0 )) {
309- $ multi -> handlesActivity [ $ id ][] = new FirstChunk () ;
299+ if (0 === curl_multi_add_handle ( $ multi -> handle , $ ch )) {
300+ continue ;
310301 }
311-
312- $ multi ->handlesActivity [$ id ][] = null ;
313- $ multi ->handlesActivity [$ id ][] = \in_array ($ result , [\CURLE_OK , \CURLE_TOO_MANY_REDIRECTS ], true ) || '_0 ' === $ waitFor || curl_getinfo ($ ch , \CURLINFO_SIZE_DOWNLOAD ) === curl_getinfo ($ ch , \CURLINFO_CONTENT_LENGTH_DOWNLOAD ) ? null : new TransportException (sprintf ('%s for "%s". ' , curl_strerror ($ result ), curl_getinfo ($ ch , \CURLINFO_EFFECTIVE_URL )));
314302 }
315303
316- if (!$ active && 0 < $ i ) {
317- curl_multi_close ($ mh );
318- unset($ multi ->handles [$ i ]);
304+ if (\CURLE_RECV_ERROR === $ result && 'H ' === $ waitFor [0 ] && 400 <= ($ responses [(int ) $ ch ]->info ['http_code ' ] ?? 0 )) {
305+ $ multi ->handlesActivity [$ id ][] = new FirstChunk ();
319306 }
307+
308+ $ multi ->handlesActivity [$ id ][] = null ;
309+ $ multi ->handlesActivity [$ id ][] = \in_array ($ result , [\CURLE_OK , \CURLE_TOO_MANY_REDIRECTS ], true ) || '_0 ' === $ waitFor || curl_getinfo ($ ch , \CURLINFO_SIZE_DOWNLOAD ) === curl_getinfo ($ ch , \CURLINFO_CONTENT_LENGTH_DOWNLOAD ) ? null : new TransportException (sprintf ('%s for "%s". ' , curl_strerror ($ result ), curl_getinfo ($ ch , \CURLINFO_EFFECTIVE_URL )));
320310 }
321311 } finally {
322312 self ::$ performing = false ;
@@ -335,7 +325,7 @@ private static function select(ClientState $multi, float $timeout): int
335325 $ timeout = min ($ timeout , 0.01 );
336326 }
337327
338- return curl_multi_select ($ multi ->handles [ array_key_last ( $ multi -> handles )] , $ timeout );
328+ return curl_multi_select ($ multi ->handle , $ timeout );
339329 }
340330
341331 /**
0 commit comments