MINOR: vars: make the vars() sample fetch function support a default value
authorWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 10:00:13 +0000 (12:00 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 10:08:54 +0000 (12:08 +0200)
commit54496a6a5b29ca0a5ad0e0a916722d17cb734f2d
tree1edd8ecc5847951bab8bb9054967545678d4a6e6
parente352b9dac744da99076b9dc1534825298bc0428a
MINOR: vars: make the vars() sample fetch function support a default value

It is quite common to see in configurations constructions like the
following one:

    http-request set-var(txn.bodylen) 0
    http-request set-var(txn.bodylen) req.hdr(content-length)
    ...
    http-request set-header orig-len %[var(txn.bodylen)]

The set-var() rules are almost always duplicated when manipulating
integers or any other value that is mandatory along operations. This is
a problem because it makes the configurations complicated to maintain
and slower than needed. And it becomes even more complicated when several
conditions may set the same variable because the risk of forgetting to
initialize it or to accidentally reset it is high.

This patch extends the var() sample fetch function to take an optional
argument which contains a default value to be returned if the variable
was not set. This way it becomes much simpler to use the variable, just
set it where needed, and read it with a fall back to the default value:

    http-request set-var(txn.bodylen) req.hdr(content-length)
    ...
    http-request set-header orig-len %[var(txn.bodylen,0)]

The default value is always passed as a string, thus it will experience
a cast to the output type. It doesn't seem userful to complicate the
configuration to pass an explicit type at this point.

The vars.vtc regtest was updated accordingly.
doc/configuration.txt
reg-tests/sample_fetches/vars.vtc
src/vars.c