dav/webdav.c

changeset 17
11dffb40cd91
parent 16
5dbef9e07376
child 19
18efd2c2973d
equal deleted inserted replaced
16:5dbef9e07376 17:11dffb40cd91
32 #include <libxml/tree.h> 32 #include <libxml/tree.h>
33 33
34 #include "utils.h" 34 #include "utils.h"
35 #include "webdav.h" 35 #include "webdav.h"
36 #include "methods.h" 36 #include "methods.h"
37 #include "davql.h"
37 #include "ucx/buffer.h" 38 #include "ucx/buffer.h"
38 #include "ucx/utils.h" 39 #include "ucx/utils.h"
39 40
40 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) 41 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
41 42
207 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); 208 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
208 int status = 0; 209 int status = 0;
209 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); 210 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
210 if(ret == CURLE_OK && status == 207) { 211 if(ret == CURLE_OK && status == 207) {
211 //printf("response\n%s\n", rpbuf->space); 212 //printf("response\n%s\n", rpbuf->space);
212 resource = parse_propfind_response(sn, rpbuf); 213 resource = parse_propfind_response(sn, NULL, rpbuf);
213 sn->error = DAV_OK; 214 sn->error = DAV_OK;
214 } else { 215 } else {
215 session_set_error(sn, ret, status); 216 session_set_error(sn, ret, status);
217 }
218 return resource;
219 }
220
221 DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path) {
222 char *url = util_concat_path(sn->base_url, path);
223 CURL *handle = sn->handle;
224 curl_easy_setopt(handle, CURLOPT_URL, url);
225 free(url);
226
227 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
228 DavResource *resource = root;
229 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
230 int status = 0;
231 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
232 if(ret == CURLE_OK && status == 207) {
233 //printf("response\n%s\n", rpbuf->space);
234 resource = parse_propfind_response(sn, resource, rpbuf);
235 sn->error = DAV_OK;
236 } else {
237 session_set_error(sn, ret, status);
238 resource = NULL;
239 }
240 ucx_buffer_free(rpbuf);
241 return resource;
242 }
243
244 UcxList* propfind_stack_push(UcxList *stack, DavResource *children) {
245 while(children) {
246 if(children->iscollection) {
247 stack = ucx_list_prepend(stack, children);
248 }
249 children = children->next;
250 }
251 return stack;
252 }
253
254 DavResource* dav_get2(DavSession *sn, DavGetQuery *query) {
255 char *path;
256 int depth = 0;
257 if(parse_path_query(query->from, &path, &depth)) {
258 sn->error = DAV_ERROR;
259 return NULL;
260 }
261
262 sstr_t ps = query->properties;
263 UcxBuffer *rqbuf;
264 if(!sstrcmp(ps, S("*"))) {
265 rqbuf = create_allprop_propfind_request();
266 } else if(!sstrcmp(ps, S("-"))) {
267 rqbuf = create_propfind_request(NULL);
268 } else {
269 UcxList *proplist = parse_properties_string(sn->context, ps);
270 rqbuf = create_propfind_request(proplist);
271 }
272
273 //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
274 //printf("\n");
275
276 DavResource *resource = dav_propfind(sn, NULL, rqbuf, path);
277 free(path);
278 int error = 0;
279 if(resource && depth == -1) {
280 UcxList *stack = NULL; // stack with davResource* elements
281 stack = propfind_stack_push(stack, resource->children);
282 while(stack) {
283 DavResource *sr = stack->data; // get first element from the stack
284 stack = ucx_list_remove(stack, stack); // remove first element
285 // do propfind request for sr
286 sr = dav_propfind(sn, sr, rqbuf, sr->path);
287 if(!sr) {
288 error = 1;
289 printf("subrequest failed\n");
290 break;
291 }
292 stack = propfind_stack_push(stack, sr->children); // add children
293 }
216 } 294 }
217 return resource; 295 return resource;
218 } 296 }
219 297
220 UcxList* parse_properties_string(DavContext *context, sstr_t str) { 298 UcxList* parse_properties_string(DavContext *context, sstr_t str) {
245 } 323 }
246 free(props); 324 free(props);
247 return proplist; 325 return proplist;
248 } 326 }
249 327
328 DavResource* dav_query(DavSession *sn, char *query, ...) {
329 va_list ap;
330 va_start(ap, query);
331 DavQuery q = dav_ql_parse(query, ap);
332 va_end(ap);
333 DavResource *res = NULL;
334 switch(q.command) {
335 case DAV_QUERY_GET: {
336 res = dav_get2(sn, q.command_data);
337 free_get_query(q.command_data);
338 break;
339 }
340 }
341 return res;
342 }
250 343
251 344
252 345
253 DavResource* dav_resource_new(DavSession *sn, char *path) { 346 DavResource* dav_resource_new(DavSession *sn, char *path) {
254 char *url = util_concat_path(sn->base_url, path); 347 char *url = util_concat_path(sn->base_url, path);

mercurial