libidav/config.c

changeset 49
2f71f4ee247a
parent 18
af411868ab9b
equal deleted inserted replaced
48:ae61523bce20 49:2f71f4ee247a
96 char *value = util_xml_get_text(node); 96 char *value = util_xml_get_text(node);
97 cbool->value = util_getboolean(value); 97 cbool->value = util_getboolean(value);
98 } 98 }
99 99
100 100
101 DavConfig* dav_config_new(xmlDoc *doc) {
102 CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
103 DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
104 memset(config, 0, sizeof(DavConfig));
105 config->mp = cfg_mp;
106
107 if(!doc) {
108 doc = xmlNewDoc(BAD_CAST "1.0");
109 xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
110 xmlDocSetRootElement(doc, root);
111 }
112 config->doc = doc;
113
114 return config;
115 }
116
101 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error) { 117 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error) {
102 xmlDoc *doc = xmlReadMemory(xmlfilecontent.ptr, xmlfilecontent.length, NULL, NULL, 0); 118 xmlDoc *doc = xmlReadMemory(xmlfilecontent.ptr, xmlfilecontent.length, NULL, NULL, 0);
103 if(!doc) { 119 if(!doc) {
104 if(error) { 120 if(error) {
105 *error = DAV_CONFIG_ERROR_XML; 121 *error = DAV_CONFIG_ERROR_XML;
106 } 122 }
107 return NULL; 123 return NULL;
108 } 124 }
109 125
110 CxMempool *cfg_mp = cxMempoolCreate(128, NULL); 126 DavConfig *config = dav_config_new(doc);
127 CxMempool *cfg_mp = config->mp;
111 cxMempoolRegister(cfg_mp, doc, (cx_destructor_func)xmlFreeDoc); 128 cxMempoolRegister(cfg_mp, doc, (cx_destructor_func)xmlFreeDoc);
112 DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
113 memset(config, 0, sizeof(DavConfig));
114 config->mp = cfg_mp;
115 config->doc = doc;
116 129
117 DavCfgRepository *repos_begin = NULL; 130 DavCfgRepository *repos_begin = NULL;
118 DavCfgRepository *repos_end = NULL; 131 DavCfgRepository *repos_end = NULL;
119 DavCfgKey *keys_begin = NULL; 132 DavCfgKey *keys_begin = NULL;
120 DavCfgKey *keys_end = NULL; 133 DavCfgKey *keys_end = NULL;
471 repo->url.value = cx_strdup_a(config->mp->allocator, newurl); 484 repo->url.value = cx_strdup_a(config->mp->allocator, newurl);
472 } 485 }
473 486
474 void dav_repository_set_auth(DavConfig *config, DavCfgRepository *repo, cxstring user, cxstring password) { 487 void dav_repository_set_auth(DavConfig *config, DavCfgRepository *repo, cxstring user, cxstring password) {
475 const CxAllocator *a = config->mp->allocator; 488 const CxAllocator *a = config->mp->allocator;
476 repo->user.value = cx_strdup_a(a, user); 489 if(user.length > 0) {
477 char *pwenc = util_base64encode(password.ptr, password.length); 490 repo->user.value = cx_strdup_a(a, user);
478 repo->password.value = cx_strdup_a(a, cx_str(pwenc)); 491 }
479 free(pwenc); 492 if(password.length > 0) {
493 char *pwenc = util_base64encode(password.ptr, password.length);
494 repo->password.value = cx_strdup_a(a, cx_str(pwenc));
495 free(pwenc);
496 }
480 } 497 }
481 498
482 cxmutstr dav_repository_get_decodedpassword(DavCfgRepository *repo) { 499 cxmutstr dav_repository_get_decodedpassword(DavCfgRepository *repo) {
483 cxmutstr pw = { NULL, 0 }; 500 cxmutstr pw = { NULL, 0 };
484 if(repo->password.value.ptr) { 501 if(repo->password.value.ptr) {
721 738
722 739
723 740
724 741
725 DavCfgRepository* dav_config_get_repository(DavConfig *config, cxstring name) { 742 DavCfgRepository* dav_config_get_repository(DavConfig *config, cxstring name) {
743 if(!config) {
744 return NULL;
745 }
726 DavCfgRepository *repo = config->repositories; 746 DavCfgRepository *repo = config->repositories;
727 while(repo) { 747 while(repo) {
728 if(!cx_strcmp(cx_strcast(repo->name.value), name)) { 748 if(!cx_strcmp(cx_strcast(repo->name.value), name)) {
729 return repo; 749 return repo;
730 } 750 }
816 836
817 dav_context_add_key(ctx, davkey); 837 dav_context_add_key(ctx, davkey);
818 } 838 }
819 return 0; 839 return 0;
820 } 840 }
841
842 int dav_config_register_namespaces(DavConfig *config, DavContext *ctx) {
843 DavCfgNamespace *ns = config->namespaces;
844 while(ns) {
845 dav_add_namespace(ctx, ns->prefix.ptr, ns->uri.ptr);
846 ns = ns->next;
847 }
848 return 0;
849 }

mercurial