mime
MIME type lookup and Content-Type handling - look up the MIME type for a file extension (falls back to application/octet-stream), reverse-lookup extension from MIME type, parse a Content-Type header into type and charset, format one back, extract a multipart boundary, and match a MIME type against a wildcard pattern like text/*.
Load with: use mime
What this module does
mime covers the MIME plumbing needed for HTTP servers and file
servers. The built-in table covers web (HTML, CSS, JS, JSON, XML, Markdown),
images (PNG, JPEG, GIF, SVG, WebP, ICO), fonts (WOFF, WOFF2, TTF, OTF),
archives (ZIP, GZ, TAR), media (MP4, WebM, MP3, OGG), and WASM. Unknown
extensions fall back to application/octet-stream.
Quick example
use mime
# Extension → MIME type
prn(mime("html")) # "text/html"
prn(mime(".js")) # "text/javascript"
prn(mime("png")) # "image/png"
prn(mime("xyz")) # "application/octet-stream"
# MIME type → extension
prn(mext("application/json")) # "json"
# Parse Content-Type header
ct = mimpa("text/html; charset=utf-8")
prn(ct.t) # "text/html"
prn(ct.cs) # "utf-8"
# Format Content-Type
prn(mimfm("text/html", "utf-8")) # "text/html; charset=utf-8"
# Extract multipart boundary
bnd = mpbnd("multipart/form-data; boundary=----WebKit12345")
# Wildcard match
prn(mimok("text/html", "text/*")) # tru
prn(mimok("image/png", "text/*")) # fls
Functions
Type lookup
mime(ext)Returns the MIME type string for a file extension (with or without leading dot). Case-insensitive. Returns "application/octet-stream" for unknown extensions.
mext(mime_type)Returns a common file extension for a MIME type string (e.g. "image/png" → "png"). Returns "bin" for unknown types.
mimem()Returns the raw MIME type table as an object mapping extension strings to MIME type strings.
Content-Type header
mimpa(content_type)Parses a Content-Type header value. Returns {t, cs} where t is the base MIME type and cs is the charset (or nil if absent).
mimfm(mime_type, charset)Formats a Content-Type header value. Returns "mime_type; charset=charset", or just mime_type if charset is nil.
mpbnd(content_type)Extracts the boundary parameter from a multipart/form-data or multipart/* Content-Type header. Returns the boundary string or nil.
Pattern matching
mimok(mime_type, pattern)Returns tru if mime_type matches pattern. Supports exact match or wildcard suffix like "text/*".
Notes
- Requires
trlandtxt.