Skip to content

Commit d91efd3

Browse files
committed
bug #34921 [HttpFoundation] Removed "Content-Type" from the preferred format guessing mechanism (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation] Removed "Content-Type" from the preferred format guessing mechanism | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34906, Fix #34857 | License | MIT | Doc PR | - Confirmed, inferring the `Content-Type` of the response using the `Content-Type` provided for the request body is NOT a good idea. The HTTP RFC explicitly states that `Accept` must be used to hint a preferred response format (`Content-Type` on the request indicates the type of associated its the body). Use `Accept` if provided (a best practice anyway), and fallback to the default value (HTML by default) otherwise. Commits ------- 776523e Removed request header "Content-Type" from the preferred format guessing mechanism
2 parents 6bf764a + 776523e commit d91efd3

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,20 +1586,17 @@ public function isNoCache()
15861586
*/
15871587
public function getPreferredFormat(?string $default = 'html'): ?string
15881588
{
1589-
if (null !== $this->preferredFormat) {
1589+
if (null !== $this->preferredFormat || null !== $this->preferredFormat = $this->getRequestFormat(null)) {
15901590
return $this->preferredFormat;
15911591
}
15921592

1593-
$preferredFormat = null;
1594-
foreach ($this->getAcceptableContentTypes() as $contentType) {
1595-
if ($preferredFormat = $this->getFormat($contentType)) {
1596-
break;
1593+
foreach ($this->getAcceptableContentTypes() as $mimeType) {
1594+
if ($this->preferredFormat = $this->getFormat($mimeType)) {
1595+
return $this->preferredFormat;
15971596
}
15981597
}
15991598

1600-
$this->preferredFormat = $this->getRequestFormat($preferredFormat ?: $this->getContentType());
1601-
1602-
return $this->preferredFormat ?: $default;
1599+
return $default;
16031600
}
16041601

16051602
/**

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,10 @@ public function testGetPreferredFormat()
408408

409409
$request->setRequestFormat('atom');
410410
$request->headers->set('Accept', 'application/ld+json');
411-
$request->headers->set('Content-Type', 'application/merge-patch+json');
412411
$this->assertSame('atom', $request->getPreferredFormat());
413412

414413
$request = new Request();
415414
$request->headers->set('Accept', 'application/xml');
416-
$request->headers->set('Content-Type', 'application/json');
417415
$this->assertSame('xml', $request->getPreferredFormat());
418416

419417
$request = new Request();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy