@@ -76,17 +76,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
76
76
}
77
77
78
78
curl_setopt ($ ch , \CURLOPT_HEADERFUNCTION , static function ($ ch , string $ data ) use (&$ info , &$ headers , $ options , $ multi , $ id , &$ location , $ resolveRedirect , $ logger ): int {
79
- if (0 !== substr_compare ($ data , "\r\n" , -2 )) {
80
- return 0 ;
81
- }
82
-
83
- $ len = 0 ;
84
-
85
- foreach (explode ("\r\n" , substr ($ data , 0 , -2 )) as $ data ) {
86
- $ len += 2 + self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger );
87
- }
88
-
89
- return $ len ;
79
+ return self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger );
90
80
});
91
81
92
82
if (null === $ options ) {
@@ -381,19 +371,29 @@ private static function select(ClientState $multi, float $timeout): int
381
371
*/
382
372
private static function parseHeaderLine ($ ch , string $ data , array &$ info , array &$ headers , ?array $ options , CurlClientState $ multi , int $ id , ?string &$ location , ?callable $ resolveRedirect , ?LoggerInterface $ logger ): int
383
373
{
374
+ if (!str_ends_with ($ data , "\r\n" )) {
375
+ return 0 ;
376
+ }
377
+
384
378
$ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
385
379
386
380
if ('H ' !== $ waitFor [0 ]) {
387
381
return \strlen ($ data ); // Ignore HTTP trailers
388
382
}
389
383
390
- if ('' !== $ data ) {
384
+ $ statusCode = curl_getinfo ($ ch , \CURLINFO_RESPONSE_CODE );
385
+
386
+ if ($ statusCode !== $ info ['http_code ' ] && !preg_match ("#^HTTP/\d+(?:\.\d+)? {$ statusCode }(?: | \r\n$)# " , $ data )) {
387
+ return \strlen ($ data ); // Ignore headers from responses to CONNECT requests
388
+ }
389
+
390
+ if ("\r\n" !== $ data ) {
391
391
// Regular header line: add it to the list
392
- self ::addResponseHeaders ([$ data ], $ info , $ headers );
392
+ self ::addResponseHeaders ([substr ( $ data, 0 , - 2 ) ], $ info , $ headers );
393
393
394
394
if (!str_starts_with ($ data , 'HTTP/ ' )) {
395
395
if (0 === stripos ($ data , 'Location: ' )) {
396
- $ location = trim (substr ($ data , 9 ));
396
+ $ location = trim (substr ($ data , 9 , - 2 ));
397
397
}
398
398
399
399
return \strlen ($ data );
@@ -416,7 +416,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
416
416
417
417
// End of headers: handle informational responses, redirects, etc.
418
418
419
- if (200 > $ statusCode = curl_getinfo ( $ ch , \ CURLINFO_RESPONSE_CODE ) ) {
419
+ if (200 > $ statusCode ) {
420
420
$ multi ->handlesActivity [$ id ][] = new InformationalChunk ($ statusCode , $ headers );
421
421
$ location = null ;
422
422
0 commit comments