aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_tcp.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-21 18:26:20 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-21 18:26:20 +0100
commitd24ef5624218f6e967a91ef46e5e77fb1f287a87 (patch)
treee917aa948f7106ce4d4c175a6b063e8f90bfdcc0 /src/lua/lua_tcp.c
parent29572e62d552ba094688ac59eb488bbb4536b15f (diff)
downloadrspamd-d24ef5624218f6e967a91ef46e5e77fb1f287a87.tar.gz
rspamd-d24ef5624218f6e967a91ef46e5e77fb1f287a87.zip
Allow to specify custom stop pattern for lua_tcp.
Diffstat (limited to 'src/lua/lua_tcp.c')
-rw-r--r--src/lua/lua_tcp.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c
index 67110c3ed..4cff7d0cc 100644
--- a/src/lua/lua_tcp.c
+++ b/src/lua/lua_tcp.c
@@ -72,6 +72,7 @@ struct lua_tcp_cbdata {
rspamd_mempool_t *pool;
struct iovec *iov;
GString *in;
+ gchar *stop_pattern;
struct event ev;
gint fd;
gint cbref;
@@ -241,6 +242,7 @@ lua_tcp_handler (int fd, short what, gpointer ud)
struct lua_tcp_cbdata *cbd = ud;
gchar inbuf[BUFSIZ];
gssize r;
+ guint slen;
if (what == EV_READ) {
g_assert (cbd->partial || cbd->in != NULL);
@@ -274,6 +276,18 @@ lua_tcp_handler (int fd, short what, gpointer ud)
}
else {
g_string_append_len (cbd->in, inbuf, r);
+
+ if (cbd->stop_pattern) {
+ slen = strlen (cbd->stop_pattern);
+
+ if (cbd->in->len >= slen) {
+ if (memcmp (cbd->stop_pattern, cbd->in->str +
+ (cbd->in->len - slen), slen) == 0) {
+ lua_tcp_push_data (cbd, cbd->in->str, cbd->in->len);
+ lua_tcp_maybe_free (cbd);
+ }
+ }
+ }
}
}
}
@@ -385,12 +399,14 @@ lua_tcp_arg_toiovec (lua_State *L, gint pos, rspamd_mempool_t *pool,
* - `callback`: continuation function (required)
* - `timeout`: floating point value that specifies timeout for IO operations in seconds
* - `partial`: boolean flag that specifies that callback should be called on any data portion received
+ * - `stop_pattern`: stop reading on finding a certain pattern (e.g. \r\n.\r\n for smtp)
* @return {boolean} true if request has been sent
*/
static gint
lua_tcp_request (lua_State *L)
{
const gchar *host;
+ gchar *stop_pattern = NULL;
guint port;
gint cbref, tp;
struct event_base *ev_base;
@@ -485,6 +501,13 @@ lua_tcp_request (lua_State *L)
}
lua_pop (L, 1);
+ lua_pushstring (L, "stop_pattern");
+ lua_gettable (L, -2);
+ if (lua_type (L, -1) == LUA_TSTRING) {
+ stop_pattern = rspamd_mempool_strdup (pool, lua_tostring (L, -1));
+ }
+ lua_pop (L, 1);
+
lua_pushstring (L, "partial");
lua_gettable (L, -2);
if (lua_type (L, -1) == LUA_TBOOLEAN) {
@@ -566,6 +589,7 @@ lua_tcp_request (lua_State *L)
cbd->total = total_out;
cbd->pos = 0;
cbd->port = port;
+ cbd->stop_pattern = stop_pattern;
if (session) {
cbd->session = session;