MINOR: cfgcond: Implement strstr condition expression
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 20 Feb 2023 16:55:58 +0000 (17:55 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 21 Feb 2023 10:44:55 +0000 (11:44 +0100)
Implement a way to match a substring in a string. The strstr expresionn can
now be used to do so.

doc/configuration.txt
include/haproxy/cfgcond-t.h
src/cfgcond.c

index 3c60634..8aea55e 100644 (file)
@@ -865,6 +865,7 @@ The list of currently supported predicates is the following:
 
   - streq(<str1>,<str2>)  : returns true only if the two strings are equal
   - strneq(<str1>,<str2>) : returns true only if the two strings differ
+  - strstr(<str1>,<str2>) : returns true only if the second string is found in the first one
 
   - version_atleast(<ver>): returns true if the current haproxy version is
                             at least as recent as <ver> otherwise false. The
index 1e09293..206ae06 100644 (file)
@@ -48,6 +48,7 @@ enum cond_predicate {
        CFG_PRED_FEATURE,                // "feature"
        CFG_PRED_STREQ,                  // "streq"
        CFG_PRED_STRNEQ,                 // "strneq"
+       CFG_PRED_STRSTR,                 // "strstr"
        CFG_PRED_VERSION_ATLEAST,        // "version_atleast"
        CFG_PRED_VERSION_BEFORE,         // "version_before"
        CFG_PRED_OSSL_VERSION_ATLEAST,   // "openssl_version_atleast"
index 5fb7069..b29fcbf 100644 (file)
@@ -22,6 +22,7 @@ const struct cond_pred_kw cond_predicates[] = {
        { "feature",                 CFG_PRED_FEATURE,                ARG1(1, STR)         },
        { "streq",                   CFG_PRED_STREQ,                  ARG2(2, STR, STR)    },
        { "strneq",                  CFG_PRED_STRNEQ,                 ARG2(2, STR, STR)    },
+       { "strstr",                  CFG_PRED_STRSTR,                 ARG2(2, STR, STR)    },
        { "version_atleast",         CFG_PRED_VERSION_ATLEAST,        ARG1(1, STR)         },
        { "version_before",          CFG_PRED_VERSION_BEFORE,         ARG1(1, STR)         },
        { "openssl_version_atleast", CFG_PRED_OSSL_VERSION_ATLEAST,   ARG1(1, STR)         },
@@ -225,6 +226,10 @@ int cfg_eval_cond_term(const struct cfg_cond_term *term, char **err)
                        ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) != 0;
                        break;
 
+               case CFG_PRED_STRSTR:   // checks if the 2nd arg is found in the first one
+                       ret = strstr(term->args[0].data.str.area, term->args[1].data.str.area) != NULL;
+                       break;
+
                case CFG_PRED_VERSION_ATLEAST: // checks if the current version is at least this one
                        ret = compare_current_version(term->args[0].data.str.area) <= 0;
                        break;