]> source.dussan.org Git - rspamd.git/commitdiff
* Introduce new system of contexts initialization. It would be used for other things...
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 18 Aug 2011 13:32:55 +0000 (17:32 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 18 Aug 2011 13:32:55 +0000 (17:32 +0400)
Ignore unknown headers in case of http.

src/cfg_file.h
src/cfg_xml.c
src/main.c
src/main.h
src/protocol.c

index f58c228c76ae61547aa30bb27f3ace54cb17e19e..fc1ba547aed51e4e18ec73a87b4245e005ff58ff 100644 (file)
@@ -236,6 +236,7 @@ struct worker_conf {
        GHashTable *params;                                                             /**< params for worker                                                                  */
        GQueue *active_workers;                                                 /**< linked list of spawned workers                                             */
        gboolean has_socket;                                                    /**< whether we should make listening socket in main process */
+       gpointer *ctx;                                                                  /**< worker's context                                                                   */
 };
 
 /**
index 4acc486732ca61e3ab040b26f789b82fd4ab51e1..8fad5f03aa1287635c0caff20c85a57649f0c7af 100644 (file)
@@ -826,13 +826,44 @@ options_handle_nameserver (struct config_file *cfg, struct rspamd_xml_userdata *
 }
 
 /* Worker section */
+
+struct wrk_cbdata {
+       struct worker_conf *wrk;
+       struct config_file *cfg;
+       struct rspamd_xml_userdata *ctx;
+};
+
+static void
+worker_foreach_callback (gpointer k, gpointer v, gpointer ud)
+{
+       struct wrk_cbdata              *cd = ud;
+       struct xml_config_param        *cparam;
+       GHashTable                     *worker_config;
+
+       if (cd->wrk->ctx == NULL) {
+               cd->wrk->ctx = init_workers_ctx (cd->wrk->type);
+       }
+
+       if (!worker_options || (worker_config = g_hash_table_lookup (worker_options, &cd->wrk->type)) == NULL ||
+                       (cparam = g_hash_table_lookup (worker_config, k)) == NULL) {
+               msg_warn ("unregistered worker attribute '%s' for worker %s", k, process_to_str (cd->wrk->type));
+       }
+       else {
+
+               if (cd->wrk->ctx != NULL) {
+                       cparam->handler (cd->cfg, cd->ctx, NULL, v, NULL, cd->wrk->ctx, cparam->offset);
+               }
+               else {
+                       msg_err ("Bad error detected: module %s has not initialized its context", process_to_str (cd->wrk->type));
+               }
+       }
+}
+
 gboolean 
 worker_handle_param (struct config_file *cfg, struct rspamd_xml_userdata *ctx, const gchar *tag, GHashTable *attrs, gchar *data, gpointer user_data, gpointer dest_struct, gint offset)
 {
        struct worker_conf             *wrk = ctx->section_pointer;
        const gchar                    *name;
-       struct xml_config_param        *cparam;
-       GHashTable                     *worker_config;
 
        if (g_ascii_strcasecmp (tag, "option") == 0 || g_ascii_strcasecmp (tag, "param") == 0)  {
                if (attrs == NULL || (name = g_hash_table_lookup (attrs, "name")) == NULL) {
@@ -844,18 +875,11 @@ worker_handle_param (struct config_file *cfg, struct rspamd_xml_userdata *ctx, c
                name = memory_pool_strdup (cfg->cfg_pool, tag);
        }
 
-       if (!worker_options ||
-                       (worker_config = g_hash_table_lookup (worker_options, &wrk->type)) == NULL ||
-                       (cparam = g_hash_table_lookup (worker_config, name)) == NULL) {
-               msg_warn ("unregistered worker attribute '%s' for worker %s", name, process_to_str (wrk->type));
-               g_hash_table_insert (wrk->params, (char *)name, memory_pool_strdup (cfg->cfg_pool, data));
-       }
-       else {
-               return cparam->handler (cfg, ctx, attrs, data, NULL, cparam->user_data, cparam->offset);
-       }
+       g_hash_table_insert (wrk->params, (char *)name, memory_pool_strdup (cfg->cfg_pool, data));
 
        return TRUE;
 }
+
 gboolean 
 worker_handle_type (struct config_file *cfg, struct rspamd_xml_userdata *ctx, GHashTable *attrs, gchar *data, gpointer user_data, gpointer dest_struct, gint offset)
 {
@@ -1764,6 +1788,7 @@ rspamd_xml_end_element (GMarkupParseContext       *context, const gchar *element_name,
        struct statfile            *st;
        gboolean                    res;
        gpointer                    tptr;
+       struct wrk_cbdata           wcd;
        
        if (g_ascii_strcasecmp (element_name, "if") == 0) {
                tptr = g_queue_pop_head (ud->if_stack);
@@ -1845,6 +1870,12 @@ rspamd_xml_end_element (GMarkupParseContext      *context, const gchar *element_name,
                case XML_READ_WORKER:
                        CHECK_TAG ("worker", FALSE);
                        if (res) {
+                               /* Parse params */
+                               wcd.wrk = ud->section_pointer;
+                               wcd.cfg = ud->cfg;
+                               wcd.ctx = ud;
+                               g_hash_table_foreach (wcd.wrk->params,
+                                               worker_foreach_callback, &wcd);
                                /* Insert object to list */
                                ud->cfg->workers = g_list_prepend (ud->cfg->workers, ud->section_pointer);
                        }
@@ -2163,17 +2194,6 @@ register_worker_opt (gint wtype, const gchar *optname, element_handler_func func
                param->name = optname;
                g_hash_table_insert (worker, (char *)optname, param);
        }
-       else {
-               /* Param already exists replace it */
-               msg_warn ("replace old handler for param '%s'", optname);
-               g_free (param);
-               param = g_malloc (sizeof (struct xml_config_param));
-               param->handler = func;
-               param->user_data = dest_struct;
-               param->offset = offset;
-               param->name = optname;
-               g_hash_table_insert (worker, (char *)optname, param);
-       }
 }
 
 /* Register new classifier option */
index 23c67912d275fbbf7c91e0b76444054fe32ffcef..c6055e2deafef16354c3705f236d0ae53a85a448 100644 (file)
@@ -331,7 +331,13 @@ fork_worker (struct rspamd_main *rspamd, struct worker_conf *cf)
                cur->type = cf->type;
                cur->pid = fork ();
                cur->cf = g_malloc (sizeof (struct worker_conf));
-               cur->ctx = rspamd->workers_ctx[cf->type];
+               /* Copy or init context */
+               if (cf->ctx) {
+                       cur->ctx = cf->ctx;
+               }
+               else {
+                       cur->ctx = init_workers_ctx (cf->type);
+               }
                memcpy (cur->cf, cf, sizeof (struct worker_conf));
                cur->pending = FALSE;
                switch (cur->pid) {
@@ -788,13 +794,21 @@ print_symbols_cache (struct config_file *cfg)
        }
 }
 
-static void
-init_workers_ctx (struct rspamd_main *main)
+gpointer
+init_workers_ctx (enum process_type type)
 {
-       main->workers_ctx[TYPE_WORKER] = init_worker ();
-       main->workers_ctx[TYPE_CONTROLLER] = init_controller ();
-       main->workers_ctx[TYPE_FUZZY] = init_fuzzy_storage ();
-       main->workers_ctx[TYPE_SMTP] = init_smtp_worker ();
+       switch (type) {
+       case TYPE_WORKER:
+               return init_worker ();
+       case TYPE_CONTROLLER:
+               return init_controller ();
+       case TYPE_FUZZY:
+               return init_fuzzy_storage ();
+       case TYPE_SMTP:
+               return init_smtp_worker ();
+       default:
+               return NULL;
+       }
 }
 
 gint
@@ -866,9 +880,6 @@ main (gint argc, gchar **argv, gchar **env)
        counters = rspamd_hash_new_shared (rspamd_main->server_pool, g_str_hash, g_str_equal, 64);
        /* Init listen sockets hash */
        listen_sockets = g_hash_table_new (g_direct_hash, g_direct_equal);
-       
-       /* Init contextes */
-       init_workers_ctx (rspamd_main);
 
        /* Init classifiers options */
        register_classifier_opt ("bayes", "min_tokens");
index 6ac24e7ada5bbc8f661780aa31859cf7d5ba886b..812877d15e972b930c7f6d23f80ab87243640025 100644 (file)
@@ -92,8 +92,6 @@ struct rspamd_main {
        guint ev_initialized;                                                                           /**< is event system is initialized                                     */
        struct rspamd_stat *stat;                                                                       /**< pointer to statistics                                                      */
 
-       gpointer workers_ctx[TYPE_MAX];                                                         /** Array of workers' contexts                                          */
-
        memory_pool_t *server_pool;                                                                     /**< server's memory pool                                                       */
        statfile_pool_t *statfile_pool;                                                         /**< shared statfiles pool                                                      */
        GHashTable *workers;                                        /**< workers pool indexed by pid                    */
@@ -279,6 +277,11 @@ void start_greylist_storage (struct rspamd_worker *worker);
  */
 void register_custom_controller_command (const gchar *name, controller_func_t handler, gboolean privilleged, gboolean require_message);
 
+/**
+ * Initialize context for worker of specified type
+ */
+gpointer init_workers_ctx (enum process_type type);
+
 /**
  * If set, reopen log file on next write
  */
index fe9621e21aa9037732d6d993cbc2aa5774e1a511..ebca96bc7b229d7ab6c282098a282a8883704e8f 100644 (file)
@@ -582,8 +582,10 @@ parse_header (struct worker_task *task, f_str_t * line)
                }
                break;
        default:
-               msg_info ("wrong header: %s", headern);
-               return FALSE;
+               if (!task->is_http) {
+                       msg_info ("wrong header: %s", headern);
+                       return FALSE;
+               }
        }
 
        return TRUE;