MINOR: vars: Parse optional conditions passed to the set-var actions

This patch adds the parsing of the optional condition parameters that
can be passed to the set-var and set-var-fmt actions (http as well as
tcp). Those conditions will not be taken into account yet in the var_set
function so conditions passed as parameters will not have any effect.
Since actions do not benefit from the parameter preparsing that
converters have, parsing conditions needed to be done by hand.
This commit is contained in:
Remi Tricot-Le Breton 2021-12-16 17:14:38 +01:00 committed by Willy Tarreau
parent 51899d251c
commit bb6bc95b1e
2 changed files with 28 additions and 2 deletions

View File

@ -170,6 +170,7 @@ struct act_rule {
struct sample_expr *expr;
uint64_t name_hash;
enum vars_scope scope;
uint conditions; /* Bitfield of the conditions passed to this set-var call */
} vars;
struct {
int sc;

View File

@ -840,8 +840,8 @@ static int conv_check_var(struct arg *args, struct sample_conv *conv,
/* This function is a common parser for using variables. It understands
* the format:
*
* set-var-fmt(<variable-name>) <format-string>
* set-var(<variable-name>) <expression>
* set-var-fmt(<variable-name>[,<cond> ...]) <format-string>
* set-var(<variable-name>[,<cond> ...]) <expression>
* unset-var(<variable-name>)
*
* It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
@ -857,6 +857,9 @@ static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy
const char *kw_name;
int flags = 0, set_var = 0; /* 0=unset-var, 1=set-var, 2=set-var-fmt */
struct sample empty_smp = { };
struct ist condition = IST_NULL;
struct ist var = IST_NULL;
struct ist varname_ist = IST_NULL;
if (strncmp(var_name, "set-var-fmt", 11) == 0) {
var_name += 11;
@ -885,6 +888,28 @@ static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy
return ACT_RET_PRS_ERR;
}
/* Parse the optional conditions. */
var = ist2(var_name, var_len);
varname_ist = istsplit(&var, ',');
var_len = istlen(varname_ist);
condition = istsplit(&var, ',');
if (istlen(condition) && set_var == 0) {
memprintf(err, "unset-var does not expect parameters after the variable name. Only \"set-var\" and \"set-var-fmt\" manage conditions");
return ACT_RET_PRS_ERR;
}
while (istlen(condition)) {
struct buffer cond = {};
chunk_initlen(&cond, istptr(condition), 0, istlen(condition));
if (vars_parse_cond_param(&cond, &rule->arg.vars.conditions, err) == 0)
return ACT_RET_PRS_ERR;
condition = istsplit(&var, ',');
}
LIST_INIT(&rule->arg.vars.fmt);
if (!vars_hash_name(var_name, var_len, &rule->arg.vars.scope, &rule->arg.vars.name_hash, err))
return ACT_RET_PRS_ERR;