fixes DavSession creation in dav-sync with enabled encryption

Fri, 16 Nov 2018 12:46:31 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 Nov 2018 12:46:31 +0100
changeset 497
411bd1098175
parent 496
5979ea04175e
child 498
37af2eac3e6a

fixes DavSession creation in dav-sync with enabled encryption

dav/sync.c file | annotate | diff | comparison | revisions
libidav/session.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Tue Nov 13 23:33:59 2018 +0100
+++ b/dav/sync.c	Fri Nov 16 12:46:31 2018 +0100
@@ -322,13 +322,23 @@
     return ret;
 }
 
-static DavSession* create_session(DavContext *ctx, Repository *repo, char *url) {
+static DavSession* create_session(DavContext *ctx, Repository *repo, char *collection) {
+    int flags = get_repository_flags(repo);
+    char *url = repo->url;
+    DavBool find_collection = TRUE;
+    if((flags & DAV_SESSION_DECRYPT_NAME) != DAV_SESSION_DECRYPT_NAME) {
+        url = util_concat_path(repo->url, collection);
+        find_collection = FALSE;
+    }
     DavSession *sn = dav_session_new_auth(
             ctx,
             url,
             repo->user,
             repo->password);
-    sn->flags = get_repository_flags(repo);
+    if(url != repo->url) {
+        free(url);
+    }
+    sn->flags = flags;
     sn->key = dav_context_get_key(ctx, repo->default_key);
     curl_easy_setopt(sn->handle, CURLOPT_HTTPAUTH, repo->authmethods);
     curl_easy_setopt(sn->handle, CURLOPT_SSLVERSION, repo->ssl_version);
@@ -339,6 +349,18 @@
         curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0);
         curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0);
     }
+    
+    if(find_collection) {
+        DavResource *col = dav_resource_new(sn, collection);
+        dav_exists(col); // exec this to get the href
+        // we actually don't care what the result is
+        // if it doesn't exists, an error will occur later
+        // and we can't handle it here
+        char *newurl = util_concat_path(repo->url, util_resource_name(col->href));
+        dav_session_set_baseurl(sn, newurl);
+        free(newurl);
+    }
+    
     return sn;
 }
 
@@ -427,15 +449,8 @@
     }
     remove_deleted_conflicts(dir, db);
     
-    char *new_url = NULL;
-    if(dir->collection) {
-        new_url = util_concat_path(repo->url, dir->collection);
-    }
-    DavSession *sn = create_session(ctx, repo, new_url ? new_url : repo->url);
+    DavSession *sn = create_session(ctx, repo, dir->collection);
     ucx_mempool_reg_destr(sn->mp, db, (ucx_destructor)destroy_db);
-    if(new_url) {
-        free(new_url);
-    }
     if (cmd_getoption(a, "verbose")) {
         curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
         curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
@@ -1019,15 +1034,8 @@
     }
     remove_deleted_conflicts(dir, db);
     
-    char *new_url = NULL;
-    if(dir->collection) {
-        new_url = util_concat_path(repo->url, dir->collection);
-    }
-    DavSession *sn = create_session(ctx, repo, new_url ? new_url : repo->url);
+    DavSession *sn = create_session(ctx, repo, dir->collection);
     ucx_mempool_reg_destr(sn->mp, db, (ucx_destructor)destroy_db);
-    if(new_url) {
-        free(new_url);
-    }
     if (cmd_getoption(a, "verbose")) {
         curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
         curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
@@ -1364,15 +1372,8 @@
         fprintf(stderr, "Unkown repository %s\n", dir->name);
         return -1;
     }
-    char *new_url = NULL;
-    if(dir->collection) {
-        new_url = util_concat_path(repo->url, dir->collection);
-    }
-    DavSession *sn = create_session(ctx, repo, new_url ? new_url : repo->url);
+    DavSession *sn = create_session(ctx, repo, dir->collection);
     ucx_mempool_reg_destr(sn->mp, db, (ucx_destructor)destroy_db);
-    if(new_url) {
-        free(new_url);
-    }
     if (cmd_getoption(a, "verbose")) {
         curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
         curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
--- a/libidav/session.c	Tue Nov 13 23:33:59 2018 +0100
+++ b/libidav/session.c	Fri Nov 16 12:46:31 2018 +0100
@@ -54,16 +54,9 @@
     sn->errorstr = NULL;
     sn->error = DAV_OK;
     sn->flags = 0;
-    if(url.ptr[url.length - 1] == '/') {
-        sstr_t url = sstrdup_a(sn->mp->allocator, sstr(base_url));
-        sn->base_url = url.ptr;
-    } else {
-        char *url_str = ucx_mempool_malloc(sn->mp, url.length + 2);
-        memcpy(url_str, base_url, url.length);
-        url_str[url.length]     = '/';
-        url_str[url.length + 1] = '\0';
-        sn->base_url = url_str;
-    }
+    
+    dav_session_set_baseurl(sn, base_url);
+    
     sn->handle = curl_easy_init();
     curl_easy_setopt(sn->handle, CURLOPT_FOLLOWLOCATION, 1L);
     
@@ -134,6 +127,24 @@
     }
 }
 
+void dav_session_set_baseurl(DavSession *sn, char *base_url) {
+    if(sn->base_url) {
+        ucx_mempool_free(sn->mp, sn->base_url);
+    }
+    
+    sstr_t url = sstr(base_url);
+    if(url.ptr[url.length - 1] == '/') {
+        sstr_t url = sstrdup_a(sn->mp->allocator, sstr(base_url));
+        sn->base_url = url.ptr;
+    } else {
+        char *url_str = ucx_mempool_malloc(sn->mp, url.length + 2);
+        memcpy(url_str, base_url, url.length);
+        url_str[url.length]     = '/';
+        url_str[url.length + 1] = '\0';
+        sn->base_url = url_str;
+    }
+}
+
 void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags) {
     sn->key = key;
     // TODO: review sanity
--- a/libidav/webdav.h	Tue Nov 13 23:33:59 2018 +0100
+++ b/libidav/webdav.h	Fri Nov 16 12:46:31 2018 +0100
@@ -228,6 +228,7 @@
         char *user,
         char *password);
 void dav_session_set_auth(DavSession *sn, char *user, char *password);
+void dav_session_set_baseurl(DavSession *sn, char *base_url);
 void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags);
 
 void dav_session_set_authcallback(DavSession *sn, dav_auth_func func, void *userdata);

mercurial