From: Willy Tarreau Date: Thu, 28 May 2015 13:26:58 +0000 (+0200) Subject: MINOR: http: prepare support for parsing redirect actions on responses X-Git-Tag: v1.6-dev2~78 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=be4653b6d4c835092dacec3a7b2431df9c6704dd;p=thirdparty%2Fhaproxy.git MINOR: http: prepare support for parsing redirect actions on responses 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. --- diff --git a/include/proto/proto_http.h b/include/proto/proto_http.h index 41a7dc6a28..5f1ac1f3cb 100644 --- a/include/proto/proto_http.h +++ b/include/proto/proto_http.h @@ -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); diff --git a/src/cfgparse.c b/src/cfgparse.c index 1403bd19d6..a030723825 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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; diff --git a/src/proto_http.c b/src/proto_http.c index 01ee439eb2..655b3ae989 100644 --- a/src/proto_http.c +++ b/src/proto_http.c @@ -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 filled with the error message. If is not null, builds a - * dynamic log-format rule instead of a static string. + * dynamic log-format rule instead of a static string. Parameter 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);