]> git.kaiwu.me - haproxy.git/commit
MINOR: sample: add a "tr" converter
authorWilly Tarreau <w@1wt.eu>
Wed, 21 Oct 2026 20:43:44 +0000 (22:43 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Nov 2026 10:11:17 +0000 (11:11 +0100)
commit6bf3333f7abe7cd8053022d48fa789ade0fb9788
treee53a250a6a7352894ad856deacf3874ec7e705be
parent546ddc5faafe4eb3c88a26d3d04d0be54f966582
MINOR: sample: add a "tr" converter

This converter works like the unix "tr" utility: it translates or deletes
characters from the input sample. As on the command line, it takes 2 or 3
arguments:
  - optional flags, which must start with '-': 'd' to delete characters
    instead of translating them, 'c' to use the complement of the first
    set ;
  - the set of characters to match ;
  - the replacement set, mandatory without 'd' and forbidden with it.

For example, tr('a-z','A-Z') upper-cases the input, and tr(-dc,'[:digit:]')
only keeps the digits. A set starting with '-' as the first argument must
be written with a backslash ('\-'), except for "-" alone, again as with the
unix utility.

Sets support ranges ("a-z"), the POSIX character classes ("[:alpha:]"
etc, ASCII only), and the usual escapes (\NNN octal, \n, \t, \\ etc).
When the replacement set is shorter than the first one, its last char is
repeated. When complemented, the chars are enumerated in ascending order.

The sets are resolved at config parsing time into a 256-byte table stored
in the second argument, and the flags are stored as an integer in the
first one, so that the runtime processing only consists in one table
lookup per byte. The deletion loop is branchless so that it remains fast
on large contents such as a request body. It relies on smp_prepare_out()
so that read-only inputs, which are the most common ones, are not
duplicated first but directly transformed into a trash chunk. The
converter works on binary contents and preserves the input sample's type.

The set parsing functions tr_set_init(), tr_read_char() and tr_set_next()
are exported, with struct tr_set_ctx in sample-t.h, so that other
converters may use the same set syntax.

The doc and a reg-test were added.
doc/configuration.txt
include/haproxy/sample-t.h
include/haproxy/sample.h
reg-tests/converter/tr.vtc [new file with mode: 0644]
src/sample.c