From 8cb0e4aa8199af52316806f41f1a7cd1e25ceedb Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 11 Jan 2017 12:56:44 +0000 Subject: [PATCH] [Minor] Allow to append headers for rspamd http router --- src/libserver/worker_util.c | 3 +++ src/libutil/http.c | 32 ++++++++++++++++++++++++++++++++ src/libutil/http.h | 18 ++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 91416c5ab..1cdea22c8 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -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, diff --git a/src/libutil/http.c b/src/libutil/http.c index 635bfea27..df167d615 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -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); } } diff --git a/src/libutil/http.h b/src/libutil/http.h index 0e828d3ff..f41c3ee31 100644 --- a/src/libutil/http.h +++ b/src/libutil/http.h @@ -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 -- 2.39.5