Skip to content
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

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

Merged
merged 1 commit into from Feb 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be merged in 3.4 as well, right?

} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getPreferredFormat();
$format = $request->getPreferredFormat(null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Content-Type gets set to html anyway a few lines below (284). So we don't to ask for the html default here. Otherwise line 284 is basically unreachable.

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