]> source.dussan.org Git - rspamd.git/commitdiff
Fix buffers code.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2015 14:55:24 +0000 (15:55 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2015 14:55:24 +0000 (15:55 +0100)
src/libserver/buffer.c
src/libserver/buffer.h
src/lua/lua_buffer.c

index e4268863cdfe2ba2169f55752366ee6e3196bafa..0875de300396a7226f82d64002545c8dfcdbc439 100644 (file)
@@ -310,14 +310,27 @@ write_buffers (gint fd, rspamd_io_dispatcher_t * d, gboolean is_delayed)
        return TRUE;
 }
 
+static struct rspamd_buffer_buf *
+allocate_buffer (rspamd_mempool_t *pool, gsize size)
+{
+       struct rspamd_buffer_buf *b;
+
+       b = rspamd_mempool_alloc_tmp (pool, sizeof (*b));
+       b->begin = rspamd_mempool_alloc_tmp (pool, size);
+       b->size = size;
+       b->len = 0;
+
+       return b;
+}
+
 static void
 read_buffers (gint fd, rspamd_io_dispatcher_t * d, gboolean skip_read)
 {
        ssize_t r;
        GError *err = NULL;
-       rspamd_fstring_t res;
-       gchar *c, *b;
-       gchar *end;
+       rspamd_ftok_t res;
+       guchar *c, *b;
+       guchar *end;
        size_t len;
        enum io_policy saved_policy;
 
@@ -330,10 +343,10 @@ read_buffers (gint fd, rspamd_io_dispatcher_t * d, gboolean skip_read)
                d->in_buf =
                        rspamd_mempool_alloc_tmp (d->pool, sizeof (rspamd_buffer_t));
                if (d->policy == BUFFER_LINE || d->policy == BUFFER_ANY) {
-                       d->in_buf->data = rspamd_fstralloc_tmp (d->pool, d->default_buf_size);
+                       d->in_buf->data = allocate_buffer (d->pool, d->default_buf_size);
                }
                else {
-                       d->in_buf->data = rspamd_fstralloc_tmp (d->pool, d->nchars + 1);
+                       d->in_buf->data = allocate_buffer (d->pool, d->nchars + 1);
                }
                d->in_buf->pos = d->in_buf->data->begin;
        }
@@ -657,7 +670,7 @@ rspamd_set_dispatcher_policy (rspamd_io_dispatcher_t * d,
        enum io_policy policy,
        size_t nchars)
 {
-       rspamd_fstring_t *tmp;
+       struct rspamd_buffer_buf *tmp;
        gint t;
 
        if (d->policy != policy || nchars != d->nchars) {
@@ -666,7 +679,7 @@ rspamd_set_dispatcher_policy (rspamd_io_dispatcher_t * d,
                /* Resize input buffer if needed */
                if (policy == BUFFER_CHARACTER && nchars != 0) {
                        if (d->in_buf && d->in_buf->data->size < nchars) {
-                               tmp = rspamd_fstralloc_tmp (d->pool, d->nchars + 1);
+                               tmp = allocate_buffer (d->pool, d->nchars + 1);
                                memcpy (tmp->begin, d->in_buf->data->begin,
                                        d->in_buf->data->len);
                                t = d->in_buf->pos - d->in_buf->data->begin;
@@ -677,7 +690,7 @@ rspamd_set_dispatcher_policy (rspamd_io_dispatcher_t * d,
                }
                else if (policy == BUFFER_LINE || policy == BUFFER_ANY) {
                        if (d->in_buf && d->nchars < d->default_buf_size) {
-                               tmp = rspamd_fstralloc_tmp (d->pool, d->default_buf_size);
+                               tmp = allocate_buffer (d->pool, d->default_buf_size);
                                memcpy (tmp->begin, d->in_buf->data->begin,
                                        d->in_buf->data->len);
                                t = d->in_buf->pos - d->in_buf->data->begin;
@@ -731,33 +744,6 @@ rspamd_dispatcher_write (rspamd_io_dispatcher_t * d,
        return TRUE;
 }
 
-gboolean
-rspamd_dispatcher_write_string (rspamd_io_dispatcher_t *d,
-       GString *str,
-       gboolean delayed,
-       gboolean free_on_write)
-{
-       struct rspamd_out_buffer_s *newbuf;
-
-       newbuf = g_slice_alloc (sizeof (struct rspamd_out_buffer_s));
-       newbuf->data = str;
-       newbuf->allocated = free_on_write;
-
-       APPEND_OUT_BUFFER (d, newbuf);
-
-       if (!delayed) {
-               debug_ip ("plan write event");
-               return write_buffers (d->fd, d, FALSE);
-       }
-       /* Otherwise plan write event */
-       event_del (d->ev);
-       event_set (d->ev, d->fd, EV_WRITE, dispatcher_cb, (void *)d);
-       event_base_set (d->ev_base, d->ev);
-       event_add (d->ev, d->tv);
-
-       return TRUE;
-}
-
 gboolean
 rspamd_dispatcher_sendfile (rspamd_io_dispatcher_t *d, gint fd, size_t len)
 {
@@ -824,7 +810,3 @@ rspamd_dispacther_cleanup (rspamd_io_dispatcher_t *d)
 }
 
 #undef debug_ip
-
-/*
- * vi:ts=4
- */
index 7fa799573b66b113dd5da0ccc0847dccd553b2b4..26c60548498451ff861a16d63739f466b14143bc 100644 (file)
@@ -10,7 +10,7 @@
 #include "mem_pool.h"
 #include "fstring.h"
 
-typedef gboolean (*dispatcher_read_callback_t)(rspamd_fstring_t *in, void *user_data);
+typedef gboolean (*dispatcher_read_callback_t)(rspamd_ftok_t *in, void *user_data);
 typedef gboolean (*dispatcher_write_callback_t)(void *user_data);
 typedef void (*dispatcher_err_callback_t)(GError *err, void *user_data);
 
@@ -26,9 +26,15 @@ enum io_policy {
 /**
  * Buffer structure
  */
+struct rspamd_buffer_buf {
+       gsize size;
+       gsize len;
+       guchar *begin;
+};
+
 typedef struct rspamd_buffer_s {
-       rspamd_fstring_t *data;                                                  /**< buffer logic                      */
-       gchar *pos;                                                     /**< current position           */
+       struct rspamd_buffer_buf *data;
+       guchar *pos;                                                     /**< current position          */
 } rspamd_buffer_t;
 
 struct rspamd_out_buffer_s {
@@ -111,19 +117,6 @@ gboolean rspamd_dispatcher_write (rspamd_io_dispatcher_t *d,
        size_t len, gboolean delayed,
        gboolean allocated) G_GNUC_WARN_UNUSED_RESULT;
 
-/**
- * Write a GString to dispatcher
- * @param d dipatcher object
- * @param str string to write
- * @param delayed delay write
- * @param free_on_write free string after writing to a socket
- * @return TRUE if write has been queued successfully
- */
-gboolean rspamd_dispatcher_write_string (rspamd_io_dispatcher_t *d,
-       GString *str,
-       gboolean delayed,
-       gboolean free_on_write) G_GNUC_WARN_UNUSED_RESULT;
-
 /**
  * Send specified descriptor to dispatcher
  * @param d pointer to dispatcher's object
index eedd7f05b7740ec9416d6576925cc7315f58272f..021b13a0d4a632fc55f12b26eedd015911e8ebbf 100644 (file)
@@ -79,7 +79,7 @@ lua_check_event_base (lua_State *L)
 /* Dispatcher callbacks */
 
 static gboolean
-lua_io_read_cb (rspamd_fstring_t * in, void *arg)
+lua_io_read_cb (rspamd_ftok_t * in, void *arg)
 {
        struct lua_dispatcher_cbdata *cbdata = arg;
        gboolean res;
@@ -91,7 +91,7 @@ lua_io_read_cb (rspamd_fstring_t * in, void *arg)
                lua_newuserdata (cbdata->L, sizeof (struct rspamd_io_dispatcher_s *));
        rspamd_lua_setclass (cbdata->L, "rspamd{io_dispatcher}", -1);
        *pdispatcher = cbdata->d;
-       lua_pushlstring (cbdata->L, in->str, in->len);
+       lua_pushlstring (cbdata->L, in->begin, in->len);
 
        if (lua_pcall (cbdata->L, 2, 1, 0) != 0) {
                msg_info ("call to session finalizer failed: %s",