]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: cfgparse: make backend_parse_balance() use memprintf to report errors
authorWilly Tarreau <w@1wt.eu>
Tue, 8 May 2012 16:14:39 +0000 (18:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 8 May 2012 19:28:17 +0000 (21:28 +0200)
Using the new error reporting framework makes it easier to report complex
errors.

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

index b4dfc835c36d5fed0fbe1252105ddf1cb9a4872a..24cd9093625aaf94820b718cf1ba5c56aff5f652 100644 (file)
@@ -35,8 +35,7 @@ int assign_server_and_queue(struct session *s);
 int connect_server(struct session *s);
 int srv_redispatch_connect(struct session *t);
 const char *backend_lb_algo_str(int algo);
-int backend_parse_balance(const char **args, char *err,
-                         int errlen, struct proxy *curproxy);
+int backend_parse_balance(const char **args, char **err, struct proxy *curproxy);
 int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit);
 
 int be_downtime(struct proxy *px);
index 4652fba047018eccbcb93350062c7b623b75e63b..7c1c92d68231ce8267a45abc168b808937185730 100644 (file)
@@ -1215,12 +1215,12 @@ const char *backend_lb_algo_str(int algo) {
 
 /* This function parses a "balance" statement in a backend section describing
  * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
- * returns -1, it may write an error message into ther <err> buffer, for at
- * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
- * written. The function must be called with <args> pointing to the first word
- * after "balance".
+ * returns -1, it will write an error message into the <err> buffer which will
+ * automatically be allocated and must be passed as NULL. The trailing '\n'
+ * will not be written. The function must be called with <args> pointing to the
+ * first word after "balance".
  */
-int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
+int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
 {
        if (!*(args[0])) {
                /* if no option is set, use round-robin by default */
@@ -1258,7 +1258,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                while (*args[arg]) {
                        if (!strcmp(args[arg], "len")) {
                                if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
-                                       snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
+                                       memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
                                        return -1;
                                }
                                curproxy->uri_len_limit = atoi(args[arg+1]);
@@ -1266,7 +1266,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                        }
                        else if (!strcmp(args[arg], "depth")) {
                                if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
-                                       snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
+                                       memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
                                        return -1;
                                }
                                /* hint: we store the position of the ending '/' (depth+1) so
@@ -1276,14 +1276,14 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                                arg += 2;
                        }
                        else {
-                               snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
+                               memprintf(err, "%s only accepts parameters 'len' and 'depth' (got '%s').", args[0], args[arg]);
                                return -1;
                        }
                }
        }
        else if (!strcmp(args[0], "url_param")) {
                if (!*args[1]) {
-                       snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
+                       memprintf(err, "%s requires an URL parameter name.", args[0]);
                        return -1;
                }
                curproxy->lbprm.algo &= ~BE_LB_ALGO;
@@ -1294,7 +1294,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                curproxy->url_param_len  = strlen(args[1]);
                if (*args[2]) {
                        if (strcmp(args[2], "check_post")) {
-                               snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
+                               memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
                                return -1;
                        }
                        if (*args[3]) {
@@ -1315,7 +1315,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                end = strchr(beg, ')');
 
                if (!end || end == beg) {
-                       snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
+                       memprintf(err, "hdr requires an http header field name.");
                        return -1;
                }
 
@@ -1329,7 +1329,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
 
                if (*args[1]) {
                        if (strcmp(args[1], "use_domain_only")) {
-                               snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
+                               memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
                                return -1;
                        }
                        curproxy->hh_match_domain = 1;
@@ -1347,7 +1347,7 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                        end = strchr(beg, ')');
 
                        if (!end || end == beg) {
-                               snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
+                               memprintf(err, "rdp-cookie : missing cookie name.");
                                return -1;
                        }
 
@@ -1361,12 +1361,12 @@ int backend_parse_balance(const char **args, char *err, int errlen, struct proxy
                        curproxy->hh_len  = strlen(curproxy->hh_name);
                }
                else { /* syntax */
-                       snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
+                       memprintf(err, "rdp-cookie : missing cookie name.");
                        return -1;
                }
        }
        else {
-               snprintf(err, errlen, "'balance' only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
+               memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
                return -1;
        }
        return 0;
index 7e8e7d56c09985fbb24c1871e817763191cbf71e..134794049ca465833ad1b1a636878a54a6a3ca88 100644 (file)
@@ -3963,9 +3963,8 @@ stats_error_parsing:
                if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
                        err_code |= ERR_WARN;
 
-               memcpy(trash, "error near 'balance'", 21);
-               if (backend_parse_balance((const char **)args + 1, trash, sizeof(trash), curproxy) < 0) {
-                       Alert("parsing [%s:%d] : %s\n", file, linenum, trash);
+               if (backend_parse_balance((const char **)args + 1, &errmsg, curproxy) < 0) {
+                       Alert("parsing [%s:%d] : %s %s\n", file, linenum, args[0], errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }