]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: vars: adds get and set functions
authorThierry FOURNIER <tfournier@arpalert.org>
Tue, 9 Jun 2015 10:27:17 +0000 (12:27 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 13 Jun 2015 21:01:37 +0000 (23:01 +0200)
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
src/vars.c

index dadbf3b1437b612a8b0ee7e1a5d5b56d46f8aa3c..e1092c75aa4777046d2e0aec908280c8629112d5 100644 (file)
@@ -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
index 74033a52efab35abc41810afa96047443a84770b..cf3275cd381e1ba933ef841860f062cc043046b7 100644 (file)
@@ -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,