fix: Fix XMPP stream parsing depth tracking and add debug logging

- Reset depth counter in parser_init() to fix stanza parsing between connections
- Properly track depth for virtual wrapper element
- Add debug logging in stanzas_verify_any() to trace stanza matching
This commit is contained in:
2025-12-02 19:42:40 +03:00
parent 14b664f4c7
commit 0c38ca0c50
2 changed files with 26 additions and 4 deletions

View File

@@ -75,15 +75,25 @@ stanzas_verify_any(XMPPStanza *stanza)
return 0; return 0;
} }
char *expected_str = stanza_to_string(stanza);
fprintf(stderr, "DEBUG verify_any: Looking for: %s\n", expected_str);
free(expected_str);
GList *curr = g_list_last(stanzas); GList *curr = g_list_last(stanzas);
int idx = g_list_length(stanzas) - 1;
while (curr) { while (curr) {
XMPPStanza *curr_stanza = curr->data; XMPPStanza *curr_stanza = curr->data;
char *curr_str = stanza_to_string(curr_stanza);
fprintf(stderr, "DEBUG verify_any: [%d] %s\n", idx, curr_str);
free(curr_str);
if (_stanzas_equal(stanza, curr_stanza) == 0) { if (_stanzas_equal(stanza, curr_stanza) == 0) {
pthread_mutex_unlock(&stanzas_lock); pthread_mutex_unlock(&stanzas_lock);
return 1; return 1;
} }
curr = g_list_previous(curr); curr = g_list_previous(curr);
idx--;
} }
pthread_mutex_unlock(&stanzas_lock); pthread_mutex_unlock(&stanzas_lock);

View File

@@ -53,6 +53,7 @@ parser_init(stream_start_func startcb, auth_func authcb, id_func idcb, query_fun
g_string_free(curr_string, TRUE); g_string_free(curr_string, TRUE);
} }
curr_string = g_string_new(""); curr_string = g_string_new("");
depth = 0; // Reset depth on parser init
stream_start_cb = startcb; stream_start_cb = startcb;
auth_cb = authcb; auth_cb = authcb;
@@ -116,15 +117,17 @@ parser_reset(void)
parser_close(); parser_close();
parser_init(stream_start_cb, auth_cb, id_cb, query_cb); parser_init(stream_start_cb, auth_cb, id_cb, query_cb);
// Feed a virtual root element to allow multiple stanzas after reset
XML_Parse(parser, "<wrapper>", 9, 0);
do_reset = 0; do_reset = 0;
} }
static void static void
_start_element(void *data, const char *element, const char **attributes) _start_element(void *data, const char *element, const char **attributes)
{ {
// Virtual wrapper element - track depth but don't create stanza
// Ignore our virtual wrapper element
if (g_strcmp0(element, "wrapper") == 0) { if (g_strcmp0(element, "wrapper") == 0) {
depth++;
return; return;
} }
@@ -137,7 +140,8 @@ _start_element(void *data, const char *element, const char **attributes)
XMPPStanza *stanza = stanza_new(element, attributes); XMPPStanza *stanza = stanza_new(element, attributes);
if (depth == 0) { // depth == 1 means we're at wrapper level, so this is a root XMPP stanza
if (depth == 1) {
curr_stanza = stanza; curr_stanza = stanza;
curr_stanza->parent = NULL; curr_stanza->parent = NULL;
} else { } else {
@@ -151,8 +155,16 @@ _start_element(void *data, const char *element, const char **attributes)
static void static void
_end_element(void *data, const char *element) _end_element(void *data, const char *element)
{ {
// Virtual wrapper element - just track depth
if (g_strcmp0(element, "wrapper") == 0) {
depth--;
return;
}
depth--; depth--;
if (depth > 0) {
// depth == 1 means we're back at wrapper level - stanza is complete
if (depth > 1) {
stanza_add_child(curr_stanza->parent, curr_stanza); stanza_add_child(curr_stanza->parent, curr_stanza);
curr_stanza = curr_stanza->parent; curr_stanza = curr_stanza->parent;
return; return;