]> source.dussan.org Git - rspamd.git/commitdiff
Allow some custom output for rspamc.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 20 Jan 2014 20:38:00 +0000 (20:38 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 20 Jan 2014 20:38:00 +0000 (20:38 +0000)
src/client/rspamc.c
src/client/rspamdclient.c
src/client/rspamdclient.h

index 83a4c378f274197a3605a78a287ea30ea737fccf..5fd98abfed2b12f1232a47b22e5ebf308e8d9f9c 100644 (file)
@@ -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 }
 };
 
@@ -480,15 +486,35 @@ 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);
index c00a30f1c8107eb178b326724142af53381c03f4..e8f7dfe87789c0f13584bfedc2c5e804b3d74c1b 100644 (file)
@@ -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);
        }
 
index 91f42911bdc14c2f187d011cf2cc81a8092955f9..85b57c376ed2b497616ade39e2df38abe4970c8d 100644 (file)
@@ -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,