libidav/webdav.c

changeset 40
a95ee94b9204
parent 39
3e55bed345f9
child 41
1c598ee0d3d9
equal deleted inserted replaced
39:3e55bed345f9 40:a95ee94b9204
51 context->namespaces = ucx_map_new(16); 51 context->namespaces = ucx_map_new(16);
52 if(!context->namespaces) { 52 if(!context->namespaces) {
53 free(context); 53 free(context);
54 return NULL; 54 return NULL;
55 } 55 }
56 context->keys = ucx_map_new(16);
56 DavNamespace *davns = malloc(sizeof(DavNamespace)); 57 DavNamespace *davns = malloc(sizeof(DavNamespace));
57 if(!davns) { 58 if(!davns) {
58 ucx_map_free(context->namespaces); 59 ucx_map_free(context->namespaces);
59 free(context); 60 free(context);
60 return NULL; 61 return NULL;
83 UcxKey k; 84 UcxKey k;
84 DavNamespace *ns; 85 DavNamespace *ns;
85 // TODO: free map elements 86 // TODO: free map elements
86 ucx_map_free(ctx->namespaces); 87 ucx_map_free(ctx->namespaces);
87 free(ctx); 88 free(ctx);
89 }
90
91 void dav_context_add_key(DavContext *context, DavKey *key) {
92 ucx_map_cstr_put(context->keys, key->name, key);
93 }
94
95 DavKey* dav_context_get_key(DavContext *context, char *name) {
96 return ucx_map_cstr_get(context->keys, name);
88 } 97 }
89 98
90 int dav_add_namespace(DavContext *context, char *prefix, char *name) { 99 int dav_add_namespace(DavContext *context, char *prefix, char *name) {
91 DavNamespace *namespace = malloc(sizeof(DavNamespace)); 100 DavNamespace *namespace = malloc(sizeof(DavNamespace));
92 if(!namespace) { 101 if(!namespace) {
133 sstr_t url = sstr(base_url); 142 sstr_t url = sstr(base_url);
134 if(url.length == 0) { 143 if(url.length == 0) {
135 return NULL; 144 return NULL;
136 } 145 }
137 DavSession *sn = malloc(sizeof(DavSession)); 146 DavSession *sn = malloc(sizeof(DavSession));
147 sn->key = NULL;
138 sn->errorstr = NULL; 148 sn->errorstr = NULL;
139 sn->error = DAV_OK; 149 sn->error = DAV_OK;
140 sn->flags = 0; 150 sn->flags = 0;
141 if(url.ptr[url.length - 1] == '/') { 151 if(url.ptr[url.length - 1] == '/') {
142 sn->base_url = strdup(base_url); 152 sn->base_url = strdup(base_url); // TODO: mempool
143 } else { 153 } else {
144 char *url_str = malloc(url.length + 2); 154 char *url_str = malloc(url.length + 2);
145 memcpy(url_str, base_url, url.length); 155 memcpy(url_str, base_url, url.length);
146 url_str[url.length] = '/'; 156 url_str[url.length] = '/';
147 url_str[url.length + 1] = '\0'; 157 url_str[url.length + 1] = '\0';
176 186
177 // set url 187 // set url
178 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url); 188 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
179 189
180 sn->mp = ucx_mempool_new(1024); 190 sn->mp = ucx_mempool_new(1024);
181 sn->allocator = ucx_mempool_allocator(sn->mp);
182 191
183 context->sessions = ucx_list_append(context->sessions, sn); 192 context->sessions = ucx_list_append(context->sessions, sn);
184 193
185 return sn; 194 return sn;
186 } 195 }
248 free(sn); 257 free(sn);
249 } 258 }
250 259
251 260
252 void* dav_session_malloc(DavSession *sn, size_t size) { 261 void* dav_session_malloc(DavSession *sn, size_t size) {
253 UcxAllocator *a = sn->allocator; 262 return ucx_mempool_malloc(sn->mp, size);
254 return a->malloc(a->pool, size);
255 } 263 }
256 264
257 void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size) { 265 void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size) {
258 UcxAllocator *a = sn->allocator; 266 return ucx_mempool_calloc(sn->mp, nelm, size);
259 return a->calloc(a->pool, nelm, size);
260 } 267 }
261 268
262 void* dav_session_realloc(DavSession *sn, void *ptr, size_t size) { 269 void* dav_session_realloc(DavSession *sn, void *ptr, size_t size) {
263 UcxAllocator *a = sn->allocator; 270 return ucx_mempool_realloc(sn->mp, ptr, size);
264 return a->realloc(a->pool, ptr, size);
265 } 271 }
266 272
267 void dav_session_free(DavSession *sn, void *ptr) { 273 void dav_session_free(DavSession *sn, void *ptr) {
268 UcxAllocator *a = sn->allocator; 274 ucx_mempool_free(sn->mp, ptr);
269 a->free(a->pool, ptr); 275 }
270 } 276
271 277
272 278 DavResource* dav_get(DavSession *sn, char *path, char *properties) {
273 DavResource* dav_get(DavSession *sn, char *path, char *properties) {
274 char *url = util_concat_path(sn->base_url, path);
275
276 CURL *handle = sn->handle; 279 CURL *handle = sn->handle;
277 curl_easy_setopt(handle, CURLOPT_URL, url); 280 util_set_url(sn, path);
278 free(url);
279 281
280 UcxList *proplist = NULL; 282 UcxList *proplist = NULL;
281 if(properties) { 283 if(properties) {
282 proplist = parse_properties_string(sn->context, sstr(properties)); 284 proplist = parse_properties_string(sn->context, sstr(properties));
283 } 285 }
300 } 302 }
301 return resource; 303 return resource;
302 } 304 }
303 305
304 DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path, DavQOp *cond, size_t len) { 306 DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path, DavQOp *cond, size_t len) {
305 char *url = util_concat_path(sn->base_url, path);
306 CURL *handle = sn->handle; 307 CURL *handle = sn->handle;
307 curl_easy_setopt(handle, CURLOPT_URL, url); 308 util_set_url(sn, path);
308 free(url);
309 309
310 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); 310 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
311 DavResource *resource = root; 311 DavResource *resource = root;
312 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); 312 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
313 int status = 0; 313 int status = 0;
332 children = children->next; 332 children = children->next;
333 } 333 }
334 return stack; 334 return stack;
335 } 335 }
336 336
337 DavResource* dav_get2(DavSession *sn, DavGetQuery *query) { 337 DavResource* dav_query_get(DavSession *sn, DavGetQuery *query) {
338 char *path; 338 char *path;
339 int depth = 0; 339 int depth = 0;
340 if(parse_path_query(query->from, &path, &depth)) { 340 if(parse_path_query(query->from, &path, &depth)) {
341 sn->error = DAV_ERROR; 341 sn->error = DAV_ERROR;
342 return NULL; 342 return NULL;
358 358
359 DavResource *resource = dav_propfind(sn, NULL, rqbuf, path, query->condition, query->condlen); 359 DavResource *resource = dav_propfind(sn, NULL, rqbuf, path, query->condition, query->condlen);
360 free(path); 360 free(path);
361 int error = 0; 361 int error = 0;
362 if(resource && depth == -1) { 362 if(resource && depth == -1) {
363 UcxList *stack = NULL; // stack with davResource* elements 363 UcxList *stack = NULL; // stack with DavResource* elements
364 stack = propfind_stack_push(stack, resource->children); 364 stack = propfind_stack_push(stack, resource->children);
365 while(stack) { 365 while(stack) {
366 DavResource *sr = stack->data; // get first element from the stack 366 DavResource *sr = stack->data; // get first element from the stack
367 stack = ucx_list_remove(stack, stack); // remove first element 367 stack = ucx_list_remove(stack, stack); // remove first element
368 // do propfind request for sr 368 // do propfind request for sr
414 DavQuery q = dav_ql_parse(query, ap); 414 DavQuery q = dav_ql_parse(query, ap);
415 va_end(ap); 415 va_end(ap);
416 DavResource *res = NULL; 416 DavResource *res = NULL;
417 switch(q.command) { 417 switch(q.command) {
418 case DAV_QUERY_GET: { 418 case DAV_QUERY_GET: {
419 res = dav_get2(sn, q.command_data); 419 res = dav_query_get(sn, q.command_data);
420 free_get_query(q.command_data); 420 free_get_query(q.command_data);
421 break; 421 break;
422 } 422 }
423 } 423 }
424 return res; 424 return res;

mercurial