Skip to content

Commit

Permalink
Merge pull request #444 from mrsimonbennett/master
Browse files Browse the repository at this point in the history
Allow Explicit content length on HEAD requests
  • Loading branch information
WyriHaximus committed Mar 7, 2022
2 parents e27cfcd + 185680b commit efa9cc2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Io/StreamingServer.php
Expand Up @@ -265,6 +265,8 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
if (($method === 'CONNECT' && $code >= 200 && $code < 300) || ($code >= 100 && $code < 200) || $code === Response::STATUS_NO_CONTENT) {
// 2xx response to CONNECT and 1xx and 204 MUST NOT include Content-Length or Transfer-Encoding header
$response = $response->withoutHeader('Content-Length');
} elseif ($method === 'HEAD' && $response->hasHeader('Content-Length')) {
// HEAD Request: preserve explicit Content-Length
} elseif ($code === Response::STATUS_NOT_MODIFIED && ($response->hasHeader('Content-Length') || $body->getSize() === 0)) {
// 304 Not Modified: preserve explicit Content-Length and preserve missing header if body is empty
} elseif ($body->getSize() !== null) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Io/StreamingServerTest.php
Expand Up @@ -1426,6 +1426,38 @@ function ($data) use (&$buffer) {
$this->assertContainsString("\r\nContent-Length: 3\r\n", $buffer);
}

public function testResponseContainsExplicitContentLengthHeaderForHeadRequests()
{
$server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Length' => 3),
''
);
});

$buffer = '';
$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$server->listen($this->socket);
$this->socket->emit('connection', array($this->connection));

$data = "HEAD / HTTP/1.1\r\nHost: localhost\r\n\r\n";
$this->connection->emit('data', array($data));

$this->assertContainsString("HTTP/1.1 200 OK\r\n", $buffer);
$this->assertContainsString("\r\nContent-Length: 3\r\n", $buffer);
}

public function testResponseContainsNoResponseBodyForNotModifiedStatus()
{
$server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) {
Expand Down

0 comments on commit efa9cc2

Please sign in to comment.