From fd77e05f65e3fda86867368108782debd67ad59b Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Tue, 7 Jul 2015 21:20:42 +0200 Subject: [PATCH] MINOR: vars: returns variable content This patch copy the content of a variable in a sample. The function returns 0 if the variable is not found. --- include/proto/vars.h | 1 + src/vars.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/proto/vars.h b/include/proto/vars.h index 4c8582565..9299b9f39 100644 --- a/include/proto/vars.h +++ b/include/proto/vars.h @@ -8,6 +8,7 @@ void vars_prune(struct vars *vars, struct stream *strm); void vars_prune_per_sess(struct vars *vars); int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp); void vars_set_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp); +int vars_get_by_desc(const struct var_desc *var_desc, struct stream *strm, struct sample *smp); int vars_check_arg(struct arg *arg, char **err); #endif diff --git a/src/vars.c b/src/vars.c index da675cce1..5a2a5729b 100644 --- a/src/vars.c +++ b/src/vars.c @@ -448,6 +448,40 @@ int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct s return 1; } +/* this function fills a sample with the + * content of the varaible described by . Returns 1 + * if the sample is filled, otherwise it returns 0. + */ +int vars_get_by_desc(const struct var_desc *var_desc, struct stream *strm, struct sample *smp) +{ + struct vars *vars; + struct var *var; + + /* Select "vars" pool according with the scope. */ + switch (var_desc->scope) { + case SCOPE_SESS: vars = &strm->sess->vars; break; + case SCOPE_TXN: vars = &strm->vars_txn; break; + case SCOPE_REQ: + case SCOPE_RES: + default: vars = &strm->vars_reqres; break; + } + + /* Check if the scope is avalaible a this point of processing. */ + if (vars->scope != var_desc->scope) + return 0; + + /* Get the variable entry. */ + var = var_get(vars, var_desc->name); + if (!var) + return 0; + + /* Copy sample. */ + smp->type = var->data.type; + smp->flags = SMP_F_CONST; + memcpy(&smp->data, &var->data.data, sizeof(smp->data)); + return 1; +} + /* Returns 0 if we need to come back later to complete the sample's retrieval, * otherwise 1. For now all processing is considered final so we only return 1. */