8000 [HttpFoundation][HttpKernel] Fix deprecations when `Content-Type` is `null` by HypeMC · Pull Request #51032 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation][HttpKernel] Fix deprecations when Content-Type is null #51032

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
Jul 20, 2023
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
8000
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function prepare(Request $request)
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
} elseif (0 === stripos($headers->get('Content-Type') ?? '', 'text/') && false === stripos($headers->get('Content-Type') ?? '', 'charset')) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ public function testContentTypeCharset()
$this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
}

public function testContentTypeIsNull()
{
$response = new Response('foo');
$response->headers->set('Content-Type', null);

$response->prepare(new Request());

$this->expectNotToPerformAssertions();
}

public function testPrepareDoesNothingIfContentTypeIsSet()
{
$response = new Response('foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public function collect(Request $request, Response $response, \Throwable $except
if (!$this->requestStack
|| !$response->headers->has('X-Debug-Token')
|| $response->isRedirection()
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type') ?? '', 'html'))
|| 'html' !== $request->getRequestFormat()
|| false === strripos($response->getContent(), '</body>')
) {
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type') ?? '', 'html')) {
$dumper = new HtmlDumper('php://output', $this->charset);
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
Expand Down Expand Up @@ -152,4 +153,22 @@ public function testFlushNothingWhenDataDumperIsProvided()
$collector->__destruct();
$this->assertEmpty(ob_get_clean());
}

public function testNullContentTypeWithNoDebugEnv()
{
$request = new Request();
$requestStack = new RequestStack();
$requestStack->push($request);

$response = new Response('<html><head></head><body></body></html>');
$response->headers->set('Content-Type', null);
$response->headers->set('X-Debug-Token', 'xxxxxxxx');

$collector = new DumpDataCollector(null, null, null, $requestStack);
$collector->collect($request, $response);

ob_start();
$collector->__destruct();
$this->assertEmpty(ob_get_clean());
}
}
0