diff -r 3e55bed345f9 -r a95ee94b9204 libidav/resource.c --- a/libidav/resource.c Sat Sep 07 14:08:43 2013 +0200 +++ b/libidav/resource.c Mon Mar 17 18:42:01 2014 +0100 @@ -34,6 +34,7 @@ #include "utils.h" #include "methods.h" #include "davql.h" +#include "crypto.h" #include "ucx/buffer.h" #include "ucx/utils.h" @@ -42,7 +43,8 @@ #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) DavResource* dav_resource_new(DavSession *sn, char *path) { - char *url = util_concat_path(sn->base_url, path); + int plen = 0; + char *url = util_path_to_url(sn, path); char *href = util_url_path(url); DavResource *res = dav_resource_new_href(sn, href); free(url); @@ -50,11 +52,8 @@ } -DavResource* dav_resource_new_href(DavSession *sn, char *href) { - UcxMempool *mp = sn->mp; - UcxAllocator *a = sn->allocator; - - DavResource *res = ucx_mempool_calloc(mp, 1, sizeof(DavResource)); +DavResource* dav_resource_new_href(DavSession *sn, char *href) { + DavResource *res = ucx_mempool_calloc(sn->mp, 1, sizeof(DavResource)); res->session = sn; // set name, path and href @@ -75,10 +74,20 @@ sstr_t base_href = sstr(util_url_path(res->session->base_url)); sstr_t path = sstrsubs(href, base_href.length - 1); - UcxAllocator *a = res->session->allocator; - res->name = sstrdup_a(a, name).ptr; + UcxAllocator *a = res->session->mp->allocator; + CURL *handle = res->session->handle; + + int nlen = 0; + char *uname = curl_easy_unescape(handle, name.ptr, name.length , &nlen); + int plen = 0; + char *upath = curl_easy_unescape(handle, path.ptr, path.length, &plen); + + res->name = sstrdup_a(a, sstrn(uname, nlen)).ptr; res->href = sstrdup_a(a, href).ptr; - res->path = sstrdup_a(a, path).ptr; + res->path = sstrdup_a(a, sstrn(upath, plen)).ptr; + + curl_free(uname); + curl_free(upath); } DavResourceData* resource_data_new(DavSession *sn) { @@ -88,7 +97,7 @@ if(!data) { return NULL; } - data->properties = ucx_map_new_a(sn->allocator, 32); + data->properties = ucx_map_new_a(sn->mp->allocator, 32); data->set = NULL; data->remove = NULL; data->content = NULL; @@ -101,10 +110,9 @@ if(!val) { return; } - UcxAllocator *a = res->session->allocator; UcxKey key = dav_property_key(ns, name); - sstr_t v = sstrdup_a(a, sstr(val)); + sstr_t v = sstrdup_a(res->session->mp->allocator, sstr(val)); ucx_map_put(((DavResourceData*)res->data)->properties, key, v.ptr); free(key.data); } @@ -189,13 +197,18 @@ } void dav_set_property_ns(DavResource *res, char *ns, char *name, char *value) { - UcxAllocator *a = res->session->allocator; + UcxAllocator *a = res->session->mp->allocator; DavResourceData *data = res->data; - DavProperty *property = a->malloc(a->pool, sizeof(DavProperty)); + DavProperty *property = dav_session_malloc( + res->session, + sizeof(DavProperty)); property->name = sstrdup_a(a, sstr(name)).ptr; property->value = sstrdup_a(a, sstr(value)).ptr; - DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace)); + + DavNamespace *namespace = dav_session_malloc( + res->session, + sizeof(DavNamespace)); namespace->prefix = NULL; namespace->name = sstrdup_a(a, sstr(ns)).ptr; property->ns = namespace; @@ -211,13 +224,18 @@ } void dav_remove_property_ns(DavResource *res, char *ns, char *name) { - UcxAllocator *a = res->session->allocator; DavResourceData *data = res->data; + UcxAllocator *a = res->session->mp->allocator; - DavProperty *property = a->malloc(a->pool, sizeof(DavProperty)); + DavProperty *property = dav_session_malloc( + res->session, + sizeof(DavProperty)); property->name = sstrdup_a(a, sstr(name)).ptr; property->value = NULL; - DavNamespace *namespace = a->malloc(a->pool, sizeof(DavNamespace)); + + DavNamespace *namespace = dav_session_malloc( + res->session, + sizeof(DavNamespace)); namespace->prefix = NULL; namespace->name = sstrdup_a(a, sstr(ns)).ptr; property->ns = namespace; @@ -227,10 +245,24 @@ void dav_set_content(DavResource *res, void *stream, dav_read_func read_func) { - DavResourceData *data = res->data; - data->content = stream; - data->read = read_func; - data->length = 0; + DavSession *sn = res->session; + if((sn->flags & DAV_SESSION_ENCRYPT_FILE) == DAV_SESSION_ENCRYPT_FILE) { + AESEncrypter *enc = aes_encrypter_new(sn->key, stream, read_func); + DavResourceData *data = res->data; + data->content = enc; + data->read = (dav_read_func)aes_read; + data->length = 0; + dav_set_property_ns( + res, + "http://www.uap-core.de/", + "crypto-key", + sn->key->name); + } else { + DavResourceData *data = res->data; + data->content = stream; + data->read = read_func; + data->length = 0; + } } void dav_set_content_data(DavResource *res, char *content, size_t length) { @@ -253,11 +285,7 @@ ucx_map_remove(data->properties, key); } - char *url = util_concat_path(sn->base_url, res->path); - - CURL *handle = sn->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(sn, res->path); UcxBuffer *rqbuf = create_allprop_propfind_request(); UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); @@ -265,13 +293,13 @@ //fwrite(rpbuf->space, 1, rpbuf->size, stdout); //printf("\n"); - CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); + CURLcode ret = do_propfind_request(sn->handle, rqbuf, rpbuf); int status = 0; - curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); + curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &status); if(ret == CURLE_OK) { //printf("response\n%s\n", rpbuf->space); // TODO: use parse_propfind_response() - xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0); + xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, NULL, NULL, 0); if(!doc) { return 1; } @@ -298,16 +326,13 @@ DavSession *sn = res->session; DavResourceData *data = res->data; - char *url = util_concat_path(sn->base_url, res->path); - CURL *handle = res->session->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(sn, res->path);; // store content if(data->content) { - CURLcode ret = do_put_request(handle, data->content, data->read, data->length); + CURLcode ret = do_put_request(sn->handle, data->content, data->read, data->length); int status = 0; - curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status); + curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &status); if(ret == CURLE_OK && (status >= 200 && status < 300)) { res->session->error = 0; // cleanup node data @@ -328,9 +353,9 @@ UcxBuffer *request = create_proppatch_request(data); UcxBuffer *response = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); - CURLcode ret = do_proppatch_request(handle, request, response); + CURLcode ret = do_proppatch_request(sn->handle, request, response); int status = 0; - curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); + curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &status); if(ret == CURLE_OK && status == 207) { //printf("%s\n", response->space); // TODO: parse response @@ -347,10 +372,8 @@ } int dav_get_content(DavResource *res, void *stream, dav_write_func write_fnc) { - char *url = util_concat_path(res->session->base_url, res->path); CURL *handle = res->session->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(res->session, res->path); curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL); @@ -378,10 +401,8 @@ } int dav_delete(DavResource *res) { - char *url = util_concat_path(res->session->base_url, res->path); CURL *handle = res->session->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(res->session, res->path); UcxBuffer *response = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); CURLcode ret = do_delete_request(handle, response); @@ -401,7 +422,7 @@ } int dav_create(DavResource *res) { - char *url = util_concat_path(res->session->base_url, res->path); + //char *url = util_concat_path(res->session->base_url, res->path); char *parent = util_parent_path(res->path); DavSession *sn = res->session; @@ -422,8 +443,7 @@ } CURL *handle = res->session->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(res->session, res->path); free(parent); // create new collection or do an empty put request @@ -455,7 +475,7 @@ if(ret == CURLE_OK && (status >= 200 && status < 300)) { //printf("response\n%s\n", rpbuf->space); // TODO: use parse_propfind_response() - xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0); + xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, NULL, NULL, 0); if(!doc) { return 1; } @@ -481,10 +501,8 @@ int dav_exists(DavResource *res) { DavSession *sn = res->session; - char *url = util_concat_path(sn->base_url, res->path); CURL *handle = sn->handle; - curl_easy_setopt(handle, CURLOPT_URL, url); - free(url); + util_set_url(sn, res->path); CURLcode ret = do_head_request(handle); int status = 0;