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 src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead
* Add support for the `QUERY` HTTP method
* Add support for structured MIME suffix

7.3
---
Expand Down
71 changes: 70 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,20 @@ public static function getMimeTypes(string $format): array

/**
* Gets the format associated with the mime type.
*
* Resolution order:
* 1) Exact match on the full MIME type (e.g. "application/json").
* 2) Match on the canonical MIME type (i.e. before the first ";" parameter).
* 3) If the type is "application/*+suffix", use the structured syntax suffix
* mapping (e.g. "application/foo+json" → "json"), when available.
* 4) If $subtypeFallback is true and no match was found:
* - return the MIME subtype (without "x-" prefix), provided it does not
* contain a "+" (e.g. "application/x-yaml" → "yaml", "text/csv" → "csv").
*
* @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): ?string
public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?string
{
$canonicalMimeType = null;
if ($mimeType && false !== $pos = strpos($mimeType, ';')) {
Expand All @@ -1247,6 +1259,27 @@ public function getFormat(?string $mimeType): ?string
}
}

if (!$canonicalMimeType) {
$canonicalMimeType = $mimeType;
}

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

if ($subtypeFallback && str_contains($canonicalMimeType, '/')) {
[, $subtype] = explode('/', $canonicalMimeType, 2);
if (str_starts_with($subtype, 'x-')) {
$subtype = substr($subtype, 2);
}
if (!str_contains($subtype, '+')) {
return $subtype;
}
}

return null;
}

Expand Down Expand Up @@ -1919,6 +1952,42 @@ protected static function initializeFormats(): void
'atom' => ['application/atom+xml'],
'rss' => ['application/rss+xml'],
'form' => ['application/x-www-form-urlencoded', 'multipart/form-data'],
'soap' => ['application/soap+xml'],
'problem' => ['application/problem+json'],
'hal' => ['application/hal+json', 'application/hal+xml'],
'jsonapi' => ['application/vnd.api+json'],
'yaml' => ['text/yaml', 'application/x-yaml'],
'wbxml' => ['application/vnd.wap.wbxml'],
'pdf' => ['application/pdf'],
'csv' => ['text/csv'],
];
}

/**
* Structured MIME suffix fallback formats
*
* 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',
];
}

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,37 @@ public static function getFormatToMimeTypeMapProvider()
['rdf', ['application/rdf+xml']],
['atom', ['application/atom+xml']],
['form', ['application/x-www-form-urlencoded', 'multipart/form-data']],
['rss', ['application/rss+xml']],
['soap', ['application/soap+xml']],
['html', ['application/xhtml+xml']],
['problem', ['application/problem+json']],
['hal', ['application/hal+json', 'application/hal+xml']],
['jsonapi', ['application/vnd.api+json']],
['yaml', ['application/x-yaml', 'text/yaml']],
['wbxml', ['application/vnd.wap.wbxml']],
];
}

/**
* @dataProvider getFormatWithSubtypeFallbackProvider
*/
public function testGetFormatFromMimeTypeWithSubtypeFallback($expectedFormat, $mimeTypes)
{
$request = new Request();
foreach ($mimeTypes as $mime) {
$this->assertEquals($expectedFormat, $request->getFormat($mime, true));
}
}

public static function getFormatWithSubtypeFallbackProvider()
{
return [
['cbor', ['application/example+cbor']],
['asn1', ['application/ber-stream+ber', 'application/secure-data+der']],
['zip', ['application/foobar+zip']],
['tlv', ['application/device-config+tlv']],
['pdf', ['application/pdf']],
['csv', ['text/csv']],
];
}

Expand Down
Loading