@@ -79,17 +79,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
7979 }
8080
8181 curl_setopt ($ ch , \CURLOPT_HEADERFUNCTION , static function ($ ch , string $ data ) use (&$ info , &$ headers , $ options , $ multi , $ id , &$ location , $ resolveRedirect , $ logger ): int {
82- if (0 !== substr_compare ($ data , "\r\n" , -2 )) {
83- return 0 ;
84- }
85-
86- $ len = 0 ;
87-
88- foreach (explode ("\r\n" , substr ($ data , 0 , -2 )) as $ data ) {
89- $ len += 2 + self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger );
90- }
91-
92- return $ len ;
82+ return self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger );
9383 });
9484
9585 if (null === $ options ) {
@@ -366,19 +356,29 @@ private static function select(ClientState $multi, float $timeout): int
366356 */
367357 private static function parseHeaderLine ($ ch , string $ data , array &$ info , array &$ headers , ?array $ options , CurlClientState $ multi , int $ id , ?string &$ location , ?callable $ resolveRedirect , ?LoggerInterface $ logger ): int
368358 {
359+ if (!str_ends_with ($ data , "\r\n" )) {
360+ return 0 ;
361+ }
362+
369363 $ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
370364
371365 if ('H ' !== $ waitFor [0 ]) {
372366 return \strlen ($ data ); // Ignore HTTP trailers
373367 }
374368
375- if ('' !== $ data ) {
369+ $ statusCode = curl_getinfo ($ ch , \CURLINFO_RESPONSE_CODE );
370+
371+ if ($ statusCode !== $ info ['http_code ' ] && !preg_match ("#^HTTP/\d+(?:\.\d+)? {$ statusCode }(?: | \r\n$)# " , $ data )) {
372+ return \strlen ($ data ); // Ignore headers from responses to CONNECT requests
373+ }
374+
375+ if ("\r\n" !== $ data ) {
376376 // Regular header line: add it to the list
377- self ::addResponseHeaders ([$ data ], $ info , $ headers );
377+ self ::addResponseHeaders ([substr ( $ data, 0 , - 2 ) ], $ info , $ headers );
378378
379379 if (!str_starts_with ($ data , 'HTTP/ ' )) {
380380 if (0 === stripos ($ data , 'Location: ' )) {
381- $ location = trim (substr ($ data , 9 ));
381+ $ location = trim (substr ($ data , 9 , - 2 ));
382382 }
383383
384384 return \strlen ($ data );
@@ -401,7 +401,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
401401
402402 // End of headers: handle informational responses, redirects, etc.
403403
404- if (200 > $ statusCode = curl_getinfo ( $ ch , \ CURLINFO_RESPONSE_CODE ) ) {
404+ if (200 > $ statusCode ) {
405405 $ multi ->handlesActivity [$ id ][] = new InformationalChunk ($ statusCode , $ headers );
406406 $ location = null ;
407407
0 commit comments