implement pg getcontentlength property webdav

Mon, 25 Apr 2022 18:30:33 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Apr 2022 18:30:33 +0200
branch
webdav
changeset 309
fc021bd576d4
parent 308
c3cad8f51a24
child 310
523fe96baeca

implement pg getcontentlength property

src/server/plugins/postgresql/webdav.c file | annotate | diff | comparison | revisions
src/server/public/webdav.h file | annotate | diff | comparison | revisions
src/server/webdav/multistatus.c file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
--- a/src/server/plugins/postgresql/webdav.c	Mon Apr 25 13:54:27 2022 +0200
+++ b/src/server/plugins/postgresql/webdav.c	Mon Apr 25 18:30:33 2022 +0200
@@ -419,6 +419,7 @@
             }
             if(vfsprops.getcontentlength) {
                 char *contentlength = PQgetvalue(result, r, 7);
+                webdav_resource_add_dav_stringproperty(resource, pool, "getcontentlength", contentlength, strlen(contentlength));
             }
             if(vfsprops.getetag) {
                 
--- a/src/server/public/webdav.h	Mon Apr 25 13:54:27 2022 +0200
+++ b/src/server/public/webdav.h	Mon Apr 25 18:30:33 2022 +0200
@@ -433,6 +433,21 @@
         pool_handle_t *pool,
         const char *name);
 
+int webdav_resource_add_dav_stringproperty(
+        WebdavResource *res,
+        pool_handle_t pool,
+        const char *name,
+        const char *str,
+        size_t len);
+int webdav_resource_add_stringproperty(
+        WebdavResource *res,
+        pool_handle_t pool,
+        const char *xmlns_prefix,
+        const char *xmlns_href,
+        const char *name,
+        const char *str,
+        size_t len);
+
 int webdav_property_set_value(
         WebdavProperty *property,
         pool_handle_t *pool,
--- a/src/server/webdav/multistatus.c	Mon Apr 25 13:54:27 2022 +0200
+++ b/src/server/webdav/multistatus.c	Mon Apr 25 18:30:33 2022 +0200
@@ -258,7 +258,9 @@
 int multistatus_send(Multistatus *ms, SYS_NETFD net) { 
     // make sure every resource is closed
     if(ms->current && !ms->current->resource.isclosed) {
-        msresponse_close((WebdavResource*)ms->current);
+        if(msresponse_close((WebdavResource*)ms->current)) {
+            return 1;
+        }
     }
     
     // start http response
--- a/src/server/webdav/webdav.c	Mon Apr 25 13:54:27 2022 +0200
+++ b/src/server/webdav/webdav.c	Mon Apr 25 18:30:33 2022 +0200
@@ -1012,6 +1012,84 @@
     return property;
 }
 
+int webdav_resource_add_dav_stringproperty(
+        WebdavResource *res,
+        pool_handle_t pool,
+        const char *name,
+        const char *str,
+        size_t len)
+{
+    WebdavProperty *property = webdav_dav_property(pool, name);
+    if(!property) {
+        return 1;
+    }
+    
+    property->name = pool_strdup(pool, name);
+    if(!property->name) {
+        return 1;
+    }
+    
+    char *value = pool_malloc(pool, len+1);
+    if(!value) {
+        return 1;
+    }
+    memcpy(value, str, len);
+    value[len] = '\0';
+    property->value.text.str = value;
+    property->value.text.length = len;
+    property->vtype = WS_VALUE_TEXT;
+    
+    return res->addproperty(res, property, 200);
+}
+
+int webdav_resource_add_stringproperty(
+        WebdavResource *res,
+        pool_handle_t pool,
+        const char *xmlns_prefix,
+        const char *xmlns_href,
+        const char *name,
+        const char *str,
+        size_t len)
+{
+    WebdavProperty *property = pool_malloc(pool, sizeof(WebdavProperty));
+    if(!property) {
+        return 1;
+    }
+    memset(property, 0, sizeof(WebdavProperty));
+    
+    property->name = pool_strdup(pool, name);
+    if(!property->name) {
+        return 1;
+    }
+    
+    xmlNs *ns = pool_malloc(pool, sizeof(xmlNs));
+    if(!ns) {
+        return 1;
+    }
+    memset(ns, 0, sizeof(xmlNs));
+    ns->prefix = (const xmlChar*)pool_strdup(pool, xmlns_prefix);
+    ns->href = (const xmlChar*)pool_strdup(pool, xmlns_href);
+    if(!ns->prefix || !ns->href) {
+        return 1;
+    }
+    
+    char *value = pool_malloc(pool, len+1);
+    if(!value) {
+        return 1;
+    }
+    memcpy(value, str, len);
+    value[len] = '\0';
+    property->value.text.str = value;
+    property->value.text.length = len;
+    property->vtype = WS_VALUE_TEXT;
+    
+    property->value.text.str = value;
+    property->value.text.length = len;
+    property->vtype = WS_VALUE_TEXT;
+    
+    return res->addproperty(res, property, 200);
+}
+
 int webdav_property_set_value(
         WebdavProperty *p,
         pool_handle_t *pool,
@@ -1072,22 +1150,6 @@
     return ret;
 }
 
-static inline int w_addprop(
-        WebdavResource *res,
-        pool_handle_t *pool,
-        const char *name,
-        char *value)
-{
-    WebdavProperty *p = webdav_dav_property(pool, name);
-    if(!p) {
-        return 1;
-    }
-    if(webdav_property_set_value(p, pool, value)) {
-        return 1;
-    }
-    return res->addproperty(res, p, 200);
-}
-
 int webdav_add_vfs_properties(
         WebdavResource *res,
         pool_handle_t *pool,
@@ -1108,7 +1170,7 @@
         }
         uint64_t contentlength = s->st_size;
         snprintf(buf, 64, "%" PRIu64, contentlength);
-        if(w_addprop(res, pool, "getcontentlength", buf)) {
+        if(webdav_resource_add_dav_stringproperty(res, pool, "getcontentlength", buf, strlen(buf))) {
             return 1;
         }
     }
@@ -1124,7 +1186,7 @@
         
         if(mtm) {
             strftime(buf, HTTP_DATE_LEN, HTTP_DATE_FMT, mtm);
-            if(w_addprop(res, pool, "getlastmodified", buf)) {
+            if(webdav_resource_add_dav_stringproperty(res, pool, "getlastmodified", buf, strlen(buf))) {
                 return 1;
             }
         } else {
@@ -1144,7 +1206,7 @@
             "\"%x-%x\"",
             (int)s->st_size,
             (int)s->st_mtim.tv_sec);
-        if(w_addprop(res, pool, "getetag", buf)) {
+        if(webdav_resource_add_dav_stringproperty(res, pool, "getetag", buf, strlen(buf))) {
             return 1;
         }
     }

mercurial