Skip to content

Commit

Permalink
bug #35709 [HttpFoundation] fix not sending Content-Type header for 2…
Browse files Browse the repository at this point in the history
…04 responses (Tobion)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpFoundation] fix not sending Content-Type header for 204 responses

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

`$headers->remove('Content-Type')` did not actually work because PHP sends the Content-Type header based on the https://www.php.net/manual/en/ini.core.php#ini.default-mimetype ini setting anyway (which defaults to html). So we need to disable this ini for empty responses.

Commits
-------

06f5a11 [HttpFoundation] fix not sending Content-Type header for 204 responses
  • Loading branch information
fabpot committed Feb 14, 2020
2 parents a6773c1 + 06f5a11 commit 1a7e4ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -267,10 +267,12 @@ public function prepare(Request $request)
$this->setContent(null);
$headers->remove('Content-Type');
$headers->remove('Content-Length');
// prevent PHP from sending the Content-Type header based on default_mimetype
ini_set('default_mimetype', '');
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getPreferredFormat();
$format = $request->getPreferredFormat(null);
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
Expand Down
12 changes: 2 additions & 10 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -461,18 +461,10 @@ public function testSetVary()

public function testDefaultContentType()
{
$headerMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\ResponseHeaderBag')->setMethods(['set'])->getMock();
$headerMock->expects($this->at(0))
->method('set')
->with('Content-Type', 'text/html');
$headerMock->expects($this->at(1))
->method('set')
->with('Content-Type', 'text/html; charset=UTF-8');

$response = new Response('foo');
$response->headers = $headerMock;

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

$this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
}

public function testContentTypeCharset()
Expand Down

0 comments on commit 1a7e4ea

Please sign in to comment.