]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Add compression support to rspamd client
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 8 Sep 2016 17:42:42 +0000 (18:42 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 8 Sep 2016 17:42:42 +0000 (18:42 +0100)
src/client/rspamc.c
src/client/rspamdclient.c
src/client/rspamdclient.h

index 1d2dd39caeb063ea1b7efe86c05b09c4fdeea74e..92983b491d139436061f67389289264995a72af8 100644 (file)
@@ -57,6 +57,7 @@ static gboolean raw = FALSE;
 static gboolean extended_urls = FALSE;
 static gboolean mime_output = FALSE;
 static gboolean empty_input = FALSE;
+static gboolean compressed = FALSE;
 static gchar *key = NULL;
 static GList *children;
 
@@ -136,6 +137,8 @@ static GOptionEntry entries[] =
           "Allow empty input instead of reading from stdin", NULL },
        { "fuzzy-symbol", 'S', 0, G_OPTION_ARG_STRING, &fuzzy_symbol,
           "Learn the specified fuzzy symbol", NULL },
+       { "compressed", 'z', 0, G_OPTION_ARG_NONE, &compressed,
+          "Enable zstd compression", NULL },
        { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
 };
 
@@ -1383,7 +1386,7 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd,
 
                if (cmd->need_input) {
                        rspamd_client_command (conn, cmd->path, attrs, in, rspamc_client_cb,
-                               cbdata, &err);
+                               cbdata, compressed, &err);
                }
                else {
                        rspamd_client_command (conn,
@@ -1392,6 +1395,7 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd,
                                NULL,
                                rspamc_client_cb,
                                cbdata,
+                               compressed,
                                &err);
                }
        }
index 753f64c746f64290e86f999cc3b2209c0fda86c8..b71d6f335bf4546568581a89020d65f1fc1108e2 100644 (file)
@@ -18,6 +18,7 @@
 #include "libutil/http.h"
 #include "libutil/http_private.h"
 #include "unix-std.h"
+#include "contrib/zstd/zstd.h"
 
 #ifdef HAVE_FETCH_H
 #include <fetch.h>
@@ -197,9 +198,9 @@ rspamd_client_init (struct event_base *ev_base, const gchar *name,
 
 gboolean
 rspamd_client_command (struct rspamd_client_connection *conn,
-       const gchar *command, GQueue *attrs,
-       FILE *in, rspamd_client_callback cb,
-       gpointer ud, GError **err)
+               const gchar *command, GQueue *attrs,
+               FILE *in, rspamd_client_callback cb,
+               gpointer ud, gboolean compressed, GError **err)
 {
        struct rspamd_client_request *req;
        struct rspamd_http_client_header *nh;
@@ -246,7 +247,25 @@ rspamd_client_command (struct rspamd_client_connection *conn,
                        return FALSE;
                }
 
-               body = rspamd_fstring_new_init (input->str, input->len);
+               if (!compressed) {
+                       body = rspamd_fstring_new_init (input->str, input->len);
+               }
+               else {
+                       body = rspamd_fstring_sized_new (ZSTD_compressBound (input->len));
+                       body->len = ZSTD_compress (body->str, body->allocated, input->str,
+                                       input->len, 1);
+
+                       if (ZSTD_isError (body->len)) {
+                               g_set_error (err, RCLIENT_ERROR, ferror (
+                                               in), "compression error");
+                               g_slice_free1 (sizeof (struct rspamd_client_request), req);
+                               g_string_free (input, TRUE);
+                               rspamd_fstring_free (body);
+
+                               return FALSE;
+                       }
+               }
+
                rspamd_http_message_set_body_from_fstring_steal (req->msg, body);
                req->input = input;
        }
@@ -263,13 +282,24 @@ rspamd_client_command (struct rspamd_client_connection *conn,
                cur = g_list_next (cur);
        }
 
+       if (compressed) {
+               rspamd_http_message_add_header (req->msg, "Compression", "zstd");
+       }
+
        req->msg->url = rspamd_fstring_append (req->msg->url, "/", 1);
        req->msg->url = rspamd_fstring_append (req->msg->url, command, strlen (command));
 
        conn->req = req;
 
-       rspamd_http_connection_write_message (conn->http_conn, req->msg, NULL,
-               "text/plain", req, conn->fd, &conn->timeout, conn->ev_base);
+       if (compressed) {
+               rspamd_http_connection_write_message (conn->http_conn, req->msg, NULL,
+                               "application/x-compressed", req, conn->fd,
+                               &conn->timeout, conn->ev_base);
+       }
+       else {
+               rspamd_http_connection_write_message (conn->http_conn, req->msg, NULL,
+                               "text/plain", req, conn->fd, &conn->timeout, conn->ev_base);
+       }
 
        return TRUE;
 }
index 3b602eb3b51bc8c8da223006657ec808a1e3fc00..c7c8d274c1970e9958e75c1515f2e48e23f53f53 100644 (file)
@@ -77,6 +77,7 @@ gboolean rspamd_client_command (
        FILE *in,
        rspamd_client_callback cb,
        gpointer ud,
+       gboolean compressed,
        GError **err);
 
 /**