[MINOR] redirect: add support for unconditional rules
Sometimes it's useful to be able to specify an unconditional redirect
rule without adding "if TRUE".
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 7e7cc5d..b5d1fda 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -3296,14 +3296,14 @@
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
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 938caf1..cd7ecd1 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -1754,7 +1754,7 @@
}
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 @@
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);
diff --git a/src/haproxy.c b/src/haproxy.c
index 71537d0..48f23d6 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -797,8 +797,10 @@
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);
}
diff --git a/src/proto_http.c b/src/proto_http.c
index f0e8785..5d70000 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -2697,11 +2697,14 @@
/* 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 };