Skip to content

Commit efa9cc2

Browse files
authored
Merge pull request #444 from mrsimonbennett/master
Allow Explicit content length on HEAD requests
2 parents e27cfcd + 185680b commit efa9cc2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Io/StreamingServer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
265265
if (($method === 'CONNECT' && $code >= 200 && $code < 300) || ($code >= 100 && $code < 200) || $code === Response::STATUS_NO_CONTENT) {
266266
// 2xx response to CONNECT and 1xx and 204 MUST NOT include Content-Length or Transfer-Encoding header
267267
$response = $response->withoutHeader('Content-Length');
268+
} elseif ($method === 'HEAD' && $response->hasHeader('Content-Length')) {
269+
// HEAD Request: preserve explicit Content-Length
268270
} elseif ($code === Response::STATUS_NOT_MODIFIED && ($response->hasHeader('Content-Length') || $body->getSize() === 0)) {
269271
// 304 Not Modified: preserve explicit Content-Length and preserve missing header if body is empty
270272
} elseif ($body->getSize() !== null) {

tests/Io/StreamingServerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,38 @@ function ($data) use (&$buffer) {
14261426
$this->assertContainsString("\r\nContent-Length: 3\r\n", $buffer);
14271427
}
14281428

1429+
public function testResponseContainsExplicitContentLengthHeaderForHeadRequests()
1430+
{
1431+
$server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) {
1432+
return new Response(
1433+
200,
1434+
array('Content-Length' => 3),
1435+
''
1436+
);
1437+
});
1438+
1439+
$buffer = '';
1440+
$this->connection
1441+
->expects($this->any())
1442+
->method('write')
1443+
->will(
1444+
$this->returnCallback(
1445+
function ($data) use (&$buffer) {
1446+
$buffer .= $data;
1447+
}
1448+
)
1449+
);
1450+
1451+
$server->listen($this->socket);
1452+
$this->socket->emit('connection', array($this->connection));
1453+
1454+
$data = "HEAD / HTTP/1.1\r\nHost: localhost\r\n\r\n";
1455+
$this->connection->emit('data', array($data));
1456+
1457+
$this->assertContainsString("HTTP/1.1 200 OK\r\n", $buffer);
1458+
$this->assertContainsString("\r\nContent-Length: 3\r\n", $buffer);
1459+
}
1460+
14291461
public function testResponseContainsNoResponseBodyForNotModifiedStatus()
14301462
{
14311463
$server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) {

0 commit comments

Comments
 (0)