commit cd2f92e: [Feature] Support User-Agent in HTTP requests

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Feb 20 13:07:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-02-20 13:05:56 +0000
URL: https://github.com/rspamd/rspamd/commit/cd2f92e40eca3ac7036e5cac9f90969a8d26655f (HEAD -> master)

[Feature] Support User-Agent in HTTP requests
Issue: #2750
Closes: #2750

---
 src/client/rspamc.c           |  4 ----
 src/libutil/http_connection.c |  5 +++++
 src/libutil/http_context.c    | 14 ++++++++++++++
 src/plugins/dkim_check.c      |  8 +++++++-
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/client/rspamc.c b/src/client/rspamc.c
index d9af90b7f..0568692b4 100644
--- a/src/client/rspamc.c
+++ b/src/client/rspamc.c
@@ -554,10 +554,6 @@ add_options (GQueue *opts)
 	GString *numbuf;
 	gchar **hdr, **rcpt;
 
-	if (user_agent) {
-		ADD_CLIENT_HEADER (opts, "User-Agent", user_agent);
-	}
-
 	if (ip != NULL) {
 		rspamd_inet_addr_t *addr = NULL;
 
diff --git a/src/libutil/http_connection.c b/src/libutil/http_connection.c
index 8463ff762..fd052951b 100644
--- a/src/libutil/http_connection.c
+++ b/src/libutil/http_connection.c
@@ -1805,6 +1805,11 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn
 		}
 	}
 
+	if (priv->ctx->config.user_agent) {
+		rspamd_http_message_add_header (msg, "User-Agent",
+				priv->ctx->config.user_agent);
+	}
+
 	if (encrypted) {
 		mode = rspamd_keypair_alg (priv->local_key);
 
diff --git a/src/libutil/http_context.c b/src/libutil/http_context.c
index 0237af1ab..39a02b179 100644
--- a/src/libutil/http_context.c
+++ b/src/libutil/http_context.c
@@ -50,11 +50,13 @@ rspamd_http_context_new_default (struct rspamd_config *cfg,
 
 	static const int default_kp_size = 1024;
 	static const gdouble default_rotate_time = 120;
+	static const gchar *default_user_agent = "rspamd-" RSPAMD_VERSION_FULL;
 
 	ctx = g_malloc0 (sizeof (*ctx));
 	ctx->config.kp_cache_size_client = default_kp_size;
 	ctx->config.kp_cache_size_server = default_kp_size;
 	ctx->config.client_key_rotate_time = default_rotate_time;
+	ctx->config.user_agent = default_user_agent;
 
 	if (cfg) {
 		ctx->ssl_ctx = cfg->libs_ctx->ssl_ctx;
@@ -127,6 +129,18 @@ rspamd_http_context_create (struct rspamd_config *cfg,
 			if (rotate_time) {
 				ctx->config.client_key_rotate_time = ucl_object_todouble (rotate_time);
 			}
+
+			const ucl_object_t *user_agent;
+
+			user_agent = ucl_object_lookup (client_obj, "user_agent");
+
+			if (user_agent) {
+				ctx->config.user_agent = ucl_object_tostring (user_agent);
+
+				if (ctx->config.user_agent && strlen (ctx->config.user_agent) == 0) {
+					ctx->config.user_agent = NULL;
+				}
+			}
 		}
 
 		server_obj = ucl_object_lookup (http_obj, "server");
diff --git a/src/plugins/dkim_check.c b/src/plugins/dkim_check.c
index b7e9e53ee..f7600552b 100644
--- a/src/plugins/dkim_check.c
+++ b/src/plugins/dkim_check.c
@@ -1545,7 +1545,13 @@ dkim_module_lua_push_verify_result (struct rspamd_dkim_lua_verify_cbdata *cbd,
 	ptask = lua_newuserdata (cbd->L, sizeof (*ptask));
 	*ptask = task;
 	lua_pushboolean (cbd->L, success);
-	lua_pushstring (cbd->L, error_str);
+
+	if (error_str) {
+		lua_pushstring (cbd->L, error_str);
+	}
+	else {
+		lua_pushnil (cbd->L);
+	}
 
 	if (cbd->ctx) {
 		if (res->domain) {


More information about the Commits mailing list