]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: give HAProxy variable access to the applets
authorThierry FOURNIER / OZON.IO <thierry.fournier@ozon.io>
Mon, 12 Dec 2016 11:31:54 +0000 (12:31 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 12 Dec 2016 13:34:56 +0000 (14:34 +0100)
This patch give function for manipulating variables inside the
applet HTTP and applet TCP functions.

src/hlua.c

index a5efd247acbef4e39f7916ba870444c4db2168af..0ca3ec21ee214eaac9347029f8ca799ecc4c3381 100644 (file)
@@ -3194,6 +3194,81 @@ static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
        return 1;
 }
 
+__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       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.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       /* Converts the third argument in a sample. */
+       hlua_lua2smp(L, 3, &smp);
+
+       /* Store the sample in a variable. */
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       vars_set_by_name(name, len, &smp);
+       return 0;
+}
+
+__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       const char *name;
+       size_t len;
+       struct sample smp;
+
+       MAY_LJMP(check_args(L, 2, "unset_var"));
+
+       /* It is useles to retrieve the stream, but this function
+        * runs only in a stream context.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       /* Unset the variable. */
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       vars_unset_by_name(name, len, &smp);
+       return 0;
+}
+
+__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       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.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       if (!vars_get_by_name(name, len, &smp)) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       return hlua_smp2lua(L, &smp);
+}
+
 __LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
 {
        struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
@@ -3567,6 +3642,81 @@ static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
        return 1;
 }
 
+__LJMP static int hlua_applet_http_set_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       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.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       /* Converts the third argument in a sample. */
+       hlua_lua2smp(L, 3, &smp);
+
+       /* Store the sample in a variable. */
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       vars_set_by_name(name, len, &smp);
+       return 0;
+}
+
+__LJMP static int hlua_applet_http_unset_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       const char *name;
+       size_t len;
+       struct sample smp;
+
+       MAY_LJMP(check_args(L, 2, "unset_var"));
+
+       /* It is useles to retrieve the stream, but this function
+        * runs only in a stream context.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       /* Unset the variable. */
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       vars_unset_by_name(name, len, &smp);
+       return 0;
+}
+
+__LJMP static int hlua_applet_http_get_var(lua_State *L)
+{
+       struct hlua_appctx *appctx;
+       struct stream *s;
+       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.
+        */
+       appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
+       name = MAY_LJMP(luaL_checklstring(L, 2, &len));
+       s = appctx->htxn.s;
+
+       smp_set_owner(&smp, s->be, s->sess, s, 0);
+       if (!vars_get_by_name(name, len, &smp)) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       return hlua_smp2lua(L, &smp);
+}
+
 __LJMP static int hlua_applet_http_set_priv(lua_State *L)
 {
        struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
@@ -7197,6 +7347,9 @@ void hlua_init(void)
        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);
+       hlua_class_function(gL.T, "set_var",   hlua_applet_tcp_set_var);
+       hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
+       hlua_class_function(gL.T, "get_var",   hlua_applet_tcp_get_var);
 
        lua_settable(gL.T, -3);
 
@@ -7219,6 +7372,9 @@ void hlua_init(void)
        /* 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, "set_var",        hlua_applet_http_set_var);
+       hlua_class_function(gL.T, "unset_var",      hlua_applet_http_unset_var);
+       hlua_class_function(gL.T, "get_var",        hlua_applet_http_get_var);
        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);