From df07d992fb389fa691e4206fc27593bb96af8630 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 18 Sep 2012 20:42:09 +0400 Subject: [PATCH] Fix learning throught restfull interface. --- lib/client/librspamdclient.c | 8 ++- src/client/rspamc.c | 6 +-- src/controller.c | 98 +++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 29 deletions(-) diff --git a/lib/client/librspamdclient.c b/lib/client/librspamdclient.c index b4c6caed5..7a073f410 100644 --- a/lib/client/librspamdclient.c +++ b/lib/client/librspamdclient.c @@ -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); + } } /* diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 1c7eaf318..38855e7c4 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -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); } diff --git a/src/controller.c b/src/controller.c index a8945f8d3..5e39a7afd 100644 --- a/src/controller.c +++ b/src/controller.c @@ -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; } -- 2.39.5