src/server/safs/nametrans.c

changeset 385
a1f4cb076d2f
parent 369
e28ee9875a90
child 415
d938228c382e
--- a/src/server/safs/nametrans.c	Tue Aug 13 22:14:32 2019 +0200
+++ b/src/server/safs/nametrans.c	Sat Sep 24 16:26:10 2022 +0200
@@ -32,6 +32,80 @@
 #include "../daemon/request.h"
 #include "../util/pblock.h"
 #include "../util/util.h"
+#include "../public/webdav.h"
+
+#include "../daemon/session.h"
+#include "../daemon/config.h"
+
+#include "../public/vfs.h"
+
+static int initialize_dav_repo(pblock *pb, Session *sn, Request *rq, WebdavRepository *repo) {
+    if(repo->vfs) {
+        VFS *vfs = repo->vfs->create(sn, rq, pb, repo->vfsInitData);
+        if(!vfs) {
+            return REQ_ABORTED;
+        }
+        rq->vfs = vfs;
+    }
+    
+    WebdavBackend *backend_first = NULL;
+    WebdavBackend *backend_last = NULL;
+    UCX_FOREACH(elm, repo->davBackends) {
+        WebdavBackendInitData *davInit = elm->data;
+        WebdavBackend *backend = davInit->davType->create(sn, rq, pb, davInit->davInitData);
+        if(!backend) {
+            return REQ_ABORTED;
+        }
+        
+        if(backend_last) {
+            backend_last->next = backend;
+            backend_last = backend;
+        } else {
+            backend_first = backend;
+            backend_last = backend;
+        }
+    }
+    rq->davCollection = backend_first;
+    
+    return 0;
+}
+
+static int nametrans_set_dav_repository(pblock *pb, Session *sn, Request *rq) {
+    char *dav = pblock_findkeyval(pb_key_dav, pb);
+    if(!dav) return 0;
+    
+    ServerConfiguration *config = session_get_config(sn);
+    WebdavRepository *repo = ucx_map_cstr_get(config->dav, dav);
+    
+    if(!repo) {
+        log_ereport(LOG_MISCONFIG, "nametrans: unknown dav repository '%s'", dav);
+        return REQ_ABORTED;
+    }
+    
+    return initialize_dav_repo(pb, sn, rq, repo);
+}
+
+static int nametrans_set_vfs(pblock *pb, Session *sn, Request *rq) {
+    char *vfsclass = pblock_findkeyval(pb_key_vfsclass, pb);
+    if(!vfsclass) return 0;
+    
+    VFS *vfs = vfs_create(sn, rq, vfsclass, pb, NULL);
+    if(!vfs) {
+        return 1;
+    }
+    rq->vfs = vfs;
+    return 0;
+}
+
+static int nametrans_set_dav(pblock *pb, Session *sn, Request *rq) {
+    char *davclass = pblock_findkeyval(pb_key_davclass, pb);
+    if(!davclass) return 0;
+    
+    WebdavBackend *dav = webdav_create(sn, rq, davclass, pb, NULL);
+    
+    rq->davCollection = dav;
+    return 0;
+}
 
 /*
  * assign_name
@@ -66,6 +140,19 @@
             i++;
         }
     }
+    
+    if(nametrans_set_dav_repository(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "assign-name: cannot create dav repository: name=%s from=%s", name, from);
+        return REQ_ABORTED;
+    }
+    if(nametrans_set_vfs(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "assign-name: cannot create VFS: name=%s from=%s", name, from);
+        return REQ_ABORTED;
+    }
+    if(nametrans_set_dav(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "assign-name: cannot create Webdav Backend: name=%s from=%s", name, from);
+        return REQ_ABORTED;
+    }
 
     // add object to rq->vars
     pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
@@ -89,6 +176,11 @@
         return REQ_ABORTED;
     }
     
+    if(nametrans_set_vfs(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "document-root: cannot create VFS");
+        return REQ_ABORTED;
+    }
+    
     sstr_t root_str = sstr(root);
     sstr_t uri_str = sstr(pblock_findkeyval(pb_key_uri, rq->reqpb));  
     
@@ -106,6 +198,9 @@
  * from     prefix
  * dir      file system directory
  * name     (optional) object name
+ * dav      (optional) dav repository name
+ * vfsclass (optional) vfs name
+ * davclass (optional) dav backend
  * 
  */
 int pfx2dir(pblock *pb, Session *sn, Request *rq) {
@@ -147,6 +242,19 @@
         uri++;
     }
     
+    if(nametrans_set_dav_repository(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "pfx2dir: cannot create dav repository: from=%s dir=%s name=%s", from, dir, name);
+        return REQ_ABORTED;
+    }
+    if(nametrans_set_vfs(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "pfx2dir: cannot create VFS: from=%s dir=%s name=%s", from, dir, name);
+        return REQ_ABORTED;
+    }
+    if(nametrans_set_dav(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "pfx2dir: cannot create Webdav Backend: from=%s dir=%s name=%s", from, dir, name);
+        return REQ_ABORTED;
+    }
+    
     request_set_path(sstr(dir), sstr(uri), rq->vars);
     
     if(name) {
@@ -192,6 +300,11 @@
         return REQ_ABORTED;
     }
     
+    if(nametrans_set_vfs(pb, sn, rq)) {
+        log_ereport(LOG_FAILURE, "simple-rewrite: cannot create VFS: from=%s root=%s path=%s name=%s", from, root, path, name);
+        return REQ_ABORTED;
+    }
+    
     char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
     sstr_t u = sstr(uri);
     sstr_t f = sstr(from);

mercurial