]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: convert IP addresses to type string
authorWilly Tarreau <w@1wt.eu>
Tue, 10 Mar 2015 16:28:54 +0000 (17:28 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 10 Mar 2015 16:30:41 +0000 (17:30 +0100)
For now we don't perform any operation on IP addresses, so at least
we'd like to be able to pass them as strings so that we can log them
or whatever in Lua. Without this patch txn.src(txn) returns "nil" and
now it returns the correct IP address.

src/hlua.c

index 18c901c98b51eb2f2ddd9af12a70dc8ff35f7f16..58eb3e070068f7e2d475bde35f3962c38b9ea413 100644 (file)
@@ -116,7 +116,7 @@ static unsigned int hlua_nb_instruction = 10000;
 static int hlua_arg2lua(lua_State *L, const struct arg *arg);
 static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
 __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, unsigned int mask);
-static int hlua_smp2lua(lua_State *L, const struct sample *smp);
+static int hlua_smp2lua(lua_State *L, struct sample *smp);
 static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
 
 /* Used to check an Lua function type in the stack. It creates and
@@ -353,7 +353,7 @@ static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
  * in Lua type. This useful to convert the return of the
  * fetchs or converters.
  */
-static int hlua_smp2lua(lua_State *L, const struct sample *smp)
+static int hlua_smp2lua(lua_State *L, struct sample *smp)
 {
        switch (smp->type) {
        case SMP_T_SINT:
@@ -389,6 +389,12 @@ static int hlua_smp2lua(lua_State *L, const struct sample *smp)
        case SMP_T_IPV4:
        case SMP_T_IPV6:
        case SMP_T_ADDR: /* This type is never used to qualify a sample. */
+               if (sample_casts[smp->type][SMP_T_STR] &&
+                   sample_casts[smp->type][SMP_T_STR](smp))
+                       lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
+               else
+                       lua_pushnil(L);
+               break;
        default:
                lua_pushnil(L);
                break;