libidav/session.c

Sun, 24 Jun 2018 12:18:12 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 24 Jun 2018 12:18:12 +0200
changeset 429
1607450065ba
parent 420
ad419882e06e
child 437
70c7d8e437f9
permissions
-rw-r--r--

this could be the 1.2 release

43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
404
5c08b8e14df8 updates copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 355
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <ucx/buffer.h>
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <ucx/utils.h>
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "utils.h"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "session.h"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "resource.h"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "methods.h"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 DavSession* dav_session_new(DavContext *context, char *base_url) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 if(!base_url) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 sstr_t url = sstr(base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 if(url.length == 0) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 DavSession *sn = malloc(sizeof(DavSession));
355
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
50 memset(sn, 0, sizeof(DavSession));
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 sn->mp = ucx_mempool_new(DAV_SESSION_MEMPOOL_SIZE);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 sn->pathcache = ucx_map_new_a(sn->mp->allocator, DAV_PATH_CACHE_SIZE);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 sn->key = NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 sn->errorstr = NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 sn->error = DAV_OK;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 sn->flags = 0;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 if(url.ptr[url.length - 1] == '/') {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 sstr_t url = sstrdup_a(sn->mp->allocator, sstr(base_url));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 sn->base_url = url.ptr;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 } else {
222
7b73058d782e fixed some memory leaks in dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
61 char *url_str = ucx_mempool_malloc(sn->mp, url.length + 2);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 memcpy(url_str, base_url, url.length);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 url_str[url.length] = '/';
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 url_str[url.length + 1] = '\0';
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 sn->base_url = url_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 sn->handle = curl_easy_init();
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 curl_easy_setopt(sn->handle, CURLOPT_FOLLOWLOCATION, 1L);
208
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
69
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
70 // create lock manager
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
71 DavLockManager *locks = ucx_mempool_malloc(sn->mp, sizeof(DavLockManager));
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
72 locks->resource_locks = ucx_map_new_a(sn->mp->allocator, 16);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
73 locks->collection_locks = NULL;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
74 sn->locks = locks;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 // set proxy
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 DavProxy *proxy = sstrprefix(url, S("https")) ? context->https_proxy
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 : context->http_proxy;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if (proxy->url) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 curl_easy_setopt(sn->handle, CURLOPT_PROXY, proxy->url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 if (proxy->username) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 curl_easy_setopt(sn->handle, CURLOPT_PROXYUSERNAME,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 proxy->username);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 if (proxy->password) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 curl_easy_setopt(sn->handle, CURLOPT_PROXYPASSWORD,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 proxy->password);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 // TODO: prompt
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 if(proxy->no_proxy) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 curl_easy_setopt(sn->handle, CURLOPT_NOPROXY,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 proxy->no_proxy);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 // set url
420
ad419882e06e sets curl default protocol to http
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
99 #if LIBCURL_VERSION_NUM >= 0x072D00
ad419882e06e sets curl default protocol to http
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
100 curl_easy_setopt(sn->handle, CURLOPT_DEFAULT_PROTOCOL, "http");
ad419882e06e sets curl default protocol to http
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
101 #endif
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 curl_easy_setopt(sn->handle, CURLOPT_URL, base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
68
f6d3db6113d3 added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
104 // add to context
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 context->sessions = ucx_list_append(context->sessions, sn);
68
f6d3db6113d3 added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
106 sn->context = context;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 return sn;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 DavSession* dav_session_new_auth(
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 DavContext *context,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 char *base_url,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 char *user,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 char *password)
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 DavSession *sn = dav_session_new(context, base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 if(!sn) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 dav_session_set_auth(sn, user, password);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 return sn;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 void dav_session_set_auth(DavSession *sn, char *user, char *password) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 if(user && password) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 size_t ulen = strlen(user);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 size_t plen = strlen(password);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 size_t upwdlen = ulen + plen + 2;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 char *upwdbuf = malloc(upwdlen);
104
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
131 snprintf(upwdbuf, upwdlen, "%s:%s", user, password);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 curl_easy_setopt(sn->handle, CURLOPT_USERPWD, upwdbuf);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 free(upwdbuf);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 sn->key = key;
68
f6d3db6113d3 added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
139 // TODO: review sanity
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 if(flags != 0) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 sn->flags |= flags;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 sn->flags |= DAV_SESSION_ENCRYPT_CONTENT;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
354
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
147 void dav_session_set_authcallback(DavSession *sn, dav_auth_func func, void *userdata) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
148 sn->auth_prompt = func;
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
149 sn->authprompt_userdata = userdata;
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
150 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
151
355
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
152 void dav_session_set_progresscallback(DavSession *sn, dav_progress_func get, dav_progress_func put, void *userdata) {
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
153 sn->get_progress = get;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
154 sn->put_progress = put;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
155 sn->progress_userdata = userdata;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
156 }
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
157
354
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
158 CURLcode dav_session_curl_perform(DavSession *sn, long *status) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
159 return dav_session_curl_perform_buf(sn, NULL, NULL, status);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
160 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
161
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
162 CURLcode dav_session_curl_perform_buf(DavSession *sn, UcxBuffer *request, UcxBuffer *response, long *status) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
163 CURLcode ret = curl_easy_perform(sn->handle);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
164 long http_status;
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
165 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
166 if(ret == CURLE_OK && http_status == 401 && sn->auth_prompt) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
167 if(!sn->auth_prompt(sn, sn->authprompt_userdata)) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
168 if(request) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
169 ucx_buffer_seek(request, 0, SEEK_SET);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
170 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
171 if(response) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
172 ucx_buffer_seek(response, 0, SEEK_SET);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
173 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
174 ret = curl_easy_perform(sn->handle);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
175 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
176 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
177 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
178 if(status) {
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
179 *status = http_status;
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
180 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
181 return ret;
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
182 }
067ea2315a8a moves auth prompt functionality to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 292
diff changeset
183
355
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
184 int dav_session_get_progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
185 DavResource *res = clientp;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
186 DavSession *sn = res->session;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
187 if(sn->get_progress) {
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
188 sn->get_progress(res, (int64_t)dltotal, (int64_t)dlnow, sn->progress_userdata);
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
189 }
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
190 return 0;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
191 }
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
192
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
193 int dav_session_put_progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
194 DavResource *res = clientp;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
195 DavSession *sn = res->session;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
196 if(sn->put_progress) {
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
197 sn->put_progress(res, (int64_t)ultotal, (int64_t)ulnow, sn->progress_userdata);
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
198 }
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
199 return 0;
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
200 }
5da2cf15eb44 adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
201
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 void dav_session_set_error(DavSession *sn, CURLcode c, int status) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 if(status > 0) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 switch(status) {
100
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
205 default: {
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
206 switch(c) {
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
207 default: sn->error = DAV_ERROR;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
208 }
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
209 break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
210 }
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 case 401: sn->error = DAV_UNAUTHORIZED; break;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 case 403: sn->error = DAV_FORBIDDEN; break;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 case 404: sn->error = DAV_NOT_FOUND; break;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 case 405: sn->error = DAV_METHOD_NOT_ALLOWED; break;
238
b167226fb804 added support for error codes 407,413,414 and 511
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
215 case 407: sn->error = DAV_PROXY_AUTH_REQUIRED; break;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 case 409: sn->error = DAV_CONFLICT; break;
238
b167226fb804 added support for error codes 407,413,414 and 511
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
217 case 413: sn->error = DAV_REQUEST_ENTITY_TOO_LARGE; break;
b167226fb804 added support for error codes 407,413,414 and 511
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
218 case 414: sn->error = DAV_REQUEST_URL_TOO_LONG; break;
207
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 199
diff changeset
219 case 423: sn->error = DAV_LOCKED; break;
238
b167226fb804 added support for error codes 407,413,414 and 511
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
220 case 511: sn->error = DAV_NET_AUTH_REQUIRED; break;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 } else {
100
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
223 switch(c) {
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
224 case CURLE_UNSUPPORTED_PROTOCOL: sn->error = DAV_UNSUPPORTED_PROTOCOL; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
225 case CURLE_COULDNT_RESOLVE_PROXY: sn->error = DAV_COULDNT_RESOLVE_PROXY; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
226 case CURLE_COULDNT_RESOLVE_HOST: sn->error = DAV_COULDNT_RESOLVE_HOST; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
227 case CURLE_COULDNT_CONNECT: sn->error = DAV_COULDNT_CONNECT; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
228 case CURLE_OPERATION_TIMEDOUT: sn->error = DAV_TIMEOUT; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
229 case CURLE_SSL_CONNECT_ERROR:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
230 case CURLE_PEER_FAILED_VERIFICATION:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
231 case CURLE_SSL_ENGINE_NOTFOUND:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
232 case CURLE_SSL_ENGINE_SETFAILED:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
233 case CURLE_SSL_CERTPROBLEM:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
234 case CURLE_SSL_CIPHER:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
235 case CURLE_SSL_CACERT:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
236 case CURLE_SSL_CACERT_BADFILE:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
237 case CURLE_SSL_SHUTDOWN_FAILED:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
238 case CURLE_SSL_CRL_BADFILE:
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
239 case CURLE_SSL_ISSUER_ERROR: sn->error = DAV_SSL_ERROR; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
240 default: sn->error = DAV_ERROR; break;
f4127c4d1018 improved error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
241 }
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 if(c != CURLE_OK) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
244 dav_session_set_errstr(sn, curl_easy_strerror(c));
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 } else {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
246 dav_session_set_errstr(sn, NULL);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
250 void dav_session_set_errstr(DavSession *sn, const char *str) {
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
251 if(sn->errorstr) {
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
252 dav_session_free(sn, sn->errorstr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
253 }
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
254 char *errstr = NULL;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
255 if(str) {
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
256 errstr = dav_session_strdup(sn, str);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
257 }
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
258 sn->errorstr = errstr;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
259 }
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
260
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 void dav_session_destroy(DavSession *sn) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 // remove session from context
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 UcxList *sessions = sn->context->sessions;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 ssize_t i = ucx_list_find(sessions, sn, ucx_ptrcmp, NULL);
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 68
diff changeset
265 if(i >= 0) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 UcxList *elm = ucx_list_get(sessions, i);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 if(elm) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 sn->context->sessions = ucx_list_remove(sessions, elm);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 ucx_mempool_destroy(sn->mp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 curl_easy_cleanup(sn->handle);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 free(sn);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 void* dav_session_malloc(DavSession *sn, size_t size) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 return ucx_mempool_malloc(sn->mp, size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 return ucx_mempool_calloc(sn->mp, nelm, size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 void* dav_session_realloc(DavSession *sn, void *ptr, size_t size) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 return ucx_mempool_realloc(sn->mp, ptr, size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 void dav_session_free(DavSession *sn, void *ptr) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 ucx_mempool_free(sn->mp, ptr);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
294 char* dav_session_strdup(DavSession *sn, const char *str) {
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 222
diff changeset
295 return sstrdup_a(sn->mp->allocator, sstr((char*)str)).ptr;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 char* dav_session_create_plain_href(DavSession *sn, char *path) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 if(!DAV_ENCRYPT_NAME(sn)) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 // non encrypted file names
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 char *url = util_path_to_url(sn, path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 char *href = dav_session_strdup(sn, util_url_path(url));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 free(url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 return href;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 char* dav_session_get_href(DavSession *sn, char *path) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 if(DAV_ENCRYPT_NAME(sn)) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 sstr_t p = sstr(path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 UcxBuffer *href = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 UcxBuffer *pbuf = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 int start = 0;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 int begin = 0;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 // check path cache
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 char *cp = strdup(path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 //printf("cp: %s\n", cp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 while(strlen(cp) > 1) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 char *cached = ucx_map_cstr_get(sn->pathcache, cp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 if(cached) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 start = strlen(cp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 begin = start;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 ucx_buffer_puts(href, cached);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 break;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 // check, if the parent path is cached
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 char *f = cp;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 cp = util_parent_path(cp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 free(f);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 free(cp);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 if(href->pos == 0) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 // if there are no cached elements we have to add the base url path
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 // to the href buffer
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 ucx_buffer_puts(href, util_url_path(sn->base_url));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 // create resource for name lookup
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 sstr_t rp = sstrdup(sstrn(path, start));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 DavResource *root = dav_resource_new(sn, rp.ptr);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 135
diff changeset
346 free(rp.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 resource_set_href(root, sstrn(href->space, href->pos));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 // create request buffer for propfind requests
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 UcxBuffer *rqbuf = create_basic_propfind_request();
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 sstr_t remaining = sstrsubs(p, start);
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 68
diff changeset
353 ssize_t nelm = 0;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 sstr_t *elms = sstrsplit(remaining, S("/"), &nelm);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 DavResource *res = root;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 ucx_buffer_puts(pbuf, res->path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 // iterate over all remaining path elements
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 for(int i=0;i<nelm;i++) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 sstr_t elm = elms[i];
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 if(elm.length > 0) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 //printf("elm: %.*s\n", elm.length, elm.ptr);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 DavResource *child = dav_find_child(sn, res, rqbuf, elm.ptr);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 // if necessary add a path separator
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 if(pbuf->space[pbuf->pos-1] != '/') {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 if(href->space[href->pos-1] != '/') {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 ucx_buffer_putc(href, '/');
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 ucx_buffer_putc(pbuf, '/');
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 // add last path/href to the cache
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 sstr_t pp = sstrn(pbuf->space, pbuf->size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 sstr_t hh = sstrn(href->space, href->size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 dav_session_cache_path(sn, pp, hh);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 ucx_buffer_write(elm.ptr, 1, elm.length, pbuf);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 if(child) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 ucx_buffer_puts(href, util_resource_name(child->href));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 res = child;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 //printf("random\n");
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 char *random_name = util_random_str();
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 ucx_buffer_puts(href, random_name);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 free(random_name);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 }
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
387
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
388 // cleanup
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
389 free(elm.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 }
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
391 free(elms);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 // if necessary add a path separator
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 if(p.ptr[p.length-1] == '/') {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 if(href->space[href->pos-1] != '/') {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 ucx_buffer_putc(href, '/');
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 ucx_buffer_putc(pbuf, '/');
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 // add the final path to the cache
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 sstr_t pp = sstrn(pbuf->space, pbuf->size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 sstr_t hh = sstrn(href->space, href->size);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 dav_session_cache_path(sn, pp, hh);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 sstr_t href_str = sstrdup_a(
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 sn->mp->allocator,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 sstrn(href->space,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 href->size));
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 // cleanup
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 dav_resource_free_all(root);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 135
diff changeset
412 ucx_buffer_free(rqbuf);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 ucx_buffer_free(pbuf);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 ucx_buffer_free(href);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 return href_str.ptr;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 } else {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 return dav_session_create_plain_href(sn, path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421
292
0a47ffc8b73b fixes wrong int type that results in a crash on some platforms
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 238
diff changeset
422 DavResource* dav_find_child(DavSession *sn, DavResource *res, UcxBuffer *rqbuf, char *name) {
135
664aeaec8d25 replaced old davql (still buggy)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
423 if(res && !dav_propfind(sn, res, rqbuf)) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 DavResource *child = res->children;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 while(child) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 if(!strcmp(child->name, name)) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 return child;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 child = child->next;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 void dav_session_cache_path(DavSession *sn, sstr_t path, sstr_t href) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 char *elm = ucx_map_sstr_get(sn->pathcache, path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 if(!elm) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 href = sstrdup_a(sn->mp->allocator, href);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 ucx_map_sstr_put(sn->pathcache, path, href.ptr);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 }
208
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
442
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
443
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
444 DavLock* dav_create_lock(DavSession *sn, char *token, char *timeout) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
445 DavLock *lock = dav_session_malloc(sn, sizeof(DavLock));
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
446 lock->path = NULL;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
447 lock->token = dav_session_strdup(sn, token);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
448
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
449 // TODO: timeout
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
450
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
451 return lock;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
452 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
453
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
454 void dav_destroy_lock(DavSession *sn, DavLock *lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
455 dav_session_free(sn, lock->token);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
456 if(lock->path) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
457 dav_session_free(sn, lock->path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
458 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
459 dav_session_free(sn, lock);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
460 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
461
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
462 int dav_add_resource_lock(DavSession *sn, char *path, DavLock *lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
463 DavLockManager *locks = sn->locks;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
464 if(ucx_map_cstr_get(locks->resource_locks, path)) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
465 return -1;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
466 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
467
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
468 ucx_map_cstr_put(locks->resource_locks, path, lock);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
469 return 0;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
470 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
471
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
472 static void insert_lock(DavSession *sn, UcxList *elm, UcxList *newelm) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
473 UcxList *next = elm->next;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
474 if(next) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
475 next->prev = newelm;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
476 newelm->next = next;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
477 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
478 newelm->prev = elm;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
479 elm->next = newelm;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
480 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
481
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
482 int dav_add_collection_lock(DavSession *sn, char *path, DavLock *lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
483 DavLockManager *locks = sn->locks;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
484 if(!locks->collection_locks) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
485 locks->collection_locks = ucx_list_append_a(
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
486 sn->mp->allocator,
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
487 NULL,
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
488 lock);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
489 lock->path = dav_session_strdup(sn, path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
490 return 0;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
491 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
492
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
493 UcxList *elm = locks->collection_locks;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
494 for(;;) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
495 DavLock *l = elm->data;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
496 int cmp = strcmp(path, l->path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
497 if(cmp > 0) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
498 UcxList *newelm = ucx_list_append_a(sn->mp->allocator, NULL, lock);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
499 lock->path = dav_session_strdup(sn, path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
500 insert_lock(sn, elm, newelm);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
501 } else if(cmp == 0) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
502 return -1;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
503 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
504
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
505 if(elm->next) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
506 elm = elm->next;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
507 } else {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
508 UcxList *newelm = ucx_list_append_a(sn->mp->allocator, NULL, lock);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
509 lock->path = dav_session_strdup(sn, path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
510 ucx_list_concat(elm, newelm);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
511 break;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
512 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
513 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
514
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
515 return 0;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
516 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
517
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
518 DavLock* dav_get_lock(DavSession *sn, char *path) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
519 DavLockManager *locks = sn->locks;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
520
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
521 DavLock *lock = ucx_map_cstr_get(locks->resource_locks, path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
522 if(lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
523 return lock;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
524 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
525
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
526 sstr_t p = sstr(path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
527 UCX_FOREACH(elm, locks->collection_locks) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
528 DavLock *cl = elm->data;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
529 int cmd = strcmp(path, cl->path);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
530 if(cmd == 0) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
531 return cl;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
532 } else if(sstrprefix(p, sstr(cl->path))) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
533 return cl;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
534 } else if(cmd > 0) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
535 break;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
536 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
537 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
538
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
539 return NULL;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
540 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
541
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
542 void dav_remove_lock(DavSession *sn, char *path, DavLock *lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
543 DavLockManager *locks = sn->locks;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
544
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
545 if(ucx_map_cstr_remove(locks->resource_locks, path)) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
546 return;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
547 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
548
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
549 UcxList *rm = NULL;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
550 UCX_FOREACH(elm, locks->collection_locks) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
551 DavLock *cl = elm->data;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
552 if(cl == lock) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
553 rm = elm;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
554 break;
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
555 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
556 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
557
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
558 if(rm) {
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
559 locks->collection_locks = ucx_list_remove_a(
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
560 sn->mp->allocator,
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
561 locks->collection_locks,
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
562 rm);
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
563 }
1fb26aca5093 added API for webdav locking
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
564 }

mercurial