mime_parse: Parse and serialise MIME types

View source: R/record.R

mime_parseR Documentation

Parse and serialise MIME types

Description

mime_parse() is the WHATWG MIME Sniffing Standard's "parse a MIME type". Type and subtype are lower-cased HTTP tokens. Parameters keep their order, and their names are lower-cased. Quoted values are unescaped. A repeated name keeps its first value. mime_format() is "serialize a MIME type". It quotes a value that is empty or not a token.

Usage

mime_parse(x, strict = FALSE)

mime_format(record)

Arguments

x

A character vector of MIME types, such as Content-Type header values.

strict

If TRUE, an input that fails to parse raises zumime_parse_error instead of giving NULL.

record

One record from mime_parse(), or a list of them.

Details

mime_format() checks a record before serialising it, so an edited record cannot carry a line break into a header. Type, subtype and parameter names must be non-empty HTTP tokens. Values must hold only HTTP quoted-string token code points: tab, U+0020 to U+007E and U+0080 to U+00FF. A record that breaks this raises zumime_type_error.

Value

mime_parse() returns a list with one record per input: a list of type, subtype and parameters (a named list of strings), or NULL for input that fails to parse and for NA.

mime_format() returns a character vector, NA for a NULL record.

References

https://mimesniff.spec.whatwg.org/#parsing-a-mime-type

See Also

mime_sniff(), which takes a supplied type in this grammar.

Examples

rec <- mime_parse("Text/HTML; Charset=\"UTF-8\"; charset=latin1")
str(rec)
mime_format(rec)
mime_format(mime_parse("text/plain;name=\"a b\""))
mime_parse("not a type")

zumime documentation built on Nov. 12, 2026, 1:07 a.m.