Sovite.Message.Headers (sovite v0.2.0)

Copy Markdown View Source

The header section of a message (RFC 5322 §2.2), as a list of fields that keeps every byte of the original, so unchanged fields are passed through exactly (folding and all), which matters for signatures.

{:ok, header, body} = Headers.split(data)
fields = Headers.parse(header)
fields |> Headers.delete(["Return-Path"]) |> Headers.encode()

Each field is {name, raw}: the lower-cased field name and the whole field with its continuation lines and final CRLF. A line in the header section that is not a field is kept with name nil.

Summary

Functions

Appends a field. value must already be folded if long.

Removes every field with one of names (case-insensitive).

Encodes fields back into a header section.

Returns whether a field named name is present (case-insensitive).

Parses a header section (CRLF line endings) into fields.

Splits message data at the empty line that ends the header section. Returns :more when data does not contain it yet. A message that starts with an empty line has no header fields.

Types

field()

@type field() :: {String.t() | nil, binary()}

Functions

append(fields, name, value)

@spec append([field()], String.t(), String.t()) :: [field()]

Appends a field. value must already be folded if long.

delete(fields, names)

@spec delete([field()], [String.t()]) :: [field()]

Removes every field with one of names (case-insensitive).

encode(fields)

@spec encode([field()]) :: iodata()

Encodes fields back into a header section.

has?(fields, name)

@spec has?([field()], String.t()) :: boolean()

Returns whether a field named name is present (case-insensitive).

parse(header)

@spec parse(binary()) :: [field()]

Parses a header section (CRLF line endings) into fields.

split(data)

@spec split(binary()) :: {:ok, header :: binary(), body :: binary()} | :more

Splits message data at the empty line that ends the header section. Returns :more when data does not contain it yet. A message that starts with an empty line has no header fields.