8 Headers: Parsing and Constructing
A header is represented as a string or byte string containing CRLF-delimited lines. Each field within the header spans one or more lines. In addition, the header ends with two CRLFs (because the first one terminates the last field, and the second terminates the header).
8.1 Functions
value
procedure
(validate-header candidate) → void?
candidate : (or string? bytes?)
procedure
(extract-field field header) → (or/c string? bytes? false/c)
field : (or/c string? bytes?) header : (or/c string? bytes?)
The field and header arguments must be both strings or both byte strings, and the result (if not #f) is of the same type.
> (extract-field "TO" (insert-field "to" "me@localhost" empty-header)) "me@localhost"
procedure
(extract-all-fields header)
→
(listof (cons/c (or/c string? bytes?) (or/c string? bytes?))) header : (or/c string? bytes?)
The result provides strings if header is a string, byte strings if header is a byte string.
procedure
(remove-field field header) → (or/c string? bytes?)
field : (or/c string? bytes?) header : (or/c string? bytes?)
The field and header arguments must be both strings or both byte strings, and the result is of the same type.
procedure
(insert-field field value header) → (or/c string? bytes?)
field : (or/c string? bytes?) value : (or/c string? bytes?) header : (or/c string? bytes?)
The field, value, and header arguments must be all strings or all byte strings, and the result is of the same type.
procedure
(replace-field field value header) → (or/c string? bytes?)
field : (or/c string? bytes?) value : (or/c string? bytes? false/c) header : (or/c string? bytes?)
procedure
(append-headers header1 header2) → (or/c string? bytes?)
header1 : (or/c string? bytes?) header2 : (or/c string? bytes?)
The header1 and header2 arguments must be both strings or both byte strings, and the result is of the same type.
procedure
(standard-message-header from to cc bcc subject) → string? from : string? to : (listof string?) cc : (listof string?) bcc : (listof string?) subject : string?
The BCC recipients do not actually appear in the header, but they’re accepted anyway to complete the abstraction.
procedure
(data-lines->data listof) → string?
listof : string?
procedure
(extract-addresses line kind)
→
(or/c (listof string?) (listof (list/c string? string? string?))) line : string?
kind :
(one-of/c 'name 'address 'full 'all)
The kind argument specifies which portion of an address should be returned:
'name —
the free-form name in the address, or the address itself if no name is available. Examples:> (extract-addresses "John Doe <doe@localhost>" 'name) '("John Doe")
> (extract-addresses "doe@localhost (Johnny Doe)" 'name) '("Johnny Doe")
> (extract-addresses "doe@localhost" 'name) '("doe@localhost")
> (extract-addresses " \"Doe, John\" <doe@localhost>, jane" 'name) '("\"Doe, John\"" "jane")
'address —
just the mailing address, without any free-form names. Examples:> (extract-addresses "John Doe <doe@localhost>" 'address) '("doe@localhost")
> (extract-addresses "doe@localhost (Johnny Doe)" 'address) '("doe@localhost")
> (extract-addresses "doe@localhost" 'address) '("doe@localhost")
> (extract-addresses " \"Doe, John\" <doe@localhost>, jane" 'address) '("doe@localhost" "jane")
'full —
the full address, essentially as it appears in the input, but normalized. Examples:> (extract-addresses "John Doe < doe@localhost >" 'full) '("John Doe <doe@localhost>")
> (extract-addresses " doe@localhost (Johnny Doe)" 'full) '("doe@localhost (Johnny Doe)")
> (extract-addresses "doe@localhost" 'full) '("doe@localhost")
> (extract-addresses " \"Doe, John\" <doe@localhost>, jane" 'full) '("\"Doe, John\" <doe@localhost>" "jane")
'all —
a list containing each of the three possibilities: free-form name, address, and full address (in that order). Examples:> (extract-addresses "John Doe <doe@localhost>" 'all) '(("John Doe" "doe@localhost" "John Doe <doe@localhost>"))
> (extract-addresses "doe@localhost (Johnny Doe)" 'all) '(("Johnny Doe" "doe@localhost" "doe@localhost (Johnny Doe)"))
> (extract-addresses "doe@localhost" 'all) '(("doe@localhost" "doe@localhost" "doe@localhost"))
> (define r (extract-addresses " \"John\" <doe@localhost>, jane" 'all)) > (length r) 2
> (car r) '("\"John\"" "doe@localhost" "\"John\" <doe@localhost>")
> (cadr r) '("jane" "jane" "jane")
procedure
(assemble-address-field addrs) → string?
addrs : (listof string?)
> (assemble-address-field '("doe@localhost" "Jane <jane@elsewhere>")) "doe@localhost, Jane <jane@elsewhere>"
8.2 Header Unit
head@ and head^ are deprecated. They exist for backward-compatibility and will likely be removed in the future. New code should use the net/head module.
(require net/head-unit) | package: compatibility-lib |
value
head@ : unit?
8.3 Header Signature
(require net/head-sig) | package: compatibility-lib |
signature
head^ : signature
Includes everything exported by the net/head module.