adds set-variable saf

Tue, 24 Jan 2017 17:36:04 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 24 Jan 2017 17:36:04 +0100
changeset 154
6394ce09889a
parent 152
8b85c5face66
child 155
36cd2e280386

adds set-variable saf

most features of this saf are still not implemented

src/server/daemon/httprequest.c file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
src/server/daemon/ws-fn.c file | annotate | diff | comparison | revisions
src/server/safs/common.c file | annotate | diff | comparison | revisions
src/server/safs/common.h file | annotate | diff | comparison | revisions
--- a/src/server/daemon/httprequest.c	Sat Jan 21 16:40:59 2017 +0100
+++ b/src/server/daemon/httprequest.c	Tue Jan 24 17:36:04 2017 +0100
@@ -89,7 +89,7 @@
 
 int handle_request(HTTPRequest *request, threadpool_t *thrpool) {
     // handle nsapi request
-    
+     
     // create pool
     pool_handle_t *pool = pool_create();
 
--- a/src/server/daemon/webserver.c	Sat Jan 21 16:40:59 2017 +0100
+++ b/src/server/daemon/webserver.c	Tue Jan 24 17:36:04 2017 +0100
@@ -48,6 +48,8 @@
 #include "../util/io.h"
 #include "../util/util.h"
 
+#include "../safs/common.h"
+
 #include "func.h"
 #include "config.h"
 #include "configmanager.h"
@@ -103,6 +105,9 @@
     fclose(pidfile);
     free(pid_file_path);
     
+    // init SAFs
+    common_saf_init();
+    
     // set global vars
     conf_global_vars_s *vars = conf_getglobals();
     
--- a/src/server/daemon/ws-fn.c	Sat Jan 21 16:40:59 2017 +0100
+++ b/src/server/daemon/ws-fn.c	Tue Jan 24 17:36:04 2017 +0100
@@ -67,6 +67,7 @@
     { "find-index", find_index, NULL, NULL, 0},
     { "dir-redirect", dir_redirect, NULL, NULL, 0},
     { "print-message", print_message, NULL, NULL, 0},
+    { "set-variable", set_variable, NULL, NULL, 0},
     { "common-log", common_log, NULL, NULL, 0},
     { "send-cgi", send_cgi, NULL, NULL, 0},
     {NULL, NULL, NULL, NULL, 0}
--- a/src/server/safs/common.c	Sat Jan 21 16:40:59 2017 +0100
+++ b/src/server/safs/common.c	Tue Jan 24 17:36:04 2017 +0100
@@ -31,12 +31,147 @@
 #include "../daemon/httprequest.h"
 #include "../daemon/log.h"
 
+#include "../util/pblock.h"
+#include "../util/util.h"
+#include "../../ucx/map.h"
+
+static UcxMap *var_names;
+
+enum SAFVarNames {
+    COMMONSAF_INSERT_CLIENT = 0,
+    COMMONSAF_INSERT_VARS,
+    COMMONSAF_INSERT_REQPB,
+    COMMONSAF_INSERT_HEADERS,
+    COMMONSAF_INSERT_SRVHDRS,
+    COMMONSAF_SET_CLIENT,
+    COMMONSAF_SET_VARS,
+    COMMONSAF_SET_REQPB,
+    COMMONSAF_SET_HEADERS,
+    COMMONSAF_SET_SRVHDRS,
+    COMMONSAF_REMOVE_CLIENT,
+    COMMONSAF_REMOVE_VARS,
+    COMMONSAF_REMOVE_REQPB,
+    COMMONSAF_REMOVE_HEADERS,
+    COMMONSAF_REMOVE_SRVHDRS,
+    COMMONSAF_ABORT,
+    COMMONSAF_NOACTION,
+    COMMONSAF_ERROR,
+    COMMONSAF_ESCAPE,
+    COMMONSAF_FIND_PATHINFO_FORWARD,
+    COMMONSAF_HTTP_DOWNGRADE,
+    COMMONSAF_HTTP_UPGRADE,
+    COMMONSAF_KEEP_ALIVE,
+    COMMONSAF_NAME,
+    COMMONSAF_SENTHDRS,
+    COMMONSAF_STOP,
+    COMMONSAF_URL
+};
+
+#define COMMONSAF_RET_DEF 0
+#define COMMONSAF_RET_NOACTION 1
+#define COMMONSAF_RET_STOP 2
+#define COMMONSAF_REQ_ABORTED -1
+#define COMMONSAF_RET_ERROR -2
+
+void common_saf_init() {
+    var_names = ucx_map_new(12);
+    
+    ucx_map_cstr_put(var_names, "insert-client", (intptr_t)COMMONSAF_INSERT_CLIENT);
+    ucx_map_cstr_put(var_names, "insert-vars", (intptr_t)COMMONSAF_INSERT_VARS);
+    ucx_map_cstr_put(var_names, "insert-reqpb", (intptr_t)COMMONSAF_INSERT_REQPB);
+    ucx_map_cstr_put(var_names, "insert-headers", (intptr_t)COMMONSAF_INSERT_HEADERS);
+    ucx_map_cstr_put(var_names, "insert-srvhdrs", (intptr_t)COMMONSAF_INSERT_SRVHDRS);
+    
+    ucx_map_cstr_put(var_names, "set-client", (intptr_t)COMMONSAF_SET_CLIENT);
+    ucx_map_cstr_put(var_names, "set-vars", (intptr_t)COMMONSAF_SET_VARS);
+    ucx_map_cstr_put(var_names, "set-reqpb", (intptr_t)COMMONSAF_SET_REQPB);
+    ucx_map_cstr_put(var_names, "set-headers", (intptr_t)COMMONSAF_SET_HEADERS);
+    ucx_map_cstr_put(var_names, "set-srvhdrs", (intptr_t)COMMONSAF_SET_SRVHDRS);
+    
+    ucx_map_cstr_put(var_names, "remove-client", (intptr_t)COMMONSAF_REMOVE_CLIENT);
+    ucx_map_cstr_put(var_names, "remove-vars", (intptr_t)COMMONSAF_REMOVE_VARS);
+    ucx_map_cstr_put(var_names, "remove-reqpb", (intptr_t)COMMONSAF_REMOVE_REQPB);
+    ucx_map_cstr_put(var_names, "remove-headers", (intptr_t)COMMONSAF_REMOVE_HEADERS);
+    ucx_map_cstr_put(var_names, "remove-srvhdrs", (intptr_t)COMMONSAF_REMOVE_SRVHDRS);
+    
+    ucx_map_cstr_put(var_names, "abort", (intptr_t)COMMONSAF_ABORT);
+    ucx_map_cstr_put(var_names, "noaction", (intptr_t)COMMONSAF_NOACTION);
+    ucx_map_cstr_put(var_names, "error", (intptr_t)COMMONSAF_ERROR);
+    ucx_map_cstr_put(var_names, "escape", (intptr_t)COMMONSAF_ESCAPE);
+    ucx_map_cstr_put(var_names, "find-pathinfo-forward", (intptr_t)COMMONSAF_FIND_PATHINFO_FORWARD);
+    ucx_map_cstr_put(var_names, "http-downgrade", (intptr_t)COMMONSAF_HTTP_DOWNGRADE);
+    ucx_map_cstr_put(var_names, "http-upgrade", (intptr_t)COMMONSAF_HTTP_UPGRADE);
+    ucx_map_cstr_put(var_names, "keep-alive", (intptr_t)COMMONSAF_KEEP_ALIVE);
+    ucx_map_cstr_put(var_names, "name", (intptr_t)COMMONSAF_NAME);
+}
+
 int print_message(pblock *pb, Session *sn, Request *rq) {
     char *msg = pblock_findval("msg", pb);
     if(msg) {
-        printf("%s\n", msg);
         log_ereport(LOG_INFORM, "%s", msg);
     }    
     
     return REQ_NOACTION;
 }
+
+
+static int set_var(Session *sn, Request *rq, char *var, char *value) {
+    intptr_t v = (intptr_t)ucx_map_cstr_get(var_names, var);
+    switch(v) {
+        default: break;
+        case COMMONSAF_INSERT_CLIENT: break;
+        case COMMONSAF_INSERT_VARS: break;
+        case COMMONSAF_INSERT_REQPB: break;
+        case COMMONSAF_INSERT_HEADERS: break;
+        case COMMONSAF_INSERT_SRVHDRS: break;
+        case COMMONSAF_SET_CLIENT: break;
+        case COMMONSAF_SET_VARS: break;
+        case COMMONSAF_SET_REQPB: break;
+        case COMMONSAF_SET_HEADERS: break;
+        case COMMONSAF_SET_SRVHDRS: break;
+        case COMMONSAF_REMOVE_CLIENT: break;
+        case COMMONSAF_REMOVE_VARS: break;
+        case COMMONSAF_REMOVE_HEADERS: break;
+        case COMMONSAF_REMOVE_SRVHDRS: break;
+        case COMMONSAF_ABORT: return COMMONSAF_REQ_ABORTED;
+        case COMMONSAF_ERROR: {
+            return COMMONSAF_REQ_ABORTED;
+        }
+        case COMMONSAF_ESCAPE: break;
+        case COMMONSAF_FIND_PATHINFO_FORWARD: break;
+        case COMMONSAF_HTTP_DOWNGRADE: break;
+        case COMMONSAF_HTTP_UPGRADE: break;
+        case COMMONSAF_KEEP_ALIVE: {
+            rq->rq_attr.keep_alive = util_getboolean(var, 0);
+            break;
+        }
+        case COMMONSAF_NAME: break;
+        case COMMONSAF_SENTHDRS: break;
+        case COMMONSAF_STOP: return COMMONSAF_RET_STOP;
+        case COMMONSAF_URL: break;
+    }
+    return COMMONSAF_RET_DEF;
+}
+
+int set_variable(pblock *pb, Session *sn, Request *rq) {
+    int ret = REQ_NOACTION;
+    int set = 0;
+    
+    for(int i=0;i<pb->hsize;i++) {
+        pb_entry *entry = pb->ht[i];
+        while(entry) {
+            int r = set_var(sn, rq, entry->param->name, entry->param->value);
+            switch(r) {
+                default:
+                case COMMONSAF_RET_DEF: break;
+                case COMMONSAF_RET_NOACTION: set = 1; ret = REQ_NOACTION; break;
+                case COMMONSAF_RET_STOP: set = 1; ret = REQ_PROCEED; break;
+                case COMMONSAF_REQ_ABORTED: ret = set ? ret : REQ_ABORTED; break;
+                case COMMONSAF_RET_ERROR: return REQ_ABORTED;
+            }
+            entry = entry->next;
+        }
+    }
+    
+    return ret;
+}
--- a/src/server/safs/common.h	Sat Jan 21 16:40:59 2017 +0100
+++ b/src/server/safs/common.h	Tue Jan 24 17:36:04 2017 +0100
@@ -35,8 +35,12 @@
 extern "C" {
 #endif
 
+void common_saf_init();
+    
 int print_message(pblock *pb, Session *sn, Request *rq);
 
+int set_variable(pblock *pb, Session *sn, Request *rq);
+
 
 #ifdef	__cplusplus
 }

mercurial