MEDIUM: http: add the "set-nice" action to http-request and http-response

This new action changes the nice factor of the task processing the current
request.
diff --git a/doc/configuration.txt b/doc/configuration.txt
index a0f8827..93d2f2c 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -2668,7 +2668,8 @@
   See also : "option httpchk", "http-check disable-on-404"
 
 http-request { allow | deny | tarpit | auth [realm <realm>] | redirect <rule> |
-              add-header <name> <fmt> | set-header <name> <fmt> }
+              add-header <name> <fmt> | set-header <name> <fmt> |
+              set-nice <nice> }
              [ { if | unless } <condition> ]
   Access control for Layer 7 requests
 
@@ -2726,6 +2727,15 @@
       information to the server, where the header must not be manipulated by
       external users.
 
+    - "set-nice" sets the "nice" factor of the current request being processed.
+      It only has effect against the other requests being processed at the same
+      time. The default value is 0, unless altered by the "nice" setting on the
+      "bind" line. The accepted range is -1024..1024. The higher the value, the
+      nicest the request will be. Lower values will make the request more
+      important than other ones. This can be useful to improve the speed of
+      some requests, or lower the priority of non-important requests. Using
+      this setting without prior experimentation can cause some major slowdown.
+
   There is no limit to the number of http-request statements per instance.
 
   It is important to know that http-request rules are processed very early in
@@ -2761,7 +2771,7 @@
   See also : "stats http-request", section 3.4 about userlists and section 7
              about ACL usage.
 
-http-response { allow | deny | add-header <name> <fmt> |
+http-response { allow | deny | add-header <name> <fmt> | set-nice <nice> |
                 set-header <name> <fmt> } [ { if | unless } <condition> ]
   Access control for Layer 7 responses
 
@@ -2797,6 +2807,15 @@
       information to the server, where the header must not be manipulated by
       external users.
 
+    - "set-nice" sets the "nice" factor of the current request being processed.
+      It only has effect against the other requests being processed at the same
+      time. The default value is 0, unless altered by the "nice" setting on the
+      "bind" line. The accepted range is -1024..1024. The higher the value, the
+      nicest the request will be. Lower values will make the request more
+      important than other ones. This can be useful to improve the speed of
+      some requests, or lower the priority of non-important requests. Using
+      this setting without prior experimentation can cause some major slowdown.
+
   There is no limit to the number of http-response statements per instance.
 
   It is important to know that http-reqsponse rules are processed very early in
diff --git a/include/types/proto_http.h b/include/types/proto_http.h
index 6190e6c..b50375f 100644
--- a/include/types/proto_http.h
+++ b/include/types/proto_http.h
@@ -246,6 +246,7 @@
 	HTTP_REQ_ACT_ADD_HDR,
 	HTTP_REQ_ACT_SET_HDR,
 	HTTP_REQ_ACT_REDIR,
+	HTTP_REQ_ACT_SET_NICE,
 	HTTP_REQ_ACT_MAX /* must always be last */
 };
 
@@ -256,6 +257,7 @@
 	HTTP_RES_ACT_DENY,
 	HTTP_RES_ACT_ADD_HDR,
 	HTTP_RES_ACT_SET_HDR,
+	HTTP_RES_ACT_SET_NICE,
 	HTTP_RES_ACT_MAX /* must always be last */
 };
 
@@ -368,6 +370,7 @@
 			struct list fmt;       /* log-format compatible expression */
 		} hdr_add;                     /* args used by "add-header" and "set-header" */
 		struct redirect_rule *redir;   /* redirect rule or "http-request redirect" */
+		int nice;                      /* nice value for HTTP_REQ_ACT_SET_NICE */
 	} arg;                                 /* arguments used by some actions */
 };
 
@@ -381,6 +384,7 @@
 			int name_len;          /* header name's length */
 			struct list fmt;       /* log-format compatible expression */
 		} hdr_add;                     /* args used by "add-header" and "set-header" */
+		int nice;                      /* nice value for HTTP_RES_ACT_SET_NICE */
 	} arg;                                 /* arguments used by some actions */
 };
 
diff --git a/src/proto_http.c b/src/proto_http.c
index 47f413e..ed6c90b 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -3206,6 +3206,10 @@
 		case HTTP_REQ_ACT_REDIR:
 			return rule;
 
+		case HTTP_REQ_ACT_SET_NICE:
+			s->task->nice = rule->arg.nice;
+			break;
+
 		case HTTP_REQ_ACT_SET_HDR:
 			ctx.idx = 0;
 			/* remove all occurrences of the header */
@@ -3271,6 +3275,10 @@
 			txn->flags |= TX_SVDENY;
 			return rule;
 
+		case HTTP_RES_ACT_SET_NICE:
+			s->task->nice = rule->arg.nice;
+			break;
+
 		case HTTP_RES_ACT_SET_HDR:
 			ctx.idx = 0;
 			/* remove all occurrences of the header */
@@ -8386,6 +8394,22 @@
 			} else
 				break;
 		}
+	} else if (!strcmp(args[0], "set-nice")) {
+		rule->action = HTTP_REQ_ACT_SET_NICE;
+		cur_arg = 1;
+
+		if (!*args[cur_arg] ||
+		    (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
+			Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
+			      file, linenum, args[0]);
+			goto out_err;
+		}
+		rule->arg.nice = atoi(args[cur_arg]);
+		if (rule->arg.nice < -1024)
+			rule->arg.nice = -1024;
+		else if (rule->arg.nice > 1024)
+			rule->arg.nice = 1024;
+		cur_arg++;
 	} else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
 		rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
 		cur_arg = 1;
@@ -8425,7 +8449,7 @@
 		cur_arg = 2;
 		return rule;
 	} else {
-		Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n",
+		Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', but got '%s'%s.\n",
 		      file, linenum, args[0], *args[0] ? "" : " (missing argument)");
 		goto out_err;
 	}
@@ -8473,6 +8497,22 @@
 	} else if (!strcmp(args[0], "deny")) {
 		rule->action = HTTP_RES_ACT_DENY;
 		cur_arg = 1;
+	} else if (!strcmp(args[0], "set-nice")) {
+		rule->action = HTTP_RES_ACT_SET_NICE;
+		cur_arg = 1;
+
+		if (!*args[cur_arg] ||
+		    (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
+			Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
+			      file, linenum, args[0]);
+			goto out_err;
+		}
+		rule->arg.nice = atoi(args[cur_arg]);
+		if (rule->arg.nice < -1024)
+			rule->arg.nice = -1024;
+		else if (rule->arg.nice > 1024)
+			rule->arg.nice = 1024;
+		cur_arg++;
 	} else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
 		rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
 		cur_arg = 1;
@@ -8493,7 +8533,7 @@
 				       (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
 		cur_arg += 2;
 	} else {
-		Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', but got '%s'%s.\n",
+		Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', but got '%s'%s.\n",
 		      file, linenum, args[0], *args[0] ? "" : " (missing argument)");
 		goto out_err;
 	}