[HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected
This commit is contained in:
parent
591ad2203c
commit
50a88c59f6
@ -323,6 +323,7 @@ final class CurlResponse implements ResponseInterface
|
|||||||
|
|
||||||
if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
|
if (200 > $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE)) {
|
||||||
$multi->handlesActivity[$id][] = new InformationalChunk($statusCode, $headers);
|
$multi->handlesActivity[$id][] = new InformationalChunk($statusCode, $headers);
|
||||||
|
$location = null;
|
||||||
|
|
||||||
return \strlen($data);
|
return \strlen($data);
|
||||||
}
|
}
|
||||||
@ -346,9 +347,7 @@ final class CurlResponse implements ResponseInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$location = null;
|
if ($statusCode < 300 || 400 <= $statusCode || null === $location || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
|
||||||
|
|
||||||
if ($statusCode < 300 || 400 <= $statusCode || curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {
|
|
||||||
// Headers and redirects completed, time to get the response's body
|
// Headers and redirects completed, time to get the response's body
|
||||||
$multi->handlesActivity[$id][] = new FirstChunk();
|
$multi->handlesActivity[$id][] = new FirstChunk();
|
||||||
|
|
||||||
@ -361,6 +360,8 @@ final class CurlResponse implements ResponseInterface
|
|||||||
$logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url']));
|
$logger->info(sprintf('Redirecting: "%s %s"', $info['http_code'], $info['redirect_url']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$location = null;
|
||||||
|
|
||||||
return \strlen($data);
|
return \strlen($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,7 +257,7 @@ class MockResponse implements ResponseInterface
|
|||||||
$info = $mock->getInfo() ?: [];
|
$info = $mock->getInfo() ?: [];
|
||||||
$response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode() ?: 200;
|
$response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode() ?: 200;
|
||||||
$response->addResponseHeaders($info['response_headers'] ?? [], $response->info, $response->headers);
|
$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 = [
|
$response->info = [
|
||||||
'start_time' => $response->info['start_time'],
|
'start_time' => $response->info['start_time'],
|
||||||
|
@ -176,7 +176,7 @@ final class NativeResponse implements ResponseInterface
|
|||||||
|
|
||||||
$this->multi->handlesActivity[$this->id] = [new FirstChunk()];
|
$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;
|
||||||
$this->multi->handlesActivity[$this->id][] = null;
|
$this->multi->handlesActivity[$this->id][] = null;
|
||||||
|
|
||||||
|
@ -82,6 +82,11 @@ switch ($vars['REQUEST_URI']) {
|
|||||||
header('Location: ..', true, 302);
|
header('Location: ..', true, 302);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '/304':
|
||||||
|
header('Content-Length: 10', true, 304);
|
||||||
|
echo '12345';
|
||||||
|
return;
|
||||||
|
|
||||||
case '/307':
|
case '/307':
|
||||||
header('Location: http://localhost:8057/post', true, 307);
|
header('Location: http://localhost:8057/post', true, 307);
|
||||||
break;
|
break;
|
||||||
|
@ -260,6 +260,17 @@ abstract class HttpClientTestCase extends TestCase
|
|||||||
$response->getStatusCode();
|
$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()
|
public function testRedirects()
|
||||||
{
|
{
|
||||||
$client = $this->getHttpClient(__FUNCTION__);
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
|
Reference in New Issue
Block a user