]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Allow to append headers for rspamd http router
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 11 Jan 2017 12:56:44 +0000 (12:56 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 11 Jan 2017 12:56:44 +0000 (12:56 +0000)
src/libserver/worker_util.c
src/libutil/http.c
src/libutil/http.h

index 91416c5ab72e00479825116dd37c1879b92c105d..1cdea22c8c4334962f02b447daa217e6d1447a58 100644 (file)
@@ -380,6 +380,7 @@ rspamd_controller_send_error (struct rspamd_http_connection_entry *entry,
        rspamd_printf_fstring (&reply, "{\"error\":\"%V\"}", msg->status);
        rspamd_http_message_set_body_from_fstring_steal (msg, reply);
        rspamd_http_connection_reset (entry->conn);
+       rspamd_http_router_insert_headers (entry->rt, msg);
        rspamd_http_connection_write_message (entry->conn,
                msg,
                NULL,
@@ -405,6 +406,7 @@ rspamd_controller_send_string (struct rspamd_http_connection_entry *entry,
        reply = rspamd_fstring_new_init (str, strlen (str));
        rspamd_http_message_set_body_from_fstring_steal (msg, reply);
        rspamd_http_connection_reset (entry->conn);
+       rspamd_http_router_insert_headers (entry->rt, msg);
        rspamd_http_connection_write_message (entry->conn,
                msg,
                NULL,
@@ -431,6 +433,7 @@ rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
        rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &reply);
        rspamd_http_message_set_body_from_fstring_steal (msg, reply);
        rspamd_http_connection_reset (entry->conn);
+       rspamd_http_router_insert_headers (entry->rt, msg);
        rspamd_http_connection_write_message (entry->conn,
                msg,
                NULL,
index 635bfea271f5c66a591ca5cecbe9de5624c54e4a..df167d61528e1084e6e8c5bd5217b9d8978adf7d 100644 (file)
@@ -2946,6 +2946,7 @@ rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry,
        reply_msg = rspamd_http_new_message (HTTP_RESPONSE);
        reply_msg->date = time (NULL);
        reply_msg->code = 200;
+       rspamd_http_router_insert_headers (entry->rt, reply_msg);
 
        if (!rspamd_http_message_set_body_from_fd (reply_msg, fd)) {
                close (fd);
@@ -3024,6 +3025,7 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn,
                        err_msg->code = err->code;
                        rspamd_http_message_set_body (err_msg, err->message,
                                        strlen (err->message));
+                       rspamd_http_router_insert_headers (router, err_msg);
                        rspamd_http_connection_reset (entry->conn);
                        rspamd_http_connection_write_message (entry->conn,
                                        err_msg,
@@ -3070,6 +3072,7 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn,
                                err_msg = rspamd_http_new_message (HTTP_RESPONSE);
                                err_msg->date = time (NULL);
                                err_msg->code = err->code;
+                               rspamd_http_router_insert_headers (router, err_msg);
                                rspamd_http_message_set_body (err_msg, err->message,
                                                strlen (err->message));
                                rspamd_http_connection_reset (entry->conn);
@@ -3107,6 +3110,8 @@ rspamd_http_router_new (rspamd_http_router_error_handler_t eh,
        new->error_handler = eh;
        new->finish_handler = fh;
        new->ev_base = base;
+       new->response_headers = g_hash_table_new_full (rspamd_strcase_hash,
+                       rspamd_strcase_equal, g_free, g_free);
 
        if (timeout) {
                new->tv = *timeout;
@@ -3166,6 +3171,32 @@ rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
        }
 }
 
+void
+rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
+               const gchar *name, const gchar *value)
+{
+       if (name != NULL && value != NULL && router != NULL) {
+               g_hash_table_replace (router->response_headers, g_strdup (name),
+                               g_strdup (value));
+       }
+}
+
+void
+rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router,
+               struct rspamd_http_message *msg)
+{
+       GHashTableIter it;
+       gpointer k, v;
+
+       if (router && msg) {
+               g_hash_table_iter_init (&it, router->response_headers);
+
+               while (g_hash_table_iter_next (&it, &k, &v)) {
+                       rspamd_http_message_add_header (msg, k, v);
+               }
+       }
+}
+
 void
 rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router,
                struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler)
@@ -3240,6 +3271,7 @@ rspamd_http_router_free (struct rspamd_http_connection_router *router)
 
                g_ptr_array_free (router->regexps, TRUE);
                g_hash_table_unref (router->paths);
+               g_hash_table_unref (router->response_headers);
                g_slice_free1 (sizeof (struct rspamd_http_connection_router), router);
        }
 }
index 0e828d3ff12134f613758eda01a3f4782fac000b..f41c3ee31ad3a8fcf93eabad46bc9fac2de4dd8f 100644 (file)
@@ -127,6 +127,7 @@ struct rspamd_http_connection_entry {
 struct rspamd_http_connection_router {
        struct rspamd_http_connection_entry *conns;
        GHashTable *paths;
+       GHashTable *response_headers;
        GPtrArray *regexps;
        struct timeval tv;
        struct timeval *ptv;
@@ -457,6 +458,23 @@ void rspamd_http_router_set_key (struct rspamd_http_connection_router *router,
 void rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
                const gchar *path, rspamd_http_router_handler_t handler);
 
+/**
+ * Add custom header to append to router replies
+ * @param router
+ * @param name
+ * @param value
+ */
+void rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
+               const gchar *name, const gchar *value);
+
+/**
+ * Inserts router headers to the outbound message
+ * @param router
+ * @param msg
+ */
+void rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router,
+               struct rspamd_http_message *msg);
+
 struct rspamd_regexp_s;
 /**
  * Adds new pattern to router, regexp object is refcounted by this function