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