mime +linux +x86_64

The mime module implements a subset of RFC 2045, namely the subset necessary to handle parsing and encoding Media Types (formerly "MIME types"), and to map them with file extensions.

This module implements a "reasonable subset" of the specification which aims to address contemporary use-cases of Media Types outside of the broader context of the Content-Type header as it's used by emails. The implementation should not parse any Media Types which are not conformant, but may not parse all conformant Media Types.



type mimetype;

// Undocumented types:
type type_params;


const SYSTEM_DB: str;


fn lookup_ext(str) const nullable *mimetype;
fn lookup_mime(str) const nullable *mimetype;
fn next_param(*type_params) ((str, str) | void | errors::invalid);
fn parse(str) ((str, type_params) | errors::invalid);
fn register(*mimetype...) void;


type mimetype[link]

type mimetype = struct {
	mime: str,
	exts: []str,

A pair of a Media Type and a list of file extensions associated with it. The extension list does not include the leading '.' character.

type type_params[link]

Show undocumented member
type type_params = strings::tokenizer;


def SYSTEM_DB[link]

def SYSTEM_DB: str;

Path to the system MIME database.


fn lookup_ext[link]

fn lookup_ext(ext: str) const nullable *mimetype;

Looks up a Media Type based on a file extension, with or without the leading '.' character, returning null if unknown.

fn lookup_mime[link]

fn lookup_mime(mime: str) const nullable *mimetype;

Looks up a Media Type based on the mime type string, returning null if unknown.

fn next_param[link]

fn next_param(in: *type_params) ((str, str) |
void | errors::invalid);

Returns the next parameter as a (key, value) tuple from a type_params object that was prepared via parse, void if there are no remaining parameters, and errors::invalid if a syntax error was encountered.

fn parse[link]

fn parse(in: str) ((str, type_params) | errors::invalid);

Parses a Media Type, returning a tuple of the content type (e.g. "text/plain") and a parameter parser object, or errors::invalid if the input cannot be parsed.

To enumerate the Media Type parameter list, pass the type_params object into next_param. If you do not need the parameter list, you can safely discard the object. Note that any format errors following the ";" token will not cause errors::invalid to be returned unless next_param is used to enumerate all of the parameters.

fn register[link]

fn register(mime: *mimetype...) void;

Registers a Media Type and its extensions in the internal MIME database. This function is designed to be used by @init functions for modules which implement new Media Types.