Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ HttpFoundation
--------------

* Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead
* Add argument `$subtypeFallback` to `Request::getFormat()`

HttpKernel
----------
Expand Down
55 changes: 26 additions & 29 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ class Request
self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX',
];

/**
* This mapping is used when no exact MIME match is found in $formats.
*
* It enables mappings like application/soap+xml -> xml.
*
* @see https://datatracker.ietf.org/doc/html/rfc6839
* @see https://datatracker.ietf.org/doc/html/rfc7303
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
private const STRUCTURED_SUFFIX_FORMATS = [
'json' => 'json',
'xml' => 'xml',
'xhtml' => 'html',
'cbor' => 'cbor',
'zip' => 'zip',
'ber' => 'asn1',
'der' => 'asn1',
'tlv' => 'tlv',
'wbxml' => 'xml',
'yaml' => 'yaml',
];

private bool $isIisRewrite = false;

/**
Expand Down Expand Up @@ -1239,8 +1261,9 @@ public static function getMimeTypes(string $format): array
* @param string|null $mimeType The mime type to check
* @param bool $subtypeFallback Whether to fall back to the subtype if no exact match is found
*/
public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?string
public function getFormat(?string $mimeType/* , bool $subtypeFallback = false */): ?string
{
$subtypeFallback = 2 <= \func_num_args() ? func_get_arg(1) : false;
$canonicalMimeType = null;
if ($mimeType && false !== $pos = strpos($mimeType, ';')) {
$canonicalMimeType = trim(substr($mimeType, 0, $pos));
Expand All @@ -1265,8 +1288,8 @@ public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?st

if (str_starts_with($canonicalMimeType, 'application/') && str_contains($canonicalMimeType, '+')) {
$suffix = substr(strrchr($canonicalMimeType, '+'), 1);
if (isset(static::getStructuredSuffixFormats()[$suffix])) {
return static::getStructuredSuffixFormats()[$suffix];
if (isset(self::STRUCTURED_SUFFIX_FORMATS[$suffix])) {
return self::STRUCTURED_SUFFIX_FORMATS[$suffix];
}
}

Expand Down Expand Up @@ -1963,32 +1986,6 @@ protected static function initializeFormats(): void
];
}

/**
* This mapping is used when no exact MIME match is found in $formats.
* It enables handling of types like application/soap+xml → 'xml'.
*
* @see https://datatracker.ietf.org/doc/html/rfc6839
* @see https://datatracker.ietf.org/doc/html/rfc7303
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*
* @return array<string, string>
*/
private static function getStructuredSuffixFormats(): array
{
return [
'json' => 'json',
'xml' => 'xml',
'xhtml' => 'html',
'cbor' => 'cbor',
'zip' => 'zip',
'ber' => 'asn1',
'der' => 'asn1',
'tlv' => 'tlv',
'wbxml' => 'xml',
'yaml' => 'yaml',
];
}

private function setPhpDefaultLocale(string $locale): void
{
// if either the class Locale doesn't exist, or an exception is thrown when
Expand Down
Loading