aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-04-07 22:22:47 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-04-07 22:22:47 +0100
commitcdd553cc27cc230662025663e353a9a7c8ce5e8a (patch)
treec0bce846181827baf04283b38a05787613f91f2e /src
parent0d097bd44950e3439d7200a1f2c81a8d8c9e9b39 (diff)
downloadrspamd-cdd553cc27cc230662025663e353a9a7c8ce5e8a.tar.gz
rspamd-cdd553cc27cc230662025663e353a9a7c8ce5e8a.zip
Add rspamd{text} type to avoid unnecessary copying of large chunks of
data.
Diffstat (limited to 'src')
-rw-r--r--src/lua/lua_common.c1
-rw-r--r--src/lua/lua_common.h1
-rw-r--r--src/lua/lua_task.c65
3 files changed, 67 insertions, 0 deletions
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
@@ -2598,6 +2655,13 @@ luaopen_url (lua_State * L)
}
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)
{
struct rspamd_task **ptask;
@@ -2606,3 +2670,4 @@ rspamd_lua_task_push (lua_State *L, struct rspamd_task *task)
rspamd_lua_setclass (L, "rspamd{task}", -1);
*ptask = task;
}
+