Skip to content

Commit 7200fb5

Browse files
author
Kyra Farrow
committed
bug #32141 [HttpClient] fix dealing with 1xx informational responses (nicolas-grekas)
This PR was merged into the 4.3 branch. Discussion ---------- [HttpClient] fix dealing with 1xx informational responses | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I never had a look at 1xx status codes until today. This PR fixes reading them when using curl. If one wonders: - `NativeHttpClient` uses `fopen()`, which skips informational parts as allowed by the HTTP spec and doesn't give any way to access their response headers. - `CurlHttpClient` allows reading informational responses using the progress callback or via the getInfo() method. That's the way if you need to implement e.g. HTTP 103 early hints. Commits ------- 412411d795 [HttpClient] fix dealing with 1xx informational responses
2 parents 5690582 + 6fa8387 commit 7200fb5

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Response/CurlResponse.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,19 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
287287
// Regular header line: add it to the list
288288
self::addResponseHeaders([substr($data, 0, -2)], $info, $headers);
289289

290-
if (0 === strpos($data, 'HTTP') && 300 <= $info['http_code'] && $info['http_code'] < 400) {
290+
if (0 !== strpos($data, 'HTTP/')) {
291+
if (0 === stripos($data, 'Location:')) {
292+
$location = trim(substr($data, 9, -2));
293+
}
294+
295+
return \strlen($data);
296+
}
297+
298+
if (\function_exists('openssl_x509_read') && $certinfo = curl_getinfo($ch, CURLINFO_CERTINFO)) {
299+
$info['peer_certificate_chain'] = array_map('openssl_x509_read', array_column($certinfo, 'Cert'));
300+
}
301+
302+
if (300 <= $info['http_code'] && $info['http_code'] < 400) {
291303
if (curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
292304
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
293305
} elseif (303 === $info['http_code'] || ('POST' === $info['http_method'] && \in_array($info['http_code'], [301, 302], true))) {
@@ -296,15 +308,14 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
296308
}
297309
}
298310

299-
if (0 === stripos($data, 'Location:')) {
300-
$location = trim(substr($data, 9, -2));
301-
}
302-
303311
return \strlen($data);
304312
}
305313

306314
// End of headers: handle redirects and add to the activity list
307-
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
315+
if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
316+
return \strlen($data);
317+
}
318+
308319
$info['redirect_url'] = null;
309320

310321
if (300 <= $statusCode && $statusCode < 400 && null !== $location) {
@@ -334,10 +345,6 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
334345
return 0;
335346
}
336347

337-
if (\function_exists('openssl_x509_read') && $certinfo = curl_getinfo($ch, CURLINFO_CERTINFO)) {
338-
$info['peer_certificate_chain'] = array_map('openssl_x509_read', array_column($certinfo, 'Cert'));
339-
}
340-
341348
curl_setopt($ch, CURLOPT_PRIVATE, 'content');
342349
} elseif (null !== $info['redirect_url'] && $logger) {
343350
$logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url']));

0 commit comments

Comments
 (0)