src/server/webdav/webdav.c

branch
webdav
changeset 309
fc021bd576d4
parent 307
8787cb5ebab3
child 318
60870dbac94f
--- 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