]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: vars: reduce the code size of some wrappers
authorThierry FOURNIER <tfournier@arpalert.org>
Tue, 28 Jul 2015 17:00:28 +0000 (19:00 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Aug 2015 12:14:10 +0000 (14:14 +0200)
Some function are just a wrappers. This patch reduce the size of
this wrapper for improving the readability. One check is moved
from the wrapper to the main function, and some middle vars are
removed.

src/vars.c

index 02d472acf27216694a2a5bdff9394ce6e4711c78..8cd09a61a0095364ec8f08bbf5d1bd645fe87b4e 100644 (file)
@@ -501,46 +501,34 @@ static inline int action_store(struct sample_expr *expr, const char *name,
        return 1;
 }
 
-/* Returns 0 if miss data, else returns 1. */
+/* Wrapper for action_store */
 static int action_tcp_req_store(struct tcp_rule *rule, struct proxy *px,
                                 struct session *sess, struct stream *s)
 {
-       struct sample_expr *expr = rule->act_prm.data[0];
-       const char *name = rule->act_prm.data[1];
-       int scope = (long)rule->act_prm.data[2];
-
-       return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
+       return action_store(rule->act_prm.data[0], rule->act_prm.data[1],
+                           (long)rule->act_prm.data[2], px, s, SMP_OPT_DIR_REQ);
 }
 
-/* Returns 0 if miss data, else returns 1. */
+/* Wrapper for action_store */
 static int action_tcp_res_store(struct tcp_rule *rule, struct proxy *px,
                                 struct session *sess, struct stream *s)
 {
-       struct sample_expr *expr = rule->act_prm.data[0];
-       const char *name = rule->act_prm.data[1];
-       int scope = (long)rule->act_prm.data[2];
-
-       return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
+       return action_store(rule->act_prm.data[0], rule->act_prm.data[1],
+                           (long)rule->act_prm.data[2], px, s, SMP_OPT_DIR_RES);
 }
 
-/* Returns 0 if miss data, else returns 1. */
+/* Wrapper for action_store */
 static int action_http_req_store(struct http_req_rule *rule, struct proxy *px, struct stream *s)
 {
-       struct sample_expr *expr = rule->arg.act.p[0];
-       const char *name = rule->arg.act.p[1];
-       int scope = (long)rule->arg.act.p[2];
-
-       return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
+       return action_store(rule->arg.act.p[0], rule->arg.act.p[1],
+                           (long)rule->arg.act.p[2], px, s, SMP_OPT_DIR_REQ);
 }
 
-/* Returns 0 if miss data, else returns 1. */
+/* Wrapper for action_store */
 static int action_http_res_store(struct http_res_rule *rule, struct proxy *px, struct stream *s)
 {
-       struct sample_expr *expr = rule->arg.act.p[0];
-       const char *name = rule->arg.act.p[1];
-       int scope = (long)rule->arg.act.p[2];
-
-       return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
+       return action_store(rule->arg.act.p[0], rule->arg.act.p[1],
+                           (long)rule->arg.act.p[2], px, s, SMP_OPT_DIR_RES);
 }
 
 /* This two function checks the variable name and replace the
@@ -572,11 +560,12 @@ static int conv_check_var(struct arg *args, struct sample_conv *conv,
  * expression to execute.
  */
 static int parse_vars(const char **args, int *arg, struct proxy *px,
-                      struct sample_expr **expr, char **name,
-                      enum vars_scope *scope, char **err)
+                      int flags, char **err, struct sample_expr **expr,
+                      char **name, enum vars_scope *scope)
 {
        const char *var_name = args[*arg-1];
        int var_len;
+       const char *kw_name;
 
        var_name += strlen("set-var");
        if (*var_name != '(') {
@@ -595,123 +584,77 @@ static int parse_vars(const char **args, int *arg, struct proxy *px,
        if (!*name)
                return 0;
 
+       kw_name = args[*arg-1];
+
        *expr = sample_parse_expr((char **)args, arg, px->conf.args.file, px->conf.args.line,
                                  err, &px->conf.args);
        if (!*expr)
                return 0;
 
+       if (!((*expr)->fetch->val & flags)) {
+               memprintf(err,
+                         "fetch method '%s' extracts information from '%s', none of which is available here",
+                         kw_name, sample_src_names((*expr)->fetch->use));
+               free(*expr);
+               return 0;
+       }
+
        return 1;
 }
 
+/* Wrapper for parse_vars */
 static int parse_tcp_req_store(const char **args, int *arg, struct proxy *px,
                                struct tcp_rule *rule, char **err)
 {
-       struct sample_expr *expr;
-       int cur_arg = *arg;
-       char *name;
-       enum vars_scope scope;
-
-       if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
-               return 0;
-
-       if (!(expr->fetch->val & SMP_VAL_FE_REQ_CNT)) {
-               memprintf(err,
-                         "fetch method '%s' extracts information from '%s', none of which is available here",
-                         args[cur_arg-1], sample_src_names(expr->fetch->use));
-               free(expr);
+       if (!parse_vars(args, arg, px, SMP_VAL_FE_REQ_CNT, err,
+                   (struct sample_expr **)rule->act_prm.data[0],
+                   (char **)rule->act_prm.data[1],
+                   (enum vars_scope *)rule->act_prm.data[2]))
                return 0;
-       }
-
        rule->action       = TCP_ACT_CUSTOM_CONT;
        rule->action_ptr   = action_tcp_req_store;
-       rule->act_prm.data[0] = expr;
-       rule->act_prm.data[1] = name;
-       rule->act_prm.data[2] = (void *)(long)scope;
-
        return 1;
 }
 
+/* Wrapper for parse_vars */
 static int parse_tcp_res_store(const char **args, int *arg, struct proxy *px,
                          struct tcp_rule *rule, char **err)
 {
-       struct sample_expr *expr;
-       int cur_arg = *arg;
-       char *name;
-       enum vars_scope scope;
-
-       if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
+       if (!parse_vars(args, arg, px, SMP_VAL_BE_RES_CNT, err,
+                   (struct sample_expr **)rule->act_prm.data[0],
+                   (char **)rule->act_prm.data[1],
+                   (enum vars_scope *)rule->act_prm.data[2]))
                return 0;
-
-       if (!(expr->fetch->val & SMP_VAL_BE_RES_CNT)) {
-               memprintf(err,
-                         "fetch method '%s' extracts information from '%s', none of which is available here",
-                         args[cur_arg-1], sample_src_names(expr->fetch->use));
-               free(expr);
-               return 0;
-       }
-
        rule->action       = TCP_ACT_CUSTOM_CONT;
        rule->action_ptr   = action_tcp_res_store;
-       rule->act_prm.data[0] = expr;
-       rule->act_prm.data[1] = name;
-       rule->act_prm.data[2] = (void *)(long)scope;
-
        return 1;
 }
 
+/* Wrapper for parse_vars */
 static int parse_http_req_store(const char **args, int *arg, struct proxy *px,
                          struct http_req_rule *rule, char **err)
 {
-       struct sample_expr *expr;
-       int cur_arg = *arg;
-       char *name;
-       enum vars_scope scope;
-
-       if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
-               return -1;
-
-       if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
-               memprintf(err,
-                         "fetch method '%s' extracts information from '%s', none of which is available here",
-                         args[cur_arg-1], sample_src_names(expr->fetch->use));
-               free(expr);
+       if (!parse_vars(args, arg, px, SMP_VAL_FE_HRQ_HDR, err,
+                   (struct sample_expr **)rule->arg.act.p[0],
+                   (char **)rule->arg.act.p[1],
+                   (enum vars_scope *)rule->arg.act.p[2]))
                return -1;
-       }
-
        rule->action       = HTTP_REQ_ACT_CUSTOM_CONT;
        rule->action_ptr   = action_http_req_store;
-       rule->arg.act.p[0] = expr;
-       rule->arg.act.p[1] = name;
-       rule->arg.act.p[2] = (void *)(long)scope;
-
        return 0;
 }
 
+/* Wrapper for parse_vars */
 static int parse_http_res_store(const char **args, int *arg, struct proxy *px,
                          struct http_res_rule *rule, char **err)
 {
-       struct sample_expr *expr;
-       int cur_arg = *arg;
-       char *name;
-       enum vars_scope scope;
-
-       if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
+       if (!parse_vars(args, arg, px, SMP_VAL_BE_HRS_HDR, err,
+                   (struct sample_expr **)rule->arg.act.p[0],
+                   (char **)rule->arg.act.p[1],
+                   (enum vars_scope *)rule->arg.act.p[2]))
                return -1;
-
-       if (!(expr->fetch->val & SMP_VAL_BE_HRS_HDR)) {
-               memprintf(err,
-                         "fetch method '%s' extracts information from '%s', none of which is available here",
-                         args[cur_arg-1], sample_src_names(expr->fetch->use));
-               free(expr);
-               return -1;
-       }
-
        rule->action       = HTTP_RES_ACT_CUSTOM_CONT;
        rule->action_ptr   = action_http_res_store;
-       rule->arg.act.p[0] = expr;
-       rule->arg.act.p[1] = name;
-       rule->arg.act.p[2] = (void *)(long)scope;
-
        return 0;
 }