]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Simplify host element handling in HTTP message
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Mar 2019 15:56:45 +0000 (15:56 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Mar 2019 15:56:45 +0000 (15:56 +0000)
src/libutil/http_connection.c
src/libutil/http_message.c
src/libutil/http_private.h
src/lua/lua_http.c

index e75e26a45e52877cfcaf52c9b3035755d4ca43bd..78ec2582fde78d58f08c27f7a116d63fc9f542f0 100644 (file)
@@ -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) {
index 0720dc416949be09b9d45038afc27492ed8a86c2..13241034c9b808cc6038dcbbbcfb542e6ead2388 100644 (file)
@@ -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);
index dd4ca3435b2882397213bde33f3b4d8bca9bdf57..fbc4c17e38536a95bdecca4c4bb20f5af230d387 100644 (file)
@@ -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;
 
index 19cf8f2c99aff00e6bf61f4e894b9ad37ac46d0b..a7b8c0a899d8810fda883ca693caa231f089708a 100644 (file)
@@ -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) {