Files
Wez Furlong ee1e5b980e message: add msg:import_headers, a flexible alternative to import_x_headers
import_headers takes an array of per-spec option tables, each describing
how a single header name or pattern should be imported into the message
metadata. Compared to import_x_headers it adds:

 * Trailing-`*` wildcard patterns (e.g. `X-*`) alongside exact names.
   Bare/leading/interior wildcards are rejected at compile time.
 * `match` of `first`, `last` (default), or `all`. `all` captures every
   matching header instance as an array of strings; the others capture
   a string. Specs that produce no matches write nothing.
 * `transform` selects the metadata key style: `snake_case` (default,
   matches the existing import_x_headers behavior), `kebab_case`,
   `camel_case`, or `pascal_case`. Header matching itself is always
   case-insensitive.
 * `target` overrides the metadata key for exact-name specs.
 * `remove` strips the matched headers from the message body in a
   single follow-up pass.

When more than one spec could match a header, the first matching spec
wins, so callers can place specific rules ahead of a wildcard catch-all.

import_x_headers now delegates to import_headers, so its behavior is
unchanged and the two share a single implementation.

retain_headers now passes the header index alongside the &Header to its
closure, which import_headers uses for its post-pass removal step
instead of tracking a parallel counter. Existing callers that don't
need the index ignore it with `_`.

Closes: #515
2026-05-06 16:30:36 +01:00

1.3 KiB

tags
tags
meta

import_x_headers

message:import_x_headers([NAMES])

When called with no parameters, iterates the headers of the message, and for each header with an "X-" prefix, imports the header into the message metadata.

When called with a list of header names, only those headers, if present in the message, will be imported to the message metadata. Header names passed in this way do not need to have an X- prefix, making it convenient to use this method as a way to import an arbitrary list of headers for logging purposes.

When importing an X- header, the header name is normalized to lowercase and any - are transformed to underscores _.

For more flexible matching (wildcards, choosing first/last/all instances, optional removal of matched headers, alternate naming transforms), see message:import_headers.

For example, with a message content of:

X-Campaign-ID: 12345
X-Mailer: foobar
Subject: the subject

The body

calling:

message:import_x_headers()
print(message:get_meta 'x_campaign_id') -- prints 12345
print(message:get_meta 'x_mailer') -- prints foobar

but calling:

message:import_x_headers { 'x-campaign-id' }
print(message:get_meta 'x_campaign_id') -- prints 12345
print(message:get_meta 'x_mailer') -- prints nothing