adds support for xml properties in set-property command

Sun, 04 Feb 2018 15:58:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 04 Feb 2018 15:58:07 +0100
changeset 371
604e7e335b3b
parent 370
ab9c5afdc243
child 372
2e15ff88a0ab

adds support for xml properties in set-property command

dav/main.c file | annotate | diff | comparison | revisions
libidav/methods.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
libidav/xml.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sun Feb 04 12:36:26 2018 +0100
+++ b/dav/main.c	Sun Feb 04 15:58:07 2018 +0100
@@ -1487,23 +1487,42 @@
     }
     
     char *namespace = cmd_getoption(a, "namespace");
+    char *xml = cmd_getoption(a, "xml");
+    
     char *property = a->argv[1];
     char *value = a->argc > 2 ? a->argv[2] : stdin2str();
     
-    if(namespace) {
-        dav_set_string_property_ns(res, namespace, property, value);
+    int ret = 0;
+    if(xml) {
+        DavXmlNode *xmlvalue = dav_parse_xml(sn, value, strlen(value));
+        if(xmlvalue) {
+            if(namespace) {
+                dav_set_property_ns(res, namespace, property, xmlvalue->children);
+            } else {
+                dav_set_property(res, property, xmlvalue->children);
+            }
+        } else {
+            fprintf(stderr, "Error: property content is not valid xml\n");
+            ret = 1;
+        }
     } else {
-        dav_set_string_property(res, property, value);
+        if(namespace) {
+            dav_set_string_property_ns(res, namespace, property, value);
+        } else {
+            dav_set_string_property(res, property, value);
+        }
     }
     
-    int ret = 0;
-    if(dav_store(res)) {
-        print_resource_error(sn, res->path);
-        fprintf(stderr, "Cannot set property.\n");
-        ret = -1;
-    }
+    if(ret == 0) {
+        if(dav_store(res)) {
+            print_resource_error(sn, res->path);
+            fprintf(stderr, "Cannot set property.\n");
+            ret = -1;
+        }
+    } else 
     
     free(path);
+    dav_session_destroy(sn);
     return ret;
 }
 
--- a/libidav/methods.c	Sun Feb 04 12:36:26 2018 +0100
+++ b/libidav/methods.c	Sun Feb 04 15:58:07 2018 +0100
@@ -741,6 +741,9 @@
     ucx_buffer_seek(request, 0, SEEK_SET);
     CURLcode ret = dav_session_curl_perform_buf(sn, request, response, NULL);
     curl_slist_free_all(headers);
+    
+    printf("proppatch: \n%.*s\n", request->size, request->space);
+    
     return ret;
 }
 
@@ -814,7 +817,7 @@
             
             // content
             DavXmlNode *content = property->value;
-            if(content->type == DAV_XML_TEXT) {
+            if(content->type == DAV_XML_TEXT && !content->next) {
                 ucx_buffer_write(content->content, 1, content->contentlength, buf);
             } else {
                 dav_print_node(buf, (write_func)ucx_buffer_write, namespaces, content);
--- a/libidav/webdav.h	Sun Feb 04 12:36:26 2018 +0100
+++ b/libidav/webdav.h	Sun Feb 04 15:58:07 2018 +0100
@@ -321,6 +321,8 @@
 DavXmlNode* dav_xml_createtextnode(const char *text);
 void dav_xml_add_child(DavXmlNode *node, DavXmlNode *child);
 
+DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/libidav/xml.c	Sun Feb 04 12:36:26 2018 +0100
+++ b/libidav/xml.c	Sun Feb 04 15:58:07 2018 +0100
@@ -140,28 +140,37 @@
 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) {
     while(node) {
         if(node->type == DAV_XML_ELEMENT) {
-            char *prefix = ucx_map_cstr_get(nsmap, node->namespace);
             char *tagend = node->children ? ">" : " />";
-            if(!prefix) {
-                sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count);
-                ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
-                prefix = newpre.ptr;
-                ucx_fprintf(
-                        stream,
-                        writef,
-                        "<%s:%s xmlns:%s=\"%s\"%s",
-                        prefix,
-                        node->name,
-                        prefix,
-                        node->namespace,
-                        tagend);
+            char *prefix = NULL;
+            if(node->namespace) {
+                prefix = ucx_map_cstr_get(nsmap, node->namespace);
+                if(!prefix) {
+                    sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count);
+                    ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
+                    prefix = newpre.ptr;
+                    ucx_fprintf(
+                            stream,
+                            writef,
+                            "<%s:%s xmlns:%s=\"%s\"%s",
+                            prefix,
+                            node->name,
+                            prefix,
+                            node->namespace,
+                            tagend);
+                } else {
+                    ucx_fprintf(stream, writef, "<%s:%s%s", prefix, node->name, tagend);
+                }
             } else {
-                ucx_fprintf(stream, writef, "<%s:%s%s", prefix, node->name, tagend);
+                ucx_fprintf(stream, writef, "<%s%s", node->name, tagend);
             }
             
             if(node->children) {
                 dav_print_node(stream, writef, nsmap, node->children);
-                ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
+                if(prefix) {
+                    ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
+                } else {
+                    ucx_fprintf(stream, writef, "</%s>", node->name);
+                }
             }
         } else if(node->type == DAV_XML_TEXT) {
             writef(node->content, 1, node->contentlength, stream);
@@ -275,3 +284,18 @@
         node->children = child;
     }
 }
+
+DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len) {
+    xmlDoc *doc = xmlReadMemory(str, len, NULL, NULL, 0);
+    if(!doc) {
+        return NULL;
+    }
+    xmlNode *xml_root = xmlDocGetRootElement(doc);
+    if(!xml_root) {
+        xmlFreeDoc(doc);
+        return NULL;
+    }
+    DavXmlNode *x = dav_convert_xml(sn, xml_root);
+    xmlFreeDoc(doc);
+    return x;
+}

mercurial