Skip to content

Commit 86f3976

Browse files
committed
Fix head
1 parent 61dde50 commit 86f3976

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/Request.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ public function doSend(): void
324324
$this->writeable = false;
325325

326326
$body_size = $this->getBody()->getSize();
327-
if ($body_size) {
328-
$this->withHeaders(['Content-Length' => $body_size]);
327+
if ($body_size !== null) {
328+
$this->withHeader('Content-Length', (string)$body_size);
329329
}
330330

331331
$package = str($this);
@@ -380,6 +380,10 @@ public function onMessage($connection, $receive_buffer): void
380380
protected function checkComplete($body): void
381381
{
382382
$status_code = $this->response->getStatusCode();
383+
if (strtoupper($this->getMethod()) === 'HEAD') {
384+
$this->emitSuccess();
385+
return;
386+
}
383387
$content_length = $this->response->getHeaderLine('Content-Length');
384388
if ($content_length === '0' || ($status_code >= 100 && $status_code < 200)
385389
|| $status_code === 204 || $status_code === 304) {

tests/RequestTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,39 @@ public function testProgressStreamHandling()
242242
$this->assertTrue($successCalled);
243243
}
244244

245+
public function testHeadRequestWithoutBody()
246+
{
247+
$successCalled = false;
248+
$errorCalled = false;
249+
$http = new Client();
250+
$expectedBody = json_encode(['ok' => true]);
251+
252+
$options = [
253+
'method' => 'HEAD',
254+
'success' => function ($response) use (&$successCalled, $expectedBody) {
255+
$this->assertInstanceOf(Response::class, $response);
256+
$this->assertEquals(200, $response->getStatusCode());
257+
$this->assertSame('', (string)$response->getBody());
258+
$this->assertSame((string)strlen($expectedBody), $response->getHeaderLine('Content-Length'));
259+
$successCalled = true;
260+
},
261+
'error' => function ($exception) use (&$errorCalled) {
262+
$errorCalled = true;
263+
}
264+
];
265+
266+
$http->request('http://127.0.0.1:7171/head', $options);
267+
268+
for ($i = 0; $i < 10; $i++) {
269+
if ($successCalled || $errorCalled) {
270+
break;
271+
}
272+
Timer::sleep(0.1);
273+
}
274+
$this->assertTrue($successCalled);
275+
$this->assertFalse($errorCalled);
276+
}
277+
245278
/**
246279
* Test synchronous GET request
247280
*/

tests/start.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ function create_test_worker(Closure $callable, $eventLoopClass): void
7878
case '/get':
7979
$connection->send(json_encode($request->get()));
8080
return;
81+
case '/head':
82+
$body = json_encode(['ok' => true]);
83+
if (strtoupper($request->method()) === 'HEAD') {
84+
$length = strlen($body);
85+
$connection->send("HTTP/1.1 200 OK\r\nServer: workerman\r\nContent-Length: $length\r\nConnection: keep-alive\r\n\r\n", true);
86+
return;
87+
}
88+
$connection->send($body);
89+
return;
8190
case '/post':
8291
$connection->send(json_encode($request->post()));
8392
return;

0 commit comments

Comments
 (0)