Skip to content

net.textproto #

textproto

textproto provides helpers for MIME-style text protocol headers, similar to Go's net/textproto.

canonical_mime_header_key changes only valid ASCII field names. Empty names or names containing spaces, control bytes, or other invalid field-name bytes are returned unchanged.

read_mime_header accepts CRLF or LF line endings and stops at the first empty line. Repeated names share a canonical key and retain their value order. A line beginning with a space or tab continues the previous valid field. Only spaces and tabs are trimmed from each value line; other non-ASCII bytes are preserved. Folding an empty initial value does not add leading whitespace.

Parsing is lenient: malformed fields and orphan continuations are skipped, and a malformed line ends the previous field's continuation. Field names must be nonempty ASCII tokens; values may contain tabs but no other control bytes. Unlike Go's ReadMIMEHeader, this helper returns only a map and does not report parsing errors.

import net.textproto

assert textproto.canonical_mime_header_key('content-type') == 'Content-Type'

h := textproto.read_mime_header('Content-Type: text/plain\r\nX-Custom: 1\r\n')
assert h['Content-Type'][0] == 'text/plain'

fn canonical_mime_header_key #

fn canonical_mime_header_key(s string) string

canonical_mime_header_key returns the canonical form of a MIME header key: each hyphen-separated word is title-cased (e.g. "content-type" -> "Content-Type"). Empty names and names containing invalid MIME field-name bytes are returned unchanged, as in Go's textproto.CanonicalMIMEHeaderKey.

fn read_mime_header #

fn read_mime_header(text string) map[string][]string

read_mime_header parses a block of MIME header text into a map from canonical key to its values. Continuation lines (starting with a space or tab) are folded into the previous value. A blank line terminates the headers. CRLF and LF line endings are accepted. Keys are canonicalized and only spaces and tabs are trimmed from each value line. Folding an empty initial value does not add leading whitespace. Malformed fields and orphan continuations are ignored; a malformed line also ends the previous field's continuation. This lenient helper does not report parsing errors.