mime
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,
};
type type_params = strings::tokenizer;
Errors
type syntax = !void;
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 | syntax);
fn parse(in: str) ((str, type_params) | syntax);
fn register(mime: *mimetype...) void;
fn strerror(err: syntax) str;
Types
type mimetype
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
Show undocumented member
type type_params = strings::tokenizer;
Errors
type syntax
type syntax = !void;
A syntax error.
Constants
def SYSTEM_DB
def SYSTEM_DB: str = "/etc/mime.types";
Path to the system MIME database.
Functions
fn lookup_ext
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
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
fn next_param(in: *type_params) ((str, str) | done | syntax);
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 syntax if a syntax error was encountered.
fn parse
fn parse(in: str) ((str, type_params) | syntax);
Parses a Media Type, returning a tuple of the content type (e.g. "text/plain") and a parameter parser object, or syntax 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 syntax to be returned unless next_param is used to enumerate all of the parameters.
fn register
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.
fn strerror
fn strerror(err: syntax) str;
Converts an error into a human-friendly string.