@@ -106,9 +106,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
106
106
if (0 < $ duration ) {
107
107
if ($ execCounter === $ multi ->execCounter ) {
108
108
$ multi ->execCounter = !\is_float ($ execCounter ) ? 1 + $ execCounter : \PHP_INT_MIN ;
109
- foreach ($ multi ->handles as $ mh ) {
110
- curl_multi_remove_handle ($ mh , $ ch );
111
- }
109
+ curl_multi_remove_handle ($ multi ->handle , $ ch );
112
110
}
113
111
114
112
$ lastExpiry = end ($ multi ->pauseExpiries );
@@ -120,7 +118,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
120
118
} else {
121
119
unset($ multi ->pauseExpiries [(int ) $ ch ]);
122
120
curl_pause ($ ch , \CURLPAUSE_CONT );
123
- curl_multi_add_handle ($ multi ->handles [ 0 ] , $ ch );
121
+ curl_multi_add_handle ($ multi ->handle , $ ch );
124
122
}
125
123
};
126
124
@@ -174,7 +172,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
174
172
// Schedule the request in a non-blocking way
175
173
$ multi ->lastTimeout = null ;
176
174
$ multi ->openHandles [$ id ] = [$ ch , $ options ];
177
- curl_multi_add_handle ($ multi ->handles [ 0 ] , $ ch );
175
+ curl_multi_add_handle ($ multi ->handle , $ ch );
178
176
179
177
$ this ->canary = new Canary (static function () use ($ ch , $ multi , $ id ) {
180
178
unset($ multi ->pauseExpiries [$ id ], $ multi ->openHandles [$ id ], $ multi ->handlesActivity [$ id ]);
@@ -184,9 +182,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
184
182
return ;
185
183
}
186
184
187
- foreach ($ multi ->handles as $ mh ) {
188
- curl_multi_remove_handle ($ mh , $ ch );
189
- }
185
+ curl_multi_remove_handle ($ multi ->handle , $ ch );
190
186
curl_setopt_array ($ ch , [
191
187
\CURLOPT_NOPROGRESS => true ,
192
188
\CURLOPT_PROGRESSFUNCTION => null ,
@@ -268,7 +264,7 @@ public function __destruct()
268
264
*/
269
265
private static function schedule (self $ response , array &$ runningResponses ): void
270
266
{
271
- if (isset ($ runningResponses [$ i = (int ) $ response ->multi ->handles [ 0 ] ])) {
267
+ if (isset ($ runningResponses [$ i = (int ) $ response ->multi ->handle ])) {
272
268
$ runningResponses [$ i ][1 ][$ response ->id ] = $ response ;
273
269
} else {
274
270
$ runningResponses [$ i ] = [$ response ->multi , [$ response ->id => $ response ]];
@@ -301,47 +297,39 @@ private static function perform(ClientState $multi, array &$responses = null): v
301
297
try {
302
298
self ::$ performing = true ;
303
299
++$ multi ->execCounter ;
300
+ $ active = 0 ;
301
+ while (\CURLM_CALL_MULTI_PERFORM === ($ err = curl_multi_exec ($ multi ->handle , $ active ))) {
302
+ }
304
303
305
- foreach ($ multi ->handles as $ i => $ mh ) {
306
- $ active = 0 ;
307
- while (\CURLM_CALL_MULTI_PERFORM === ($ err = curl_multi_exec ($ mh , $ active ))) {
308
- }
304
+ if (\CURLM_OK !== $ err ) {
305
+ throw new TransportException (curl_multi_strerror ($ err ));
306
+ }
309
307
310
- if (\CURLM_OK !== $ err ) {
311
- throw new TransportException (curl_multi_strerror ($ err ));
308
+ while ($ info = curl_multi_info_read ($ multi ->handle )) {
309
+ if (\CURLMSG_DONE !== $ info ['msg ' ]) {
310
+ continue ;
312
311
}
312
+ $ result = $ info ['result ' ];
313
+ $ id = (int ) $ ch = $ info ['handle ' ];
314
+ $ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
313
315
314
- while ($ info = curl_multi_info_read ($ mh )) {
315
- if (\CURLMSG_DONE !== $ info ['msg ' ]) {
316
- continue ;
317
- }
318
- $ result = $ info ['result ' ];
319
- $ id = (int ) $ ch = $ info ['handle ' ];
320
- $ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
321
-
322
- if (\in_array ($ result , [\CURLE_SEND_ERROR , \CURLE_RECV_ERROR , /*CURLE_HTTP2*/ 16 , /*CURLE_HTTP2_STREAM*/ 92 ], true ) && $ waitFor [1 ] && 'C ' !== $ waitFor [0 ]) {
323
- curl_multi_remove_handle ($ mh , $ ch );
324
- $ waitFor [1 ] = (string ) ((int ) $ waitFor [1 ] - 1 ); // decrement the retry counter
325
- curl_setopt ($ ch , \CURLOPT_PRIVATE , $ waitFor );
326
- curl_setopt ($ ch , \CURLOPT_FORBID_REUSE , true );
327
-
328
- if (0 === curl_multi_add_handle ($ mh , $ ch )) {
329
- continue ;
330
- }
331
- }
316
+ if (\in_array ($ result , [\CURLE_SEND_ERROR , \CURLE_RECV_ERROR , /*CURLE_HTTP2*/ 16 , /*CURLE_HTTP2_STREAM*/ 92 ], true ) && $ waitFor [1 ] && 'C ' !== $ waitFor [0 ]) {
317
+ curl_multi_remove_handle ($ multi ->handle , $ ch );
318
+ $ waitFor [1 ] = (string ) ((int ) $ waitFor [1 ] - 1 ); // decrement the retry counter
319
+ curl_setopt ($ ch , \CURLOPT_PRIVATE , $ waitFor );
320
+ curl_setopt ($ ch , \CURLOPT_FORBID_REUSE , true );
332
321
333
- if (\ CURLE_RECV_ERROR === $ result && ' H ' === $ waitFor [ 0 ] && 400 <= ( $ responses [( int ) $ ch ]-> info [ ' http_code ' ] ?? 0 )) {
334
- $ multi -> handlesActivity [ $ id ][] = new FirstChunk () ;
322
+ if (0 === curl_multi_add_handle ( $ multi -> handle , $ ch )) {
323
+ continue ;
335
324
}
336
-
337
- $ multi ->handlesActivity [$ id ][] = null ;
338
- $ 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 )));
339
325
}
340
326
341
- if (!$ active && 0 < $ i ) {
342
- curl_multi_close ($ mh );
343
- unset($ multi ->handles [$ i ]);
327
+ if (\CURLE_RECV_ERROR === $ result && 'H ' === $ waitFor [0 ] && 400 <= ($ responses [(int ) $ ch ]->info ['http_code ' ] ?? 0 )) {
328
+ $ multi ->handlesActivity [$ id ][] = new FirstChunk ();
344
329
}
330
+
331
+ $ multi ->handlesActivity [$ id ][] = null ;
332
+ $ 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 )));
345
333
}
346
334
} finally {
347
335
self ::$ performing = false ;
@@ -371,11 +359,11 @@ private static function select(ClientState $multi, float $timeout): int
371
359
372
360
unset($ multi ->pauseExpiries [$ id ]);
373
361
curl_pause ($ multi ->openHandles [$ id ][0 ], \CURLPAUSE_CONT );
374
- curl_multi_add_handle ($ multi ->handles [ 0 ] , $ multi ->openHandles [$ id ][0 ]);
362
+ curl_multi_add_handle ($ multi ->handle , $ multi ->openHandles [$ id ][0 ]);
375
363
}
376
364
}
377
365
378
- if (0 !== $ selected = curl_multi_select ($ multi ->handles [ array_key_last ( $ multi -> handles )] , $ timeout )) {
366
+ if (0 !== $ selected = curl_multi_select ($ multi ->handle , $ timeout )) {
379
367
return $ selected ;
380
368
}
381
369
0 commit comments