libidav/config.c

changeset 49
2f71f4ee247a
parent 18
af411868ab9b
--- a/libidav/config.c	Thu Oct 03 18:52:51 2024 +0200
+++ b/libidav/config.c	Sun Oct 06 18:18:04 2024 +0200
@@ -98,6 +98,22 @@
 }
 
 
+DavConfig* dav_config_new(xmlDoc *doc) {
+    CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
+    DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
+    memset(config, 0, sizeof(DavConfig));
+    config->mp = cfg_mp;
+    
+    if(!doc) {
+        doc = xmlNewDoc(BAD_CAST "1.0");
+        xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
+        xmlDocSetRootElement(doc, root);
+    }
+    config->doc = doc;
+    
+    return config;
+}
+
 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error) {
     xmlDoc *doc = xmlReadMemory(xmlfilecontent.ptr, xmlfilecontent.length, NULL, NULL, 0);
     if(!doc) {
@@ -107,12 +123,9 @@
         return NULL;
     }
     
-    CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
+    DavConfig *config = dav_config_new(doc);
+    CxMempool *cfg_mp = config->mp;
     cxMempoolRegister(cfg_mp, doc, (cx_destructor_func)xmlFreeDoc);
-    DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
-    memset(config, 0, sizeof(DavConfig));
-    config->mp = cfg_mp;
-    config->doc = doc;
     
     DavCfgRepository *repos_begin = NULL;
     DavCfgRepository *repos_end = NULL;
@@ -473,10 +486,14 @@
 
 void dav_repository_set_auth(DavConfig *config, DavCfgRepository *repo, cxstring user, cxstring password) {
     const CxAllocator *a = config->mp->allocator;
-    repo->user.value = cx_strdup_a(a, user);
-    char *pwenc = util_base64encode(password.ptr, password.length);
-    repo->password.value = cx_strdup_a(a, cx_str(pwenc));
-    free(pwenc);
+    if(user.length > 0) {
+        repo->user.value = cx_strdup_a(a, user);
+    }
+    if(password.length > 0) {
+        char *pwenc = util_base64encode(password.ptr, password.length);
+        repo->password.value = cx_strdup_a(a, cx_str(pwenc));
+        free(pwenc);
+    }
 }
 
 cxmutstr dav_repository_get_decodedpassword(DavCfgRepository *repo) {
@@ -723,6 +740,9 @@
 
 
 DavCfgRepository* dav_config_get_repository(DavConfig *config, cxstring name) {
+    if(!config) {
+        return NULL;
+    }
     DavCfgRepository *repo = config->repositories;
     while(repo) {
         if(!cx_strcmp(cx_strcast(repo->name.value), name)) {
@@ -818,3 +838,12 @@
     }
     return 0;
 }
+
+int dav_config_register_namespaces(DavConfig *config, DavContext *ctx) {
+    DavCfgNamespace *ns = config->namespaces;
+    while(ns) {
+        dav_add_namespace(ctx, ns->prefix.ptr, ns->uri.ptr);
+        ns = ns->next;
+    }
+    return 0;
+}

mercurial