fix multistatus response creating namespace definitions twice in some cases

Tue, 01 Nov 2022 16:40:03 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 01 Nov 2022 16:40:03 +0100
changeset 403
0f678595d497
parent 402
712aca08da7f
child 404
e0e0754efd46

fix multistatus response creating namespace definitions twice in some cases

src/server/plugins/postgresql/pgtest.c file | annotate | diff | comparison | revisions
src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/webdav/multistatus.c file | annotate | diff | comparison | revisions
src/server/webdav/multistatus.h file | annotate | diff | comparison | revisions
--- a/src/server/plugins/postgresql/pgtest.c	Tue Nov 01 15:59:28 2022 +0100
+++ b/src/server/plugins/postgresql/pgtest.c	Tue Nov 01 16:40:03 2022 +0100
@@ -173,9 +173,11 @@
                         if(value && value->type == XML_TEXT_NODE) {
                             property->value = sstrdup_a(a, scstr((const char*)value->content)).ptr;
                         }
-                        sstr_t pname = sstrcat(2, sstr(property->namespace), sstr(property->name));
-                        ucx_map_sstr_put(properties, pname, property);
-                        free(pname.ptr);
+                        if(property->namespace && property->name) {
+                            sstr_t pname = sstrcat(2, sstr(property->namespace), sstr(property->name));
+                            ucx_map_sstr_put(properties, pname, property);
+                            free(pname.ptr);
+                        }
                     }
                     n = n->next;
                 }
--- a/src/server/test/main.c	Tue Nov 01 15:59:28 2022 +0100
+++ b/src/server/test/main.c	Tue Nov 01 16:40:03 2022 +0100
@@ -62,6 +62,7 @@
 
 int main(int argc, char **argv) {
     pool_init(NULL, NULL, NULL);
+    pblock_init_default_keys();
     vfs_init();
     
     //test();
--- a/src/server/webdav/multistatus.c	Tue Nov 01 15:59:28 2022 +0100
+++ b/src/server/webdav/multistatus.c	Tue Nov 01 16:40:03 2022 +0100
@@ -155,23 +155,6 @@
     writer_putc(out, ':');
     writer_puts(out, sstr((char*)property->name));
     
-    // check if the namespace is already defined
-    WSBool need_nsdef = TRUE;
-    WSNamespace *ns = ucx_map_cstr_get(
-            ms->namespaces,
-            (char*)property->namespace->prefix);
-    if(ns && !strcmp(
-            (const char*)ns->href,
-            (const char*)property->namespace->href))
-    {
-        need_nsdef = FALSE; // prefix and href are the same, no need for nsdef
-    }
-    
-    // send definition for the element's namespace
-    if(need_nsdef) {
-        send_nsdef(property->namespace, out);
-    }
-    
     // send additional namespace definitions required for the value
     WebdavNSList *def = nsdef;
     while(def) {
@@ -420,6 +403,62 @@
     return 0;
 }
 
+/*
+ * should only be called from msresponse_addproperty
+ * 
+ * Adds a property to the error list with the specified statuscode
+ */
+static int msresponse_addproperror(
+        MSResponse *response,
+        WebdavProperty *property,
+        int statuscode)
+{
+    pool_handle_t *pool = response->multistatus->sn->pool;
+    UcxAllocator *a = session_get_allocator(response->multistatus->sn);
+       
+    response->resource.err++;
+      
+    // MSResponse contains a list of properties for each status code
+    // at first find the list for this status code
+    PropertyErrorList *errlist = NULL;
+    PropertyErrorList *list = response->errors;
+    PropertyErrorList *last = NULL;
+    while(list) {
+        if(list->status == statuscode) {
+            errlist = list;
+            break;
+        }
+        last = list;
+        list = list->next;
+    }
+    
+    if(!errlist) {
+        // no list available for this statuscode
+        PropertyErrorList *newelm = pool_malloc(pool,
+                sizeof(PropertyErrorList));
+        if(!newelm) {
+            return 1;
+        }
+        newelm->begin = NULL;
+        newelm->end = NULL;
+        newelm->next = NULL;
+        newelm->status = statuscode;
+        
+        if(last) {
+            last->next = newelm;
+        } else {
+            response->errors = newelm;
+        }
+        errlist = newelm;
+    }
+    
+    // we have the list -> add the new element
+    if(webdav_plist_add(pool, &errlist->begin, &errlist->end, property)) {
+        return 1;
+    }
+    return 0;
+}
+
 int msresponse_addproperty(
         WebdavResource *res,
         WebdavProperty *property,
@@ -564,57 +603,6 @@
     return 0;
 }
 
-int msresponse_addproperror(
-        MSResponse *response,
-        WebdavProperty *property,
-        int statuscode)
-{
-    pool_handle_t *pool = response->multistatus->sn->pool;
-    UcxAllocator *a = session_get_allocator(response->multistatus->sn);
-    
-    response->resource.err++;
-      
-    // MSResponse contains a list of properties for each status code
-    // at first find the list for this status code
-    PropertyErrorList *errlist = NULL;
-    PropertyErrorList *list = response->errors;
-    PropertyErrorList *last = NULL;
-    while(list) {
-        if(list->status == statuscode) {
-            errlist = list;
-            break;
-        }
-        last = list;
-        list = list->next;
-    }
-    
-    if(!errlist) {
-        // no list available for this statuscode
-        PropertyErrorList *newelm = pool_malloc(pool,
-                sizeof(PropertyErrorList));
-        if(!newelm) {
-            return 1;
-        }
-        newelm->begin = NULL;
-        newelm->end = NULL;
-        newelm->next = NULL;
-        newelm->status = statuscode;
-        
-        if(last) {
-            last->next = newelm;
-        } else {
-            response->errors = newelm;
-        }
-        errlist = newelm;
-    }
-    
-    // we have the list -> add the new element
-    if(webdav_plist_add(pool, &errlist->begin, &errlist->end, property)) {
-        return 1;
-    }
-    return 0;
-}
-
 int msresponse_close(WebdavResource *res) {
     MSResponse *response = (MSResponse*)res;
     if(response->closing) {
@@ -646,12 +634,12 @@
         if(!ucx_map_sstr_get(response->properties, key)) {
             // property was not added to this response
             if(ms->proppatch) {
-                if(msresponse_addproperror(response, pl->property, 424)) {
+                if(msresponse_addproperty(res, pl->property, 424)) {
                     ret = REQ_ABORTED;
                     break;
                 }
             } else {
-                if(msresponse_addproperror(response, pl->property, 404)) {
+                if(msresponse_addproperty(res, pl->property, 404)) {
                     ret = REQ_ABORTED;
                     break;
                 }
@@ -668,7 +656,7 @@
         PropertyOkList *elm = response->plist_begin;
         PropertyOkList *nextelm;
         while(elm) {
-            if(msresponse_addproperror(response, elm->property, 424)) {
+            if(msresponse_addproperty(res, elm->property, 424)) {
                 return 1;
             }
             nextelm = elm->next;
--- a/src/server/webdav/multistatus.h	Tue Nov 01 15:59:28 2022 +0100
+++ b/src/server/webdav/multistatus.h	Tue Nov 01 16:40:03 2022 +0100
@@ -142,11 +142,6 @@
         WebdavProperty *property,
         int status);
 
-int msresponse_addproperror(
-        MSResponse *response,
-        WebdavProperty *property,
-        int statuscode);
-
 int msresponse_close(WebdavResource *res);
 
 #ifdef __cplusplus

mercurial