bug #35709 [HttpFoundation] fix not sending Content-Type header for 204 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
-------
06f5a1113d
[HttpFoundation] fix not sending Content-Type header for 204 responses
This commit is contained in:
commit
1a7e4ea746
@ -267,10 +267,12 @@ class Response
|
|||||||
$this->setContent(null);
|
$this->setContent(null);
|
||||||
$headers->remove('Content-Type');
|
$headers->remove('Content-Type');
|
||||||
$headers->remove('Content-Length');
|
$headers->remove('Content-Length');
|
||||||
|
// prevent PHP from sending the Content-Type header based on default_mimetype
|
||||||
|
ini_set('default_mimetype', '');
|
||||||
} else {
|
} else {
|
||||||
// Content-type based on the Request
|
// Content-type based on the Request
|
||||||
if (!$headers->has('Content-Type')) {
|
if (!$headers->has('Content-Type')) {
|
||||||
$format = $request->getPreferredFormat();
|
$format = $request->getPreferredFormat(null);
|
||||||
if (null !== $format && $mimeType = $request->getMimeType($format)) {
|
if (null !== $format && $mimeType = $request->getMimeType($format)) {
|
||||||
$headers->set('Content-Type', $mimeType);
|
$headers->set('Content-Type', $mimeType);
|
||||||
}
|
}
|
||||||
|
@ -461,18 +461,10 @@ class ResponseTest extends ResponseTestCase
|
|||||||
|
|
||||||
public function testDefaultContentType()
|
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 = new Response('foo');
|
||||||
$response->headers = $headerMock;
|
|
||||||
|
|
||||||
$response->prepare(new Request());
|
$response->prepare(new Request());
|
||||||
|
|
||||||
|
$this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testContentTypeCharset()
|
public function testContentTypeCharset()
|
||||||
|
Reference in New Issue
Block a user