]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy: refactor mode parsing
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Mon, 12 Jan 2026 13:47:51 +0000 (14:47 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 6 Feb 2026 13:35:18 +0000 (14:35 +0100)
Define a new utility function str_to_proxy_mode() which is able to
convert a string into the corresponding proxy mode if possible. This new
function is used for the parsing of "mode" configuration proxy keyword.

This patch will be reused for dynamic backend implementation, in order
to parse a similar "mode" argument via a CLI handler.

include/haproxy/proxy.h
src/cfgparse-listen.c
src/proxy.c

index 089ab9e222dfeebca76f9f26ca683559fe0ba44e..57dbbd9ff727ec9530e6a90df96ccf0b24e3f37b 100644 (file)
@@ -59,6 +59,7 @@ void deinit_proxy(struct proxy *p);
 void free_proxy(struct proxy *p);
 const char *proxy_cap_str(int cap);
 const char *proxy_mode_str(int mode);
+enum pr_mode str_to_proxy_mode(const char *mode);
 const char *proxy_find_best_option(const char *word, const char **extra);
 uint proxy_get_next_id(uint from);
 void proxy_store_name(struct proxy *px);
index 68b2eba88ba5654d1cda3b278d522a3929a084ed..3c27a638e620f3f781be71e87482390ca9f06008 100644 (file)
@@ -633,23 +633,30 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
                goto out;
        }
        else if (strcmp(args[0], "mode") == 0) {  /* sets the proxy mode */
+               enum pr_mode mode;
                if (alertif_too_many_args(1, file, linenum, args, &err_code))
                        goto out;
 
-               if (strcmp(args[1], "http") == 0) curproxy->mode = PR_MODE_HTTP;
-               else if (strcmp(args[1], "tcp") == 0) curproxy->mode = PR_MODE_TCP;
-               else if (strcmp(args[1], "log") == 0 && (curproxy->cap & PR_CAP_BE)) curproxy->mode = PR_MODE_SYSLOG;
-               else if (strcmp(args[1], "spop") == 0 && (curproxy->cap & PR_CAP_BE)) curproxy->mode = PR_MODE_SPOP;
-               else if (strcmp(args[1], "health") == 0) {
+               if (unlikely(strcmp(args[1], "health") == 0)) {
                        ha_alert("parsing [%s:%d] : 'mode health' doesn't exist anymore. Please use 'http-request return status 200' instead.\n", file, linenum);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
-               else {
+
+               mode = str_to_proxy_mode(args[1]);
+               if (!mode) {
                        ha_alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
+               else if ((mode == PR_MODE_SYSLOG || mode == PR_MODE_SPOP) &&
+                        !(curproxy->cap & PR_CAP_BE)) {
+                       ha_alert("parsing [%s:%d] : mode %s is only applicable on proxies with backend capability.\n", file, linenum, proxy_mode_str(mode));
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
+
+               curproxy->mode = mode;
        }
        else if (strcmp(args[0], "id") == 0) {
                struct proxy *conflict;
index fc65f3c0668f80d08b71d7d0e7de78e46ddc3f0f..207f85a98d7446727173087b01b4b5aeb7c3ce8b 100644 (file)
@@ -501,6 +501,21 @@ const char *proxy_mode_str(int mode) {
                return "unknown";
 }
 
+/* Convert <mode> string into proxy mode type. PR_MODES is returned for unknown values. */
+enum pr_mode str_to_proxy_mode(const char *mode)
+{
+       if (strcmp(mode, "http") == 0)
+               return PR_MODE_HTTP;
+       else if (strcmp(mode, "tcp") == 0)
+               return PR_MODE_TCP;
+       else if (strcmp(mode, "log") == 0)
+               return PR_MODE_SYSLOG;
+       else if (strcmp(mode, "spop") == 0)
+               return PR_MODE_SPOP;
+
+       return PR_MODES;
+}
+
 /* try to find among known options the one that looks closest to <word> by
  * counting transitions between letters, digits and other characters. Will
  * return the best matching word if found, otherwise NULL. An optional array