63 url_str[url.length + 1] = '\0'; |
63 url_str[url.length + 1] = '\0'; |
64 sn->base_url = url_str; |
64 sn->base_url = url_str; |
65 } |
65 } |
66 sn->handle = curl_easy_init(); |
66 sn->handle = curl_easy_init(); |
67 curl_easy_setopt(sn->handle, CURLOPT_FOLLOWLOCATION, 1L); |
67 curl_easy_setopt(sn->handle, CURLOPT_FOLLOWLOCATION, 1L); |
|
68 |
|
69 // create lock manager |
|
70 DavLockManager *locks = ucx_mempool_malloc(sn->mp, sizeof(DavLockManager)); |
|
71 locks->resource_locks = ucx_map_new_a(sn->mp->allocator, 16); |
|
72 locks->collection_locks = NULL; |
|
73 sn->locks = locks; |
68 |
74 |
69 // set proxy |
75 // set proxy |
70 DavProxy *proxy = sstrprefix(url, S("https")) ? context->https_proxy |
76 DavProxy *proxy = sstrprefix(url, S("https")) ? context->https_proxy |
71 : context->http_proxy; |
77 : context->http_proxy; |
72 |
78 |
357 if(!elm) { |
363 if(!elm) { |
358 href = sstrdup_a(sn->mp->allocator, href); |
364 href = sstrdup_a(sn->mp->allocator, href); |
359 ucx_map_sstr_put(sn->pathcache, path, href.ptr); |
365 ucx_map_sstr_put(sn->pathcache, path, href.ptr); |
360 } |
366 } |
361 } |
367 } |
|
368 |
|
369 |
|
370 DavLock* dav_create_lock(DavSession *sn, char *token, char *timeout) { |
|
371 DavLock *lock = dav_session_malloc(sn, sizeof(DavLock)); |
|
372 lock->path = NULL; |
|
373 lock->token = dav_session_strdup(sn, token); |
|
374 |
|
375 // TODO: timeout |
|
376 |
|
377 return lock; |
|
378 } |
|
379 |
|
380 void dav_destroy_lock(DavSession *sn, DavLock *lock) { |
|
381 dav_session_free(sn, lock->token); |
|
382 if(lock->path) { |
|
383 dav_session_free(sn, lock->path); |
|
384 } |
|
385 dav_session_free(sn, lock); |
|
386 } |
|
387 |
|
388 int dav_add_resource_lock(DavSession *sn, char *path, DavLock *lock) { |
|
389 DavLockManager *locks = sn->locks; |
|
390 if(ucx_map_cstr_get(locks->resource_locks, path)) { |
|
391 return -1; |
|
392 } |
|
393 |
|
394 ucx_map_cstr_put(locks->resource_locks, path, lock); |
|
395 return 0; |
|
396 } |
|
397 |
|
398 static void insert_lock(DavSession *sn, UcxList *elm, UcxList *newelm) { |
|
399 UcxList *next = elm->next; |
|
400 if(next) { |
|
401 next->prev = newelm; |
|
402 newelm->next = next; |
|
403 } |
|
404 newelm->prev = elm; |
|
405 elm->next = newelm; |
|
406 } |
|
407 |
|
408 int dav_add_collection_lock(DavSession *sn, char *path, DavLock *lock) { |
|
409 DavLockManager *locks = sn->locks; |
|
410 if(!locks->collection_locks) { |
|
411 locks->collection_locks = ucx_list_append_a( |
|
412 sn->mp->allocator, |
|
413 NULL, |
|
414 lock); |
|
415 lock->path = dav_session_strdup(sn, path); |
|
416 return 0; |
|
417 } |
|
418 |
|
419 UcxList *elm = locks->collection_locks; |
|
420 for(;;) { |
|
421 DavLock *l = elm->data; |
|
422 int cmp = strcmp(path, l->path); |
|
423 if(cmp > 0) { |
|
424 UcxList *newelm = ucx_list_append_a(sn->mp->allocator, NULL, lock); |
|
425 lock->path = dav_session_strdup(sn, path); |
|
426 insert_lock(sn, elm, newelm); |
|
427 } else if(cmp == 0) { |
|
428 return -1; |
|
429 } |
|
430 |
|
431 if(elm->next) { |
|
432 elm = elm->next; |
|
433 } else { |
|
434 UcxList *newelm = ucx_list_append_a(sn->mp->allocator, NULL, lock); |
|
435 lock->path = dav_session_strdup(sn, path); |
|
436 ucx_list_concat(elm, newelm); |
|
437 break; |
|
438 } |
|
439 } |
|
440 |
|
441 return 0; |
|
442 } |
|
443 |
|
444 DavLock* dav_get_lock(DavSession *sn, char *path) { |
|
445 DavLockManager *locks = sn->locks; |
|
446 |
|
447 DavLock *lock = ucx_map_cstr_get(locks->resource_locks, path); |
|
448 if(lock) { |
|
449 return lock; |
|
450 } |
|
451 |
|
452 sstr_t p = sstr(path); |
|
453 UCX_FOREACH(elm, locks->collection_locks) { |
|
454 DavLock *cl = elm->data; |
|
455 int cmd = strcmp(path, cl->path); |
|
456 if(cmd == 0) { |
|
457 return cl; |
|
458 } else if(sstrprefix(p, sstr(cl->path))) { |
|
459 return cl; |
|
460 } else if(cmd > 0) { |
|
461 break; |
|
462 } |
|
463 } |
|
464 |
|
465 return NULL; |
|
466 } |
|
467 |
|
468 void dav_remove_lock(DavSession *sn, char *path, DavLock *lock) { |
|
469 DavLockManager *locks = sn->locks; |
|
470 |
|
471 if(ucx_map_cstr_remove(locks->resource_locks, path)) { |
|
472 return; |
|
473 } |
|
474 |
|
475 UcxList *rm = NULL; |
|
476 UCX_FOREACH(elm, locks->collection_locks) { |
|
477 DavLock *cl = elm->data; |
|
478 if(cl == lock) { |
|
479 rm = elm; |
|
480 break; |
|
481 } |
|
482 } |
|
483 |
|
484 if(rm) { |
|
485 locks->collection_locks = ucx_list_remove_a( |
|
486 sn->mp->allocator, |
|
487 locks->collection_locks, |
|
488 rm); |
|
489 } |
|
490 } |