dav/config.c

changeset 36
c8755c87ce7f
parent 33
0bbbb0341606
child 38
b855f76e965b
--- a/dav/config.c	Fri Aug 30 12:48:15 2013 +0200
+++ b/dav/config.c	Mon Sep 02 10:31:29 2013 +0200
@@ -47,6 +47,8 @@
 
 static UcxMap *repos;
 static UcxMap *keys;
+static Proxy  *http_proxy;
+static Proxy  *https_proxy;
 
 int check_config_dir() {
     char *file = util_concat_path(ENV_HOME, ".dav");
@@ -61,8 +63,11 @@
 }
 
 void load_config() {
+    // TODO: free the config somewhere
     repos = ucx_map_new(16);
     keys = ucx_map_new(16);
+    http_proxy = calloc(1, sizeof(Proxy));
+    https_proxy = calloc(1, sizeof(Proxy));
     if(check_config_dir()) {
         return;
     }
@@ -88,6 +93,10 @@
                 load_repository(node);
             } else if(xstreq(node->name, "key")) {
                 load_key(node);
+            } else if (xstreq(node->name, "http-proxy")) {
+                load_proxy(node, HTTP_PROXY);
+            } else if (xstreq(node->name, "https-proxy")) {
+                load_proxy(node, HTTPS_PROXY);
             }
         }
         node = node->next;
@@ -146,6 +155,44 @@
     ucx_map_cstr_put(repos, repo->name, repo);
 }
 
+void load_proxy(xmlNode *proxynode, int type) {
+    Proxy *proxy;
+    const char *stype;
+    if (type == HTTPS_PROXY) {
+        proxy = https_proxy;
+        stype = "https";
+    } else if (type == HTTP_PROXY) {
+        proxy = http_proxy;
+        stype = "http";
+    }
+    
+    xmlNode *node = proxynode->children;
+    while(node) {
+        if(node->type == XML_ELEMENT_NODE) {
+            char *value = util_xml_get_text(node);
+            if(!value) {
+                // next
+            } else if(xstreq(node->name, "url")) {
+                proxy->url = strdup(value);
+            } else if(xstreq(node->name, "user")) {
+                proxy->user = strdup(value);
+            } else if(xstreq(node->name, "password")) {
+                proxy->password = util_base64decode(value);
+            } else if(xstreq(node->name, "no")) {
+                proxy->no = strdup(value);
+            }
+        }
+        node = node->next;
+    }
+    
+    if(!proxy->url) {
+        fprintf(stderr,
+            "Cannot load config.xml: missing url for %s proxy.\n", stype);
+        fprintf(stderr, "Abort.\n");
+        exit(-1);
+    }
+}
+
 void load_key(xmlNode *keynode) {
     xmlNode *node = keynode->children;
     Key *key = calloc(1, sizeof(Key));
@@ -238,3 +285,11 @@
     }
     return ucx_map_cstr_get(keys, name);
 }
+
+Proxy* get_http_proxy() {
+    return http_proxy;
+}
+
+Proxy* get_https_proxy() {
+    return https_proxy;
+}

mercurial