commit 3babc53: [Minor] Add port to the `Host` header if using non-standard ports

Vsevolod Stakhov vsevolod at rspamd.com
Sat Nov 19 20:28:22 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-11-19 14:24:41 +0000
URL: https://github.com/rspamd/rspamd/commit/3babc536a5065b44e50a433df6cb8a19f4cd1be3

[Minor] Add port to the `Host` header if using non-standard ports

---
 src/libserver/http/http_connection.c | 57 ++++++++++++++++++++++++++++++++++--
 src/libserver/http/http_message.c    | 10 +++++++
 src/libserver/http/http_message.h    |  7 +++++
 3 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/src/libserver/http/http_connection.c b/src/libserver/http/http_connection.c
index a0cbf0dce..e1c6ccf31 100644
--- a/src/libserver/http/http_connection.c
+++ b/src/libserver/http/http_connection.c
@@ -1867,7 +1867,8 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 
 			if (encrypted) {
 				/* TODO: Add proxy support to HTTPCrypt */
-				rspamd_printf_fstring (buf,
+				if (rspamd_http_message_is_standard_port(msg)) {
+					rspamd_printf_fstring(buf,
 						"%s %s HTTP/1.1\r\n"
 						"Connection: %s\r\n"
 						"Host: %s\r\n"
@@ -1878,9 +1879,25 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 						conn_type,
 						host,
 						enclen);
+				}
+				else {
+					rspamd_printf_fstring(buf,
+						"%s %s HTTP/1.1\r\n"
+						"Connection: %s\r\n"
+						"Host: %s:%d\r\n"
+						"Content-Length: %z\r\n"
+						"Content-Type: application/octet-stream\r\n",
+						"POST",
+						"/post",
+						conn_type,
+						host,
+						msg->port,
+						enclen);
+				}
 			}
 			else {
 				if (conn->priv->flags & RSPAMD_HTTP_CONN_FLAG_PROXY) {
+					/* Write proxied request */
 					if ((msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER)) {
 						rspamd_printf_fstring(buf,
 								"%s %s://%s:%d/%V HTTP/1.1\r\n"
@@ -1895,7 +1912,8 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 								bodylen);
 					}
 					else {
-						rspamd_printf_fstring(buf,
+						if (rspamd_http_message_is_standard_port(msg)) {
+							rspamd_printf_fstring(buf,
 								"%s %s://%s:%d/%V HTTP/1.1\r\n"
 								"Connection: %s\r\n"
 								"Host: %s\r\n"
@@ -1908,9 +1926,27 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 								conn_type,
 								host,
 								bodylen);
+						}
+						else {
+							rspamd_printf_fstring(buf,
+								"%s %s://%s:%d/%V HTTP/1.1\r\n"
+								"Connection: %s\r\n"
+								"Host: %s:%d\r\n"
+								"Content-Length: %z\r\n",
+								http_method_str(msg->method),
+								(conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http",
+								host,
+								msg->port,
+								msg->url,
+								conn_type,
+								host,
+								msg->port,
+								bodylen);
+						}
 					}
 				}
 				else {
+					/* Unproxied version */
 					if ((msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER)) {
 						rspamd_printf_fstring(buf,
 								"%s %V HTTP/1.1\r\n"
@@ -1922,7 +1958,8 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 								bodylen);
 					}
 					else {
-						rspamd_printf_fstring(buf,
+						if (rspamd_http_message_is_standard_port(msg)) {
+							rspamd_printf_fstring(buf,
 								"%s %V HTTP/1.1\r\n"
 								"Connection: %s\r\n"
 								"Host: %s\r\n"
@@ -1932,6 +1969,20 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted,
 								conn_type,
 								host,
 								bodylen);
+						}
+						else {
+							rspamd_printf_fstring(buf,
+								"%s %V HTTP/1.1\r\n"
+								"Connection: %s\r\n"
+								"Host: %s:%d\r\n"
+								"Content-Length: %z\r\n",
+								http_method_str(msg->method),
+								msg->url,
+								conn_type,
+								host,
+								msg->port,
+								bodylen);
+						}
 					}
 				}
 
diff --git a/src/libserver/http/http_message.c b/src/libserver/http/http_message.c
index 23ff85cd7..435cdcf13 100644
--- a/src/libserver/http/http_message.c
+++ b/src/libserver/http/http_message.c
@@ -720,4 +720,14 @@ rspamd_http_message_get_http_host (struct rspamd_http_message *msg,
 	}
 
 	return NULL;
+}
+
+bool
+rspamd_http_message_is_standard_port(struct rspamd_http_message *msg)
+{
+	if (msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) {
+		return msg->port == 443;
+	}
+
+	return msg->port == 80;
 }
\ No newline at end of file
diff --git a/src/libserver/http/http_message.h b/src/libserver/http/http_message.h
index 38f599048..f0c0cc2dc 100644
--- a/src/libserver/http/http_message.h
+++ b/src/libserver/http/http_message.h
@@ -239,6 +239,13 @@ guint rspamd_http_message_get_flags (struct rspamd_http_message *msg);
 const gchar* rspamd_http_message_get_http_host (struct rspamd_http_message *msg,
 		gsize *hostlen);
 
+/**
+ * Returns true if a message has standard port (80 or 443 for https)
+ * @param msg
+ * @return
+ */
+bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg);
+
 #ifdef  __cplusplus
 }
 #endif


More information about the Commits mailing list