diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-01-20 20:38:00 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-01-20 20:38:00 +0000 |
commit | b692b8ebe5c5af408daf8a132d8ebd8b1ec1c7d1 (patch) | |
tree | 4e8fe3c377e8064b9f208d9e9f09d2d4329b6769 /src | |
parent | aa29c7e4cbc6c4c904e5bd265ceb3596a85a5d1e (diff) | |
download | rspamd-b692b8ebe5c5af408daf8a132d8ebd8b1ec1c7d1.tar.gz rspamd-b692b8ebe5c5af408daf8a132d8ebd8b1ec1c7d1.zip |
Allow some custom output for rspamc.
Diffstat (limited to 'src')
-rw-r--r-- | src/client/rspamc.c | 28 | ||||
-rw-r--r-- | src/client/rspamdclient.c | 8 | ||||
-rw-r--r-- | src/client/rspamdclient.h | 2 |
3 files changed, 33 insertions, 5 deletions
diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 83a4c378f..5fd98abfe 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -24,7 +24,9 @@ #include "config.h" #include "util.h" +#include "http.h" #include "rspamdclient.h" +#include "utlist.h" #define PRINT_FUNC printf @@ -49,6 +51,8 @@ static gboolean pass_all; static gboolean tty = FALSE; static gboolean verbose = FALSE; static gboolean print_commands = FALSE; +static gboolean json = FALSE; +static gboolean headers = FALSE; static GOptionEntry entries[] = { @@ -69,6 +73,8 @@ static GOptionEntry entries[] = { "timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout, "Time in seconds to wait for a reply", NULL }, { "bind", 'b', 0, G_OPTION_ARG_STRING, &local_addr, "Bind to specified ip address", NULL }, { "commands", 0, 0, G_OPTION_ARG_NONE, &print_commands, "List available commands", NULL }, + { "json", 'j', 0, G_OPTION_ARG_NONE, &json, "Output json reply", NULL }, + { "headers", 0, 0, G_OPTION_ARG_NONE, &headers, "Output HTTP headers", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; @@ -481,14 +487,34 @@ add_options (GHashTable *opts) } static void +rspamc_output_headers (struct rspamd_http_message *msg) +{ + struct rspamd_http_header *h; + + LL_FOREACH (msg->headers, h) { + rspamd_fprintf (stdout, "%v: %v\n", h->name, h->value); + } + rspamd_fprintf (stdout, "\n"); +} + +static void rspamc_client_cb (struct rspamd_client_connection *conn, + struct rspamd_http_message *msg, const gchar *name, ucl_object_t *result, gpointer ud, GError *err) { gchar *out; if (result != NULL) { - out = ucl_object_emit (result, UCL_EMIT_CONFIG); + if (headers && msg != NULL) { + rspamc_output_headers (msg); + } + if (json) { + out = ucl_object_emit (result, UCL_EMIT_JSON); + } + else { + out = ucl_object_emit (result, UCL_EMIT_CONFIG); + } printf ("%s", out); ucl_object_unref (result); free (out); diff --git a/src/client/rspamdclient.c b/src/client/rspamdclient.c index c00a30f1c..e8f7dfe87 100644 --- a/src/client/rspamdclient.c +++ b/src/client/rspamdclient.c @@ -70,7 +70,7 @@ rspamd_client_error_handler (struct rspamd_http_connection *conn, GError *err) struct rspamd_client_connection *c; c = req->conn; - req->cb (c, c->server_name->str, NULL, req->ud, err); + req->cb (c, NULL, c->server_name->str, NULL, req->ud, err); } static gint @@ -93,7 +93,7 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, else { if (msg->body == NULL || msg->body->len == 0 || msg->code != 200) { err = g_error_new (RCLIENT_ERROR, msg->code, "HTTP error occurred: %d", msg->code); - req->cb (c, c->server_name->str, NULL, req->ud, err); + req->cb (c, msg, c->server_name->str, NULL, req->ud, err); g_error_free (err); return -1; } @@ -103,12 +103,12 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, err = g_error_new (RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s", ucl_parser_get_error (parser)); ucl_parser_free (parser); - req->cb (c, c->server_name->str, NULL, req->ud, err); + req->cb (c, msg, c->server_name->str, NULL, req->ud, err); g_error_free (err); return -1; } - req->cb (c, c->server_name->str, ucl_parser_get_object (parser), req->ud, NULL); + req->cb (c, msg, c->server_name->str, ucl_parser_get_object (parser), req->ud, NULL); ucl_parser_free (parser); } diff --git a/src/client/rspamdclient.h b/src/client/rspamdclient.h index 91f42911b..85b57c376 100644 --- a/src/client/rspamdclient.h +++ b/src/client/rspamdclient.h @@ -28,6 +28,7 @@ #include "ucl.h" struct rspamd_client_connection; +struct rspamd_http_message; /** * Callback is called on client connection completed @@ -39,6 +40,7 @@ struct rspamd_client_connection; */ typedef void (*rspamd_client_callback) ( struct rspamd_client_connection *conn, + struct rspamd_http_message *msg, const gchar *name, ucl_object_t *result, gpointer ud, |