add plist iterator webdav

Sun, 29 Dec 2019 22:39:35 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Dec 2019 22:39:35 +0100
branch
webdav
changeset 215
68e824ba4a4f
parent 214
4d7ac67a1c14
child 216
ce2866ec97f6

add plist iterator

src/server/public/webdav.h file | annotate | diff | comparison | revisions
src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/test/webdav.c file | annotate | diff | comparison | revisions
src/server/test/webdav.h file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
--- a/src/server/public/webdav.h	Sun Dec 29 21:43:14 2019 +0100
+++ b/src/server/public/webdav.h	Sun Dec 29 22:39:35 2019 +0100
@@ -46,6 +46,8 @@
 typedef struct WebdavPList            WebdavPList;
 typedef struct WebdavNSList           WebdavNSList;
 
+typedef struct WebdavPListIterator    WebdavPListIterator;
+
 typedef enum   WebdavLockScope        WebdavLockScope;
 typedef enum   WebdavLockType         WebdavLockType;
 
@@ -124,6 +126,13 @@
     WebdavNSList *next;
 };
 
+struct WebdavPListIterator {
+    WebdavPList **list;
+    WebdavPList *cur;
+    WebdavPList *next;
+    size_t      index;
+};
+
 enum WebdavLockScope {
     WEBDAV_LOCK_EXCLUSIVE = 0,
     WEBDAV_LOCK_SHARED,
@@ -273,6 +282,10 @@
 
 size_t webdav_plist_count(WebdavPList *list);
 
+WebdavPListIterator webdav_plist_iterator(WebdavPList **list);
+int webdav_plist_iterator_next(WebdavPListIterator *i, WebdavPList **cur);
+void webdav_plist_iterator_remove_current(WebdavPListIterator *i);
+
 WSNamespace* webdav_dav_namespace(void);
 WebdavProperty* webdav_dav_property(
         pool_handle_t *pool,
--- a/src/server/test/main.c	Sun Dec 29 21:43:14 2019 +0100
+++ b/src/server/test/main.c	Sun Dec 29 22:39:35 2019 +0100
@@ -66,6 +66,7 @@
     ucx_test_register(suite, test_proppatch_parse);
     ucx_test_register(suite, test_lock_parse);
     ucx_test_register(suite, test_rqbody2buffer);
+    ucx_test_register(suite, test_webdav_plist_iterator);
     ucx_test_register(suite, test_msresponse_addproperty);
     ucx_test_register(suite, test_webdav_propfind_init);
     
--- a/src/server/test/webdav.c	Sun Dec 29 21:43:14 2019 +0100
+++ b/src/server/test/webdav.c	Sun Dec 29 22:39:35 2019 +0100
@@ -38,6 +38,39 @@
 
 #include "webdav.h"
 
+static int test_init(
+        Session **out_sn,
+        Request **out_rq,
+        WebdavPropfindRequest **out_propfind,
+        const char *xml)
+{
+    Session *sn = testutil_session();
+    Request *rq = testutil_request(sn->pool, "PROPFIND", "/");
+    
+    int error = 0;
+    
+    WebdavPropfindRequest *propfind = propfind_parse(
+            sn,
+            rq,
+            xml,
+            strlen(xml),
+            &error);
+    
+    if(error) {
+        return 1;
+    }
+    
+    if(!propfind || !propfind->properties) {
+        return 1;
+    }
+    
+    *out_sn = sn;
+    *out_rq = rq;
+    *out_propfind = propfind;
+    return 0;
+}
+
+
 UCX_TEST(test_propfind_parse) {
     Session *sn = testutil_session();
     Request *rq = testutil_request(sn->pool, "PROPFIND", "/");
@@ -370,6 +403,56 @@
     UCX_TEST_END;
 }
 
+UCX_TEST(test_webdav_plist_iterator) {
+    Session *sn;
+    Request *rq;
+    WebdavPropfindRequest *propfind;
+    
+    UCX_TEST_BEGIN;
+    UCX_TEST_ASSERT(!test_init(&sn, &rq, &propfind, TEST_PROPFIND1), "init failed");
+    
+    WebdavPList *properties = propfind->properties;
+    size_t count = 0;
+    
+    WebdavPListIterator i = webdav_plist_iterator(&properties);
+    WebdavPList *cur;
+    while(webdav_plist_iterator_next(&i, &cur)) {
+        switch(i.index) {
+            case 0: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "displayname"), "wrong property 1");
+                break;
+            }
+            case 1: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "getcontentlength"), "wrong property 2");
+                break;
+            }
+            case 2: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "getcontenttype"), "wrong property 3");
+                break;
+            }
+            case 3: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "getlastmodified"), "wrong property 4");
+                break;
+            }
+            case 4: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "resourcetype"), "wrong property 5");
+                break;
+            }
+            case 5: {
+                UCX_TEST_ASSERT(!strcmp(cur->property->name, "getetag"), "wrong property 6");
+                break;
+            }
+        }
+        count++;
+    }
+    
+    UCX_TEST_ASSERT(count == propfind->propcount, "wrong count");
+    
+    
+    UCX_TEST_END;
+    testutil_destroy_session(sn);
+}
+
 UCX_TEST(test_msresponse_addproperty) {
     Session *sn = testutil_session();
     Request *rq = testutil_request(sn->pool, "PROPFIND", "/");
@@ -502,44 +585,12 @@
 
 /* ----------------------------------------------------------------------*/
 
-static int backend_test_init(
-        Session **out_sn,
-        Request **out_rq,
-        WebdavPropfindRequest **out_propfind,
-        const char *xml)
-{
-    Session *sn = testutil_session();
-    Request *rq = testutil_request(sn->pool, "PROPFIND", "/");
-    
-    int error = 0;
-    
-    WebdavPropfindRequest *propfind = propfind_parse(
-            sn,
-            rq,
-            xml,
-            strlen(xml),
-            &error);
-    
-    if(error) {
-        return 1;
-    }
-    
-    if(!propfind || !propfind->properties) {
-        return 1;
-    }
-    
-    *out_sn = sn;
-    *out_rq = rq;
-    *out_propfind = propfind;
-    return 0;
-}
-
 UCX_TEST(test_webdav_propfind_init) {
     Session *sn;
     Request *rq;
     WebdavPropfindRequest *propfind;
     UCX_TEST_BEGIN;
-    UCX_TEST_ASSERT(!backend_test_init(&sn, &rq, &propfind, TEST_PROPFIND1), "init failed");
+    UCX_TEST_ASSERT(!test_init(&sn, &rq, &propfind, TEST_PROPFIND1), "init failed");
     
     UcxList *requests = NULL;
     int err = webdav_propfind_init(&backend1, propfind, "/", &requests);
@@ -551,6 +602,9 @@
     WebdavPropfindRequest *p1 = requests->data;
     WebdavPropfindRequest *p2 = requests->next->data;
     
+    // backend1 removes the first property from the plist
+    // backend2 should have one property less 
+    
     UCX_TEST_ASSERT(p1 && p2, "missing requests objects");
     UCX_TEST_ASSERT(p1 != p2, "request objects equal");
     UCX_TEST_ASSERT(p1->properties != p2->properties, "plists equal");
--- a/src/server/test/webdav.h	Sun Dec 29 21:43:14 2019 +0100
+++ b/src/server/test/webdav.h	Sun Dec 29 22:39:35 2019 +0100
@@ -44,6 +44,8 @@
 
 UCX_TEST(test_rqbody2buffer);
 
+UCX_TEST(test_webdav_plist_iterator);
+
 UCX_TEST(test_msresponse_addproperty);
 
 UCX_TEST(test_webdav_propfind_init);
--- a/src/server/webdav/webdav.c	Sun Dec 29 21:43:14 2019 +0100
+++ b/src/server/webdav/webdav.c	Sun Dec 29 22:39:35 2019 +0100
@@ -654,6 +654,43 @@
     return count;
 }
 
+WebdavPListIterator webdav_plist_iterator(WebdavPList **list) {
+    WebdavPListIterator i;
+    i.list = list;
+    i.cur = NULL;
+    i.next = *list;
+    i.index;
+    return i;
+}
+
+int webdav_plist_iterator_next(WebdavPListIterator *i, WebdavPList **cur) {
+    if(i->cur) {
+        i->index++;
+    }
+    
+    i->cur = i->next;
+    i->next = i->cur ? i->cur->next : NULL;
+    *cur = i->cur;
+    
+    return i->cur != NULL;
+}
+
+void webdav_plist_iterator_remove_current(WebdavPListIterator *i) {
+    WebdavPList *cur = i->cur;
+    if(cur->prev) {
+        cur->prev = cur->next;
+        if(cur->next) {
+            cur->next->prev = cur->prev;
+        }
+    } else {
+        *i->list = cur->next;
+        if(cur->next) {
+            cur->next->prev = NULL;
+        }
+    }
+}
+
+
 WSNamespace* webdav_dav_namespace(void) {
     return &dav_namespace;
 }

mercurial