Karta.Mail.Headers (Karta v1.0.0)

The header block of a message (RFC 5322 §2.2), split into fields without changing a byte, and header fields built for messages Karta writes.

split/2 keeps every field's raw bytes (Karta.Mail.Header), so DKIM and ARC can work on what was received rather than on a re-encoded copy.

The block ends at the first empty line. A line that is neither a header field nor a continuation also ends it: that line starts the body, as most mail software treats it.

Parsing is bounded, so a hostile message cannot make it expensive:

  • :max_line_length - octets per physical line, line break excluded (default 8192). RFC 5322 §2.1.1 allows 998; real mail sometimes has longer lines, so the parse limit is looser than the limit Karta writes.
  • :max_bytes - octets in the whole header block (default 1 MiB).
  • :max_fields - number of fields (default 1000).

Fields Karta builds (field/2) are folded at white space to lines of at most 78 octets where possible and never longer than 998.

Summary

Types

Why a header block was refused.

Functions

Builds one header field, folded, ending in CRLF.

The unfolded value of the first field with this name, or nil.

All fields with this name, in order. Names compare case-insensitively.

Splits the header block at the start of data.

The block's bytes again: the raw of every field, in order.

Removes spaces and tabs at both ends.

Removes every line break that is followed by a space or tab.

The field's value unfolded (RFC 5322 §2.2.3: a line break followed by white space is removed) and without surrounding white space.

Types

error()

@type error() :: :line_too_long | :too_large | :too_many_fields

Why a header block was refused.

Functions

field(name, value)

@spec field(String.t(), String.t()) :: binary()

Builds one header field, folded, ending in CRLF.

The value is split at white space so that lines stay within 78 octets where the words allow it. Raises ArgumentError if the name is not a valid field name, the value contains a line break or another control character, or a single word would make a line longer than 998 octets.

get(fields, name)

@spec get([Karta.Mail.Header.t()], String.t()) :: binary() | nil

The unfolded value of the first field with this name, or nil.

get_all(fields, name)

@spec get_all([Karta.Mail.Header.t()], String.t()) :: [Karta.Mail.Header.t()]

All fields with this name, in order. Names compare case-insensitively.

split(data, opts \\ [])

@spec split(binary(), keyword()) ::
  {:ok, [Karta.Mail.Header.t()], body_offset :: non_neg_integer()}
  | {:error, error()}

Splits the header block at the start of data.

Returns the fields and the offset of the body: the octet after the empty line, or the start of the first line that is not a header field. A message without a body has body_offset == byte_size(data).

to_iodata(fields)

@spec to_iodata([Karta.Mail.Header.t()]) :: iodata()

The block's bytes again: the raw of every field, in order.

trim_wsp(value)

@spec trim_wsp(binary()) :: binary()

Removes spaces and tabs at both ends.

unfold(value)

@spec unfold(binary()) :: binary()

Removes every line break that is followed by a space or tab.

value(header)

@spec value(Karta.Mail.Header.t()) :: binary()

The field's value unfolded (RFC 5322 §2.2.3: a line break followed by white space is removed) and without surrounding white space.