diff options
author | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2012-09-18 20:42:09 +0400 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2012-09-18 20:42:09 +0400 |
commit | df07d992fb389fa691e4206fc27593bb96af8630 (patch) | |
tree | a884bea7a40f8e4d06f17780342d2a57e406592f /src/controller.c | |
parent | d071dc92a2b0f853309b1c450d739dd4bb69f108 (diff) | |
download | rspamd-df07d992fb389fa691e4206fc27593bb96af8630.tar.gz rspamd-df07d992fb389fa691e4206fc27593bb96af8630.zip |
Fix learning throught restfull interface.
Diffstat (limited to 'src/controller.c')
-rw-r--r-- | src/controller.c | 98 |
1 files changed, 74 insertions, 24 deletions
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; } |