]> source.dussan.org Git - rspamd.git/commitdiff
Add rspamd{text} type to avoid unnecessary copying of large chunks of
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 7 Apr 2015 21:22:47 +0000 (22:22 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 7 Apr 2015 21:22:47 +0000 (22:22 +0100)
data.

src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_task.c

index 1b065c2e618a612aa3e29a97b755e5d63a5aaa5e..0364dff665a6ad62c0c4adb3139d097d4ab9e134 100644 (file)
@@ -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);
 
index f9d365ae0c259ded95bfe24dac7583e4d4938717..07ed77c145af07779511152c2e009a10e1349ed0 100644 (file)
@@ -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,
index ef05102c541afbf139c8c3991ec221b904955cde..13fe9042e394993f396c0a16103283b53df1f40b 100644 (file)
@@ -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;
 }
+