# HG changeset patch # User Olaf Wintermann # Date 1542368791 -3600 # Node ID 411bd1098175ce506339e1d125726e5c312e4e88 # Parent 5979ea04175ef8d7ca25fda72d12a43fa1328d9d fixes DavSession creation in dav-sync with enabled encryption diff -r 5979ea04175e -r 411bd1098175 dav/sync.c --- 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); diff -r 5979ea04175e -r 411bd1098175 libidav/session.c --- 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 diff -r 5979ea04175e -r 411bd1098175 libidav/webdav.h --- 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);