dav/webdav.c

changeset 17
11dffb40cd91
parent 16
5dbef9e07376
child 19
18efd2c2973d
--- a/dav/webdav.c	Fri Aug 16 12:41:30 2013 +0200
+++ b/dav/webdav.c	Sat Aug 17 12:04:04 2013 +0200
@@ -34,6 +34,7 @@
 #include "utils.h"
 #include "webdav.h"
 #include "methods.h"
+#include "davql.h"
 #include "ucx/buffer.h"
 #include "ucx/utils.h"
 
@@ -209,10 +210,87 @@
     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK && status == 207) {
         //printf("response\n%s\n", rpbuf->space); 
-        resource = parse_propfind_response(sn, rpbuf);
+        resource = parse_propfind_response(sn, NULL, rpbuf);
+        sn->error = DAV_OK;
+    } else  {
+        session_set_error(sn, ret, status);
+    }
+    return resource;
+}
+
+DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path) {
+    char *url = util_concat_path(sn->base_url, path);  
+    CURL *handle = sn->handle;
+    curl_easy_setopt(handle, CURLOPT_URL, url);
+    free(url);
+    
+    UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
+    DavResource *resource = root;
+    CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
+    int status = 0;
+    curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
+    if(ret == CURLE_OK && status == 207) {
+        //printf("response\n%s\n", rpbuf->space); 
+        resource = parse_propfind_response(sn, resource, rpbuf);
         sn->error = DAV_OK;
     } else  {
         session_set_error(sn, ret, status);
+        resource = NULL;
+    }
+    ucx_buffer_free(rpbuf);
+    return resource;
+}
+
+UcxList* propfind_stack_push(UcxList *stack, DavResource *children) {
+    while(children) {
+        if(children->iscollection) {
+            stack = ucx_list_prepend(stack, children);
+        }
+        children = children->next;
+    }
+    return stack;
+}
+
+DavResource* dav_get2(DavSession *sn, DavGetQuery *query) {
+    char *path;
+    int depth = 0;
+    if(parse_path_query(query->from, &path, &depth)) {
+        sn->error = DAV_ERROR;
+        return NULL;
+    }
+    
+    sstr_t ps = query->properties;
+    UcxBuffer *rqbuf;
+    if(!sstrcmp(ps, S("*"))) {
+        rqbuf = create_allprop_propfind_request();
+    } else if(!sstrcmp(ps, S("-"))) {
+        rqbuf = create_propfind_request(NULL);
+    } else {
+        UcxList *proplist = parse_properties_string(sn->context, ps);
+        rqbuf = create_propfind_request(proplist);
+    }
+    
+    //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
+    //printf("\n");
+    
+    DavResource *resource = dav_propfind(sn, NULL, rqbuf, path);
+    free(path);
+    int error = 0;
+    if(resource && depth == -1) {
+        UcxList *stack = NULL; // stack with davResource* elements
+        stack = propfind_stack_push(stack, resource->children);
+        while(stack) {
+            DavResource *sr = stack->data; // get first element from the stack
+            stack = ucx_list_remove(stack, stack); // remove first element
+            // do propfind request for sr
+            sr = dav_propfind(sn, sr, rqbuf, sr->path);
+            if(!sr) {
+                error = 1;
+                printf("subrequest failed\n");
+                break;
+            }
+            stack = propfind_stack_push(stack, sr->children); // add children
+        }
     }
     return resource;
 }
@@ -247,6 +325,21 @@
     return proplist;
 }
 
+DavResource* dav_query(DavSession *sn, char *query, ...) {
+    va_list ap;
+    va_start(ap, query);
+    DavQuery q = dav_ql_parse(query, ap);
+    va_end(ap);
+    DavResource *res = NULL;
+    switch(q.command) {
+        case DAV_QUERY_GET: {
+            res = dav_get2(sn, q.command_data);
+            free_get_query(q.command_data);
+            break;
+        }
+    }
+    return res;
+}
 
 
 

mercurial