MIME · MoonBit

moonmime

Media types, Content-Disposition, multipart bodies, form uploads — parsed and written from bytes, whole or a piece at a time. No sockets, no files, no globals.

live · compiled from MoonBit
…
…
…

This widget calls the real library, compiled to JavaScript and running in this page.

11
jobs: type, registry, disposition, multipart, form, writing, streaming, related, words, quoted-printable, bounds
100+
extensions in the registry
5
RFCs honoured · 2045 · 2046 · 2387 · 6266 · 7578
0
sockets, files, or global state
What it does

Everything a message needs before it leaves the bytes.

One package, eleven jobs. Each takes a header or a body and hands back data you can act on — and two of them hand a body back, or take one in pieces.

Media types

media_type reads Content-Type into an essence and ordered parameters, then writes it back with correct quoting.

The registry

guess_type names the type a filename implies; guess_extension gives the extension people use for a type.

Content-Disposition

Build and parse the header that names a download, with filename* (RFC 5987) for names that escape ASCII.

Multipart

multipart splits any multipart body into parts — the shape an email and a form upload share.

Form data

formdata reads multipart/form-data into text fields and file uploads, distinguishing them the way the wire does.

Writing bodies

Form::encode and Multipart::new frame parts the way a browser or a mail client does, with a boundary proven absent and filename* for names that leave ASCII.

Streaming

PartsReader gives a part's headers before its body and the body a chunk at a time, with the bound holding as it reads; MultipartWriter frames one part at a time into a sink.

Related bodies

Related frames multipart/related (RFC 2387) with type, start and start-info, and reads start back into the part it names.

Encoded words

encode_words writes a header value as RFC 2047 words, B or Q, split so none passes 75 characters; decode_words reads any mix back.

Quoted-printable

moonmime/qp encodes and decodes a body (RFC 2045 §6.7): soft breaks at 76 columns, =XY escapes, trailing whitespace protected.

Bounds

Limits caps part count and part size. A body over its bound is refused whole, never truncated.

Quick start

Add it, then parse a header.

shell
moon add moonbitstack/moonmime
main.mbt
let mt = @moonmime.media_type("text/html; charset=utf-8")
println(mt.essence)
println(mt.type_())
println(mt.param("charset").unwrap_or(""))
text/html text utf-8

The guide walks every call in order →