adds namespace config element

Sun, 24 Sep 2017 14:31:42 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 24 Sep 2017 14:31:42 +0200
changeset 317
7eea57f6d847
parent 316
9d0505d80044
child 318
7e0694423838

adds namespace config element

dav/config.c file | annotate | diff | comparison | revisions
dav/config.h file | annotate | diff | comparison | revisions
dav/main.c file | annotate | diff | comparison | revisions
docs/schema/sync.xsd file | annotate | diff | comparison | revisions
libidav/webdav.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/config.c	Sun Sep 24 11:31:01 2017 +0200
+++ b/dav/config.c	Sun Sep 24 14:31:42 2017 +0200
@@ -139,6 +139,8 @@
                 ret = load_proxy(ctx->http_proxy, node, HTTP_PROXY);
             } else if (xstreq(node->name, "https-proxy")) {
                 ret = load_proxy(ctx->https_proxy, node, HTTPS_PROXY);
+            } else if (xstreq(node->name, "namespace")) {
+                ret = load_namespace(node);
             } else {
                 fprintf(stderr, "Unknown config element: %s\n", node->name);
                 ret = 1;
@@ -496,7 +498,63 @@
     
     fclose(file);
     return k;
-} 
+}
+
+static char* get_attr_content(xmlNode *node) {
+    // TODO: remove code duplication (util_xml_get_text)
+    while(node) {
+        if(node->type == XML_TEXT_NODE) {
+            return (char*)node->content;
+        }
+        node = node->next;
+    }
+    return NULL;
+}
+
+int load_namespace(const xmlNode *node) {
+    const char *prefix = NULL;
+    const char *uri = NULL;
+    
+    xmlAttr *attr = node->properties;
+    while(attr) {
+        if(attr->type == XML_ATTRIBUTE_NODE) {
+            char *value = get_attr_content(attr->children);
+            if(!value) {
+                print_error(
+                        node->line,
+                        "missing value for attribute %s\n", (char*)attr->name);
+                return 1;
+            }
+            if(xstreq(attr->name, "prefix")) {
+                prefix = value;
+            } else if(xstreq(attr->name, "uri")) {
+                uri = value;
+            } else {
+                print_error(
+                        node->line,
+                        "unknown attribute %s\n", (char*)attr->name);
+                return 1;
+            }
+        }
+        attr = attr->next;
+    }
+    
+    if(!prefix) {
+        print_error(node->line, "missing prefix attribute\n");
+        return 1;
+    }
+    if(!uri) {
+        print_error(node->line, "missing uri attribute\n");
+        return 1;
+    }
+    
+    if(dav_get_namespace(context, prefix)) {
+        fprintf(stderr, "Error: namespace prefix '%s' already used\n", prefix);
+        return 1;
+    }
+    
+    return dav_add_namespace(context, prefix, uri);
+}
 
 Repository* get_repository(sstr_t name) {
     if(!name.ptr) {
--- a/dav/config.h	Sun Sep 24 11:31:01 2017 +0200
+++ b/dav/config.h	Sun Sep 24 14:31:42 2017 +0200
@@ -75,6 +75,7 @@
 int load_key(const xmlNode *keynode);
 int load_proxy(DavProxy*, const xmlNode *proxynode, int type);
 sstr_t load_key_file(char *filename);
+int load_namespace(const xmlNode *node);
 
 Repository* repository_new(void);
 
--- a/dav/main.c	Sun Sep 24 11:31:01 2017 +0200
+++ b/dav/main.c	Sun Sep 24 14:31:42 2017 +0200
@@ -1175,6 +1175,10 @@
         propname.name = property;
     } else {
         dav_get_property_namespace_str(ctx, property, &propname.ns, &propname.name);
+        if(!propname.ns || !propname.name) {
+            fprintf(stderr, "Error: unknown namespace prefix\n");
+            return -1;
+        }
     }
     
     DavResource *res = dav_resource_new(sn, path);
--- a/docs/schema/sync.xsd	Sun Sep 24 11:31:01 2017 +0200
+++ b/docs/schema/sync.xsd	Sun Sep 24 14:31:42 2017 +0200
@@ -58,6 +58,8 @@
                         minOccurs="0" type="xs:boolean" />
             <xs:element name="lock-push" default="false"
                         minOccurs="0" type="xs:boolean" />
+            <xs:element name="lock-timeout" default="0"
+                        minOccurs="0" type="xs:integer"/>
         </xs:all>
     </xs:complexType>
     
--- a/libidav/webdav.c	Sun Sep 24 11:31:01 2017 +0200
+++ b/libidav/webdav.c	Sun Sep 24 14:31:42 2017 +0200
@@ -190,7 +190,7 @@
     return NULL;
 }
 
-int dav_add_namespace(DavContext *context, char *prefix, char *name) {
+int dav_add_namespace(DavContext *context, const char *prefix, const char *name) {
     DavNamespace *namespace = malloc(sizeof(DavNamespace));
     if(!namespace) {
         return 1;
@@ -200,7 +200,7 @@
     return ucx_map_cstr_put(context->namespaces, prefix, namespace);
 }
 
-DavNamespace* dav_get_namespace(DavContext *context, char *prefix) {
+DavNamespace* dav_get_namespace(DavContext *context, const char *prefix) {
     return ucx_map_cstr_get(context->namespaces, prefix);
 }
 
--- a/libidav/webdav.h	Sun Sep 24 11:31:01 2017 +0200
+++ b/libidav/webdav.h	Sun Sep 24 14:31:42 2017 +0200
@@ -180,8 +180,8 @@
 void dav_context_add_key(DavContext *context, DavKey *key);
 DavKey* dav_context_get_key(DavContext *context, char *name);
 
-int dav_add_namespace(DavContext *context, char *prefix, char *ns);
-DavNamespace* dav_get_namespace(DavContext *context, char *prefix);
+int dav_add_namespace(DavContext *context, const char *prefix, const char *ns);
+DavNamespace* dav_get_namespace(DavContext *context, const char *prefix);
 
 DavSession* dav_session_new(DavContext *context, char *base_url);
 DavSession* dav_session_new_auth(

mercurial