libidav/webdav.c

changeset 135
664aeaec8d25
parent 134
4bccc18820e8
child 147
458a8dc68048
--- a/libidav/webdav.c	Tue Jul 07 20:47:02 2015 +0200
+++ b/libidav/webdav.c	Wed Jul 08 17:31:26 2015 +0200
@@ -35,9 +35,10 @@
 #include "webdav.h"
 #include "session.h"
 #include "methods.h"
-#include "davql.h"
 #include "ucx/buffer.h"
 #include "ucx/utils.h"
+#include "davqlparser.h"
+#include "davqlexec.h"
 
 
 DavContext* dav_context_new() {
@@ -282,7 +283,8 @@
     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, NULL, 0);
+        // TODO: use PropfindParser
+        resource = parse_propfind_response(sn, resource, rpbuf);
         sn->error = DAV_OK;
     } else  {
         dav_session_set_error(sn, ret, status);
@@ -296,7 +298,8 @@
     return resource;
 }
 
-int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, DavQOp *cond, size_t len) {
+
+int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf) {
     // clean resource properties
     DavResourceData *data = root->data;
     size_t pcount = data->properties->count;
@@ -326,7 +329,7 @@
     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, cond, len);
+        resource = parse_propfind_response(sn, resource, rpbuf);
         sn->error = DAV_OK;
     } else  {
         dav_session_set_error(sn, ret, status);
@@ -336,75 +339,6 @@
     return error;
 }
 
-static 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_query_get(DavSession *sn, DavGetQuery *query) {
-    char *path;
-    int depth = query->depth;
-    /*
-    if(parse_path_query(query->from, &path, &depth)) {
-        sn->error = DAV_ERROR;
-        return NULL;
-    }
-    */
-    path = sstrdup(query->from).ptr;
-    
-    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(sn, NULL);
-    } else {
-        UcxList *proplist = parse_properties_string(sn->context, ps);
-        rqbuf = create_propfind_request(sn, proplist);
-        UCX_FOREACH(elm, proplist) {
-            DavProperty *prop = elm->data;
-            free(prop->name);
-            free(prop);
-        }
-        ucx_list_free(proplist);
-    }
-    
-    //fwrite(rqbuf->space, 1, rqbuf->size, stdout);
-    //printf("\n");
-    
-    DavResource *resource = dav_resource_new(sn, path);
-    free(path);
-    if(dav_propfind(sn, resource, rqbuf, query->condition, query->condlen)) {
-        dav_resource_free(resource);
-        resource = NULL;
-    }
-    
-    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
-            if(dav_propfind(sn, sr, rqbuf, query->condition, query->condlen)) {
-                error = 1;
-                printf("subrequest failed\n");
-                break;
-            }
-            stack = propfind_stack_push(stack, sr->children); // add children
-        }
-    }
-    
-    ucx_buffer_free(rqbuf);
-    return resource;
-}
-
 UcxList* parse_properties_string(DavContext *context, sstr_t str) {
     UcxList *proplist = NULL;
     ssize_t nprops = 0;
@@ -435,23 +369,24 @@
 }
 
 DavResource* dav_query(DavSession *sn, char *query, ...) {
+    DavQLStatement *stmt = dav_parse_statement(sstr(query));
+    if(!stmt) {
+        sn->error = DAV_ERROR;
+        return NULL;
+    }
+    if(stmt->errorcode != 0) {
+        sn->error = DAV_QL_ERROR;
+        dav_free_statement(stmt);
+        return NULL;
+    }
+    
     va_list ap;
     va_start(ap, query);
-    DavQuery q = dav_ql_parse(query, ap);
+    DavResult result = dav_statement_execv(sn, stmt, ap);
     va_end(ap);
-    DavResource *res = NULL;
-    switch(q.command) {
-        case DAV_QUERY_GET: {
-            res = dav_query_get(sn, q.command_data);
-            free_get_query(q.command_data);
-            break;
-        }
-        case DAV_QUERY_ERROR: {
-            // TODO
-            break;
-        }
-    }
-    return res;
+    
+    dav_free_statement(stmt);
+    return result.result;
 }
 
 

mercurial