Skip to content

Commit 0219834

Browse files
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 ------- 412411d [HttpClient] fix dealing with 1xx informational responses
2 parents c245f7c + 412411d commit 0219834

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/Symfony/Component/HttpClient/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']));

src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
ob_start('ob_gzhandler');
4242
break;
4343

44+
case '/103':
45+
header('HTTP/1.1 103 Early Hints');
46+
header('Link: </style.css>; rel=preload; as=style', false);
47+
header('Link: </script.js>; rel=preload; as=script', false);
48+
echo "HTTP/1.1 200 OK\r\n";
49+
echo "Date: Fri, 26 May 2017 10:02:11 GMT\r\n";
50+
echo "Content-Length: 13\r\n";
51+
echo "\r\n";
52+
echo 'Here the body';
53+
exit;
54+
4455
case '/404':
4556
header('Content-Type: application/json', true, 404);
4657
break;

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,15 @@ public function testQuery()
721721
$this->assertSame('/?a=a&b=b', $body['REQUEST_URI']);
722722
}
723723

724+
public function testInformationalResponse()
725+
{
726+
$client = $this->getHttpClient(__FUNCTION__);
727+
$response = $client->request('GET', 'http://localhost:8057/103');
728+
729+
$this->assertSame('Here the body', $response->getContent());
730+
$this->assertSame(200, $response->getStatusCode());
731+
}
732+
724733
/**
725734
* @requires extension zlib
726735
*/

0 commit comments

Comments
 (0)