8000 [HttpClient] fix handling of 3xx with no Location header - ignore Con… · symfony/symfony@50a88c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50a88c5

Browse files
[HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected
1 parent 591ad22 commit 50a88c5

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
323323

324324
if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
325325
$multi->handlesActivity[$id][] = new InformationalChunk($statusCode, $headers);
326+
$location = null;
326327

327328
return \strlen($data);
328329
}
@@ -346,9 +347,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
346347
}
347348
}
348349

349-
$location = null;
350-
351-
if ($statusCode < 300 || 400 <= $statusCode || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
350+
if ($statusCode < 300 || 400 <= $statusCode || null === $location || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
352351
// Headers and redirects completed, time to get the response's body
353352
$multi->handlesActivity[$id][] = new FirstChunk();
354353

@@ -361,6 +360,8 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
361360
$logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url']));
362361
}
363362

363+
$location = null;
364+
364365
return \strlen($data);
365366
}
366367
}

src/Symfony/Component/HttpClient/Response/MockResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private static function readResponse(self $response, array $options, ResponseInt
257257
$info = $mock->getInfo() ?: [];
258258
$response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode() ?: 200;
259259
$response->addResponseHeaders($info['response_headers'] ?? [], $response->info, $response->headers);
260-
$dlSize = isset($response->headers['content-encoding']) ? 0 : (int) ($response->headers['content-length'][0] ?? 0);
260+
$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);
261261

262262
$response->info = [
263263
'start_time' => $response->info['start_time'],

src/Symfony/Component/HttpClient/Response/NativeResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function open(): void
176176

177177
$this->multi->handlesActivity[$this->id] = [new FirstChunk()];
178178

179-
if ('HEAD' === $context['http']['method']) {
179+
if ('HEAD' === $context['http']['method'] || \in_array($this->info['http_code'], [204, 304], true)) {
180180
$this->multi->handlesActivity[$this->id][] = null;
181181
$this->multi->handlesActivity[$this->id][] = null;
182182

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
header('Location: ..', true, 302);
8383
break;
8484

85+
case '/304':
86+
header('Content-Length: 10', true, 304);
87+
echo '12345';
88+
return;
89+
8590
case '/307':
8691
header('Location: http://localhost:8057/post', true, 307);
8792
break;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ public function testBadRequestBody()
260260
$response->getStatusCode();
261261
}
262262

263+
public function test304()
264+
{
265+
$client = $this->getHttpClient(__FUNCTION__);
266+
$response = $client->request('GET', 'http://localhost:8057/304', [
267+
'headers' => ['If-Match' => '"abc"'],
268+
]);
269+
270+
$this->assertSame(304, $response->getStatusCode());
271+
$this->assertSame('', $response->getContent(false));
272+
}
273+
263274
public function testRedirects()
264275
{
265276
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)
0