8000 [HttpFoundation] Send `Content-Length` when calling `Response::send()` and the content is a non-empty string by nicolas-grekas · Pull Request #45092 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] Send Content-Length when calling Response::send() and the content is a non-empty string #45092

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

Merged
merged 1 commit into from
Apr 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add stale while revalidate and stale if error cache header
* Allow dynamic session "ttl" when using a remote storage
* Send `Content-Length` when calling `Response::send()` and the content is a non-empty string

6.0
---
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ public function sendContent(): static
*/
public function send(): static
{
if (\is_string($this->content) && '' !== $this->content && !$this->headers->has('Transfer-Encoding')) {
$this->headers->set('Content-Length', \strlen($this->content));
}

$this->sendHeaders();
$this->sendContent();

Expand Down
28 changes: 21 additions & 7 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,27 @@ public function testSendHeaders()
public function testSend()
{
$response = new Response();
$responseSend = $response->send();
$this->assertObjectHasAttribute('headers', $responseSend);
$this->assertObjectHasAttribute('content', $responseSend);
$this->assertObjectHasAttribute('version', $responseSend);
$this->assertObjectHasAttribute('statusCode', $responseSend);
$this->assertObjectHasAttribute('statusText', $responseSend);
$this->assertObjectHasAttribute('charset', $responseSend);
$responseSent = $response->send();
$this->assertObjectHasAttribute('headers', $responseSent);
$this->assertObjectHasAttribute('content', $responseSent);
$this->assertObjectHasAttribute('version', $responseSent);
$this->assertObjectHasAttribute('statusCode', $responseSent);
$this->assertObjectHasAttribute('statusText', $responseSent);
$this->assertObjectHasAttribute('charset', $responseSent);
$this->assertFalse($responseSent->headers->has('Content-Length'));

ob_start();

$response = new Response('foo');
$responseSent = $response->send();
$this->assertSame('3', $responseSent->headers->get('Content-Length'));

$response = new Response('bar');
$response->headers->set('Transfer-Encoding', 'chunked');
$responseSent = $response->send();
$this->assertFalse($responseSent->headers->has('Content-Length'));

$this->assertSame('foobar', ob_get_clean());
}

public function testGetCharset()
Expand Down
0