144 } |
144 } |
145 |
145 |
146 free(ctx); |
146 free(ctx); |
147 } |
147 } |
148 |
148 |
|
149 #ifndef _WIN32 |
|
150 |
|
151 void dav_context_set_mtsafe(DavContext *ctx, DavBool enable) { |
|
152 if (enable) { |
|
153 pthread_mutex_init(&ctx->mutex, NULL); |
|
154 } else { |
|
155 pthread_mutex_destroy(&ctx->mutex); |
|
156 } |
|
157 ctx->mtsafe = enable; |
|
158 } |
|
159 |
|
160 void dav_context_lock(DavContext *ctx) { |
|
161 if (ctx->mtsafe) { |
|
162 pthread_mutex_lock(&ctx->mutex); |
|
163 } |
|
164 } |
|
165 |
|
166 void dav_context_unlock(DavContext *ctx) { |
|
167 if (ctx->mtsafe) { |
|
168 pthread_mutex_unlock(&ctx->mutex); |
|
169 } |
|
170 } |
|
171 |
|
172 #else |
|
173 |
|
174 void dav_context_set_mtsafe(DavContext *ctx, DavBool enable) { |
|
175 if (enable) { |
|
176 ctx->mutex = CreateMutex(NULL, FALSE, NULL); |
|
177 } else { |
|
178 CloseHandle(ctx->mutex); |
|
179 } |
|
180 ctx->mtsafe = enable; |
|
181 } |
|
182 |
|
183 void dav_context_lock(DavContext *ctx) { |
|
184 if (ctx->mtsafe) { |
|
185 WaitForSingleObject(ctx->mutex, INFINITE); |
|
186 } |
|
187 } |
|
188 |
|
189 void dav_context_unlock(DavContext *ctx) { |
|
190 if (ctx->mtsafe) { |
|
191 ReleaseMutex(ctx->mutex); |
|
192 } |
|
193 } |
|
194 |
|
195 #endif |
|
196 |
149 void dav_context_add_key(DavContext *context, DavKey *key) { |
197 void dav_context_add_key(DavContext *context, DavKey *key) { |
|
198 dav_context_lock(context); |
150 cxMapPut(context->keys, cx_hash_key_str(key->name), key); |
199 cxMapPut(context->keys, cx_hash_key_str(key->name), key); |
|
200 dav_context_unlock(context); |
151 } |
201 } |
152 |
202 |
153 DavKey* dav_context_get_key(DavContext *context, const char *name) { |
203 DavKey* dav_context_get_key(DavContext *context, const char *name) { |
|
204 DavKey *key = NULL; |
|
205 dav_context_lock(context); |
154 if(name) { |
206 if(name) { |
155 return cxMapGet(context->keys, cx_hash_key_str(name)); |
207 key = cxMapGet(context->keys, cx_hash_key_str(name)); |
156 } |
208 } |
157 return NULL; |
209 dav_context_unlock(context); |
|
210 return key; |
158 } |
211 } |
159 |
212 |
160 int dav_add_namespace(DavContext *context, const char *prefix, const char *name) { |
213 int dav_add_namespace(DavContext *context, const char *prefix, const char *name) { |
161 DavNamespace *namespace = malloc(sizeof(DavNamespace)); |
214 DavNamespace *namespace = malloc(sizeof(DavNamespace)); |
162 if(!namespace) { |
215 if(!namespace) { |
163 return 1; |
216 return 1; |
164 } |
217 } |
165 |
218 |
166 char *p = strdup(prefix); |
219 char *p = strdup(prefix); |
|
220 if (!p) { |
|
221 free(namespace); |
|
222 return 1; |
|
223 } |
167 char *n = strdup(name); |
224 char *n = strdup(name); |
168 |
225 if (!n) { |
|
226 free(namespace); |
|
227 free(p); |
|
228 return 1; |
|
229 } |
|
230 |
|
231 dav_context_lock(context); |
|
232 |
169 int err = 0; |
233 int err = 0; |
170 if(p && n) { |
234 if(p && n) { |
171 namespace->prefix = p; |
235 namespace->prefix = p; |
172 namespace->name = n; |
236 namespace->name = n; |
173 err = cxMapPut(context->namespaces, cx_hash_key_str(prefix), namespace); |
237 err = cxMapPut(context->namespaces, cx_hash_key_str(prefix), namespace); |
176 if(err) { |
240 if(err) { |
177 free(namespace); |
241 free(namespace); |
178 if(p) free(p); |
242 if(p) free(p); |
179 if(n) free(n); |
243 if(n) free(n); |
180 } |
244 } |
|
245 |
|
246 dav_context_unlock(context); |
181 |
247 |
182 return err; |
248 return err; |
183 } |
249 } |
184 |
250 |
185 DavNamespace* dav_get_namespace(DavContext *context, const char *prefix) { |
251 DavNamespace* dav_get_namespace(DavContext *context, const char *prefix) { |
186 return cxMapGet(context->namespaces, cx_hash_key_str(prefix)); |
252 dav_context_lock(context); |
|
253 DavNamespace *ns = cxMapGet(context->namespaces, cx_hash_key_str(prefix)); |
|
254 dav_context_unlock(context); |
|
255 return ns; |
187 } |
256 } |
188 |
257 |
189 DavNamespace* dav_get_namespace_s(DavContext *context, cxstring prefix) { |
258 DavNamespace* dav_get_namespace_s(DavContext *context, cxstring prefix) { |
190 return cxMapGet(context->namespaces, cx_hash_key(prefix.ptr, prefix.length)); |
259 dav_context_lock(context); |
|
260 DavNamespace *ns = cxMapGet(context->namespaces, cx_hash_key(prefix.ptr, prefix.length)); |
|
261 dav_context_unlock(context); |
|
262 return ns; |
191 } |
263 } |
192 |
264 |
193 int dav_enable_namespace_encryption(DavContext *context, const char *ns, DavBool encrypt) { |
265 int dav_enable_namespace_encryption(DavContext *context, const char *ns, DavBool encrypt) { |
|
266 dav_context_lock(context); |
|
267 |
194 CxHashKey hkey = cx_hash_key_str(ns); |
268 CxHashKey hkey = cx_hash_key_str(ns); |
195 DavNSInfo *info = cxMapGet(context->namespaceinfo, hkey); |
269 DavNSInfo *info = cxMapGet(context->namespaceinfo, hkey); |
196 if(!info) { |
270 if(!info) { |
197 info = calloc(1, sizeof(DavNSInfo)); |
271 info = calloc(1, sizeof(DavNSInfo)); |
198 info->encrypt = encrypt; |
272 info->encrypt = encrypt; |
199 cxMapPut(context->namespaceinfo, hkey, info); |
273 cxMapPut(context->namespaceinfo, hkey, info); |
200 } else { |
274 } else { |
201 info->encrypt = encrypt; |
275 info->encrypt = encrypt; |
202 } |
276 } |
|
277 |
|
278 dav_context_unlock(context); |
203 return 0; |
279 return 0; |
204 } |
280 } |
205 |
281 |
206 int dav_namespace_is_encrypted(DavContext *context, const char *ns) { |
282 int dav_namespace_is_encrypted(DavContext *context, const char *ns) { |
|
283 int ret = 0; |
|
284 dav_context_lock(context); |
|
285 |
207 DavNSInfo *info = cxMapGet(context->namespaceinfo, cx_hash_key_str(ns)); |
286 DavNSInfo *info = cxMapGet(context->namespaceinfo, cx_hash_key_str(ns)); |
208 if(info) { |
287 if(info) { |
209 return info->encrypt; |
288 ret = info->encrypt; |
210 } |
289 } |
211 return 0; |
290 dav_context_unlock(context); |
|
291 return ret; |
212 } |
292 } |
213 |
293 |
214 void dav_get_property_namespace_str( |
294 void dav_get_property_namespace_str( |
215 DavContext *ctx, |
295 DavContext *ctx, |
216 char *prefixed_name, |
296 char *prefixed_name, |
259 } else { |
339 } else { |
260 *name = prefixed_name; |
340 *name = prefixed_name; |
261 return dav_get_namespace_s(ctx, cx_str("D")); |
341 return dav_get_namespace_s(ctx, cx_str("D")); |
262 } |
342 } |
263 } |
343 } |
|
344 |
|
345 int dav_context_add_session(DavContext *context, DavSession *sn) { |
|
346 dav_context_lock(context); |
|
347 int ret = cxListAdd(context->sessions, sn); |
|
348 dav_context_unlock(context); |
|
349 return ret; |
|
350 } |
|
351 |
|
352 int dav_context_remove_session(DavContext *context, DavSession *sn) { |
|
353 int ret = 0; |
|
354 dav_context_lock(context); |
|
355 CxList *sessions = context->sessions; |
|
356 ssize_t i = cxListFind(sessions, sn); |
|
357 if(i >= 0) { |
|
358 cxListRemove(sessions, i); |
|
359 } else { |
|
360 ret = 1; |
|
361 } |
|
362 dav_context_unlock(context); |
|
363 return ret; |
|
364 } |
|
365 |
264 |
366 |
265 // TODO: add sstr_t version of dav_get_property_ns |
367 // TODO: add sstr_t version of dav_get_property_ns |
266 |
368 |
267 void dav_set_effective_href(DavSession *sn, DavResource *resource) { |
369 void dav_set_effective_href(DavSession *sn, DavResource *resource) { |
268 char *eff_url; |
370 char *eff_url; |