From 053ba8adfd21c1aa9d747a016e5b9caf6421f528 Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Mon, 8 Jun 2015 13:05:33 +0200 Subject: [PATCH] MINOR: lua: Variable access This patch adds two Lua function for using HAPRoxy's vraibles. The function are stored in the TXN class, and her name is "set_var" and "get_var". --- doc/lua-api/index.rst | 15 +++++++++++++ src/hlua.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 3c7201860..26641baeb 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -888,6 +888,21 @@ TXN class :param class_txn txn: The class txn object containing the data. :param opaque data: The data which is stored in the transaction. +.. js:function:: TXN.set_var(TXN, var, value) + + Converts an Lua type in a HAProxy type and store it in a variable . + + :param class_txn txn: The class txn object containing the data. + :param string var: The variable name according with the HAProxy variable syntax. + :param opaque value: The data which is stored in the variable. + +.. js:function:: TXN.get_var(TXN, var) + + Returns data stored in the variable converter in Lua type. + + :param class_txn txn: The class txn object containing the data. + :param string var: The variable name according with the HAProxy variable syntax. + .. js:function:: TXN.get_headers(txn) This function returns an array of headers. diff --git a/src/hlua.c b/src/hlua.c index 7919ce0e0..320e72900 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -38,6 +38,7 @@ #include #include #include +#include /* Lua uses longjmp to perform yield or throwing errors. This * macro is used only for identifying the function that can @@ -3322,6 +3323,52 @@ __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); } +__LJMP static int hlua_set_var(lua_State *L) +{ + struct hlua_txn *htxn; + const char *name; + size_t len; + struct sample smp; + + MAY_LJMP(check_args(L, 3, "set_var")); + + /* It is useles to retrieve the stream, but this function + * runs only in a stream context. + */ + htxn = MAY_LJMP(hlua_checktxn(L, 1)); + name = MAY_LJMP(luaL_checklstring(L, 2, &len)); + + /* Converts the third argument in a sample. */ + hlua_lua2smp(L, 3, &smp); + + /* Store the sample in a variable. */ + vars_set_by_name(name, len, htxn->s, &smp); + return 0; +} + +__LJMP static int hlua_get_var(lua_State *L) +{ + struct hlua_txn *htxn; + const char *name; + size_t len; + struct sample smp; + + MAY_LJMP(check_args(L, 2, "get_var")); + + /* It is useles to retrieve the stream, but this function + * runs only in a stream context. + */ + htxn = MAY_LJMP(hlua_checktxn(L, 1)); + name = MAY_LJMP(luaL_checklstring(L, 2, &len)); + + if (!vars_get_by_name(name, len, htxn->s, &smp)) { + lua_pushnil(L); + return 1; + } + + return hlua_smp2lua(L, &smp); +} + __LJMP static int hlua_set_priv(lua_State *L) { struct hlua *hlua; @@ -4948,6 +4995,8 @@ void hlua_init(void) /* Register Lua functions. */ hlua_class_function(gL.T, "set_priv", hlua_set_priv); hlua_class_function(gL.T, "get_priv", hlua_get_priv); + hlua_class_function(gL.T, "set_var", hlua_set_var); + hlua_class_function(gL.T, "get_var", hlua_get_var); hlua_class_function(gL.T, "close", hlua_txn_close); hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);