Skip to contents

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.

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.

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.

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)
#> List of 1
#>  $ :List of 3
#>   ..$ type      : chr "text"
#>   ..$ subtype   : chr "html"
#>   ..$ parameters:List of 1
#>   .. ..$ charset: chr "UTF-8"
mime_format(rec)
#> [1] "text/html;charset=UTF-8"
mime_format(mime_parse("text/plain;name=\"a b\""))
#> [1] "text/plain;name=\"a b\""
mime_parse("not a type")
#> [[1]]
#> NULL
#>