]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: config: add predicates "streq()" and "strneq()" to conditional expressions
authorWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 14:10:09 +0000 (16:10 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 15:02:36 +0000 (17:02 +0200)
"streq(str1,str2)" will return true if the two strings match while
"strneq(str1,str2)" will return true only if they differ. This is
convenient to match an environment variable against a predefined value.

doc/configuration.txt
src/cfgparse.c

index ff90526d868fcd653c7b3d050c7c4ab20d0a6696..4242e014e97f32349540c7970f224fbf271d210f 100644 (file)
@@ -814,6 +814,9 @@ The list of currently supported predicates is the following:
   - defined(<name>)       : returns true if an environment variable <name>
                             exists, regardless of its contents
 
+  - streq(<str1>,<str2>)  : returns true only if the two strings are equal
+  - strneq(<str1>,<str2>) : returns true only if the two strings differ
+
 Example:
 
    .if defined(HAPROXY_MWORKER)
@@ -822,6 +825,14 @@ Example:
           ...
    .endif
 
+   .if strneq("$SSL_ONLY",yes)
+          bind :80
+   .endif
+
+   .if streq("$WITH_SSL",yes)
+          bind :443 ssl crt ...
+   .endif
+
 Three other directives are provided to report some status:
 
   - .notice "message"  : emit this message at level NOTICE
index d8ba76a8b460c442e1ff3f92959567d951292084..9daac06c2f027d74679285f965b34136e426142e 100644 (file)
@@ -137,6 +137,8 @@ enum nested_cond_state {
 enum cond_predicate {
        CFG_PRED_NONE,            // none
        CFG_PRED_DEFINED,         // "defined"
+       CFG_PRED_STREQ,           // "streq"
+       CFG_PRED_STRNEQ,          // "strneq"
 };
 
 struct cond_pred_kw {
@@ -148,6 +150,8 @@ struct cond_pred_kw {
 /* supported condition predicates */
 const struct cond_pred_kw cond_predicates[] = {
        { "defined",          CFG_PRED_DEFINED,         ARG1(1, STR)         },
+       { "streq",            CFG_PRED_STREQ,           ARG2(2, STR, STR)    },
+       { "strneq",           CFG_PRED_STRNEQ,          ARG2(2, STR, STR)    },
        { NULL, CFG_PRED_NONE, 0 }
 };
 
@@ -1718,6 +1722,14 @@ static int cfg_eval_condition(char **args, char **err, const char **errptr)
                        ret = getenv(argp[0].data.str.area) != NULL;
                        goto done;
 
+               case CFG_PRED_STREQ:    // checks if the two arg are equal
+                       ret = strcmp(argp[0].data.str.area, argp[1].data.str.area) == 0;
+                       goto done;
+
+               case CFG_PRED_STRNEQ:   // checks if the two arg are different
+                       ret = strcmp(argp[0].data.str.area, argp[1].data.str.area) != 0;
+                       goto done;
+
                default:
                        memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word);
                        if (errptr)