mime+x86_64 +linux

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.

Index

Types

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

// Undocumented types:
type type_params = strings::tokenizer;

Constants

def SYSTEM_DB: str = "/etc/mime.types";

Functions

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

Types

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;

Constants

def SYSTEM_DB[link]

def SYSTEM_DB: str = "/etc/mime.types";

Path to the system MIME database.

Functions

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) | done | errors::invalid);

Returns the next parameter as a (key, value) tuple from a type_params object that was prepared via parse, done 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.