]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: add set/get priv for applets
authorThierry FOURNIER <tfournier@arpalert.org>
Fri, 25 Dec 2015 00:33:18 +0000 (01:33 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 25 Dec 2015 09:34:28 +0000 (10:34 +0100)
The applet can't have access to the session private data. This patch
fix this problem. Now an applet can use private data stored by actions
and fecthes.

doc/lua-api/index.rst
src/hlua.c

index 7eef9e4708065d3347abc345b6f1eeb7e6e7d6b3..6adb72ef63f73aa093c40d724923d30b0716e3d0 100644 (file)
@@ -1596,6 +1596,24 @@ AppletHTTP class
   :param class_AppletHTTP applet: An :ref:`applethttp_class`
   :param string msg: the message to send.
 
+.. js:function:: AppletHTTP.get_priv(applet)
+
+  Return Lua data stored in the current transaction (with the
+  `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
+  value.
+
+  :param class_AppletHTTP applet: An :ref:`applethttp_class`
+  :returns: the opaque data previsously stored, or nil if nothing is
+     avalaible.
+
+.. js:function:: AppletHTTP.set_priv(applet, data)
+
+  Store any data in the current HAProxy transaction. This action replace the
+  old stored data.
+
+  :param class_AppletHTTP applet: An :ref:`applethttp_class`
+  :param opaque data: The data which is stored in the transaction.
+
 .. _applettcp_class:
 
 AppletTCP class
@@ -1659,6 +1677,24 @@ AppletTCP class
   :param class_AppletTCP applet: An :ref:`applettcp_class`
   :param string msg: the message to send.
 
+.. js:function:: AppletTCP.get_priv(applet)
+
+  Return Lua data stored in the current transaction (with the
+  `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
+  value.
+
+  :param class_AppletTCP applet: An :ref:`applettcp_class`
+  :returns: the opaque data previsously stored, or nil if nothing is
+     avalaible.
+
+.. js:function:: AppletTCP.set_priv(applet, data)
+
+  Store any data in the current HAProxy transaction. This action replace the
+  old stored data.
+
+  :param class_AppletTCP applet: An :ref:`applettcp_class`
+  :param opaque data: The data which is stored in the transaction.
+
 External Lua libraries
 ======================
 
index d70b01b6cafe2405490a423d5af21132eba13dbd..5cf23203fc6e40bfc8de5101e122f32e88eca35d 100644 (file)
@@ -3254,6 +3254,37 @@ static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
        return 1;
 }
 
+__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
+{
+       struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       struct stream *s = appctx->htxn.s;
+       struct hlua *hlua = &s->hlua;
+
+       MAY_LJMP(check_args(L, 2, "set_priv"));
+
+       /* Remove previous value. */
+       if (hlua->Mref != -1)
+               luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
+
+       /* Get and store new value. */
+       lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
+       hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
+
+       return 0;
+}
+
+__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
+{
+       struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       struct stream *s = appctx->htxn.s;
+       struct hlua *hlua = &s->hlua;
+
+       /* Push configuration index in the stack. */
+       lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
+
+       return 1;
+}
+
 /* If expected data not yet available, it returns a yield. This function
  * consumes the data in the buffer. It returns a string containing the
  * data. This string can be empty.
@@ -3596,6 +3627,37 @@ static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
        return 1;
 }
 
+__LJMP static int hlua_applet_http_set_priv(lua_State *L)
+{
+       struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
+       struct stream *s = appctx->htxn.s;
+       struct hlua *hlua = &s->hlua;
+
+       MAY_LJMP(check_args(L, 2, "set_priv"));
+
+       /* Remove previous value. */
+       if (hlua->Mref != -1)
+               luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
+
+       /* Get and store new value. */
+       lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
+       hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
+
+       return 0;
+}
+
+__LJMP static int hlua_applet_http_get_priv(lua_State *L)
+{
+       struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
+       struct stream *s = appctx->htxn.s;
+       struct hlua *hlua = &s->hlua;
+
+       /* Push configuration index in the stack. */
+       lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
+
+       return 1;
+}
+
 /* If expected data not yet available, it returns a yield. This function
  * consumes the data in the buffer. It returns a string containing the
  * data. This string can be empty.
@@ -6826,9 +6888,11 @@ void hlua_init(void)
        lua_newtable(gL.T);
 
        /* Register Lua functions. */
-       hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
-       hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
-       hlua_class_function(gL.T, "send",    hlua_applet_tcp_send);
+       hlua_class_function(gL.T, "getline",  hlua_applet_tcp_getline);
+       hlua_class_function(gL.T, "receive",  hlua_applet_tcp_recv);
+       hlua_class_function(gL.T, "send",     hlua_applet_tcp_send);
+       hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
+       hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
 
        lua_settable(gL.T, -3);
 
@@ -6857,6 +6921,8 @@ void hlua_init(void)
        lua_newtable(gL.T);
 
        /* Register Lua functions. */
+       hlua_class_function(gL.T, "set_priv",       hlua_applet_http_set_priv);
+       hlua_class_function(gL.T, "get_priv",       hlua_applet_http_get_priv);
        hlua_class_function(gL.T, "getline",        hlua_applet_http_getline);
        hlua_class_function(gL.T, "receive",        hlua_applet_http_recv);
        hlua_class_function(gL.T, "send",           hlua_applet_http_send);