Skip to content

Commit d09a9dc

Browse files
minor #51032 [HttpFoundation][HttpKernel] Fix deprecations when Content-Type is null (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpFoundation][HttpKernel] Fix deprecations when `Content-Type` is `null` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Followup to #51000. Two more places I discovered were triggering deprecations when `Content-Type` is `null`. Commits ------- 678d7be [HttpFoundation][HttpKernel] Fix deprecations when `Content-Type` is `null`
2 parents 102ed2e + 678d7be commit d09a9dc

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function prepare(Request $request)
298298
$charset = $this->charset ?: 'UTF-8';
299299
if (!$headers->has('Content-Type')) {
300300
$headers->set('Content-Type', 'text/html; charset='.$charset);
301-
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
301+
} elseif (0 === stripos($headers->get('Content-Type') ?? '', 'text/') && false === stripos($headers->get('Content-Type') ?? '', 'charset')) {
302302
// add the charset
303303
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
304304
}

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ public function testContentTypeCharset()
511511
$this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
512512
}
513513

514+
public function testContentTypeIsNull()
515+
{
516+
$response = new Response('foo');
517+
$response->headers->set('Content-Type', null);
518+
519+
$response->prepare(new Request());
520+
521+
$this->expectNotToPerformAssertions();
522+
}
523+
514524
public function testPrepareDoesNothingIfContentTypeIsSet()
515525
{
516526
$response = new Response('foo');

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public function collect(Request $request, Response $response, \Throwable $except
116116
if (!$this->requestStack
117117
|| !$response->headers->has('X-Debug-Token')
118118
|| $response->isRedirection()
119-
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
119+
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type') ?? '', 'html'))
120120
|| 'html' !== $request->getRequestFormat()
121121
|| false === strripos($response->getContent(), '</body>')
122122
) {
123-
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
123+
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type') ?? '', 'html')) {
124124
$dumper = new HtmlDumper('php://output', $this->charset);
125125
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
126126
} else {

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
1819
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
@@ -152,4 +153,22 @@ public function testFlushNothingWhenDataDumperIsProvided()
152153
$collector->__destruct();
153154
$this->assertEmpty(ob_get_clean());
154155
}
156+
157+
public function testNullContentTypeWithNoDebugEnv()
158+
{
159+
$request = new Request();
160+
$requestStack = new RequestStack();
161+
$requestStack->push($request);
162+
163+
$response = new Response('<html><head></head><body></body></html>');
164+
$response->headers->set('Content-Type', null);
165+
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
166+
167+
$collector = new DumpDataCollector(null, null, null, $requestStack);
168+
$collector->collect($request, $response);
169+
170+
ob_start();
171+
$collector->__destruct();
172+
$this->assertEmpty(ob_get_clean());
173+
}
155174
}

0 commit comments

Comments
 (0)