From 6c2c1d2be538108d2e3da9af207b0edf57f3a250 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 30 Apr 2014 14:58:42 +0100 Subject: Unify webui send functions. --- src/libserver/worker_util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/libserver/worker_util.h | 26 +++++++++++++++++++++++ 2 files changed, 77 insertions(+) (limited to 'src/libserver') diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index e8c97f20a..173415c4b 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -215,3 +215,54 @@ rspamd_worker_stop_accept (struct rspamd_worker *worker) g_list_free (worker->accept_events); } } + +void +rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, + gint code, + const gchar *error_msg) +{ + struct rspamd_http_message *msg; + + msg = rspamd_http_new_message (HTTP_RESPONSE); + msg->date = time (NULL); + msg->code = code; + msg->body = g_string_sized_new (128); + rspamd_printf_gstring (msg->body, "{\"error\":\"%s\"}", error_msg); + rspamd_http_connection_reset (entry->conn); + rspamd_http_connection_write_message (entry->conn, msg, NULL, + "application/json", entry, entry->conn->fd, entry->rt->ptv, entry->rt->ev_base); + entry->is_reply = TRUE; +} + +void +rspamd_controller_send_string (struct rspamd_http_connection_entry *entry, + const gchar *str) +{ + struct rspamd_http_message *msg; + + msg = rspamd_http_new_message (HTTP_RESPONSE); + msg->date = time (NULL); + msg->code = 200; + msg->body = g_string_new (str); + rspamd_http_connection_reset (entry->conn); + rspamd_http_connection_write_message (entry->conn, msg, NULL, + "application/json", entry, entry->conn->fd, entry->rt->ptv, entry->rt->ev_base); + entry->is_reply = TRUE; +} + +void +rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry, + ucl_object_t *obj) +{ + struct rspamd_http_message *msg; + + msg = rspamd_http_new_message (HTTP_RESPONSE); + msg->date = time (NULL); + msg->code = 200; + msg->body = g_string_sized_new (BUFSIZ); + rspamd_ucl_emit_gstring (obj, UCL_EMIT_JSON_COMPACT, msg->body); + rspamd_http_connection_reset (entry->conn); + rspamd_http_connection_write_message (entry->conn, msg, NULL, + "application/json", entry, entry->conn->fd, entry->rt->ptv, entry->rt->ev_base); + entry->is_reply = TRUE; +} diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index c8b7c1000..2372375ba 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -76,4 +76,30 @@ struct rspamd_custom_controller_command { rspamd_controller_func_t handler; }; +/** + * Send error using HTTP and JSON output + * @param entry router entry + * @param code error code + * @param error_msg error message + */ +void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, + gint code, + const gchar *error_msg); + +/** + * Send a custom string using HTTP + * @param entry router entry + * @param str string to send + */ +void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry, + const gchar *str); + +/** + * Send UCL using HTTP and JSON serialization + * @param entry router entry + * @param obj object to send + */ +void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry, + ucl_object_t *obj); + #endif /* WORKER_UTIL_H_ */ -- cgit v1.2.3