]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: Allow argument for actions
authorThierry FOURNIER / OZON.IO <thierry.fournier@ozon.io>
Fri, 9 Dec 2016 17:03:31 +0000 (18:03 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 12 Dec 2016 13:34:56 +0000 (14:34 +0100)
(http|tcp)-(request|response) action cannot take arguments from the
configuration file. Arguments are useful for executing the action with
a special context.

This patch adds the possibility of passing arguments to an action. It
runs exactly like sample fetches and other Lua wrappers.

Note that this patch implements a 'TODO'.

include/types/hlua.h
src/hlua.c

index d2aaa4a3c69de399fecc920dda9fb5ad6785298c..42cfe3b839f3d9dcbd7afb628b792c2f25bb88f4 100644 (file)
@@ -90,6 +90,7 @@ struct hlua_init_function {
 struct hlua_function {
        char *name;
        int function_ref;
+       int nargs;
 };
 
 /* This struct is used with the structs:
index 82924f5c514305ed026a42679f8dd40747de83b3..9d73fe83b45dbaf1a9e11937738260f214e4220f 100644 (file)
@@ -6147,6 +6147,7 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
                                               struct act_rule *rule, char **err)
 {
        struct hlua_function *fcn = rule->kw->private;
+       int i;
 
        /* Memory for the rule. */
        rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
@@ -6155,11 +6156,30 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
                return ACT_RET_PRS_ERR;
        }
 
+       /* Memory for arguments. */
+       rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
+       if (!rule->arg.hlua_rule->args) {
+               memprintf(err, "out of memory error");
+               return ACT_RET_PRS_ERR;
+       }
+
        /* Reference the Lua function and store the reference. */
        rule->arg.hlua_rule->fcn = *fcn;
 
-       /* TODO: later accept arguments. */
-       rule->arg.hlua_rule->args = NULL;
+       /* Expect some arguments */
+       for (i = 0; i < fcn->nargs; i++) {
+               if (*args[i+1] == '\0') {
+                       memprintf(err, "expect %d arguments", fcn->nargs);
+                       return ACT_RET_PRS_ERR;
+               }
+               rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
+               if (!rule->arg.hlua_rule->args[i]) {
+                       memprintf(err, "out of memory error");
+                       return ACT_RET_PRS_ERR;
+               }
+               (*cur_arg)++;
+       }
+       rule->arg.hlua_rule->args[i] = NULL;
 
        rule->action = ACT_CUSTOM;
        rule->action_ptr = hlua_action;
@@ -6217,8 +6237,13 @@ __LJMP static int hlua_register_action(lua_State *L)
        int ref;
        int len;
        struct hlua_function *fcn;
+       int nargs;
 
-       MAY_LJMP(check_args(L, 3, "register_action"));
+       /* Initialise the number of expected arguments at 0. */
+       nargs = 0;
+
+       if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
+               WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
 
        /* First argument : converter name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));
@@ -6230,6 +6255,10 @@ __LJMP static int hlua_register_action(lua_State *L)
        /* Third argument : lua function. */
        ref = MAY_LJMP(hlua_checkfunction(L, 3));
 
+       /* Fouth argument : number of mandatories arguments expected on the configuration line. */
+       if (lua_gettop(L) >= 4)
+               nargs = MAY_LJMP(luaL_checkinteger(L, 4));
+
        /* browse the second argulent as an array. */
        lua_pushnil(L);
        while (lua_next(L, 2) != 0) {
@@ -6251,6 +6280,9 @@ __LJMP static int hlua_register_action(lua_State *L)
                        WILL_LJMP(luaL_error(L, "lua out of memory error."));
                fcn->function_ref = ref;
 
+               /* Set the expected number od arguments. */
+               fcn->nargs = nargs;
+
                /* List head */
                akl->list.n = akl->list.p = NULL;