libidav/session.c

changeset 18
af411868ab9b
parent 2
fbdfaacc4182
--- a/libidav/session.c	Wed Jan 31 12:55:11 2024 +0100
+++ b/libidav/session.c	Tue Feb 06 14:17:22 2024 +0100
@@ -50,7 +50,7 @@
     }
     DavSession *sn = malloc(sizeof(DavSession));
     memset(sn, 0, sizeof(DavSession));
-    sn->mp = cxMempoolCreate(DAV_SESSION_MEMPOOL_SIZE, NULL);
+    sn->mp = cxBasicMempoolCreate(DAV_SESSION_MEMPOOL_SIZE);
     sn->pathcache = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, DAV_PATH_CACHE_SIZE);
     sn->key = NULL;
     sn->errorstr = NULL;
@@ -94,7 +94,7 @@
     curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
     
     // add to context
-    cxListAdd(context->sessions, sn);
+    dav_context_add_session(context, sn);
     sn->context = context;
     
     return sn;
@@ -114,6 +114,35 @@
     return sn;
 }
 
+DavSession* dav_session_clone(DavSession *sn) {
+    CURL *newhandle = curl_easy_duphandle(sn->handle);
+
+    DavSession *newsn = malloc(sizeof(DavSession));
+    memset(newsn, 0, sizeof(DavSession));
+    newsn->mp = cxMempoolCreate(DAV_SESSION_MEMPOOL_SIZE, NULL);
+    newsn->pathcache = cxHashMapCreate(sn->mp->allocator, CX_STORE_POINTERS, DAV_PATH_CACHE_SIZE);
+    newsn->key = sn->key;
+    newsn->errorstr = NULL;
+    newsn->error = DAV_OK;
+    newsn->flags = 0;
+
+    newsn->handle = newhandle;
+
+    newsn->base_url = cx_strdup_a(newsn->mp->allocator, cx_str(sn->base_url)).ptr;
+    newsn->auth_prompt = sn->auth_prompt;
+    newsn->authprompt_userdata = sn->authprompt_userdata;
+    newsn->logfunc = sn->logfunc;
+    newsn->get_progress = sn->get_progress;
+    newsn->put_progress = sn->put_progress;
+    newsn->progress_userdata = sn->progress_userdata;
+
+    // add to context
+    dav_context_add_session(sn->context, newsn);
+    newsn->context = sn->context;
+
+    return newsn;
+}
+
 void dav_session_set_auth(DavSession *sn, const char *user, const char *password) {
     if(user && password) {
         dav_session_set_auth_s(sn, cx_str(user), cx_str(password));
@@ -302,12 +331,8 @@
 
 void dav_session_destroy(DavSession *sn) { 
     // remove session from context
-    CxList *sessions = sn->context->sessions;
-    ssize_t i = cxListFind(sessions, sn);
-    if(i >= 0) {
-        cxListRemove(sessions, i);
-    } else {
-        printf("Error: session not found in ctx->sessions\n");
+    if (dav_context_remove_session(sn->context, sn)) {
+        fprintf(stderr, "Error: session not found in ctx->sessions\n");
         dav_session_destructor(sn);
     }
 }

mercurial