]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http: prepare support for parsing redirect actions on responses
authorWilly Tarreau <w@1wt.eu>
Thu, 28 May 2015 13:26:58 +0000 (15:26 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 28 May 2015 15:43:11 +0000 (17:43 +0200)
In order to support http-response redirect, the parsing needs to be
adapted a little bit to only support the "location" type, and to
adjust the log-format parser so that it knows the direction of the
sample fetch calls.

include/proto/proto_http.h
src/cfgparse.c
src/proto_http.c

index 41a7dc6a28b76b32a52d326afde79e28a8efd299..5f1ac1f3cb46ad72f316ca5df0d57d195615655f 100644 (file)
@@ -125,7 +125,7 @@ void free_http_req_rules(struct list *r);
 void free_http_res_rules(struct list *r);
 struct chunk *http_error_message(struct stream *s, int msgnum);
 struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
-                                               const char **args, char **errmsg, int use_fmt);
+                                               const char **args, char **errmsg, int use_fmt, int dir);
 int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private);
 int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private);
 
index 1403bd19d61c51b6aecea7776d69e3170e4ce2af..a030723825624d5da8f2a5b307667e0d12e0478a 100644 (file)
@@ -3327,7 +3327,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                        goto out;
                }
 
-               if ((rule = http_parse_redirect_rule(file, linenum, curproxy, (const char **)args + 1, &errmsg, 0)) == NULL) {
+               if ((rule = http_parse_redirect_rule(file, linenum, curproxy, (const char **)args + 1, &errmsg, 0, 0)) == NULL) {
                        Alert("parsing [%s:%d] : error detected in %s '%s' while parsing redirect rule : %s.\n",
                              file, linenum, proxy_type_str(curproxy), curproxy->id, errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
index 01ee439eb2e48b51f433c33dfcd9064c3b6ab007..655b3ae989fda004f6b90fda50953ef971249bb6 100644 (file)
@@ -9369,7 +9369,7 @@ struct http_req_rule *parse_http_req_cond(const char **args, const char *file, i
                struct redirect_rule *redir;
                char *errmsg = NULL;
 
-               if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
+               if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
                        Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
                              file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
                        goto out_err;
@@ -9880,10 +9880,11 @@ struct http_res_rule *parse_http_res_cond(const char **args, const char *file, i
 
 /* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
  * with <err> filled with the error message. If <use_fmt> is not null, builds a
- * dynamic log-format rule instead of a static string.
+ * dynamic log-format rule instead of a static string. Parameter <dir> indicates
+ * the direction of the rule, and equals 0 for request, non-zero for responses.
  */
 struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
-                                               const char **args, char **errmsg, int use_fmt)
+                                               const char **args, char **errmsg, int use_fmt, int dir)
 {
        struct redirect_rule *rule;
        int cur_arg;
@@ -9908,7 +9909,6 @@ struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, st
                else if (strcmp(args[cur_arg], "prefix") == 0) {
                        if (!*args[cur_arg + 1])
                                goto missing_arg;
-
                        type = REDIRECT_TYPE_PREFIX;
                        cur_arg++;
                        destination = args[cur_arg];
@@ -9979,6 +9979,11 @@ struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, st
                return NULL;
        }
 
+       if (dir && type != REDIRECT_TYPE_LOCATION) {
+               memprintf(errmsg, "response only supports redirect type 'location'");
+               return NULL;
+       }
+
        rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
        rule->cond = cond;
        LIST_INIT(&rule->rdr_fmt);
@@ -9998,7 +10003,8 @@ struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, st
                curproxy->conf.args.ctx = ARGC_RDR;
                if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
                        parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
-                                              (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
+                                              dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
+                                                  : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
                                               file, linenum);
                        free(curproxy->conf.lfs_file);
                        curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);