From c365d99a0703955422867dd88011887483d2358d Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Tue, 9 Jun 2015 12:27:17 +0200 Subject: [PATCH] MINOR: vars: adds get and set functions This patch adds two functions used for variable acces using the variable full name. If the variable doesn't exists in the variable pool name, it is created. --- include/proto/vars.h | 2 ++ src/vars.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/proto/vars.h b/include/proto/vars.h index dadbf3b14..e1092c75a 100644 --- a/include/proto/vars.h +++ b/include/proto/vars.h @@ -5,6 +5,8 @@ void vars_init(struct vars *vars, enum vars_scope scope); void vars_prune(struct vars *vars, struct stream *strm); +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_check_arg(struct arg *arg, char **err); #endif diff --git a/src/vars.c b/src/vars.c index 74033a52e..cf3275cd3 100644 --- a/src/vars.c +++ b/src/vars.c @@ -360,6 +360,60 @@ int vars_check_arg(struct arg *arg, char **err) return 1; } +/* This function store a sample in a variable. + * In error case, it fails silently. + */ +void vars_set_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) +{ + enum vars_scope scope; + + /* Resolve name and scope. */ + name = register_name(name, len, &scope, NULL); + if (!name) + return; + + sample_store_stream(name, scope, strm, smp); +} + +/* this function fills a sample with the + * variable content. Returns 1 if the sample + * is filled, otherwise it returns 0. + */ +int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) +{ + struct vars *vars; + struct var *var; + enum vars_scope scope; + + /* Resolve name and scope. */ + name = register_name(name, len, &scope, NULL); + if (!name) + return 0; + + /* Select "vars" pool according with the scope. */ + switch (scope) { + case SCOPE_SESS: vars = &strm->vars_sess; break; + case SCOPE_TXN: vars = &strm->vars_txn; break; + case SCOPE_REQ: + case SCOPE_RES: vars = &strm->vars_reqres; break; + } + + /* Check if the scope is avalaible a this point of processing. */ + if (vars->scope != scope) + return 0; + + /* Get the variable entry. */ + var = var_get(vars, 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 miss data, else returns 1. */ static inline int action_store(struct sample_expr *expr, const char *name, enum vars_scope scope, struct proxy *px,