]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Fix init parts
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 11 Jul 2019 15:10:07 +0000 (16:10 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 12 Jul 2019 14:18:17 +0000 (15:18 +0100)
src/libmime/message.c
src/libmime/message.h
src/libmime/mime_headers.c
src/libmime/mime_headers.h
src/libserver/task.c

index 500590e0204fafa226ef22c065275bee69545bc1..1ecd38629efbe8468ab577549177bb50db1957f1 100644 (file)
@@ -36,6 +36,7 @@
 #include <unicode/uchar.h>
 #include "libserver/cfg_file_private.h"
 #include "lua/lua_common.h"
+#include "contrib/uthash/utlist.h"
 
 #define GTUBE_SYMBOL "GTUBE"
 
@@ -1061,12 +1062,9 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start,
        part->raw_data.len = len;
        part->parsed_data.begin = start;
        part->parsed_data.len = len;
-       part->id = task->parts->len;
-       part->raw_headers =  g_hash_table_new_full (rspamd_strcase_hash,
-                       rspamd_strcase_equal, NULL, rspamd_ptr_array_free_hard);
-       part->headers_order = g_queue_new ();
-
-
+       part->id = MESSAGE_FIELD (task, parts)->len;
+       part->raw_headers = rspamd_message_headers_new ();
+       part->headers_order = NULL;
 
        tok = rspamd_task_get_request_header (task, "Filename");
 
@@ -1080,21 +1078,57 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start,
        part->cd = rspamd_content_disposition_parse (cdbuf, strlen (cdbuf),
                        task->task_pool);
 
-       g_ptr_array_add (task->parts, part);
+       g_ptr_array_add (MESSAGE_FIELD (task, parts), part);
        rspamd_mime_parser_calc_digest (part);
 
        /* Generate message ID */
        mid = rspamd_mime_message_id_generate ("localhost.localdomain");
        rspamd_mempool_add_destructor (task->task_pool,
                        (rspamd_mempool_destruct_t) g_free, mid);
-       task->message_id = mid;
+       MESSAGE_FIELD (task, message_id) = mid;
        task->queue_id = mid;
 }
 
 static void
 rspamd_message_dtor (struct rspamd_message *msg)
 {
+       guint i;
+       struct rspamd_mime_part *p;
+       struct rspamd_mime_text_part *tp;
+
 
+       PTR_ARRAY_FOREACH (msg->parts, i, p) {
+               if (p->raw_headers) {
+                       rspamd_message_headers_destroy (p->raw_headers);
+               }
+
+               if (IS_CT_MULTIPART (p->ct)) {
+                       if (p->specific.mp->children) {
+                               g_ptr_array_free (p->specific.mp->children, TRUE);
+                       }
+               }
+       }
+
+       PTR_ARRAY_FOREACH (msg->text_parts, i, tp) {
+               if (tp->utf_words) {
+                       g_array_free (tp->utf_words, TRUE);
+               }
+               if (tp->normalized_hashes) {
+                       g_array_free (tp->normalized_hashes, TRUE);
+               }
+               if (tp->languages) {
+                       g_ptr_array_unref (tp->languages);
+               }
+       }
+
+       g_ptr_array_unref (msg->text_parts);
+       g_ptr_array_unref (msg->parts);
+
+       g_ptr_array_unref (msg->from_mime);
+       g_ptr_array_unref (msg->rcpt_mime);
+
+       g_hash_table_unref (msg->urls);
+       g_hash_table_unref (msg->emails);
 }
 
 struct rspamd_message*
@@ -1102,9 +1136,15 @@ rspamd_message_new (struct rspamd_task *task)
 {
        struct rspamd_message *msg;
 
-       msg = rspamd_mempool_alloc0 (sizeof (*msg));
+       msg = rspamd_mempool_alloc0 (task->task_pool, sizeof (*msg));
 
+       msg->raw_headers = rspamd_message_headers_new ();
+       msg->emails = g_hash_table_new (rspamd_email_hash, rspamd_emails_cmp);
+       msg->urls = g_hash_table_new (rspamd_url_hash, rspamd_urls_cmp);
+       msg->parts = g_ptr_array_sized_new (4);
+       msg->text_parts = g_ptr_array_sized_new (2);
 
+       return msg;
 }
 
 gboolean
@@ -1163,6 +1203,7 @@ rspamd_message_parse (struct rspamd_task *task)
        task->msg.begin = p;
        task->msg.len = len;
        rspamd_cryptobox_hash_init (&st, NULL, 0);
+       task->message = rspamd_message_new (task);s
 
        if (task->flags & RSPAMD_TASK_FLAG_MIME) {
                enum rspamd_mime_parse_error ret;
@@ -1206,8 +1247,8 @@ rspamd_message_parse (struct rspamd_task *task)
        }
 
 
-       if (task->message_id == NULL) {
-               task->message_id = "undef";
+       if (MESSAGE_FIELD (task, message_id) == NULL) {
+               MESSAGE_FIELD (task, message_id) = "undef";
        }
 
        debug_task ("found %ud parts in message", task->parts->len);
@@ -1215,11 +1256,11 @@ rspamd_message_parse (struct rspamd_task *task)
                task->queue_id = "undef";
        }
 
-       if (task->received->len > 0) {
+       if (MESSAGE_FIELD (task, received)) {
                gboolean need_recv_correction = FALSE;
                rspamd_inet_addr_t *raddr;
 
-               recv = g_ptr_array_index (task->received, 0);
+               recv = MESSAGE_FIELD (task, received);
                /*
                 * For the first header we must ensure that
                 * received is consistent with the IP that we obtain through
@@ -1273,23 +1314,7 @@ rspamd_message_parse (struct rspamd_task *task)
                                trecv->from_hostname = trecv->real_hostname;
                        }
 
-#ifdef GLIB_VERSION_2_40
-                       g_ptr_array_insert (task->received, 0, trecv);
-#else
-                       /*
-                        * Unfortunately, before glib 2.40 we cannot insert element into a
-                        * ptr array
-                        */
-                       GPtrArray *nar = g_ptr_array_sized_new (task->received->len + 1);
-
-                       g_ptr_array_add (nar, trecv);
-                       PTR_ARRAY_FOREACH (task->received, i, recv) {
-                               g_ptr_array_add (nar, recv);
-                       }
-                       rspamd_mempool_add_destructor (task->task_pool,
-                                               rspamd_ptr_array_free_hard, nar);
-                       task->received = nar;
-#endif
+                       DL_PREPEND (MESSAGE_FIELD (task, received), trecv);
                }
        }
 
index 9cf67ba82f7265af5a5e33a8e6101c49108aae04..25d80bee925d3d0c17dee5a8e88e590c5ace8ede 100644 (file)
@@ -146,8 +146,8 @@ struct rspamd_message {
        struct rspamd_received_header *received;        /**< list of received headers                                           */
        GHashTable *urls;                                                       /**< list of parsed urls                                                        */
        GHashTable *emails;                                                     /**< list of parsed emails                                                      */
-       khash_t(rspamd_mime_headers_htb) *raw_headers;  /**< list of raw headers                                                        */
-       struct rspamd_mime_header *headers_order;       /**< inversed order of raw headers                                                      */
+       khash_t(rspamd_mime_headers_htb) *raw_headers;  /**< list of raw headers                                                */
+       struct rspamd_mime_header *headers_order;       /**< order of raw headers                                                       */
        GPtrArray *rcpt_mime;
        GPtrArray *from_mime;
        enum rspamd_newlines_type nlines_type;          /**< type of newlines (detected on most of headers      */
index 7ffa5453c7c9f5231c8a14983c28a106d2f73e52..bc8ace679e24b0974c6c539ca8881c73191fcdd6 100644 (file)
@@ -390,7 +390,7 @@ rspamd_mime_headers_process (struct rspamd_task *task,
                        /* We also validate utf8 and replace all non-valid utf8 chars */
                        rspamd_mime_charset_utf_enforce (nh->decoded, strlen (nh->decoded));
                        nh->order = norder ++;
-                       rspamd_mime_header_add (task, target, order, nh, check_newlines);
+                       rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
                        nh = NULL;
                        state = 0;
                        break;
@@ -400,7 +400,7 @@ rspamd_mime_headers_process (struct rspamd_task *task,
                        nh->decoded = "";
                        nh->raw_len = p - nh->raw_value;
                        nh->order = norder ++;
-                       rspamd_mime_header_add (task, target, order, nh, check_newlines);
+                       rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
                        nh = NULL;
                        state = 0;
                        break;
@@ -463,10 +463,12 @@ rspamd_mime_headers_process (struct rspamd_task *task,
                }
        }
 
+       /* Since we have prepended headers, we need to reverse the list to get the actual order */
+       LL_REVERSE (*order_ptr);
+
        if (check_newlines) {
                guint max_cnt = 0;
                gint sel = 0;
-               GList *cur;
                rspamd_cryptobox_hash_state_t hs;
                guchar hout[rspamd_cryptobox_HASHBYTES], *hexout;
 
@@ -479,17 +481,12 @@ rspamd_mime_headers_process (struct rspamd_task *task,
 
                MESSAGE_FIELD (task, nlines_type) = sel;
 
-               cur = order->head;
                rspamd_cryptobox_hash_init (&hs, NULL, 0);
 
-               while (cur) {
-                       nh = cur->data;
-
+               LL_FOREACH (*order_ptr, nh) {
                        if (nh->name && nh->flags != RSPAMD_HEADER_RECEIVED) {
                                rspamd_cryptobox_hash_update (&hs, nh->name, strlen (nh->name));
                        }
-
-                       cur = g_list_next (cur);
                }
 
                rspamd_cryptobox_hash_final (&hs, hout);
@@ -1620,4 +1617,16 @@ rspamd_message_get_header_array (struct rspamd_task *task,
 {
        return rspamd_message_get_header_from_hash (MESSAGE_FIELD (task, raw_headers),
                        field);
+}
+
+void
+rspamd_message_headers_destroy (khash_t(rspamd_mime_headers_htb) *htb)
+{
+       kh_destroy (rspamd_mime_headers_htb, htb);
+}
+
+khash_t(rspamd_mime_headers_htb) *
+rspamd_message_headers_new (void)
+{
+       return kh_init (rspamd_mime_headers_htb);
 }
\ No newline at end of file
index 7c17b8dddade3591e519a7a998571726622374dd..81948d11b6686f1fa3239da0b5702ca99aebe60a 100644 (file)
@@ -156,6 +156,18 @@ struct rspamd_mime_header *
 rspamd_message_get_header_from_hash (khash_t(rspamd_mime_headers_htb) *htb,
                                                                         const gchar *field);
 
+/**
+ * Cleans up hash table of the headers
+ * @param htb
+ */
+void rspamd_message_headers_destroy (khash_t(rspamd_mime_headers_htb) *htb);
+
+/**
+ * Init headers hash
+ * @return
+ */
+khash_t(rspamd_mime_headers_htb)* rspamd_message_headers_new (void);
+
 #ifdef  __cplusplus
 }
 #endif
index 3eeefb7f9732f891dcbbd9138afd9c58fa483aea..aca45a73784857f69fed57b71c1ee4590286d9d2 100644 (file)
@@ -199,8 +199,6 @@ rspamd_task_restore (void *arg)
 void
 rspamd_task_free (struct rspamd_task *task)
 {
-       struct rspamd_mime_part *p;
-       struct rspamd_mime_text_part *tp;
        struct rspamd_email_address *addr;
        struct rspamd_lua_cached_entry *entry;
        static guint free_iters = 0;
@@ -211,38 +209,6 @@ rspamd_task_free (struct rspamd_task *task)
        if (task) {
                debug_task ("free pointer %p", task);
 
-               for (i = 0; i < task->parts->len; i ++) {
-                       p = g_ptr_array_index (task->parts, i);
-
-                       if (p->raw_headers) {
-                               g_hash_table_unref (p->raw_headers);
-                       }
-
-                       if (p->headers_order) {
-                               g_queue_free (p->headers_order);
-                       }
-
-                       if (IS_CT_MULTIPART (p->ct)) {
-                               if (p->specific.mp->children) {
-                                       g_ptr_array_free (p->specific.mp->children, TRUE);
-                               }
-                       }
-               }
-
-               for (i = 0; i < task->text_parts->len; i ++) {
-                       tp = g_ptr_array_index (task->text_parts, i);
-
-                       if (tp->utf_words) {
-                               g_array_free (tp->utf_words, TRUE);
-                       }
-                       if (tp->normalized_hashes) {
-                               g_array_free (tp->normalized_hashes, TRUE);
-                       }
-                       if (tp->languages) {
-                               g_ptr_array_unref (tp->languages);
-                       }
-               }
-
                if (task->rcpt_envelope) {
                        for (i = 0; i < task->rcpt_envelope->len; i ++) {
                                addr = g_ptr_array_index (task->rcpt_envelope, i);