From 02de13af4e812e81c882fa33683429c65a475099 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 18 Mar 2019 15:56:45 +0000 Subject: [PATCH] [Minor] Simplify host element handling in HTTP message --- src/libutil/http_connection.c | 87 +++++++++++++---------------------- src/libutil/http_message.c | 4 +- src/libutil/http_private.h | 2 +- src/lua/lua_http.c | 6 +-- 4 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/libutil/http_connection.c b/src/libutil/http_connection.c index e75e26a45..78ec2582f 100644 --- a/src/libutil/http_connection.c +++ b/src/libutil/http_connection.c @@ -720,7 +720,7 @@ rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) struct rspamd_http_connection_private *priv; gpointer ssl; gint request_method; - rspamd_fstring_t *prev_host; + GString *prev_host = NULL; priv = conn->priv; ssl = priv->ssl; @@ -754,7 +754,7 @@ rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) } else { if (prev_host) { - rspamd_fstring_free (prev_host); + g_string_free (prev_host, TRUE); } } } @@ -1426,8 +1426,7 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) } if (msg->host) { - new_msg->host = rspamd_fstring_new_init (msg->host->str, - msg->host->len); + new_msg->host = g_string_new_len (msg->host->str, msg->host->len); } new_msg->method = msg->method; @@ -1812,12 +1811,15 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, } } else { + + /* Client request */ if (conn->opts & RSPAMD_HTTP_CLIENT_KEEP_ALIVE) { conn_type = "keep-alive"; } /* Format request */ - enclen += msg->url->len + strlen (http_method_str (msg->method)) + 1; + enclen += RSPAMD_FSTRING_LEN (msg->url) + + strlen (http_method_str (msg->method)) + 1; if (host == NULL && msg->host == NULL) { /* Fallback to HTTP/1.0 */ @@ -1855,59 +1857,34 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, } else { /* Normal HTTP/1.1 with Host */ + if (host == NULL) { + host = msg->host->str; + } + if (encrypted) { - if (host != NULL) { - rspamd_printf_fstring (buf, - "%s %s HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n", - "POST", - "/post", - conn_type, - host, - enclen); - } - else { - rspamd_printf_fstring (buf, - "%s %s HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %V\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n", - "POST", - "/post", - conn_type, - msg->host, - enclen); - } + rspamd_printf_fstring (buf, + "%s %s HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: application/octet-stream\r\n", + "POST", + "/post", + conn_type, + host, + enclen); } else { - if (host != NULL) { - rspamd_printf_fstring (buf, - "%s %V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n", - http_method_str (msg->method), - msg->url, - conn_type, - host, - bodylen); - } - else { - rspamd_printf_fstring (buf, - "%s %V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %V\r\n" - "Content-Length: %z\r\n", - http_method_str (msg->method), - msg->url, - conn_type, - msg->host, - bodylen); - } + rspamd_printf_fstring (buf, + "%s %V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n", + http_method_str (msg->method), + msg->url, + conn_type, + host, + bodylen); if (bodylen > 0) { if (mime_type != NULL) { diff --git a/src/libutil/http_message.c b/src/libutil/http_message.c index 0720dc416..13241034c 100644 --- a/src/libutil/http_message.c +++ b/src/libutil/http_message.c @@ -104,7 +104,7 @@ rspamd_http_message_from_url (const gchar *url) } } - msg->host = rspamd_fstring_new_init (host, pu.field_data[UF_HOST].len); + msg->host = g_string_new_len (host, pu.field_data[UF_HOST].len); msg->url = rspamd_fstring_append (msg->url, path, pathlen); REF_INIT_RETAIN (msg, rspamd_http_message_free); @@ -489,7 +489,7 @@ rspamd_http_message_free (struct rspamd_http_message *msg) rspamd_fstring_free (msg->status); } if (msg->host != NULL) { - rspamd_fstring_free (msg->host); + g_string_free (msg->host, TRUE); } if (msg->peer_key != NULL) { rspamd_pubkey_unref (msg->peer_key); diff --git a/src/libutil/http_private.h b/src/libutil/http_private.h index dd4ca3435..fbc4c17e3 100644 --- a/src/libutil/http_private.h +++ b/src/libutil/http_private.h @@ -43,7 +43,7 @@ struct rspamd_http_header { */ struct rspamd_http_message { rspamd_fstring_t *url; - rspamd_fstring_t *host; + GString *host; rspamd_fstring_t *status; struct rspamd_http_header *headers; diff --git a/src/lua/lua_http.c b/src/lua/lua_http.c index 19cf8f2c9..a7b8c0a89 100644 --- a/src/lua/lua_http.c +++ b/src/lua/lua_http.c @@ -127,10 +127,6 @@ lua_http_fin (gpointer arg) g_free (cbd->mime_type); } - if (cbd->host) { - g_free (cbd->host); - } - if (cbd->auth) { g_free (cbd->auth); } @@ -935,7 +931,7 @@ lua_http_request (lua_State *L) } if (msg->host) { - cbd->host = rspamd_fstring_cstr (msg->host); + cbd->host = msg->host->str; } if (body) { -- 2.39.5