MatchesMimeType🔗
Check that
text
conforms to the pattern of MIME type.
The definition has been taken from:
https://www.rfc-editor.org/rfc/rfc7231#section-3.1.1.1,
https://www.rfc-editor.org/rfc/rfc7230#section-3.2.3 and
https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6.
Code
tchar = (
"[!#$%&'*+\\-.^_`|~0-9a-zA-Z]"
)
token = (
f'({tchar})+'
)
type = (
f'{token}'
)
subtype = (
f'{token}'
)
ows = '[ \t]*'
obsText = '[\\x80-\\xff]'
qdText = (
f'([\t !#-\\[\\]-~]|{obsText})'
)
quotedPair = (
f'\\\\([\t !-~]|{obsText})'
)
quotedString = (
f'"({qdText}|{quotedPair})*"'
)
parameter = (
f'{token}=({token}|{quotedString})'
)
mediaType = (
f'^{type}/{subtype}({ows};{ows}{parameter})*$'
)
return match(
mediaType,
text
) is not None