Reuse stanza parsing code

This commit is contained in:
James Booth
2015-06-06 23:03:32 +01:00
parent 35babf392f
commit 2d83c6f732

View File

@@ -29,8 +29,6 @@
#include "server/stanzas.h" #include "server/stanzas.h"
#include "server/log.h" #include "server/log.h"
static int depth = 0;
static XMPPStanza *curr_stanza = NULL;
static int timeoutsecs = 0; static int timeoutsecs = 0;
void void
@@ -43,65 +41,19 @@ verify_set_timeout(int seconds)
} }
} }
static void
start_element(void *data, const char *element, const char **attributes)
{
XMPPStanza *stanza = stanza_new(element, attributes);
if (depth == 0) {
curr_stanza = stanza;
curr_stanza->parent = NULL;
} else {
stanza->parent = curr_stanza;
curr_stanza = stanza;
}
depth++;
}
static void
end_element(void *data, const char *element)
{
depth--;
if (depth > 0) {
stanza_add_child(curr_stanza->parent, curr_stanza);
curr_stanza = curr_stanza->parent;
}
}
static void
handle_data(void *data, const char *content, int length)
{
if (!curr_stanza->content) {
curr_stanza->content = g_string_new("");
}
g_string_append_len(curr_stanza->content, content, length);
}
int int
verify_any(char *stanza_text) verify_any(char *stanza_text)
{ {
depth = 0; XMPPStanza *stanza = parse_stanza(stanza_text);
if (curr_stanza) {
stanza_free(curr_stanza);
}
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start_element, end_element);
XML_SetCharacterDataHandler(parser, handle_data);
XML_Parse(parser, stanza_text, strlen(stanza_text), 0);
XML_ParserFree(parser);
int result = 0; int result = 0;
if (timeoutsecs <= 0) { if (timeoutsecs <= 0) {
result = stanzas_verify_any(curr_stanza); result = stanzas_verify_any(stanza);
} else { } else {
double elapsed = 0.0; double elapsed = 0.0;
GTimer *timer = g_timer_new(); GTimer *timer = g_timer_new();
while (elapsed < timeoutsecs * 1.0) { while (elapsed < timeoutsecs * 1.0) {
result = stanzas_verify_any(curr_stanza); result = stanzas_verify_any(stanza);
if (result) { if (result) {
break; break;
} }
@@ -123,25 +75,16 @@ verify_any(char *stanza_text)
int int
verify_last(char *stanza_text) verify_last(char *stanza_text)
{ {
depth = 0; XMPPStanza *stanza = parse_stanza(stanza_text);
if (curr_stanza) {
stanza_free(curr_stanza);
}
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start_element, end_element);
XML_SetCharacterDataHandler(parser, handle_data);
XML_Parse(parser, stanza_text, strlen(stanza_text), 0);
XML_ParserFree(parser);
int result = 0; int result = 0;
if (timeoutsecs <= 0) { if (timeoutsecs <= 0) {
result = stanzas_verify_last(curr_stanza); result = stanzas_verify_last(stanza);
} else { } else {
double elapsed = 0.0; double elapsed = 0.0;
GTimer *timer = g_timer_new(); GTimer *timer = g_timer_new();
while (elapsed < timeoutsecs * 1.0) { while (elapsed < timeoutsecs * 1.0) {
result = stanzas_verify_last(curr_stanza); result = stanzas_verify_last(stanza);
if (result) { if (result) {
break; break;
} }