src/server/safs/common.c

changeset 415
d938228c382e
parent 254
4784c14aa639
child 490
d218607f5a7e
equal deleted inserted replaced
414:99a34860c105 415:d938228c382e
31 #include "../daemon/httprequest.h" 31 #include "../daemon/httprequest.h"
32 #include "../daemon/log.h" 32 #include "../daemon/log.h"
33 33
34 #include "../util/pblock.h" 34 #include "../util/pblock.h"
35 #include "../util/util.h" 35 #include "../util/util.h"
36 #include <ucx/map.h> 36 #include <cx/map.h>
37 37 #include <cx/hash_map.h>
38 static UcxMap *var_names; 38
39 static CxMap *var_names;
39 40
40 enum SAFVarNames { 41 enum SAFVarNames {
41 COMMONSAF_INSERT_CLIENT = 1, 42 COMMONSAF_INSERT_CLIENT = 1,
42 COMMONSAF_INSERT_VARS, 43 COMMONSAF_INSERT_VARS,
43 COMMONSAF_INSERT_REQPB, 44 COMMONSAF_INSERT_REQPB,
72 #define COMMONSAF_RET_STOP 2 73 #define COMMONSAF_RET_STOP 2
73 #define COMMONSAF_REQ_ABORTED -1 74 #define COMMONSAF_REQ_ABORTED -1
74 #define COMMONSAF_RET_ERROR -2 75 #define COMMONSAF_RET_ERROR -2
75 76
76 void common_saf_init() { 77 void common_saf_init() {
77 var_names = ucx_map_new(32); 78 var_names = cxHashMapCreate(cxDefaultAllocator, 32);
78 79
79 ucx_map_cstr_put(var_names, "insert-client", (intptr_t)COMMONSAF_INSERT_CLIENT); 80 cxMapPut(var_names, cx_hash_key_str("insert-client"), (void*)(intptr_t)COMMONSAF_INSERT_CLIENT);
80 ucx_map_cstr_put(var_names, "insert-vars", (intptr_t)COMMONSAF_INSERT_VARS); 81 cxMapPut(var_names, cx_hash_key_str("insert-vars"), (void*)(intptr_t)COMMONSAF_INSERT_VARS);
81 ucx_map_cstr_put(var_names, "insert-reqpb", (intptr_t)COMMONSAF_INSERT_REQPB); 82 cxMapPut(var_names, cx_hash_key_str("insert-reqpb"), (void*)(intptr_t)COMMONSAF_INSERT_REQPB);
82 ucx_map_cstr_put(var_names, "insert-headers", (intptr_t)COMMONSAF_INSERT_HEADERS); 83 cxMapPut(var_names, cx_hash_key_str("insert-headers"), (void*)(intptr_t)COMMONSAF_INSERT_HEADERS);
83 ucx_map_cstr_put(var_names, "insert-srvhdrs", (intptr_t)COMMONSAF_INSERT_SRVHDRS); 84 cxMapPut(var_names, cx_hash_key_str("insert-srvhdrs"), (void*)(intptr_t)COMMONSAF_INSERT_SRVHDRS);
84 85
85 ucx_map_cstr_put(var_names, "set-client", (intptr_t)COMMONSAF_SET_CLIENT); 86 cxMapPut(var_names, cx_hash_key_str("set-client"), (void*)(intptr_t)COMMONSAF_SET_CLIENT);
86 ucx_map_cstr_put(var_names, "set-vars", (intptr_t)COMMONSAF_SET_VARS); 87 cxMapPut(var_names, cx_hash_key_str("set-vars"), (void*)(intptr_t)COMMONSAF_SET_VARS);
87 ucx_map_cstr_put(var_names, "set-reqpb", (intptr_t)COMMONSAF_SET_REQPB); 88 cxMapPut(var_names, cx_hash_key_str("set-reqpb"), (void*)(intptr_t)COMMONSAF_SET_REQPB);
88 ucx_map_cstr_put(var_names, "set-headers", (intptr_t)COMMONSAF_SET_HEADERS); 89 cxMapPut(var_names, cx_hash_key_str("set-headers"), (void*)(intptr_t)COMMONSAF_SET_HEADERS);
89 ucx_map_cstr_put(var_names, "set-srvhdrs", (intptr_t)COMMONSAF_SET_SRVHDRS); 90 cxMapPut(var_names, cx_hash_key_str("set-srvhdrs"), (void*)(intptr_t)COMMONSAF_SET_SRVHDRS);
90 91
91 ucx_map_cstr_put(var_names, "remove-client", (intptr_t)COMMONSAF_REMOVE_CLIENT); 92 cxMapPut(var_names, cx_hash_key_str("remove-client"), (void*)(intptr_t)COMMONSAF_REMOVE_CLIENT);
92 ucx_map_cstr_put(var_names, "remove-vars", (intptr_t)COMMONSAF_REMOVE_VARS); 93 cxMapPut(var_names, cx_hash_key_str("remove-vars"), (void*)(intptr_t)COMMONSAF_REMOVE_VARS);
93 ucx_map_cstr_put(var_names, "remove-reqpb", (intptr_t)COMMONSAF_REMOVE_REQPB); 94 cxMapPut(var_names, cx_hash_key_str("remove-reqpb"), (void*)(intptr_t)COMMONSAF_REMOVE_REQPB);
94 ucx_map_cstr_put(var_names, "remove-headers", (intptr_t)COMMONSAF_REMOVE_HEADERS); 95 cxMapPut(var_names, cx_hash_key_str("remove-headers"), (void*)(intptr_t)COMMONSAF_REMOVE_HEADERS);
95 ucx_map_cstr_put(var_names, "remove-srvhdrs", (intptr_t)COMMONSAF_REMOVE_SRVHDRS); 96 cxMapPut(var_names, cx_hash_key_str("remove-srvhdrs"), (void*)(intptr_t)COMMONSAF_REMOVE_SRVHDRS);
96 97
97 ucx_map_cstr_put(var_names, "abort", (intptr_t)COMMONSAF_ABORT); 98 cxMapPut(var_names, cx_hash_key_str("abort"), (void*)(intptr_t)COMMONSAF_ABORT);
98 ucx_map_cstr_put(var_names, "noaction", (intptr_t)COMMONSAF_NOACTION); 99 cxMapPut(var_names, cx_hash_key_str("noaction"), (void*)(intptr_t)COMMONSAF_NOACTION);
99 ucx_map_cstr_put(var_names, "error", (intptr_t)COMMONSAF_ERROR); 100 cxMapPut(var_names, cx_hash_key_str("error"), (void*)(intptr_t)COMMONSAF_ERROR);
100 ucx_map_cstr_put(var_names, "escape", (intptr_t)COMMONSAF_ESCAPE); 101 cxMapPut(var_names, cx_hash_key_str("escape"), (void*)(intptr_t)COMMONSAF_ESCAPE);
101 ucx_map_cstr_put(var_names, "find-pathinfo-forward", (intptr_t)COMMONSAF_FIND_PATHINFO_FORWARD); 102 cxMapPut(var_names, cx_hash_key_str("find-pathinfo-forward"), (void*)(intptr_t)COMMONSAF_FIND_PATHINFO_FORWARD);
102 ucx_map_cstr_put(var_names, "http-downgrade", (intptr_t)COMMONSAF_HTTP_DOWNGRADE); 103 cxMapPut(var_names, cx_hash_key_str("http-downgrade"), (void*)(intptr_t)COMMONSAF_HTTP_DOWNGRADE);
103 ucx_map_cstr_put(var_names, "http-upgrade", (intptr_t)COMMONSAF_HTTP_UPGRADE); 104 cxMapPut(var_names, cx_hash_key_str("http-upgrade"), (void*)(intptr_t)COMMONSAF_HTTP_UPGRADE);
104 ucx_map_cstr_put(var_names, "keep-alive", (intptr_t)COMMONSAF_KEEP_ALIVE); 105 cxMapPut(var_names, cx_hash_key_str("keep-alive"), (void*)(intptr_t)COMMONSAF_KEEP_ALIVE);
105 ucx_map_cstr_put(var_names, "name", (intptr_t)COMMONSAF_NAME); 106 cxMapPut(var_names, cx_hash_key_str("name"), (void*)(intptr_t)COMMONSAF_NAME);
106 } 107 }
107 108
108 int print_message(pblock *pb, Session *sn, Request *rq) { 109 int print_message(pblock *pb, Session *sn, Request *rq) {
109 char *msg = pblock_findval("msg", pb); 110 char *msg = pblock_findval("msg", pb);
110 if(msg) { 111 if(msg) {
113 114
114 return REQ_NOACTION; 115 return REQ_NOACTION;
115 } 116 }
116 117
117 static void var_set(char *value, pblock *pb, WSBool insert) { 118 static void var_set(char *value, pblock *pb, WSBool insert) {
118 sstr_t n; 119 cxstring n;
119 sstr_t v; 120 cxstring v;
120 v.ptr = NULL; 121 v.ptr = NULL;
121 122
122 n.ptr = value; 123 n.ptr = value;
123 int i; 124 int i;
124 int len = strlen(value); 125 int len = strlen(value);
125 for(i=1;i<len;i++) { 126 for(i=1;i<len;i++) {
126 if(value[i] == '=') { 127 if(value[i] == '=') {
127 n.length = i; 128 n.length = i;
128 v = sstrsubs(sstrn(value, len), i + 1); 129 v = cx_strsubs(cx_strn(value, len), i + 1);
129 break; 130 break;
130 } 131 }
131 } 132 }
132 if(!v.ptr || v.length == 0) { 133 if(!v.ptr || v.length == 0) {
133 log_ereport( 134 log_ereport(
141 // TODO 142 // TODO
142 } 143 }
143 pblock_nvlinsert(n.ptr, n.length, v.ptr, v.length, pb); 144 pblock_nvlinsert(n.ptr, n.length, v.ptr, v.length, pb);
144 } 145 }
145 146
146 static int set_var(Session *sn, Request *rq, char *var, char *value) { 147 static int set_var(Session *sn, Request *rq, const char *var, char *value) {
147 intptr_t v = (intptr_t)ucx_map_cstr_get(var_names, var); 148 intptr_t v = (intptr_t)cxMapGet(var_names, cx_hash_key_str(var));
148 switch(v) { 149 switch(v) {
149 default: break; 150 default: break;
150 case COMMONSAF_INSERT_CLIENT: var_set(value, sn->client, TRUE); break; 151 case COMMONSAF_INSERT_CLIENT: var_set(value, sn->client, TRUE); break;
151 case COMMONSAF_INSERT_VARS: var_set(value, rq->vars, TRUE); break; 152 case COMMONSAF_INSERT_VARS: var_set(value, rq->vars, TRUE); break;
152 case COMMONSAF_INSERT_REQPB: var_set(value, rq->reqpb, TRUE); break; 153 case COMMONSAF_INSERT_REQPB: var_set(value, rq->reqpb, TRUE); break;
182 "set-variable: error value must contain a 3-digit http status code"); 183 "set-variable: error value must contain a 3-digit http status code");
183 protocol_status(sn, rq, 500, NULL); 184 protocol_status(sn, rq, 500, NULL);
184 return COMMONSAF_RET_ERROR; 185 return COMMONSAF_RET_ERROR;
185 } 186 }
186 187
187 char *msg = isnum ? NULL : sstrtrim(sstr(value + i)).ptr; 188 const char *msg = isnum ? NULL : cx_strtrim(cx_str(value + i)).ptr;
188 protocol_status(sn, rq, (int)status, msg); 189 protocol_status(sn, rq, (int)status, msg);
189 190
190 return COMMONSAF_REQ_ABORTED; 191 return COMMONSAF_REQ_ABORTED;
191 } 192 }
192 case COMMONSAF_ESCAPE: break; 193 case COMMONSAF_ESCAPE: break;

mercurial