From 50a88c59f65c341a5993203d673cbf1ded551d4f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 28 Oct 2019 16:45:56 +0100 Subject: [PATCH] [HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected --- .../Component/HttpClient/Response/CurlResponse.php | 7 ++++--- .../Component/HttpClient/Response/MockResponse.php | 2 +- .../Component/HttpClient/Response/NativeResponse.php | 2 +- .../Contracts/HttpClient/Test/Fixtures/web/index.php | 5 +++++ .../Contracts/HttpClient/Test/HttpClientTestCase.php | 11 +++++++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpClient/Response/CurlResponse.php b/src/Symfony/Component/HttpClient/Response/CurlResponse.php index 6f4ebf554ff92..4b25acf79565f 100644 --- a/src/Symfony/Component/HttpClient/Response/CurlResponse.php +++ b/src/Symfony/Component/HttpClient/Response/CurlResponse.php @@ -323,6 +323,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array & if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) { $multi->handlesActivity[$id][] = new InformationalChunk($statusCode, $headers); + $location = null; return \strlen($data); } @@ -346,9 +347,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array & } } - $location = null; - - if ($statusCode < 300 || 400 <= $statusCode || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) { + if ($statusCode < 300 || 400 <= $statusCode || null === $location || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) { // Headers and redirects completed, time to get the response's body $multi->handlesActivity[$id][] = new FirstChunk(); @@ -361,6 +360,8 @@ private static function parseHeaderLine($ch, string $data, array &$info, array & $logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url'])); } + $location = null; + return \strlen($data); } } diff --git a/src/Symfony/Component/HttpClient/Response/MockResponse.php b/src/Symfony/Component/HttpClient/Response/MockResponse.php index 3b961db4d13f3..3856112a09d39 100644 --- a/src/Symfony/Component/HttpClient/Response/MockResponse.php +++ b/src/Symfony/Component/HttpClient/Response/MockResponse.php @@ -257,7 +257,7 @@ private static function readResponse(self $response, array $options, ResponseInt $info = $mock->getInfo() ?: []; $response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode() ?: 200; $response->addResponseHeaders($info['response_headers'] ?? [], $response->info, $response->headers); - $dlSize = isset($response->headers['content-encoding']) ? 0 : (int) ($response->headers['content-length'][0] ?? 0); + $dlSize = isset($response->headers['content-encoding']) || 'HEAD' === $response->info['http_method'] || \in_array($response->info['http_code'], [204, 304], true) ? 0 : (int) ($response->headers['content-length'][0] ?? 0); $response->info = [ 'start_time' => $response->info['start_time'], diff --git a/src/Symfony/Component/HttpClient/Response/NativeResponse.php b/src/Symfony/Component/HttpClient/Response/NativeResponse.php index 3115bade504d9..a9865ed09e494 100644 --- a/src/Symfony/Component/HttpClient/Response/NativeResponse.php +++ b/src/Symfony/Component/HttpClient/Response/NativeResponse.php @@ -176,7 +176,7 @@ private function open(): void $this->multi->handlesActivity[$this->id] = [new FirstChunk()]; - if ('HEAD' === $context['http']['method']) { + if ('HEAD' === $context['http']['method'] || \in_array($this->info['http_code'], [204, 304], true)) { $this->multi->handlesActivity[$this->id][] = null; $this->multi->handlesActivity[$this->id][] = null; diff --git a/src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php b/src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php index ec03bb61c2b84..10ed100ccc826 100644 --- a/src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php +++ b/src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php @@ -82,6 +82,11 @@ header('Location: ..', true, 302); break; + case '/304': + header('Content-Length: 10', true, 304); + echo '12345'; + return; + case '/307': header('Location: http://localhost:8057/post', true, 307); break; diff --git a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php index 78d0f1b39c25a..df7a597590deb 100644 --- a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php +++ b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php @@ -260,6 +260,17 @@ public function testBadRequestBody() $response->getStatusCode(); } + public function test304() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://localhost:8057/304', [ + 'headers' => ['If-Match' => '"abc"'], + ]); + + $this->assertSame(304, $response->getStatusCode()); + $this->assertSame('', $response->getContent(false)); + } + public function testRedirects() { $client = $this->getHttpClient(__FUNCTION__); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy