libidav/webdav.c

changeset 74
da079dc0724c
parent 66
f8c1f685e08e
child 75
56962faf2b42
equal deleted inserted replaced
73:41e88442ad4e 74:da079dc0724c
39 #include "ucx/buffer.h" 39 #include "ucx/buffer.h"
40 #include "ucx/utils.h" 40 #include "ucx/utils.h"
41 41
42 42
43 DavContext* dav_context_new() { 43 DavContext* dav_context_new() {
44 DavContext *context = malloc(sizeof(DavContext)); 44 // initialize
45 DavContext *context = calloc(1, sizeof(DavContext));
45 if(!context) { 46 if(!context) {
46 return NULL; 47 return NULL;
47 } 48 }
48 context->sessions = NULL; 49 context->sessions = NULL;
49 context->http_proxy = calloc(1, sizeof(DavProxy)); 50 context->http_proxy = calloc(1, sizeof(DavProxy));
51 if(!context->http_proxy) {
52 dav_context_destroy(context);
53 return NULL;
54 }
50 context->https_proxy = calloc(1, sizeof(DavProxy)); 55 context->https_proxy = calloc(1, sizeof(DavProxy));
56 if(!context->https_proxy) {
57 dav_context_destroy(context);
58 return NULL;
59 }
51 context->namespaces = ucx_map_new(16); 60 context->namespaces = ucx_map_new(16);
52 if(!context->namespaces) { 61 if(!context->namespaces) {
53 free(context); 62 dav_context_destroy(context);
54 return NULL; 63 return NULL;
55 } 64 }
56 context->keys = ucx_map_new(16); 65 context->keys = ucx_map_new(16);
66 if(!context->keys) {
67 dav_context_destroy(context);
68 return NULL;
69 }
70
71 // add DAV: namespace
57 DavNamespace *davns = malloc(sizeof(DavNamespace)); 72 DavNamespace *davns = malloc(sizeof(DavNamespace));
58 if(!davns) { 73 if(!davns) {
59 ucx_map_free(context->namespaces); 74 free(davns);
60 free(context); 75 dav_context_destroy(context);
61 return NULL; 76 return NULL;
62 } 77 }
63 davns->prefix = "D"; 78 davns->prefix = strdup("D");
64 davns->name = "DAV:"; 79 if(!davns->prefix) {
80 free(davns);
81 dav_context_destroy(context);
82 return NULL;
83 }
84 davns->name = strdup("DAV:");
85 if(!davns->name) {
86 free(davns->prefix);
87 free(davns);
88 dav_context_destroy(context);
89 return NULL;
90 }
65 if(ucx_map_cstr_put(context->namespaces, "D", davns)) { 91 if(ucx_map_cstr_put(context->namespaces, "D", davns)) {
92 free(davns->prefix);
93 free(davns->name);
66 free(davns); 94 free(davns);
67 ucx_map_free(context->namespaces); 95 dav_context_destroy(context);
68 free(context); 96 return NULL;
69 return NULL; 97 }
70 } 98
71 99 // add idav namespace
72 DavNamespace *idavns = malloc(sizeof(DavNamespace)); 100 DavNamespace *idavns = malloc(sizeof(DavNamespace));
73 if(!idavns) { 101 if(!idavns) {
74 free(davns); 102 free(idavns);
75 ucx_map_free(context->namespaces); 103 dav_context_destroy(context);
76 free(context); 104 return NULL;
77 return NULL; 105 }
78 } 106 idavns->prefix = strdup("idav");
79 idavns->prefix = "idav"; 107 if(!idavns->prefix) {
80 idavns->name = DAV_NS; 108 free(idavns);
109 dav_context_destroy(context);
110 return NULL;
111 }
112 idavns->name = strdup(DAV_NS);
113 if(!idavns->name) {
114 free(idavns->prefix);
115 free(idavns);
116 dav_context_destroy(context);
117 return NULL;
118 }
81 if(ucx_map_cstr_put(context->namespaces, "idav", idavns)) { 119 if(ucx_map_cstr_put(context->namespaces, "idav", idavns)) {
82 free(davns); 120 free(idavns->prefix);
121 free(idavns->name);
83 free(idavns); 122 free(idavns);
84 ucx_map_free(context->namespaces); 123 dav_context_destroy(context);
85 free(context); 124 return NULL;
86 return NULL; 125 }
87 }
88
89 126
90 return context; 127 return context;
91 } 128 }
92 129
93 void dav_context_destroy(DavContext *ctx) { 130 void dav_context_destroy(DavContext *ctx) {
94 // destroy all sessions assoziated with this context 131 // destroy all sessions assoziated with this context
95 UCX_FOREACH(elm, ctx->sessions) { 132 UcxList *elm = ctx->sessions;
96 dav_session_destroy(elm->data); 133 while(elm) {
97 } 134 DavSession *sn = elm->data;
98 free(ctx->http_proxy); 135 elm = elm->next;
99 free(ctx->https_proxy); 136 dav_session_destroy(sn);
100 137 }
101 UcxMapIterator i = ucx_map_iterator(ctx->namespaces); 138 if(ctx->http_proxy) {
102 UcxKey k; 139 free(ctx->http_proxy);
103 DavNamespace *ns; 140 }
104 // TODO: free map elements 141 if(ctx->https_proxy) {
105 ucx_map_free(ctx->namespaces); 142 free(ctx->https_proxy);
143 }
144
145 if(ctx->namespaces) {
146 UcxMapIterator i = ucx_map_iterator(ctx->namespaces);
147 UcxKey k;
148 DavNamespace *ns;
149 UCX_MAP_FOREACH(k, ns, i) {
150 if(!ns) continue;
151 if(ns->prefix) {
152 free(ns->prefix);
153 }
154 if(ns->name) {
155 free(ns->name);
156 }
157 free(ns);
158 }
159 ucx_map_free(ctx->namespaces);
160 }
161 if(ctx->keys) {
162 UcxMapIterator i = ucx_map_iterator(ctx->keys);
163 UcxKey k;
164 DavKey *key;
165 UCX_MAP_FOREACH(k, key, i) {
166 if(!key) continue;
167 if(key->name) {
168 free(key->name);
169 }
170 if(key->data) {
171 free(key->data);
172 }
173 free(key);
174 }
175 ucx_map_free(ctx->keys);
176 }
177
106 free(ctx); 178 free(ctx);
107 } 179 }
108 180
109 void dav_context_add_key(DavContext *context, DavKey *key) { 181 void dav_context_add_key(DavContext *context, DavKey *key) {
110 ucx_map_cstr_put(context->keys, key->name, key); 182 ucx_map_cstr_put(context->keys, key->name, key);
193 } 265 }
194 266
195 int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, DavQOp *cond, size_t len) { 267 int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, DavQOp *cond, size_t len) {
196 // clean resource properties 268 // clean resource properties
197 DavResourceData *data = root->data; 269 DavResourceData *data = root->data;
198 if(data->properties->count > 0) { 270 size_t pcount = data->properties->count;
271 if(pcount > 0) {
199 UcxKey key; 272 UcxKey key;
200 void *value; 273 void *value;
201 UcxMapIterator i = ucx_map_iterator(data->properties); 274 UcxMapIterator i = ucx_map_iterator(data->properties);
275 UcxKey mkeys[pcount];
276 int index = 0;
202 UCX_MAP_FOREACH(key, value, i) { 277 UCX_MAP_FOREACH(key, value, i) {
203 ucx_map_remove(data->properties, key); 278 mkeys[index] = key;
279 index++;
280 }
281 for(int j=0;j<index;j++) {
282 ucx_map_remove(data->properties, mkeys[j]);
204 } 283 }
205 } 284 }
206 285
207 CURL *handle = sn->handle; 286 CURL *handle = sn->handle;
208 util_set_url(sn, dav_resource_get_href(root)); 287 util_set_url(sn, dav_resource_get_href(root));
253 } else if(!sstrcmp(ps, S("-"))) { 332 } else if(!sstrcmp(ps, S("-"))) {
254 rqbuf = create_propfind_request(sn, NULL); 333 rqbuf = create_propfind_request(sn, NULL);
255 } else { 334 } else {
256 UcxList *proplist = parse_properties_string(sn->context, ps); 335 UcxList *proplist = parse_properties_string(sn->context, ps);
257 rqbuf = create_propfind_request(sn, proplist); 336 rqbuf = create_propfind_request(sn, proplist);
337 UCX_FOREACH(elm, proplist) {
338 DavProperty *prop = elm->data;
339 free(prop->name);
340 free(prop);
341 }
342 ucx_list_free(proplist);
258 } 343 }
259 344
260 //fwrite(rqbuf->space, 1, rqbuf->size, stdout); 345 //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
261 //printf("\n"); 346 //printf("\n");
262 347
288 return resource; 373 return resource;
289 } 374 }
290 375
291 UcxList* parse_properties_string(DavContext *context, sstr_t str) { 376 UcxList* parse_properties_string(DavContext *context, sstr_t str) {
292 UcxList *proplist = NULL; 377 UcxList *proplist = NULL;
293 size_t nprops = 0; 378 ssize_t nprops = 0;
294 sstr_t *props = sstrsplit(str, S(","), &nprops); 379 sstr_t *props = sstrsplit(str, S(","), &nprops);
295 for(int i=0;i<nprops;i++) { 380 for(int i=0;i<nprops;i++) {
296 sstr_t s = props[i]; 381 sstr_t s = props[i];
297 sstr_t nsname = sstrchr(s, ':'); 382 sstr_t nsname = sstrchr(s, ':');
298 if(nsname.length > 0) { 383 if(nsname.length > 0) {

mercurial