Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d91efd3

Browse filesBrowse 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
Copy full SHA for d91efd3

File tree

2 files changed

+5
-10
lines changed
Filter options

2 files changed

+5
-10
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+5-8Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.