MINOR: lua: Allow argument for actions
(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'.
diff --git a/include/types/hlua.h b/include/types/hlua.h
index d2aaa4a..42cfe3b 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -90,6 +90,7 @@
struct hlua_function {
char *name;
int function_ref;
+ int nargs;
};
/* This struct is used with the structs:
diff --git a/src/hlua.c b/src/hlua.c
index 82924f5..9d73fe8 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -6147,6 +6147,7 @@
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 @@
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 @@
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 @@
/* 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 @@
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;