@@ -319,14 +319,7 @@ private function useHeadersToDetermineWriteCallback($opts, $determineWriteCallba
319
319
{
320
320
$ rheaders = new Util \CaseInsensitiveArray ();
321
321
$ headerCallback = function ($ curl , $ header_line ) use (&$ rheaders ) {
322
- // Ignore the HTTP request line (HTTP/1.1 200 OK)
323
- if (false === \strpos ($ header_line , ': ' )) {
324
- return \strlen ($ header_line );
325
- }
326
- list ($ key , $ value ) = \explode (': ' , \trim ($ header_line ), 2 );
327
- $ rheaders [\trim ($ key )] = \trim ($ value );
328
-
329
- return \strlen ($ header_line );
322
+ return self ::parseLineIntoHeaderArray ($ header_line , $ rheaders );
330
323
};
331
324
332
325
$ writeCallback = null ;
@@ -341,6 +334,17 @@ private function useHeadersToDetermineWriteCallback($opts, $determineWriteCallba
341
334
return [$ headerCallback , $ writeCallbackWrapper ];
342
335
}
343
336
337
+ private static function parseLineIntoHeaderArray ($ line , &$ headers )
338
+ {
339
+ if (false === \strpos ($ line , ': ' )) {
340
+ return \strlen ($ line );
341
+ }
342
+ list ($ key , $ value ) = \explode (': ' , \trim ($ line ), 2 );
343
+ $ headers [\trim ($ key )] = \trim ($ value );
344
+
345
+ return \strlen ($ line );
346
+ }
347
+
344
348
/**
345
349
* Like `executeRequestWithRetries` except:
346
350
* 1. Does not buffer the body of a successful (status code < 300)
@@ -363,10 +367,6 @@ public function executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChun
363
367
/** @var int */
364
368
$ numRetries = 0 ;
365
369
366
- // Did the last request return statusCode < 300?
367
- /** @var null|bool */
368
- $ succeeded = null ;
369
-
370
370
// Will contain the bytes of the body of the last request
371
371
// if it was not successful and should not be retries
372
372
/** @var null|string */
@@ -380,23 +380,27 @@ public function executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChun
380
380
/** @var null|array */
381
381
$ lastRHeaders = null ;
382
382
383
+ $ errno = null ;
384
+ $ message = null ;
385
+
383
386
$ determineWriteCallback = function ($ rheaders ) use (
384
387
&$ readBodyChunk ,
385
388
&$ shouldRetry ,
386
- &$ succeeded ,
387
389
&$ rbody ,
388
390
&$ numRetries ,
389
391
&$ rcode ,
390
- &$ lastRHeaders
392
+ &$ lastRHeaders ,
393
+ &$ errno ,
394
+ &$ message
391
395
) {
392
396
$ lastRHeaders = $ rheaders ;
393
397
$ errno = \curl_errno ($ this ->curlHandle );
398
+
394
399
$ rcode = \curl_getinfo ($ this ->curlHandle , \CURLINFO_HTTP_CODE );
395
400
396
401
// Send the bytes from the body of a successful request to the caller-provided $readBodyChunk.
397
402
if ($ rcode < 300 ) {
398
403
$ rbody = null ;
399
- $ succeeded = true ;
400
404
401
405
return function ($ curl , $ data ) use (&$ readBodyChunk ) {
402
406
// Don't expose the $curl handle to the user, and don't require them to
@@ -406,7 +410,6 @@ public function executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChun
406
410
return \strlen ($ data );
407
411
};
408
412
}
409
- $ succeeded = false ;
410
413
411
414
$ shouldRetry = $ this ->shouldRetry ($ errno , $ rcode , $ rheaders , $ numRetries );
412
415
@@ -435,10 +438,24 @@ public function executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChun
435
438
436
439
$ shouldRetry = false ;
437
440
$ rbody = null ;
438
- $ succeeded = null ;
439
441
$ this ->resetCurlHandle ();
440
442
\curl_setopt_array ($ this ->curlHandle , $ opts );
441
443
$ result = \curl_exec ($ this ->curlHandle );
444
+ $ errno = \curl_errno ($ this ->curlHandle );
445
+ if (0 !== $ errno ) {
446
+ $ message = \curl_error ($ this ->curlHandle );
447
+ }
448
+ if (!$ this ->getEnablePersistentConnections ()) {
449
+ $ this ->closeCurlHandle ();
450
+ }
451
+
452
+ if (\is_callable ($ this ->getRequestStatusCallback ())) {
453
+ \call_user_func_array (
454
+ $ this ->getRequestStatusCallback (),
455
+ [$ rbody , $ rcode , $ lastRHeaders , $ errno , $ message , $ shouldRetry , $ numRetries ]
456
+ );
457
+ }
458
+
442
459
if ($ shouldRetry ) {
443
460
++$ numRetries ;
444
461
$ sleepSeconds = $ this ->sleepTime ($ numRetries , $ lastRHeaders );
@@ -448,9 +465,7 @@ public function executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChun
448
465
}
449
466
}
450
467
451
- $ errno = \curl_errno ($ this ->curlHandle );
452
468
if (0 !== $ errno ) {
453
- $ message = \curl_error ($ this ->curlHandle );
454
469
$ this ->handleCurlError ($ absUrl , $ errno , $ message , $ numRetries );
455
470
}
456
471
@@ -473,14 +488,7 @@ public function executeRequestWithRetries($opts, $absUrl)
473
488
// Create a callback to capture HTTP headers for the response
474
489
$ rheaders = new Util \CaseInsensitiveArray ();
475
490
$ headerCallback = function ($ curl , $ header_line ) use (&$ rheaders ) {
476
- // Ignore the HTTP request line (HTTP/1.1 200 OK)
477
- if (false === \strpos ($ header_line , ': ' )) {
478
- return \strlen ($ header_line );
479
- }
480
- list ($ key , $ value ) = \explode (': ' , \trim ($ header_line ), 2 );
481
- $ rheaders [\trim ($ key )] = \trim ($ value );
482
-
483
- return \strlen ($ header_line );
491
+ return CurlClient::parseLineIntoHeaderArray ($ header_line , $ rheaders );
484
492
};
485
493
$ opts [\CURLOPT_HEADERFUNCTION ] = $ headerCallback ;
486
494
0 commit comments