Skip to content

Commit 1767b98

Browse files
Merge branch '5.0'
* 5.0: [HttpFoundation] Do not set the default Content-Type based on the Accept header [Security] Fix access_control behavior with unanimous decision strategy
2 parents 75fa024 + 26fb006 commit 1767b98

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Request.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,9 @@ public function isNoCache()
15661566
* Gets the preferred format for the response by inspecting, in the following order:
15671567
* * the request format set using setRequestFormat
15681568
* * the values of the Accept HTTP header
1569-
* * the content type of the body of the request.
1569+
*
1570+
* Note that if you use this method, you should send the "Vary: Accept" header
1571+
* in the response to prevent any issues with intermediary HTTP caches.
15701572
*/
15711573
public function getPreferredFormat(?string $default = 'html'): ?string
15721574
{

Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function prepare(Request $request)
270270
} else {
271271
// Content-type based on the Request
272272
if (!$headers->has('Content-Type')) {
273-
$format = $request->getPreferredFormat(null);
273+
$format = $request->getRequestFormat(null);
274274
if (null !== $format && $mimeType = $request->getMimeType($format)) {
275275
$headers->set('Content-Type', $mimeType);
276276
}

Tests/ResponseTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
500500
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
501501
}
502502

503+
/**
504+
* Same URL cannot produce different Content-Type based on the value of the Accept header,
505+
* unless explicitly stated in the response object.
506+
*/
507+
public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader()
508+
{
509+
$response = new Response('foo');
510+
$request = Request::create('/');
511+
$request->headers->set('Accept', 'application/json');
512+
$response->prepare($request);
513+
514+
$this->assertSame('text/html; charset=UTF-8', $response->headers->get('content-type'));
515+
}
516+
503517
public function testPrepareSetContentType()
504518
{
505519
$response = new Response('foo');
506520
$request = Request::create('/');
507521
$request->setRequestFormat('json');
508-
$request->headers->remove('accept');
509522

510523
$response->prepare($request);
511524

0 commit comments

Comments
 (0)