Skip to content

Commit

Permalink
bug #34116 [HttpClient] ignore the body of responses to HEAD requests…
Browse files Browse the repository at this point in the history
… (nicolas-grekas)

This PR was merged into the 4.3 branch.

Discussion
----------

[HttpClient] ignore the body of responses to HEAD requests

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34102
| License       | MIT
| Doc PR        | -

Commits
-------

0fc371e [HttpClient] ignore the body of responses to HEAD requests
  • Loading branch information
fabpot committed Oct 25, 2019
2 parents 8bf8c50 + 0fc371e commit 6c08ac5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpClient/CurlHttpClient.php
Expand Up @@ -197,6 +197,8 @@ public function request(string $method, string $url, array $options = []): Respo
if ('POST' === $method) {
// Use CURLOPT_POST to have browser-like POST-to-GET redirects for 301, 302 and 303
$curlopts[CURLOPT_POST] = true;
} elseif ('HEAD' === $method) {
$curlopts[CURLOPT_NOBODY] = true;
} else {
$curlopts[CURLOPT_CUSTOMREQUEST] = $method;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/Response/MockResponse.php
Expand Up @@ -176,7 +176,7 @@ protected static function perform(ClientState $multi, array &$responses): void
try {
$offset = 0;
$chunk[1]->getStatusCode();
$response->headers = $chunk[1]->getHeaders(false);
$chunk[1]->getHeaders(false);
self::readResponse($response, $chunk[0], $chunk[1], $offset);
$multi->handlesActivity[$id][] = new FirstChunk();
} catch (\Throwable $e) {
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpClient/Response/NativeResponse.php
Expand Up @@ -174,8 +174,16 @@ private function open(): void
$this->inflate = null;
}

$this->multi->openHandles[$this->id] = [$h, $this->buffer, $this->inflate, $this->content, $this->onProgress, &$this->remaining, &$this->info];
$this->multi->handlesActivity[$this->id] = [new FirstChunk()];

if ('HEAD' === $context['http']['method']) {
$this->multi->handlesActivity[$this->id][] = null;
$this->multi->handlesActivity[$this->id][] = null;

return;
}

$this->multi->openHandles[$this->id] = [$h, $this->buffer, $this->inflate, $this->content, $this->onProgress, &$this->remaining, &$this->info];
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Expand Up @@ -72,6 +72,33 @@ public function testGetRequest()
$response->getContent();
}

public function testHeadRequest()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('HEAD', 'http://localhost:8057', [
'headers' => ['Foo' => 'baR'],
'user_data' => $data = new \stdClass(),
]);

$this->assertSame([], $response->getInfo('response_headers'));
$this->assertSame($data, $response->getInfo()['user_data']);
$this->assertSame(200, $response->getStatusCode());

$info = $response->getInfo();
$this->assertNull($info['error']);
$this->assertSame(0, $info['redirect_count']);
$this->assertSame('HTTP/1.1 200 OK', $info['response_headers'][0]);
$this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
$this->assertSame('http://localhost:8057/', $info['url']);

$headers = $response->getHeaders();

$this->assertSame('localhost:8057', $headers['host'][0]);
$this->assertSame(['application/json'], $headers['content-type']);

$this->assertSame('', $response->getContent());
}

public function testNonBufferedGetRequest()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down

0 comments on commit 6c08ac5

Please sign in to comment.