]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: config: relax use_backend check to make the condition optional
authorWilly Tarreau <w@1wt.eu>
Tue, 22 Apr 2014 23:21:56 +0000 (01:21 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 22 Apr 2014 23:21:56 +0000 (01:21 +0200)
Since it became possible to use log-format expressions in use_backend,
having a mandatory condition becomes annoying because configurations
are full of "if TRUE". Let's relax the check to accept no condition
like many other keywords (eg: redirect).

doc/configuration.txt
src/cfgparse.c
src/haproxy.c
src/session.c

index 113818fa9281d22079a061ffda5549ace0f2dab8..37321e3b6006bf0c51fe5df62efad6b3cf02f81b 100644 (file)
@@ -7705,8 +7705,7 @@ unique-id-header <name>
 
     See also: "unique-id-format"
 
-use_backend <backend> if <condition>
-use_backend <backend> unless <condition>
+use_backend <backend> [{if | unless} <condition>]
   Switch to a specific backend if/unless an ACL-based condition is matched.
   May be used in sections :   defaults | frontend | listen | backend
                                   no   |    yes   |   yes  |   no
@@ -7714,7 +7713,8 @@ use_backend <backend> unless <condition>
     <backend>   is the name of a valid backend or "listen" section, or a
                 "log-format" string resolving to a backend name.
 
-    <condition> is a condition composed of ACLs, as described in section 7.
+    <condition> is a condition composed of ACLs, as described in section 7. If
+                it is omitted, the rule is unconditionally applied.
 
   When doing content-switching, connections arrive on a frontend and are then
   dispatched to various backends depending on a number of conditions. The
index d4893a1345b4e1a63ef0ea4b2e2e385302f544c3..53b136a36a63f6e1f9c1ef64ca954d43af4f0db6 100644 (file)
@@ -2866,22 +2866,17 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                        goto out;
                }
 
-               if (strcmp(args[2], "if") != 0 && strcmp(args[2], "unless") != 0) {
-                       Alert("parsing [%s:%d] : '%s' requires either 'if' or 'unless' followed by a condition.\n",
-                             file, linenum, args[0]);
-                       err_code |= ERR_ALERT | ERR_FATAL;
-                       goto out;
-               }
+               if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
+                       if ((cond = build_acl_cond(file, linenum, curproxy, (const char **)args + 2, &errmsg)) == NULL) {
+                               Alert("parsing [%s:%d] : error detected while parsing switching rule : %s.\n",
+                                     file, linenum, errmsg);
+                               err_code |= ERR_ALERT | ERR_FATAL;
+                               goto out;
+                       }
 
-               if ((cond = build_acl_cond(file, linenum, curproxy, (const char **)args + 2, &errmsg)) == NULL) {
-                       Alert("parsing [%s:%d] : error detected while parsing switching rule : %s.\n",
-                             file, linenum, errmsg);
-                       err_code |= ERR_ALERT | ERR_FATAL;
-                       goto out;
+                       err_code |= warnif_cond_conflicts(cond, SMP_VAL_FE_SET_BCK, file, linenum);
                }
 
-               err_code |= warnif_cond_conflicts(cond, SMP_VAL_FE_SET_BCK, file, linenum);
-
                rule = (struct switching_rule *)calloc(1, sizeof(*rule));
                rule->cond = cond;
                rule->be.name = strdup(args[1]);
index 67798b4089755ea89938e7644ed03cbe2f24a6da..ed2ff21e3bea76f9da05220b06ee05f457863950 100644 (file)
@@ -1085,8 +1085,10 @@ void deinit(void)
 
                list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
                        LIST_DEL(&rule->list);
-                       prune_acl_cond(rule->cond);
-                       free(rule->cond);
+                       if (rule->cond) {
+                               prune_acl_cond(rule->cond);
+                               free(rule->cond);
+                       }
                        free(rule);
                }
 
index b37348967a3ec31c503c19c3436d7d528b429fab..dc72ba52fa49d62e9c64ccdd141a0c2e06486d6a 100644 (file)
@@ -1238,12 +1238,14 @@ static int process_switching_rules(struct session *s, struct channel *req, int a
                struct switching_rule *rule;
 
                list_for_each_entry(rule, &s->fe->switching_rules, list) {
-                       int ret;
+                       int ret = 1;
 
-                       ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
-                       ret = acl_pass(ret);
-                       if (rule->cond->pol == ACL_COND_UNLESS)
-                               ret = !ret;
+                       if (rule->cond) {
+                               ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
+                               ret = acl_pass(ret);
+                               if (rule->cond->pol == ACL_COND_UNLESS)
+                                       ret = !ret;
+                       }
 
                        if (ret) {
                                /* If the backend name is dynamic, try to resolve the name.