summaryrefslogtreecommitdiff
path: root/src/rexmpp_http_upload.c
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2023-05-23 12:05:13 +0300
committerdefanor <defanor@uberspace.net>2023-05-23 12:05:13 +0300
commit122b13ec955deb718aca280112584b645c9caea0 (patch)
treefdaa1dce84956a33f669605e67df1a587a01fc1d /src/rexmpp_http_upload.c
parentae42bb94fd49a450690014b6039a812a251f64cd (diff)
Replace libxml2's xmlNode with a custom XML structure
The new structure (rexmpp_xml) is simpler, and should allow manipulation from Rust without any dependency on libxml2 from the Rust code (while Rust has its own parsers, such as rxml). Alternative XML parsers (e.g., libexpat) now can be used from the C code. The replacement/abstraction is not quite complete yet: the parsing process itself (xmlParseChunk and friends) should be abstracted out.
Diffstat (limited to 'src/rexmpp_http_upload.c')
-rw-r--r--src/rexmpp_http_upload.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/src/rexmpp_http_upload.c b/src/rexmpp_http_upload.c
index 62d371b..2552771 100644
--- a/src/rexmpp_http_upload.c
+++ b/src/rexmpp_http_upload.c
@@ -18,6 +18,7 @@
#endif
#include "rexmpp.h"
+#include "rexmpp_xml.h"
#include "rexmpp_http_upload.h"
@@ -43,21 +44,21 @@ void rexmpp_upload_task_finish (struct rexmpp_http_upload_task *task) {
void rexmpp_http_upload_slot_cb (rexmpp_t *s,
void *ptr,
- xmlNodePtr request,
- xmlNodePtr response,
+ rexmpp_xml_t *request,
+ rexmpp_xml_t *response,
int success)
{
(void)request;
struct rexmpp_http_upload_task *task = ptr;
if (success) {
- xmlNodePtr slot = rexmpp_xml_find_child(response, "urn:xmpp:http:upload:0", "slot");
- xmlNodePtr put = rexmpp_xml_find_child(slot, "urn:xmpp:http:upload:0", "put");
- xmlNodePtr get = rexmpp_xml_find_child(slot, "urn:xmpp:http:upload:0", "get");
+ rexmpp_xml_t *slot = rexmpp_xml_find_child(response, "urn:xmpp:http:upload:0", "slot");
+ rexmpp_xml_t *put = rexmpp_xml_find_child(slot, "urn:xmpp:http:upload:0", "put");
+ rexmpp_xml_t *get = rexmpp_xml_find_child(slot, "urn:xmpp:http:upload:0", "get");
if (put != NULL && get != NULL) {
- char *put_url = xmlGetProp(put, "url");
- char *get_url = xmlGetProp(get, "url");
+ const char *put_url = rexmpp_xml_find_attr_val(put, "url");
+ const char *get_url = rexmpp_xml_find_attr_val(get, "url");
if (put_url != NULL && get_url != NULL) {
- task->get_url = get_url;
+ task->get_url = strdup(get_url);
CURL *ce = curl_easy_init();
curl_easy_setopt(ce, CURLOPT_PRIVATE, task);
@@ -65,11 +66,11 @@ void rexmpp_http_upload_slot_cb (rexmpp_t *s,
curl_easy_setopt(ce, CURLOPT_READDATA, task->fh);
curl_easy_setopt(ce, CURLOPT_URL, put_url);
- xmlNodePtr header = xmlFirstElementChild(put);
+ rexmpp_xml_t *header = rexmpp_xml_first_elem_child(put);
while (header) {
- char *header_name = xmlGetProp(header, "name");
+ const char *header_name = rexmpp_xml_find_attr_val(header, "name");
if (header_name != NULL) {
- char *header_str = xmlNodeGetContent(header);
+ const char *header_str = rexmpp_xml_text_child(header);
if (header_str != NULL) {
size_t full_header_str_len = strlen(header_name) + 3 + strlen(header_str);
char *full_header_str = malloc(full_header_str_len);
@@ -78,11 +79,9 @@ void rexmpp_http_upload_slot_cb (rexmpp_t *s,
task->http_headers =
curl_slist_append(task->http_headers, full_header_str);
free(full_header_str);
- free(header_str);
}
- free(header_name);
}
- header = header->next;
+ header = rexmpp_xml_next_elem_sibling(header);
}
curl_easy_setopt(ce, CURLOPT_HTTPHEADER, task->http_headers);
@@ -91,12 +90,6 @@ void rexmpp_http_upload_slot_cb (rexmpp_t *s,
return;
} else {
rexmpp_log(s, LOG_ERR, "Unexpected structure for a HTTP file upload slot.");
- if (get_url != NULL) {
- free(get_url);
- }
- }
- if (put_url != NULL) {
- free(put_url);
}
} else {
rexmpp_log(s, LOG_ERR, "Unexpected structure for a HTTP file upload slot.");
@@ -109,8 +102,8 @@ void rexmpp_http_upload_slot_cb (rexmpp_t *s,
void rexmpp_http_upload_feature_cb (rexmpp_t *s,
void *ptr,
- xmlNodePtr request,
- xmlNodePtr response,
+ rexmpp_xml_t *request,
+ rexmpp_xml_t *response,
int success)
{
(void)response;
@@ -120,17 +113,17 @@ void rexmpp_http_upload_feature_cb (rexmpp_t *s,
rexmpp_upload_task_finish(task);
return;
}
- xmlNodePtr req = rexmpp_xml_new_node("request", "urn:xmpp:http:upload:0");
- xmlNewProp(req, "filename", task->fname);
+ rexmpp_xml_t *req =
+ rexmpp_xml_new_elem("request", "urn:xmpp:http:upload:0");
+ rexmpp_xml_add_attr(req, "filename", task->fname);
char buf[11];
snprintf(buf, 11, "%u", task->fsize);
- xmlNewProp(req, "size", buf);
+ rexmpp_xml_add_attr(req, "size", buf);
if (task->content_type) {
- xmlNewProp(req, "content-type", task->content_type);
+ rexmpp_xml_add_attr(req, "content-type", task->content_type);
}
- char *to = xmlGetProp(request, "to");
+ const char *to = rexmpp_xml_find_attr_val(request, "to");
rexmpp_iq_new(s, "get", to, req, rexmpp_http_upload_slot_cb, task);
- free(to);
}
rexmpp_err_t