Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Explicit content length on HEAD requests #444

Merged
merged 1 commit into from Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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