libidav/methods.c

changeset 33
0bbbb0341606
child 38
b855f76e965b
equal deleted inserted replaced
32:c9d37bb97ea8 33:0bbbb0341606
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "utils.h"
34 #include "methods.h"
35 #include "davql.h"
36
37 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
38
39 /* ----------------------------- PROPFIND ----------------------------- */
40
41 CURLcode do_propfind_request(
42 CURL *handle,
43 UcxBuffer *request,
44 UcxBuffer *response)
45 {
46 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
47
48 struct curl_slist *headers = NULL;
49 headers = curl_slist_append(headers, "Content-Type: text/xml");
50 headers = curl_slist_append(headers, "Depth: 1");
51 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
52
53 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
54
55 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
56 curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
57 curl_easy_setopt(handle, CURLOPT_READDATA, request);
58 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size);
59
60 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
61 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
62
63 ucx_buffer_seek(request, 0, SEEK_SET);
64 return curl_easy_perform(handle);
65 }
66
67 UcxBuffer* create_allprop_propfind_request() {
68 UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
69 sstr_t s;
70
71 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
72 ucx_buffer_write(s.ptr, 1, s.length, buf);
73
74 s = S("<D:propfind xmlns:D=\"DAV:\">\n");
75 ucx_buffer_write(s.ptr, 1, s.length, buf);
76
77 s = S("<D:allprop/></D:propfind>\n");
78 ucx_buffer_write(s.ptr, 1, s.length, buf);
79
80 return buf;
81 }
82
83 UcxBuffer* create_propfind_request(UcxList *properties) {
84 UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
85 sstr_t s;
86
87 UcxMap *namespaces = ucx_map_new(8);
88 UCX_FOREACH(elm, properties) {
89 DavProperty *p = elm->data;
90 if(strcmp(p->ns->name, "DAV:")) {
91 ucx_map_cstr_put(namespaces, p->ns->prefix, p->ns);
92 }
93 }
94
95 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
96 ucx_buffer_write(s.ptr, 1, s.length, buf);
97
98 // write root element and namespaces
99 s = S("<D:propfind xmlns:D=\"DAV:\"");
100 ucx_buffer_write(s.ptr, 1, s.length, buf);
101 UcxMapIterator mapi = ucx_map_iterator(namespaces);
102 UcxKey key;
103 DavNamespace *ns;
104 UCX_MAP_FOREACH(key, ns, mapi) {
105 s = S(" xmlns:");
106 ucx_buffer_write(s.ptr, 1, s.length, buf);
107 s = sstr(ns->prefix);
108 ucx_buffer_write(s.ptr, 1, s.length, buf);
109 s = S("=\"");
110 ucx_buffer_write(s.ptr, 1, s.length, buf);
111 s = sstr(ns->name);
112 ucx_buffer_write(s.ptr, 1, s.length, buf);
113 s = S("\"");
114 ucx_buffer_write(s.ptr, 1, s.length, buf);
115 }
116 s = S(">\n");
117 ucx_buffer_write(s.ptr, 1, s.length, buf);
118
119 // default properties
120 s = S("<D:prop>\n");
121 ucx_buffer_write(s.ptr, 1, s.length, buf);
122
123 s = S("<D:creationdate />\n<D:getlastmodified />\n");
124 ucx_buffer_write(s.ptr, 1, s.length, buf);
125
126 s = S("<D:getcontentlength />\n<D:getcontenttype />\n");
127 ucx_buffer_write(s.ptr, 1, s.length, buf);
128
129 s = S("<D:resourcetype />\n");
130 ucx_buffer_write(s.ptr, 1, s.length, buf);
131
132 // extra properties
133 UCX_FOREACH(elm, properties) {
134 DavProperty *prop = elm->data;
135 s = S("<");
136 ucx_buffer_write(s.ptr, 1, s.length, buf);
137 s = sstr(prop->ns->prefix);
138 ucx_buffer_write(s.ptr, 1, s.length, buf);
139 s = S(":");
140 ucx_buffer_write(s.ptr, 1, s.length, buf);
141 s = sstr(prop->name);
142 ucx_buffer_write(s.ptr, 1, s.length, buf);
143 s = S(" />\n");
144 ucx_buffer_write(s.ptr, 1, s.length, buf);
145 }
146
147 // end
148 s = S("</D:prop>\n</D:propfind>\n");
149 ucx_buffer_write(s.ptr, 1, s.length, buf);
150
151 return buf;
152 }
153
154 DavResource* parse_propfind_response(DavSession *sn, DavResource *root, UcxBuffer *response, DavQOp *cond, size_t len) {
155 char *url = NULL;
156 curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_URL, &url);
157 if(!root) {
158 root = dav_resource_new_href(sn, util_url_path(url));
159 }
160
161 xmlDoc *doc = xmlReadMemory(response->space, response->size, url, NULL, 0);
162 if(!doc) {
163 // TODO: free stuff
164 sn->error = DAV_ERROR;
165 return NULL;
166 }
167
168 xmlNode *xml_root = xmlDocGetRootElement(doc);
169 xmlNode *node = xml_root->children;
170 while(node) {
171 if(node->type == XML_ELEMENT_NODE) {
172 if(xstreq(node->name, "response")) {
173 parse_response_tag(root, node, cond, len);
174 }
175 }
176
177 node = node->next;
178 }
179
180 return root;
181 }
182
183 int parse_response_tag(DavResource *resource, xmlNode *node, DavQOp *cond, size_t clen) {
184 DavResource *res = resource;
185 node = node->children;
186 while(node) {
187 if(node->type == XML_ELEMENT_NODE) {
188 if(xstreq(node->name, "href")) {
189 xmlNode *href_node = node->children;
190 if(href_node->type != XML_TEXT_NODE) {
191 // error
192 resource->session->error = DAV_ERROR;
193 return 1;
194 }
195 char *href = (char*)href_node->content;
196 href = util_url_path(href);
197 if(xstreq(resource->href, href)) {
198 res = resource;
199 } else {
200 res = dav_resource_new_href(resource->session, href);
201 res->parent = resource;
202 }
203 } else if(xstreq(node->name, "propstat")) {
204 xmlNode *n = node->children;
205 xmlNode *prop_node = NULL;
206 int ok = 0;
207 // get the status code
208 while(n) {
209 if(n->type == XML_ELEMENT_NODE) {
210 if(xstreq(n->name, "prop")) {
211 prop_node = n;
212 } else if(xstreq(n->name, "status")) {
213 xmlNode *status_node = n->children;
214 if(status_node->type != XML_TEXT_NODE) {
215 resource->session->error = DAV_ERROR;
216 return 1;
217 }
218 sstr_t status_str = sstr((char*)status_node->content);
219 if(status_str.length < 13) {
220 resource->session->error = DAV_ERROR;
221 return 1;
222 }
223 status_str = sstrsubsl(status_str, 9, 3);
224 if(!sstrcmp(status_str, S("200"))) {
225 ok = 1;
226 }
227 }
228 }
229 n = n->next;
230 }
231 // if status is ok, get all properties
232 if(ok) {
233 n = prop_node->children;
234 while(n) {
235 if(n->type == XML_ELEMENT_NODE) {
236 if(xstreq(n->name, "resourcetype")) {
237 xmlNode *rsnode = n->children;
238 if(rsnode && rsnode->type == XML_ELEMENT_NODE) {
239 // TODO: this is a ugly lazy hack
240 resource_add_property(res, "DAV:", (char*)n->name, "collection");
241 res->iscollection = 1;
242 }
243 } else {
244 xmlNode *content = n->children;
245 if(content) {
246 resource_add_property(
247 res,
248 (char*)n->ns->href,
249 (char*)n->name,
250 (char*)content->content);
251 }
252 }
253 }
254 n = n->next;
255 }
256 }
257 }
258 }
259
260 node = node->next;
261 }
262
263 set_davprops(res);
264 if(res != resource) {
265 if(clen > 0) {
266 if(!condition_eval(res, cond, clen)) {
267 // skip resource
268 return 0;
269 }
270 }
271 resource_add_child(resource, res);
272 }
273
274 return 0;
275 }
276
277 void set_davprops(DavResource *res) {
278 char *cl = dav_get_property_ns(res, "DAV:", "getcontentlength");
279 char *ct = dav_get_property_ns(res, "DAV:", "getcontenttype");
280 char *cd = dav_get_property_ns(res, "DAV:", "creationdate");
281 char *lm = dav_get_property_ns(res, "DAV:", "getlastmodified");
282
283 res->contenttype = ct;
284 if(cl) {
285 char *end = NULL;
286 res->contentlength = strtoull(cl, &end, 0);
287 }
288 res->creationdate = util_parse_creationdate(cd);
289 res->lastmodified = util_parse_lastmodified(lm);
290 }
291
292
293 /* ----------------------------- PROPPATCH ----------------------------- */
294
295 CURLcode do_proppatch_request(
296 CURL *handle,
297 UcxBuffer *request,
298 UcxBuffer *response)
299 {
300 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
301
302 struct curl_slist *headers = NULL;
303 headers = curl_slist_append(headers, "Content-Type: text/xml");
304 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
305
306 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
307
308 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
309 curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
310 curl_easy_setopt(handle, CURLOPT_READDATA, request);
311 curl_easy_setopt(handle, CURLOPT_INFILESIZE, request->size);
312
313 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
314 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
315
316 ucx_buffer_seek(request, 0, SEEK_SET);
317 return curl_easy_perform(handle);
318 }
319
320 UcxBuffer* create_proppatch_request(DavResourceData *data) {
321 UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
322 sstr_t s;
323
324 UcxMap *namespaces = ucx_map_new(8);
325 char prefix[8];
326 int pfxnum = 0;
327 UCX_FOREACH(elm, data->set) {
328 DavProperty *p = elm->data;
329 if(strcmp(p->ns->name, "DAV:")) {
330 snprintf(prefix, 8, "x%d\0", pfxnum++);
331 ucx_map_cstr_put(namespaces, p->ns->name, prefix);
332 }
333 }
334 UCX_FOREACH(elm, data->remove) {
335 DavProperty *p = elm->data;
336 if(strcmp(p->ns->name, "DAV:")) {
337 snprintf(prefix, 8, "x%d\0", pfxnum++);
338 ucx_map_cstr_put(namespaces, p->ns->name, prefix);
339 }
340 }
341
342 s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
343 ucx_buffer_write(s.ptr, 1, s.length, buf);
344
345 // write root element and namespaces
346 s = S("<D:propertyupdate xmlns:D=\"DAV:\"");
347 ucx_buffer_write(s.ptr, 1, s.length, buf);
348 UcxMapIterator mapi = ucx_map_iterator(namespaces);
349 UcxKey key;
350 char *pfxval;
351 UCX_MAP_FOREACH(key, pfxval, mapi) {
352 s = S(" xmlns:");
353 ucx_buffer_write(s.ptr, 1, s.length, buf);
354 s = sstr(pfxval);
355 ucx_buffer_write(s.ptr, 1, s.length, buf);
356 s = S("=\"");
357 ucx_buffer_write(s.ptr, 1, s.length, buf);
358 s = sstrn(key.data, key.len);
359 ucx_buffer_write(s.ptr, 1, s.length, buf);
360 s = S("\"");
361 ucx_buffer_write(s.ptr, 1, s.length, buf);
362 }
363 s = S(">\n");
364 ucx_buffer_write(s.ptr, 1, s.length, buf);
365
366 if(data->set) {
367 s = S("<D:set>\n<D:prop>\n");
368 ucx_buffer_write(s.ptr, 1, s.length, buf);
369 UCX_FOREACH(elm, data->set) {
370 DavProperty *property = elm->data;
371 char *prefix = ucx_map_cstr_get(namespaces, property->ns->name);
372
373 s = S("<");
374 ucx_buffer_write(s.ptr, 1, s.length, buf);
375 s = sstr(prefix);
376 ucx_buffer_write(s.ptr, 1, s.length, buf);
377 s = S(":");
378 ucx_buffer_write(s.ptr, 1, s.length, buf);
379 s = sstr(property->name);
380 ucx_buffer_write(s.ptr, 1, s.length, buf);
381 s = S(">");
382 ucx_buffer_write(s.ptr, 1, s.length, buf);
383 s = sstr(property->value);
384 ucx_buffer_write(s.ptr, 1, s.length, buf);
385 s = S("</");
386 ucx_buffer_write(s.ptr, 1, s.length, buf);
387 s = sstr(prefix);
388 ucx_buffer_write(s.ptr, 1, s.length, buf);
389 s = S(":");
390 ucx_buffer_write(s.ptr, 1, s.length, buf);
391 s = sstr(property->name);
392 ucx_buffer_write(s.ptr, 1, s.length, buf);
393 s = S(">\n");
394 ucx_buffer_write(s.ptr, 1, s.length, buf);
395 }
396 s = S("</D:prop>\n</D:set>\n");
397 ucx_buffer_write(s.ptr, 1, s.length, buf);
398 }
399 if(data->remove) {
400 s = S("<D:set>\n<D:prop>\n");
401 ucx_buffer_write(s.ptr, 1, s.length, buf);
402 UCX_FOREACH(elm, data->set) {
403 DavProperty *property = elm->data;
404 char *prefix = ucx_map_cstr_get(namespaces, property->ns->name);
405
406 s = S("<");
407 ucx_buffer_write(s.ptr, 1, s.length, buf);
408 s = sstr(prefix);
409 ucx_buffer_write(s.ptr, 1, s.length, buf);
410 s = S(":");
411 ucx_buffer_write(s.ptr, 1, s.length, buf);
412 s = sstr(property->name);
413 ucx_buffer_write(s.ptr, 1, s.length, buf);
414 s = S(">");
415 ucx_buffer_write(s.ptr, 1, s.length, buf);
416 s = sstr(property->value);
417 ucx_buffer_write(s.ptr, 1, s.length, buf);
418 s = S("</");
419 ucx_buffer_write(s.ptr, 1, s.length, buf);
420 s = sstr(prefix);
421 ucx_buffer_write(s.ptr, 1, s.length, buf);
422 s = S(":");
423 ucx_buffer_write(s.ptr, 1, s.length, buf);
424 s = sstr(property->name);
425 ucx_buffer_write(s.ptr, 1, s.length, buf);
426 s = S(">\n");
427 ucx_buffer_write(s.ptr, 1, s.length, buf);
428 }
429 s = S("</D:prop>\n</D:set>\n");
430 ucx_buffer_write(s.ptr, 1, s.length, buf);
431 }
432
433 s = S("</D:propertyupdate>\n");
434 ucx_buffer_write(s.ptr, 1, s.length, buf);
435
436 return buf;
437 }
438
439 /* ----------------------------- PUT ----------------------------- */
440
441 static size_t dummy_write(void *buf, size_t s, size_t n, void *data) {
442 fwrite(buf, s, n, stdout);
443 return s*n;
444 }
445
446 CURLcode do_put_request(CURL *handle, void *data, dav_read_func read_func, size_t length) {
447 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
448 curl_easy_setopt(handle, CURLOPT_PUT, 1L);
449 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
450 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
451
452 UcxBuffer *buf = NULL;
453 if(!read_func) {
454 buf = ucx_buffer_new(data, length, 0);
455 buf->size = length;
456 data = buf;
457 read_func = (dav_read_func)ucx_buffer_read;
458 curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)length);
459 } else if(length == 0) {
460 struct curl_slist *headers = NULL;
461 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
462 curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)1);
463 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
464 } else {
465 curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)length);
466 }
467
468 curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_func);
469 curl_easy_setopt(handle, CURLOPT_READDATA, data);
470
471 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
472 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
473
474 CURLcode ret = curl_easy_perform(handle);
475 if(buf) {
476 ucx_buffer_free(buf);
477 }
478 return ret;
479 }
480
481 CURLcode do_delete_request(CURL *handle, UcxBuffer *response) {
482 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "DELETE");
483 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
484 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
485 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
486
487 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
488 curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
489
490 CURLcode ret = curl_easy_perform(handle);
491 return ret;
492 }
493
494 CURLcode do_mkcol_request(CURL *handle) {
495 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "MKCOL");
496 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
497 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
498 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
499
500 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
501 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
502
503 CURLcode ret = curl_easy_perform(handle);
504 return ret;
505 }
506
507
508 CURLcode do_head_request(CURL *handle) {
509 curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD");
510 curl_easy_setopt(handle, CURLOPT_PUT, 0L);
511 curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
512 curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
513 curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
514
515 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
516 curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
517
518 CURLcode ret = curl_easy_perform(handle);
519 curl_easy_setopt(handle, CURLOPT_NOBODY, 0L);
520 return ret;
521 }

mercurial