summaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2014-04-30 14:58:42 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2014-04-30 14:58:42 +0100
commit6c2c1d2be538108d2e3da9af207b0edf57f3a250 (patch)
tree13a0785b838afb82910acae35538ce5d200e66bd /src/libserver
parent2445d1680b3d3a2b5c80deb285e857a0337e44d5 (diff)
downloadrspamd-6c2c1d2be538108d2e3da9af207b0edf57f3a250.tar.gz
rspamd-6c2c1d2be538108d2e3da9af207b0edf57f3a250.zip
Unify webui send functions.
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/worker_util.c51
-rw-r--r--src/libserver/worker_util.h26
2 files changed, 77 insertions, 0 deletions
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_ */