added document-root and pfx2dir nametrans safs

Mon, 04 Nov 2013 10:55:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 04 Nov 2013 10:55:27 +0100
changeset 96
0185b13bf41f
parent 95
74a81d9e19d0
child 97
09fbefc0e6a9

added document-root and pfx2dir nametrans safs

src/server/daemon/httprequest.c file | annotate | diff | comparison | revisions
src/server/daemon/ws-fn.c file | annotate | diff | comparison | revisions
src/server/safs/nametrans.c file | annotate | diff | comparison | revisions
src/server/safs/nametrans.h file | annotate | diff | comparison | revisions
src/server/util/util.c file | annotate | diff | comparison | revisions
src/server/util/util.h file | annotate | diff | comparison | revisions
--- a/src/server/daemon/httprequest.c	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/daemon/httprequest.c	Mon Nov 04 10:55:27 2013 +0100
@@ -526,13 +526,15 @@
             printf("docroot too short\n");
             return REQ_ABORTED; /* docroot too short */
         }
+        
         // if there is a trailing '/', remove it
+        /*
         if(docroot.ptr[docroot.length - 1] == '/') {
             docroot.length--;
         }
-
+        
         sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb));
-
+        
         sstr_t translated;
         translated.length = docroot.length + uri.length;
         translated.ptr = alloca(translated.length + 1);
@@ -543,6 +545,9 @@
             translated.ptr,
             translated.length,
             rq->rq.vars);
+        */
+        sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb));
+        util_add_ppath(docroot, uri, rq->rq.vars);
     }
 
     return REQ_PROCEED;
--- a/src/server/daemon/ws-fn.c	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/daemon/ws-fn.c	Mon Nov 04 10:55:27 2013 +0100
@@ -41,10 +41,10 @@
 #include "../admin/admin.h"
 
 struct FuncStruct webserver_funcs[] = {
-    { "init-test", init_test, NULL, NULL, 0 },
     { "load-modules", load_modules, NULL, NULL, 0},
-    { "test-nametrans", test_nametrans, NULL, NULL, 0 },
     { "assign-name", assign_name, NULL, NULL, 0},
+    { "document-root", document_root, NULL, NULL, 0},
+    { "pfx2dir", pfx2dir, NULL, NULL, 0},
     { "type-by-extension", object_type_by_extension, NULL, NULL, 0},
     { "send-file", send_file, NULL, NULL, 0},
     { "common-index", service_index, NULL, NULL, 0},
--- a/src/server/safs/nametrans.c	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/safs/nametrans.c	Mon Nov 04 10:55:27 2013 +0100
@@ -30,16 +30,7 @@
 
 #include "../daemon/log.h"
 #include "../util/pblock.h"
-
-int test_nametrans(pblock *pb, Session *sn, Request *rq) {
-    printf("test_nametrans...\n");
-
-    // set ppath
-    //char *ppath = "/export/home/olaf/Desktop/hello.txt";
-    //pblock_kvinsert(pb_key_ppath, ppath, strlen(ppath), rq->vars);
-
-    return REQ_NOACTION;
-}
+#include "../util/util.h"
 
 /*
  * assign_name
@@ -80,3 +71,92 @@
 
     return REQ_NOACTION;
 }
+
+/*
+ * document_root
+ * 
+ * Specifies the document root directory.
+ * 
+ * pblock parameter:
+ * root   path to document root
+ */
+int document_root(pblock *pb, Session *sn, Request *rq) {
+    char *root = pblock_findkeyval(pb_key_root, pb);
+    if(!root) {
+        log_ereport(LOG_MISCONFIG, "document-root: missing rootparameter");
+        protocol_status(sn, rq, 500, NULL);
+        return REQ_ABORTED;
+    }
+    
+    sstr_t root_str = sstr(root);
+    sstr_t uri_str = sstr(pblock_findkeyval(pb_key_uri, rq->reqpb));  
+    util_add_ppath(root_str, uri_str, rq->vars);
+    
+    return REQ_PROCEED;
+}
+
+/*
+ * pfx2dir
+ * 
+ * ...
+ * 
+ * pblock parameter:
+ * from     prefix
+ * dir      file system directory
+ * name     (optional) object name
+ * 
+ */
+int pfx2dir(pblock *pb, Session *sn, Request *rq) {
+    char *from = pblock_findkeyval(pb_key_from, pb);
+    char *dir = pblock_findkeyval(pb_key_dir, pb);
+    char *name = pblock_findkeyval(pb_key_name, pb);
+    
+    if(!from || !dir) {
+        if(!from && dir) {
+            log_ereport(LOG_MISCONFIG, "pfx2dir: missing from parameter");
+        } else if(!dir && from) {
+            log_ereport(LOG_MISCONFIG, "pfx2dir: missing dir parameter");
+        } else {
+            log_ereport(
+                    LOG_MISCONFIG,
+                    "pfx2dir: missing from and dir parameter");
+        }
+        protocol_status(sn, rq, 500, NULL);
+        return REQ_ABORTED;
+    }
+    
+    // check prefix
+    char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
+    char fc;
+    char uc;
+    int i = 0;
+    while((fc = from[i]) != 0) {
+        uc = uri[i];
+        if(fc != uc) {
+            return REQ_NOACTION;
+        }
+        i++;
+    }
+    
+    // url has the specified prefix
+    
+    uri = uri + i;
+    if(uri[0] == '/') {
+        uri++;
+    }
+    sstr_t ppath = util_path_append(sn->pool, dir, uri);
+    pblock_kvinsert(
+            pb_key_ppath,
+            ppath.ptr,
+            ppath.length,
+            rq->vars);
+    
+    if(name) {
+        // add object to rq->vars
+        pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
+    }
+    
+    return REQ_PROCEED;
+}
+
+
--- a/src/server/safs/nametrans.h	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/safs/nametrans.h	Mon Nov 04 10:55:27 2013 +0100
@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-int test_nametrans(pblock *pb, Session *sn, Request *rq);
-
 int assign_name(pblock *pb, Session *sn, Request *rq);
+int document_root(pblock *pb, Session *sn, Request *rq);
+int pfx2dir(pblock *pb, Session *sn, Request *rq);
 
 #ifdef	__cplusplus
 }
--- a/src/server/util/util.c	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/util/util.c	Mon Nov 04 10:55:27 2013 +0100
@@ -56,6 +56,7 @@
 #include <ucx/string.h>
 #include <ucx/mempool.h>
 
+#include "pblock.h"
 #include "util.h"
 
 
@@ -512,6 +513,22 @@
     return path;
 }
 
+void util_add_ppath(sstr_t root, sstr_t path, pblock *vars) {
+    // concat path
+    sstr_t translated_path;
+    translated_path.length = root.length + path.length;
+    translated_path.ptr = alloca(translated_path.length);
+    
+    translated_path = sstrncat(translated_path, 2, root, path);
+    
+    // add path to specified pblock
+    pblock_kvinsert(
+            pb_key_ppath,
+            translated_path.ptr,
+            translated_path.length,
+            vars);
+}
+
 
 // new - code in parts from params.cpp
 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) {
--- a/src/server/util/util.h	Sun Nov 03 16:41:42 2013 +0100
+++ b/src/server/util/util.h	Mon Nov 04 10:55:27 2013 +0100
@@ -230,6 +230,8 @@
 sstr_t util_path_append(pool_handle_t *pool, char *path, char *child);
 NSAPI_PUBLIC
 sstr_t util_path_remove_last(sstr_t path);
+NSAPI_PUBLIC
+void util_add_ppath(sstr_t root, sstr_t path, pblock *vars);
 
 /* --- End common function prototypes --- */
 

mercurial