8000 [HttpClient] ignore the body of responses to HEAD requests · symfony/symfony@0fc371e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fc371e

Browse files
[HttpClient] ignore the body of responses to HEAD requests
1 parent 5da67f9 commit 0fc371e

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public function request(string $method, string $url, array $options = []): Respo
197197
if ('POST' === $method) {
198198
// Use CURLOPT_POST to have browser-like POST-to-GET redirects for 301, 302 and 303
199199
$curlopts[CURLOPT_POST] = true;
200+
} elseif ('HEAD' === $method) {
201+
$curlopts[CURLOPT_NOBODY] = true;
200202
} else {
201203
$curlopts[CURLOPT_CUSTOMREQUEST] = $method;
202204
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected static function perform(ClientState $multi, array &$responses): void
176176
try {
177177
$offset = 0;
178178
$chunk[1]->getStatusCode();
179-
$response->headers = $chunk[1]->getHeaders(false);
179+
$chunk[1]->getHeaders(false);
180180
self::readResponse($response, $chunk[0], $chunk[1], $offset);
181181
$multi->handlesActivity[$id][] = new FirstChunk();
182182
} catch (\Throwable $e) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,16 @@ private function open(): void
174174
$this->inflate = null;
175175
}
176176

177-
$this->multi->openHandles[$this->id] = [$h, $this->buffer, $this->inflate, $this->content, $this->onProgress, &$this->remaining, &< 8000 span class=pl-s1>$this->info];
178177
$this->multi->handlesActivity[$this->id] = [new FirstChunk()];
178+
179+
if ('HEAD' === $context['http']['method']) {
180+
$this->multi->handlesActivity[$this->id][] = null;
181+
$this->multi->handlesActivity[$this->id][] = null;
182+
183+
return;
184+
}
185+
186+
$this->multi->openHandles[$this->id] = [$h, $this->buffer, $this->inflate, $this->content, $this->onProgress, &$this->remaining, &$this->info];
179187
}
180188

181189
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ public function testGetRequest()
7272
$response->getContent();
7373
}
7474

75+
public function testHeadRequest()
76+
{
77+
$client = $this->getHttpClient(__FUNCTION__);
78+
$response = $client->request('HEAD', 'http://localhost:8057', [
79+
'headers' => ['Foo' => 'baR'],
80+
'user_data' => $data = new \stdClass(),
81+
]);
82+
83+
$this->assertSame([], $response->getInfo('response_headers'));
84+
$this->assertSame($data, $response->getInfo()['user_data']);
85+
$this->assertSame(200, $response->getStatusCode());
86+
87+
$info = $response->getInfo();
88+
$this->assertNull($info['error']);
89+
$this->assertSame(0, $info['redirect_count']);
90+
$this->assertSame('HTTP/1.1 200 OK', $info['response_headers'][0]);
91+
$this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
92+
$this->assertSame('http://localhost:8057/', $info['url']);
93+
94+
$headers = $response->getHeaders();
95+
96+
$this->assertSame('localhost:8057', $headers['host'][0]);
97+
$this->assertSame(['application/json'], $headers['content-type']);
98+
99+
$this->assertSame('', $response->getContent());
100+
}
101+
75102
public function testNonBufferedGetRequest()
76103
{
77104
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)
0