From: Vsevolod Stakhov Date: Tue, 7 Apr 2015 21:22:47 +0000 (+0100) Subject: Add rspamd{text} type to avoid unnecessary copying of large chunks of X-Git-Tag: 0.9.0~302 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cdd553cc27cc230662025663e353a9a7c8ce5e8a;p=rspamd.git Add rspamd{text} type to avoid unnecessary copying of large chunks of data. --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 1b065c2e6..0364dff66 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -234,6 +234,7 @@ rspamd_lua_init (struct rspamd_config *cfg) luaopen_rsa (L); luaopen_ip (L); luaopen_expression (L); + luaopen_text (L); rspamd_lua_add_preload (L, "ucl", luaopen_ucl); diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index f9d365ae0..07ed77c14 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -189,6 +189,7 @@ void luaopen_rsa (lua_State * L); void luaopen_ip (lua_State * L); void luaopen_expression (lua_State * L); void luaopen_logger (lua_State * L); +void luaopen_text (lua_State *L); gint rspamd_lua_call_filter (const gchar *function, struct rspamd_task *task); gint rspamd_lua_call_chain_filter (const gchar *function, diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index ef05102c5..13fe9042e 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -683,6 +683,22 @@ static const struct luaL_reg urllib_m[] = { {NULL, NULL} }; +/* Blob methods */ +struct rspamd_lua_text { + const gchar *start; + gsize len; +}; + +LUA_FUNCTION_DEF (text, len); +LUA_FUNCTION_DEF (text, str); + +static const struct luaL_reg textlib_m[] = { + LUA_INTERFACE_DEF (text, len), + LUA_INTERFACE_DEF (text, str), + {"__tostring", lua_text_str}, + {NULL, NULL} +}; + /* Utility functions */ struct rspamd_task * lua_check_task (lua_State * L, gint pos) @@ -724,6 +740,16 @@ lua_check_url (lua_State * L) return ud ? *((struct rspamd_url **)ud) : NULL; } +static struct rspamd_lua_text * +lua_check_text (lua_State * L, gint pos) +{ + void *ud = luaL_checkudata (L, pos, "rspamd{text}"); + luaL_argcheck (L, ud != NULL, pos, "'text' expected"); + return ud ? (struct rspamd_lua_text *)ud : NULL; +} + +/* Task methods */ + static int lua_task_create_empty (lua_State *L) { @@ -2547,6 +2573,37 @@ lua_url_get_phished (lua_State *L) return 1; } +/* Text methods */ +static gint +lua_text_len (lua_State *L) +{ + struct rspamd_lua_text *t = lua_check_text (L, 1); + gsize l = 0; + + if (t != NULL) { + l = t->len; + } + + lua_pushnumber (L, l); + + return 1; +} + +static gint +lua_text_str (lua_State *L) +{ + struct rspamd_lua_text *t = lua_check_text (L, 1); + + if (t != NULL) { + lua_pushlstring (L, t->start, t->len); + } + else { + lua_pushnil (L); + } + + return 1; +} + /* Init part */ static gint @@ -2597,6 +2654,13 @@ luaopen_url (lua_State * L) lua_pop (L, 1); /* remove metatable from stack */ } +void +luaopen_text (lua_State *L) +{ + rspamd_lua_new_class (L, "rspamd{text}", textlib_m); + lua_pop (L, 1); +} + void rspamd_lua_task_push (lua_State *L, struct rspamd_task *task) { @@ -2606,3 +2670,4 @@ rspamd_lua_task_push (lua_State *L, struct rspamd_task *task) rspamd_lua_setclass (L, "rspamd{task}", -1); *ptask = task; } +