Skip to content

Commit 4296c86

Browse files
[HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected
1 parent 5309e5e commit 4296c86

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

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
}

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'],

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

0 commit comments

Comments
 (0)