From 678d7be1b32eb5ffad6f0141f5e5f4c234adb753 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Wed, 19 Jul 2023 22:24:03 +0200 Subject: [PATCH] [HttpFoundation][HttpKernel] Fix deprecations when `Content-Type` is `null` --- .../Component/HttpFoundation/Response.php | 2 +- .../HttpFoundation/Tests/ResponseTest.php | 10 ++++++++++ .../DataCollector/DumpDataCollector.php | 4 ++-- .../DataCollector/DumpDataCollectorTest.php | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 59e974d62d557..23bfb2199d98c 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -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); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 50198c2d7612b..a78e359b645e2 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -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'); diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php index 08026e56225fd..9a85c19267806 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php @@ -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(), '') ) { - 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 { diff --git a/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php b/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php index b86e53df79087..308ab42dd3b31 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php @@ -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; @@ -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(''); + $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()); + } }