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.
diff --git a/include/proto/vars.h b/include/proto/vars.h
index 4c85825..9299b9f 100644
--- a/include/proto/vars.h
+++ b/include/proto/vars.h
@@ -8,6 +8,7 @@
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 da675cc..5a2a572 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -448,6 +448,40 @@
return 1;
}
+/* this function fills a sample with the
+ * content of the varaible described by <var_desc>. 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.
*/