]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] redirect: add support for unconditional rules
authorWilly Tarreau <w@1wt.eu>
Sun, 3 Jan 2010 19:03:03 +0000 (20:03 +0100)
committerWilly Tarreau <w@1wt.eu>
Sun, 3 Jan 2010 20:22:08 +0000 (21:22 +0100)
Sometimes it's useful to be able to specify an unconditional redirect
rule without adding "if TRUE".

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

index 7e7cc5dcc256e1e0b917f559e5c61895b800b5fe..b5d1fda18411964f4b120001d63f046bf76f7742 100644 (file)
@@ -3296,14 +3296,14 @@ rate-limit sessions <rate>
   See also : the "backlog" keyword and the "fe_sess_rate" ACL criterion.
 
 
-redirect location <to> [code <code>] <option> {if | unless} <condition>
-redirect prefix   <to> [code <code>] <option> {if | unless} <condition>
+redirect location <to> [code <code>] <option> [{if | unless} <condition>]
+redirect prefix   <to> [code <code>] <option> [{if | unless} <condition>]
   Return an HTTP redirection if/unless a condition is matched
   May be used in sections :   defaults | frontend | listen | backend
                                  no    |    yes   |   yes  |   yes
 
   If/unless the condition is matched, the HTTP request will lead to a redirect
-  response.
+  response. If no condition is specified, the redirect applies unconditionally.
 
   Arguments :
     <to>      With "redirect location", the exact value in <to> is placed into
index 938caf16824fbf46e81ed73ca0cea69323a79a84..cd7ecd1a93e5f198ab839339afb772628301bd4e 100644 (file)
@@ -1754,7 +1754,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
        }
        else if (!strcmp(args[0], "redirect")) {
                int pol = ACL_COND_NONE;
-               struct acl_cond *cond;
+               struct acl_cond *cond = NULL;
                struct redirect_rule *rule;
                int cur_arg;
                int type = REDIRECT_TYPE_NONE;
@@ -1859,23 +1859,19 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                        goto out;
                }
 
-               if (pol == ACL_COND_NONE) {
-                       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 ((cond = parse_acl_cond((const char **)args + cur_arg, &curproxy->acl, pol)) == NULL) {
+               if (pol != ACL_COND_NONE &&
+                   (cond = parse_acl_cond((const char **)args + cur_arg, &curproxy->acl, pol)) == NULL) {
                        Alert("parsing [%s:%d] : '%s': error detected while parsing redirect condition.\n",
                              file, linenum, args[0]);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
 
-               cond->file = file;
-               cond->line = linenum;
-               curproxy->acl_requires |= cond->requires;
+               if (cond) {
+                       cond->file = file;
+                       cond->line = linenum;
+                       curproxy->acl_requires |= cond->requires;
+               }
                rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
                rule->cond = cond;
                rule->rdr_str = strdup(destination);
index 71537d02fd8bdce02cd4d1c16348822d55859824..48f23d67cc9af3ef43988e439cb8cd982ac8ac11 100644 (file)
@@ -797,8 +797,10 @@ void deinit(void)
 
                list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
                        LIST_DEL(&rdr->list);
-                       prune_acl_cond(rdr->cond);
-                       free(rdr->cond);
+                       if (rdr->cond) {
+                               prune_acl_cond(rdr->cond);
+                               free(rdr->cond);
+                       }
                        free(rdr->rdr_str);
                        free(rdr);
                }
index f0e87852673faa562e3ce776dc8e1a2485fdb2a7..5d700001996c331c1b1fda7739395a2ca0291c8a 100644 (file)
@@ -2697,11 +2697,14 @@ int http_process_req_common(struct session *s, struct buffer *req, int an_bit, s
 
        /* check whether we have some ACLs set to redirect this request */
        list_for_each_entry(rule, &px->redirect_rules, list) {
-               int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
+               int ret = ACL_PAT_PASS;
 
-               ret = acl_pass(ret);
-               if (rule->cond->pol == ACL_COND_UNLESS)
-                       ret = !ret;
+               if (rule->cond) {
+                       ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
+                       ret = acl_pass(ret);
+                       if (rule->cond->pol == ACL_COND_UNLESS)
+                               ret = !ret;
+               }
 
                if (ret) {
                        struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };