]> source.dussan.org Git - rspamd.git/commitdiff
Fix learning throught restfull interface.
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 18 Sep 2012 16:42:09 +0000 (20:42 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 18 Sep 2012 16:42:09 +0000 (20:42 +0400)
lib/client/librspamdclient.c
src/client/rspamc.c
src/controller.c

index b4c6caed515d8c798820d97b203ff0e0d6bae3fa..7a073f4104cb7f2d25fe84c389c7f3c76558dae9 100644 (file)
@@ -1523,7 +1523,9 @@ rspamd_free_result (struct rspamd_result *result)
 
        g_hash_table_destroy (result->headers);
        g_hash_table_destroy (result->metrics);
-       rspamd_free_connection (result->conn);
+       if (result->conn) {
+               rspamd_free_connection (result->conn);
+       }
 }
 
 void
@@ -1536,7 +1538,9 @@ rspamd_free_controller_result (struct rspamd_controller_result *result)
        if (result->data) {
                g_string_free (result->data, TRUE);
        }
-       rspamd_free_connection (result->conn);
+       if (result->conn) {
+               rspamd_free_connection (result->conn);
+       }
 }
 
 /*
index 1c7eaf31819ff47a9339398c11030c1a6401128d..38855e7c455e7bf4231a540ff66cdb41a9572226 100644 (file)
@@ -430,7 +430,7 @@ learn_rspamd_stdin (gboolean is_spam)
 
        results = rspamd_controller_command_memory (client, is_spam ? "learn_spam" : "learn_ham", password, params, in_buf, r, &err);
        g_hash_table_destroy (params);
-       if (results == NULL) {
+       if (results == NULL || err != NULL) {
                if (err != NULL) {
                        fprintf (stderr, "cannot learn message: %s\n", err->message);
                }
@@ -475,7 +475,7 @@ learn_rspamd_file (gboolean is_spam, const gchar *file)
 
        results = rspamd_controller_command_file (client, is_spam ? "learn_spam" : "learn_ham", password, params, file, &err);
        g_hash_table_destroy (params);
-       if (results == NULL) {
+       if (results == NULL || err != NULL) {
                if (err != NULL) {
                        fprintf (stderr, "cannot learn message: %s\n", err->message);
                }
@@ -565,7 +565,7 @@ rspamd_do_controller_simple_command (gchar *command)
        add_rspamd_server (TRUE);
 
        results = rspamd_controller_command_simple (client, command, password, NULL, &err);
-       if (results == NULL) {
+       if (results == NULL || err != NULL) {
                if (err != NULL) {
                        fprintf (stderr, "cannot perform command: %s\n", err->message);
                }
index a8945f8d396fa3588d67d8bbb91d95abd79b8c3d..5e39a7afd3e109e7daf74257c2dc03a120910bbf 100644 (file)
@@ -225,7 +225,7 @@ restful_write_reply (gint error_code, const gchar *err_message, const gchar *buf
                return rspamd_dispatcher_write (d, buf, buflen, FALSE, FALSE);
        }
        else {
-               if (!rspamd_dispatcher_write (d, hbuf, r, TRUE, TRUE)) {
+               if (!rspamd_dispatcher_write (d, hbuf, r, FALSE, TRUE)) {
                        return FALSE;
                }
        }
@@ -998,6 +998,7 @@ process_header (f_str_t *line, struct controller_session *session)
        headern = separate_command (line, ':');
 
        if (line == NULL || headern == NULL) {
+               msg_warn ("bad header: %V", line);
                return FALSE;
        }
        /* Eat whitespaces */
@@ -1142,7 +1143,7 @@ controller_read_socket (f_str_t * in, void *arg)
                if (in->len == 0) {
                        /* End of headers */
                        if (session->cmd == NULL && session->custom_handler == NULL) {
-                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "500 Bad command" CRLF);
+                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Bad command" CRLF CRLF);
                                if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
                                        return FALSE;
                                }
@@ -1152,6 +1153,7 @@ controller_read_socket (f_str_t * in, void *arg)
                        /* Perform command */
                        else if (session->cmd != NULL) {
                                if (! process_command (session->cmd, NULL, session)) {
+                                       msg_debug ("process command failed");
                                        destroy_session (session->s);
                                        return FALSE;
                                }
@@ -1161,13 +1163,13 @@ controller_read_socket (f_str_t * in, void *arg)
                        }
                        if (session->state != STATE_LEARN && session->state != STATE_LEARN_SPAM_PRE
                                        && session->state != STATE_WEIGHTS && session->state != STATE_OTHER) {
+                               msg_debug ("closing restful connection");
                                destroy_session (session->s);
                                return FALSE;
                        }
                }
-               if (!process_header (in, session)) {
-                       msg_debug ("'%V'", in);
-                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "500 Bad header" CRLF);
+               else if (!process_header (in, session)) {
+                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Bad header" CRLF CRLF);
                        if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
                                return FALSE;
                        }
@@ -1189,7 +1191,12 @@ controller_read_socket (f_str_t * in, void *arg)
                        msg_warn ("processing of message failed");
                        free_task (task, FALSE);
                        session->state = STATE_REPLY;
-                       r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       if (session->restful) {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Cannot process message" CRLF CRLF);
+                       }
+                       else {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       }
                        if (! rspamd_dispatcher_write (session->dispatcher, out_buf, r, FALSE, FALSE)) {
                                return FALSE;
                        }
@@ -1197,14 +1204,25 @@ controller_read_socket (f_str_t * in, void *arg)
                }
 
                if (!learn_task (session->learn_symbol, task, &err)) {
+                       free_task (task, FALSE);
                        if (err) {
-                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, err->message);
+                               if (session->restful) {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Learn classifier error: %s" CRLF CRLF, err->message);
+                               }
+                               else {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, err->message);
+                               }
                                g_error_free (err);
                        }
                        else {
-                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, unknown learn classifier error" CRLF END);
+                               if (session->restful) {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Learn classifier error: unknown" CRLF CRLF);
+                               }
+                               else {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, unknown learn classifier error" CRLF END);
+                               }
                        }
-                       free_task (task, FALSE);
+
                        if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
                                return FALSE;
                        }
@@ -1213,7 +1231,12 @@ controller_read_socket (f_str_t * in, void *arg)
                }
 
                free_task (task, FALSE);
-               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn ok" CRLF END);
+               if (session->restful) {
+                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 200 Learn OK" CRLF CRLF);
+               }
+               else {
+                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn ok" CRLF END);
+               }
                session->state = STATE_REPLY;
                if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
                        return FALSE;
@@ -1234,7 +1257,12 @@ controller_read_socket (f_str_t * in, void *arg)
                if (r == -1) {
                        msg_warn ("processing of message failed");
                        session->state = STATE_REPLY;
-                       r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       if (session->restful) {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Cannot process message" CRLF CRLF);
+                       }
+                       else {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       }
                        if (!session->restful) {
                                if (! rspamd_dispatcher_write (session->dispatcher, out_buf, r, FALSE, FALSE)) {
                                        return FALSE;
@@ -1252,7 +1280,12 @@ controller_read_socket (f_str_t * in, void *arg)
                r = process_filters (task);
                if (r == -1) {
                        session->state = STATE_REPLY;
-                       r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       if (session->restful) {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Cannot process message" CRLF CRLF);
+                       }
+                       else {
+                               r = rspamd_snprintf (out_buf, sizeof (out_buf), "cannot process message" CRLF);
+                       }
                        destroy_session (task->s);
                        if (!session->restful) {
                                if (! rspamd_dispatcher_write (session->dispatcher, out_buf, r, FALSE, FALSE)) {
@@ -1408,34 +1441,51 @@ controller_write_socket (void *arg)
        else if (session->state == STATE_LEARN_SPAM) {
                /* Perform actual learn here */
                if (session->learn_classifier == NULL) {
-                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, "unknown classifier");
+                       if (session->restful) {
+                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Learn classifier error: %s" CRLF CRLF, "unknown classifier");
+                       }
+                       else {
+                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, "unknown classifier");
+                       }
                }
                else {
                        if (! learn_task_spam (session->learn_classifier, session->learn_task, session->in_class, &err)) {
                                if (err) {
-                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, err->message);
+                                       if (session->restful) {
+                                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Learn classifier error: %s" CRLF CRLF, err->message);
+                                       }
+                                       else {
+                                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: %s" CRLF END, err->message);
+                                       }
                                        g_error_free (err);
                                }
                                else {
-                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, unknown learn classifier error" CRLF END);
+                                       if (session->restful) {
+                                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 500 Learn classifier error: unknown" CRLF CRLF);
+                                       }
+                                       else {
+                                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn failed, learn classifier error: unknown" CRLF END);
+                                       }
                                }
                        }
                        else {
-                               i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn ok" CRLF END);
+                               if (session->restful) {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "HTTP/1.0 200 Learn OK" CRLF CRLF);
+                               }
+                               else {
+                                       i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn ok" CRLF END);
+                               }
                        }
                }
                session->learn_task->dispatcher = NULL;
                destroy_session (session->learn_task->s);
                session->state = STATE_REPLY;
-               if (!session->restful) {
-                       if (! rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
-                               return FALSE;
-                       }
+               if (! rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
+                       return FALSE;
                }
-               else {
-                       if (! restful_write_reply (500, out_buf, NULL, 0, session->dispatcher)) {
-                               return FALSE;
-                       }
+               if (session->restful) {
+                       destroy_session (session->s);
+                       return FALSE;
                }
                return TRUE;
        }