commit 2930802: [Rework] Html: Another steps to get rid of gnode

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jun 7 16:35:07 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-06-07 17:00:42 +0100
URL: https://github.com/rspamd/rspamd/commit/2930802de7f8b64959617ea3b0948078eb3adc9b

[Rework] Html: Another steps to get rid of gnode

---
 src/libserver/html/html.cxx     | 48 +++++++++++++++--------------------------
 src/libserver/html/html_tag.hxx |  2 +-
 2 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/src/libserver/html/html.cxx b/src/libserver/html/html.cxx
index a459ee0c6..f041f45b7 100644
--- a/src/libserver/html/html.cxx
+++ b/src/libserver/html/html.cxx
@@ -182,16 +182,19 @@ html_process_tag(rspamd_mempool_t *pool,
 						return true;
 					}
 				}
-			}
 
-			if (hc->total_tags < rspamd::html::max_tags) {
-				parent->children.push_back(tag);
+				if (hc->total_tags < rspamd::html::max_tags) {
+					parent->children.push_back(tag);
 
-				if ((tag->flags & FL_CLOSED) == 0) {
-					tags_stack.push_back(tag);
-				}
+					if ((tag->flags & FL_CLOSED) == 0) {
+						tags_stack.push_back(tag);
+					}
 
-				hc->total_tags++;
+					hc->total_tags++;
+				}
+			}
+			else {
+				hc->root_tag = tag;
 			}
 
 			if (tag->flags & (CM_HEAD | CM_UNKNOWN | FL_IGNORE)) {
@@ -1526,26 +1529,6 @@ html_process_block_tag(rspamd_mempool_t *pool, struct html_tag *tag,
 	tag->block = bl;
 }
 
-static auto
-html_propagate_lengths(GNode *node, gpointer _unused) -> gboolean
-{
-	GNode *child;
-	struct html_tag *tag = static_cast<html_tag *>(node->data), *cld_tag;
-
-	if (tag) {
-		child = node->children;
-
-		/* Summarize content length from children */
-		while (child) {
-			cld_tag = static_cast<html_tag *>(child->data);
-			tag->content_length += cld_tag->content_length;
-			child = child->next;
-		}
-	}
-
-	return FALSE;
-}
-
 static auto
 html_propagate_style(struct html_content *hc,
 							struct html_tag *tag,
@@ -2260,10 +2243,13 @@ html_process_part_full (rspamd_mempool_t *pool,
 		}
 	}
 
-	if (hc->html_tags) {
-		g_node_traverse (hc->html_tags, G_POST_ORDER, G_TRAVERSE_ALL, -1,
-				html_propagate_lengths, NULL);
-	}
+	hc->traverse_tags([](const html_tag *tag) -> bool {
+		/* Summarize content length from children */
+		for (const auto *cld_tag : tag->children) {
+			tag->content_length += cld_tag->content_length;
+		}
+		return true;
+	}, html_content::traverse_type::POST_ORDER);
 
 	return hc;
 }
diff --git a/src/libserver/html/html_tag.hxx b/src/libserver/html/html_tag.hxx
index d75cfcf6d..4aba9af41 100644
--- a/src/libserver/html/html_tag.hxx
+++ b/src/libserver/html/html_tag.hxx
@@ -45,7 +45,7 @@ using html_tag_extra_t = std::variant<std::monostate, struct rspamd_url *, struc
 struct html_tag {
 	gint id = -1;
 	gint flags = 0;
-	guint content_length = 0;
+	mutable guint content_length = 0; /* Allow content length propagation */
 	goffset content_offset = 0;
 
 	std::string_view name;


More information about the Commits mailing list