added proxy config

Mon, 02 Sep 2013 10:31:29 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 02 Sep 2013 10:31:29 +0200
changeset 36
c8755c87ce7f
parent 35
ad0c9dacd7e3
child 37
1c81083a3e46

added proxy config

dav/config.c file | annotate | diff | comparison | revisions
dav/config.h file | annotate | diff | comparison | revisions
dav/main.c 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	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;
+}
--- a/dav/config.h	Fri Aug 30 12:48:15 2013 +0200
+++ b/dav/config.h	Mon Sep 02 10:31:29 2013 +0200
@@ -37,6 +37,10 @@
 
 typedef struct Repository Repository;
 typedef struct Key        Key;
+typedef struct Proxy      Proxy;
+
+#define HTTP_PROXY 1
+#define HTTPS_PROXY 2
 
 enum key_type {
     KEY_AES128 = 0,
@@ -63,14 +67,24 @@
     void    *data;
     size_t  length;
 };
+
+struct Proxy {
+    char *url;
+    char *user;
+    char *password;
+    char *no;
+};
     
 void load_config();
 void load_repository(xmlNode *reponode);
 void load_key(xmlNode *keynode);
+void load_proxy(xmlNode *proxynode, int type);
 sstr_t load_key_file(char *filename);
 
 Repository* get_repository(char *name);
 Key* get_key(char *name);
+Proxy* get_http_proxy();
+Proxy* get_https_proxy();
 
 #ifdef	__cplusplus
 }
--- a/dav/main.c	Fri Aug 30 12:48:15 2013 +0200
+++ b/dav/main.c	Mon Sep 02 10:31:29 2013 +0200
@@ -56,6 +56,9 @@
     ctx = dav_context_new();
     dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
     
+    memcpy(ctx->http_proxy, get_http_proxy(), sizeof(Proxy));
+    memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy));
+    
     if(argc < 2) {
         fprintf(stderr, "Missing command\n");
         print_usage(argv[0]);
--- a/libidav/webdav.c	Fri Aug 30 12:48:15 2013 +0200
+++ b/libidav/webdav.c	Mon Sep 02 10:31:29 2013 +0200
@@ -46,10 +46,9 @@
         return NULL;
     }
     context->sessions = NULL;
+    context->http_proxy = calloc(1, sizeof(DavProxy));
+    context->https_proxy = calloc(1, sizeof(DavProxy));
     context->namespaces = ucx_map_new(16);
-    context->http_proxy = NULL;
-    context->https_proxy = NULL;
-    context->no_proxy = NULL;
     if(!context->namespaces) {
         free(context);
         return NULL;
@@ -77,6 +76,8 @@
     UCX_FOREACH(elm, ctx->sessions) {
         dav_session_destroy(elm->data);
     }
+    free(ctx->http_proxy);
+    free(ctx->https_proxy);
     
     UcxMapIterator i = ucx_map_iterator(ctx->namespaces);
     UcxKey k;
@@ -149,23 +150,29 @@
     sn->handle = curl_easy_init();
     //curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
     //curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
-    
+
     // set proxy
-    if(sstrprefix(url, S("https"))) {
-        if(context->https_proxy) {
-            //printf("use https_proxy: %s\n", context->https_proxy);
-            curl_easy_setopt(sn->handle, CURLOPT_PROXY, context->https_proxy);
+    DavProxy *proxy = sstrprefix(url, S("https")) ? context->https_proxy
+                                                  : context->http_proxy;
+
+    if (proxy->url) {
+        curl_easy_setopt(sn->handle, CURLOPT_PROXY, proxy->url);
+        if (proxy->username) {
+            curl_easy_setopt(sn->handle, CURLOPT_PROXYUSERNAME,
+                proxy->username);
+            if (proxy->password) {
+                curl_easy_setopt(sn->handle, CURLOPT_PROXYPASSWORD,
+                    proxy->password);
+            } else {
+                // TODO: prompt
+            }
         }
-    } else {
-        if(context->http_proxy) {
-            //printf("use http_proxy: %s\n", context->http_proxy);
-            curl_easy_setopt(sn->handle, CURLOPT_PROXY, context->http_proxy);
+        if(proxy->no_proxy) {
+            curl_easy_setopt(sn->handle, CURLOPT_NOPROXY,
+                proxy->no_proxy);
         }
     }
-    if(context->no_proxy) {
-        //printf("use no_proxy: %s\n", context->no_proxy);
-        curl_easy_setopt(sn->handle, CURLOPT_NOPROXY, context->no_proxy);
-    }
+    
     // set url
     curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
     
--- a/libidav/webdav.h	Fri Aug 30 12:48:15 2013 +0200
+++ b/libidav/webdav.h	Mon Sep 02 10:31:29 2013 +0200
@@ -42,6 +42,7 @@
 #endif
 
 typedef struct DavContext    DavContext;
+typedef struct DavProxy      DavProxy;
 typedef struct DavSession    DavSession;
 typedef struct DavResource   DavResource;
 typedef struct DavRequest    DavRequest;
@@ -100,9 +101,15 @@
 struct DavContext {
     UcxMap  *namespaces;
     UcxList *sessions;
-    char    *http_proxy;
-    char    *https_proxy;
-    char    *no_proxy;
+    DavProxy *http_proxy;
+    DavProxy *https_proxy;
+};
+
+struct DavProxy {
+    char *url;
+    char *username;
+    char *password;
+    char *no_proxy;
 };
 
 struct DavProperty {

mercurial