libidav/session.c

branch
dav-2
changeset 914
42c6b071b231
parent 893
38800d479cd4
equal deleted inserted replaced
913:8e2727fc4e07 914:42c6b071b231
36 36
37 #include "utils.h" 37 #include "utils.h"
38 #include "session.h" 38 #include "session.h"
39 #include "resource.h" 39 #include "resource.h"
40 #include "methods.h" 40 #include "methods.h"
41 #include "atomic.h"
41 42
42 DavSession* dav_session_new(DavContext *context, char *base_url) { 43 DavSession* dav_session_new(DavContext *context, char *base_url) {
43 if(!base_url) { 44 if(!base_url) {
44 return NULL; 45 return NULL;
45 } 46 }
93 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url); 94 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
94 95
95 // add to context 96 // add to context
96 dav_context_add_session(context, sn); 97 dav_context_add_session(context, sn);
97 sn->context = context; 98 sn->context = context;
99 dav_context_ref(context);
100
101 sn->ref = 1;
98 102
99 return sn; 103 return sn;
100 } 104 }
101 105
102 DavSession* dav_session_new_auth( 106 DavSession* dav_session_new_auth(
136 newsn->progress_userdata = sn->progress_userdata; 140 newsn->progress_userdata = sn->progress_userdata;
137 141
138 // add to context 142 // add to context
139 dav_context_add_session(sn->context, newsn); 143 dav_context_add_session(sn->context, newsn);
140 newsn->context = sn->context; 144 newsn->context = sn->context;
145 dav_context_ref(sn->context);
146
147 newsn->ref = 1;
141 148
142 return newsn; 149 return newsn;
150 }
151
152 void dav_session_ref(DavSession *sn) {
153 dav_atomic_inc32(&sn->ref);
154 }
155
156 void dav_session_unref(DavSession *sn) {
157 if(dav_atomic_dec32(&sn->ref) == 0) {
158 dav_session_destroy(sn);
159 }
143 } 160 }
144 161
145 void dav_session_set_auth(DavSession *sn, const char *user, const char *password) { 162 void dav_session_set_auth(DavSession *sn, const char *user, const char *password) {
146 if(user && password) { 163 if(user && password) {
147 dav_session_set_auth_s(sn, cx_str(user), cx_str(password)); 164 dav_session_set_auth_s(sn, cx_str(user), cx_str(password));
342 sn->errorstr = errstr; 359 sn->errorstr = errstr;
343 } 360 }
344 361
345 void dav_session_destroy(DavSession *sn) { 362 void dav_session_destroy(DavSession *sn) {
346 // remove session from context 363 // remove session from context
347 if (dav_context_remove_session(sn->context, sn)) { 364 DavContext *ctx = sn->context;
365 dav_context_ref(ctx);
366 if (dav_context_remove_session(ctx, sn)) {
348 fprintf(stderr, "Error: session not found in ctx->sessions\n"); 367 fprintf(stderr, "Error: session not found in ctx->sessions\n");
349 dav_session_destructor(sn); 368 dav_session_destructor(sn);
350 } 369 }
370 dav_context_unref(ctx);
351 } 371 }
352 372
353 void dav_session_destructor(DavSession *sn) { 373 void dav_session_destructor(DavSession *sn) {
354 cxMempoolFree(sn->mp); 374 cxMempoolFree(sn->mp);
355 curl_easy_cleanup(sn->handle); 375 curl_easy_cleanup(sn->handle);
376 dav_context_unref(sn->context);
356 free(sn); 377 free(sn);
357 } 378 }
358 379
359 380
360 void* dav_session_malloc(DavSession *sn, size_t size) { 381 void* dav_session_malloc(DavSession *sn, size_t size) {

mercurial