@@ -79,17 +79,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
79
79
}
80
80
81
81
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 );
93
83
});
94
84
95
85
if (null === $ options ) {
@@ -366,19 +356,29 @@ private static function select(ClientState $multi, float $timeout): int
366
356
*/
367
357
private static function parseHeaderLine ($ ch , string $ data , array &$ info , array &$ headers , ?array $ options , CurlClientState $ multi , int $ id , ?string &$ location , ?callable $ resolveRedirect , ?LoggerInterface $ logger ): int
368
358
{
359
+ if (!str_ends_with ($ data , "\r\n" )) {
360
+ return 0 ;
361
+ }
362
+
369
363
$ waitFor = @curl_getinfo ($ ch , \CURLINFO_PRIVATE ) ?: '_0 ' ;
370
364
371
365
if ('H ' !== $ waitFor [0 ]) {
372
366
return \strlen ($ data ); // Ignore HTTP trailers
373
367
}
374
368
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 ) {
376
376
// Regular header line: add it to the list
377
- self ::addResponseHeaders ([$ data ], $ info , $ headers );
377
+ self ::addResponseHeaders ([substr ( $ data, 0 , - 2 ) ], $ info , $ headers );
378
378
379
379
if (!str_starts_with ($ data , 'HTTP/ ' )) {
380
380
if (0 === stripos ($ data , 'Location: ' )) {
381
- $ location = trim (substr ($ data , 9 ));
381
+ $ location = trim (substr ($ data , 9 , - 2 ));
382
382
}
383
383
384
384
return \strlen ($ data );
@@ -401,7 +401,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
401
401
402
402
// End of headers: handle informational responses, redirects, etc.
403
403
404
- if (200 > $ statusCode = curl_getinfo ( $ ch , \ CURLINFO_RESPONSE_CODE ) ) {
404
+ if (200 > $ statusCode ) {
405
405
$ multi ->handlesActivity [$ id ][] = new InformationalChunk ($ statusCode , $ headers );
406
406
$ location = null ;
407
407
0 commit comments