BUG/MEDIUM: sample: Cumulate frontend and backend sample validity flags

When the sample validity flags are computed to check if a sample is used in
a valid scope, the flags depending on the proxy capabilities must be
cumulated. Historically, for a sample on the request, only the frontend
capability was used to set the sample validity flags while for a sample on
the response only the backend was used. But it is a problem for listen or
defaults proxies. For those proxies, all frontend and backend samples should
be valid. However, at many place, only frontend ones are possible.

For instance, it is impossible to set the backend name (be_name) into a
variable from a listen proxy.

This bug exists on all stable versions. Thus this patch should probably be
backported. But with some caution because the code has probably changed
serveral times. Note that nobody has ever noticed this issue. So the need to
backport this patch must be evaluated for each branch.

(cherry picked from commit 7a06ffb854281a08dae8dc2c13669a3a43da1065)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/http_act.c b/src/http_act.c
index 0aa8a37..12a9a9f 100644
--- a/src/http_act.c
+++ b/src/http_act.c
@@ -154,6 +154,7 @@
                                              struct act_rule *rule, char **err)
 {
 	int cur_arg = *orig_arg;
+	int cap = 0;
 
 	switch (args[0][4]) {
 	case 'm' :
@@ -186,8 +187,11 @@
 
 	LIST_INIT(&rule->arg.http.fmt);
 	px->conf.args.ctx = ARGC_HRQ;
-	if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
-	                            (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
+	if (px->cap & PR_CAP_FE)
+		cap |= SMP_VAL_FE_HRQ_HDR;
+	if (px->cap & PR_CAP_BE)
+		cap |= SMP_VAL_BE_HRQ_HDR;
+	if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
 		return ACT_RET_PRS_ERR;
 	}
 
@@ -576,6 +580,7 @@
                                             struct act_rule *rule, char **err)
 {
 	int cur_arg = *orig_arg;
+	int cap = 0;
 	char *error = NULL;
 
 	switch (args[0][8]) {
@@ -610,8 +615,11 @@
 
 	LIST_INIT(&rule->arg.http.fmt);
 	px->conf.args.ctx = ARGC_HRQ;
-	if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
-	                            (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
+	if (px->cap & PR_CAP_FE)
+		cap |= SMP_VAL_FE_HRQ_HDR;
+	if (px->cap & PR_CAP_BE)
+		cap |= SMP_VAL_BE_HRQ_HDR;
+	if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
 		regex_free(rule->arg.http.re);
 		return ACT_RET_PRS_ERR;
 	}
@@ -1583,7 +1591,7 @@
 static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
 						   struct act_rule *rule, char **err)
 {
-	int cap, cur_arg;
+	int cap = 0, cur_arg;
 
 	if (args[*orig_arg-1][0] == 'e') {
 		rule->action = ACT_CUSTOM;
@@ -1611,11 +1619,17 @@
 
 	if (rule->from == ACT_F_HTTP_REQ) {
 		px->conf.args.ctx = ARGC_HRQ;
-		cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRQ_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRQ_HDR;
 	}
 	else{
 		px->conf.args.ctx =  ARGC_HRS;
-		cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRS_HDR;
 	}
 
 	cur_arg++;
@@ -1693,7 +1707,7 @@
 static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
 						    struct act_rule *rule, char **err)
 {
-	int cap, cur_arg;
+	int cap = 0, cur_arg;
 
 	if (args[*orig_arg-1][8] == 'h')
 		rule->action = 0; // replace-header
@@ -1720,11 +1734,17 @@
 
 	if (rule->from == ACT_F_HTTP_REQ) {
 		px->conf.args.ctx = ARGC_HRQ;
-		cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRQ_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRQ_HDR;
 	}
 	else{
 		px->conf.args.ctx =  ARGC_HRS;
-		cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRS_HDR;
 	}
 
 	cur_arg++;
@@ -2003,7 +2023,7 @@
 static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
 					     struct act_rule *rule, char **err)
 {
-	int cap, cur_arg;
+	int cap = 0, cur_arg;
 
 	if (args[*orig_arg-1][0] == 'a') // add-acl
 		rule->action = 0;
@@ -2040,11 +2060,17 @@
 
 	if (rule->from == ACT_F_HTTP_REQ) {
 		px->conf.args.ctx = ARGC_HRQ;
-		cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRQ_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRQ_HDR;
 	}
 	else{
 		px->conf.args.ctx =  ARGC_HRS;
-		cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_FE)
+			cap |= SMP_VAL_FE_HRS_HDR;
+		if (px->cap & PR_CAP_BE)
+			cap |= SMP_VAL_BE_HRS_HDR;
 	}
 
 	/* key pattern */