8000 [HttpClient] fix dealing with 1xx informational responses by nicolas-grekas · Pull Request #32141 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] fix dealing with 1xx informational responses #32141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpClient] fix dealing with 1xx informational responses
  • Loading branch information
nicolas-grekas committed Jun 23, 2019
commit 412411d795d8c264d6b902b95b1db5dba4bfb04d
27 changes: 17 additions & 10 deletions src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,19 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
// Regular header line: add it to the list
self::addResponseHeaders([substr($data, 0, -2)], $info, $headers);

if (0 === strpos($data, 'HTTP') && 300 <= $info['http_code'] && $info['http_code'] < 400) {
if (0 !== strpos($data, 'HTTP/')) {
if (0 === stripos($data, 'Location:')) {
$location = trim(substr($data, 9, -2));
}

return \strlen($data);
}

if (\function_exists('openssl_x509_read') && $certinfo = curl_getinfo($ch, CURLINFO_CERTINFO)) {
$info['peer_certificate_chain'] = array_map('openssl_x509_read', array_column($certinfo, 'Cert'));
}

if (300 <= $info['http_code'] && $info['http_code'] < 400) {
if (curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
} elseif (303 === $info['http_code'] || ('POST' === $info['http_method'] && \in_array($info['http_code'], [301, 302], true))) {
Expand All @@ -298,15 +310,14 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
}
}

if (0 === stripos($data, 'Location:')) {
$location = trim(substr($data, 9, -2));
}

return \strlen($data);
}

// End of headers: handle redirects and add to the activity list
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
return \strlen($data);
}

$info['redirect_url'] = null;

if (300 <= $statusCode && $statusCode < 400 && null !== $location) {
Expand Down Expand Up @@ -336,10 +347,6 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
return 0;
}

if (\function_exists('openssl_x509_read') && $certinfo = curl_getinfo($ch, CURLINFO_CERTINFO)) {
$info['peer_certificate_chain'] = array_map('openssl_x509_read', array_column($certinfo, 'Cert'));
}

curl_setopt($ch, CURLOPT_PRIVATE, 'content');
} elseif (null !== $info['redirect_url'] && $logger) {
$logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url']));
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
ob_start('ob_gzhandler');
break;

case '/103':
header('HTTP/1.1 103 Early Hints');
header('Link: </style.css>; rel=preload; as=style', false);
header('Link: </script.js>; rel=preload; as=script', false);
echo "HTTP/1.1 200 OK\r\n";
echo "Date: Fri, 26 May 2017 10:02:11 GMT\r\n";
echo "Content-Length: 13\r\n";
echo "\r\n";
echo 'Here the body';
exit;

case '/404':
header('Content-Type: application/json', true, 404);
break;
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,15 @@ public function testQuery()
$this->assertSame('/?a=a&b=b', $body['REQUEST_URI']);
}

public function testInformationalResponse()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057/103');

$this->assertSame('Here the body', $response->getContent());
$this->assertSame(200, $response->getStatusCode());
}

/**
* @requires extension zlib
*/
Expand Down
0