]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: Variable access
authorThierry FOURNIER <tfournier@arpalert.org>
Mon, 8 Jun 2015 11:05:33 +0000 (13:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 13 Jun 2015 21:01:37 +0000 (23:01 +0200)
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
src/hlua.c

index 3c7201860f5c100d6476c163f31e88915fb1fcd6..26641baebedf85a64a3db290111e7379c273e781 100644 (file)
@@ -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 <var>.
+
+  :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 <var> 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.
index 7919ce0e0983dfa84ebee26bf6725637a8e16336..320e72900c1b7a5b7e8c601d62eac224e59fba16 100644 (file)
@@ -38,6 +38,7 @@
 #include <proto/ssl_sock.h>
 #include <proto/stream_interface.h>
 #include <proto/task.h>
+#include <proto/vars.h>
 
 /* 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);