|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "../public/nsapi.h" |
|
30 |
|
31 #include <stdio.h> |
|
32 #include <stdlib.h> |
|
33 |
|
34 #include <fcntl.h> |
|
35 #include <sys/types.h> |
|
36 #include <sys/file.h> |
|
37 #include <sys/stat.h> |
|
38 #include <sys/mman.h> |
|
39 |
|
40 #include "../ucx/sstring.h" |
|
41 |
|
42 #include "httplistener.h" |
|
43 #include "conf.h" |
|
44 #include "func.h" |
|
45 |
|
46 #include "vserver.h" |
|
47 #include "../util/pblock.h" |
|
48 |
|
49 VirtualServer *default_vs; |
|
50 |
|
51 void load_init_conf(char *file) { |
|
52 printf("load_init_conf\n"); |
|
53 |
|
54 pool_handle_t *pool = pool_create(); |
|
55 |
|
56 FILE *in = fopen("conf/init.conf", "r"); |
|
57 if(in == NULL) { |
|
58 fprintf(stderr, "Cannot open conf/init.conf\n"); |
|
59 return; |
|
60 } |
|
61 |
|
62 char buf[512]; |
|
63 buf[0] = 0; |
|
64 int len = 512; |
|
65 |
|
66 while(!feof(in)) { |
|
67 fgets(buf, len, in); |
|
68 |
|
69 if(*buf == 0) { |
|
70 continue; |
|
71 } |
|
72 |
|
73 char *ptr; |
|
74 if((ptr = strrchr(buf, '\n'))) { |
|
75 ptr[0] = 0; |
|
76 } |
|
77 |
|
78 sstr_t line = string_trim(sstr(buf)); |
|
79 if(line.length > 0) { |
|
80 sstr_t type; |
|
81 directive *d = parse_directive(pool, line, &type); |
|
82 if(sstrcmp(type, sstr("Init"))) { |
|
83 d->func->func(d->param, NULL, NULL); |
|
84 } |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 void load_server_conf(char *file) { |
|
90 printf("load_server_conf\n"); |
|
91 |
|
92 ListenerConfig *conf = malloc(sizeof(ListenerConfig)); |
|
93 conf->port = 9090; |
|
94 conf->nacceptors = 1; |
|
95 conf->name = "default"; |
|
96 |
|
97 http_listener_new(conf); |
|
98 |
|
99 // virtual server |
|
100 default_vs = vs_new(); |
|
101 // load obj.conf |
|
102 default_vs->objects = load_obj_conf("conf/obj.conf"); |
|
103 default_vs->default_obj_name = "default"; |
|
104 |
|
105 // begin objset test |
|
106 /* |
|
107 httpd_objset *objset = default_vs->objset; |
|
108 for(int i=0;i<objset->pos;i++) { |
|
109 httpd_object *obj = objset->obj[i]; |
|
110 printf("<object [%s]>\n", obj->name); |
|
111 for(int j=0;j<obj->nd;j++) { |
|
112 dtable *dt; |
|
113 switch(j) { |
|
114 case NSAPIAuthTrans: { |
|
115 printf(" Get AuthTrans Directives\n"); |
|
116 dt = object_get_dtable(obj, NSAPIAuthTrans); |
|
117 break; |
|
118 } |
|
119 case NSAPINameTrans: { |
|
120 printf(" Get NameTrans Directives\n"); |
|
121 dt = object_get_dtable(obj, NSAPINameTrans); |
|
122 break; |
|
123 } |
|
124 case NSAPIPathCheck: { |
|
125 printf(" Get PathCheck Directives\n"); |
|
126 dt = object_get_dtable(obj, NSAPIPathCheck); |
|
127 break; |
|
128 } |
|
129 case NSAPIService: { |
|
130 printf(" Get Service Directives\n"); |
|
131 dt = object_get_dtable(obj, NSAPIService); |
|
132 break; |
|
133 } |
|
134 default: { |
|
135 printf("j: %d\n", j); |
|
136 dt = object_get_dtable(obj, j); |
|
137 break; |
|
138 } |
|
139 } |
|
140 if(dt != NULL) { |
|
141 printf(" dtable[%d].length = %d\n", dt, dt->ndir); |
|
142 } else { |
|
143 continue; |
|
144 } |
|
145 for(int k=0;k<dt->ndir;k++) { |
|
146 directive *d = dt->directive[k]; |
|
147 if(d == NULL) { |
|
148 printf("d is null\n"); |
|
149 } else { |
|
150 printf(" Directive[%d].name = %s\n", d, d->func->name); |
|
151 } |
|
152 } |
|
153 } |
|
154 } |
|
155 */ |
|
156 // end objset test |
|
157 } |
|
158 |
|
159 VirtualServer* conf_get_default_vs() { |
|
160 return default_vs; |
|
161 } |
|
162 |
|
163 HTTPObjectConfig* load_obj_conf(char *file) { |
|
164 printf("load_obj_conf\n"); |
|
165 |
|
166 /* create object config */ |
|
167 ObjectConfParser parser; |
|
168 HTTPObjectConfig *conf = calloc(sizeof(HTTPObjectConfig), 1); |
|
169 conf->pool = pool_create(); |
|
170 parser.conf = conf; |
|
171 |
|
172 FILE *in = fopen("conf/obj.conf", "r"); |
|
173 if(in == NULL) { |
|
174 fprintf(stderr, "Cannot open conf/obj.conf\n"); |
|
175 return NULL; |
|
176 } |
|
177 |
|
178 char buf[512]; |
|
179 int len = 512; |
|
180 |
|
181 while(!feof(in)) { |
|
182 fgets(buf, len, in); |
|
183 |
|
184 if(*buf == 0) { |
|
185 continue; |
|
186 } |
|
187 |
|
188 char *ptr; |
|
189 if((ptr = strrchr(buf, '\n'))) { |
|
190 ptr[0] = 0; |
|
191 } |
|
192 |
|
193 sstr_t line = string_trim(sstr(buf)); |
|
194 if(line.length > 0) { |
|
195 obj_conf_parse_line(&parser, line); |
|
196 } |
|
197 } |
|
198 |
|
199 |
|
200 |
|
201 return conf; |
|
202 } |
|
203 |
|
204 void obj_conf_parse_line(ObjectConfParser *parser, sstr_t line) { |
|
205 //printf("{%s}[%d]\n", line.ptr, line.length); |
|
206 if(line.ptr[0] == '#') { |
|
207 return; |
|
208 } |
|
209 |
|
210 if(line.length < 3) { |
|
211 // to short for everything |
|
212 fprintf(stderr, "obj.conf: line to short \"%s\"\n", line.ptr); |
|
213 return; |
|
214 } |
|
215 |
|
216 // TODO: ersetzen |
|
217 if(line.ptr[0] == '<') { |
|
218 if(line.ptr[1] == '/') { |
|
219 // end tag |
|
220 if(line.ptr[2] == 'O' && parser->obj != NULL) { |
|
221 // end of Object |
|
222 httpobjconf_add_object(parser->conf, parser->obj); |
|
223 parser->obj = NULL; |
|
224 return; |
|
225 } |
|
226 } else { |
|
227 // new tag |
|
228 httpd_object *obj = parse_new_object_tag(line); |
|
229 parser->obj = obj; |
|
230 } |
|
231 } else { |
|
232 // directive |
|
233 sstr_t dtype; |
|
234 directive *d = parse_directive(parser->conf->pool, line, &dtype); |
|
235 int dt = get_directive_type_from_string(dtype); |
|
236 object_add_directive(parser->obj, d, dt); |
|
237 } |
|
238 } |
|
239 |
|
240 |
|
241 /* utils */ |
|
242 |
|
243 sstr_t string_trim(sstr_t string) { |
|
244 sstr_t newstr = string; |
|
245 int nsoff = 0; |
|
246 int l = 1; |
|
247 for(int i=0;i<string.length;i++) { |
|
248 char c = string.ptr[i]; |
|
249 if(l) { |
|
250 /* leading whitespace */ |
|
251 if(c > 32) { |
|
252 l = 0; |
|
253 nsoff = i; |
|
254 newstr.ptr = &string.ptr[i]; |
|
255 newstr.length = string.length - nsoff; |
|
256 } |
|
257 } else { |
|
258 /* trailing whitespace */ |
|
259 if(c > 32) { |
|
260 newstr.length = (i - nsoff) + 1; |
|
261 } |
|
262 } |
|
263 } |
|
264 return newstr; |
|
265 } |
|
266 |
|
267 httpd_object* parse_new_object_tag(sstr_t line) { |
|
268 int i = 0; |
|
269 int b = 0; |
|
270 sstr_t name; |
|
271 sstr_t value; |
|
272 |
|
273 char *obj_name = NULL; |
|
274 char *obj_ppath = NULL; |
|
275 |
|
276 for(;i<line.length;i++) { |
|
277 if(line.ptr[i] < 33) { |
|
278 b = 1; |
|
279 } else if(b == 1) { |
|
280 break; |
|
281 } |
|
282 } |
|
283 if(!b || line.ptr[i] < 33) { |
|
284 printf("1\n"); |
|
285 return NULL; |
|
286 } |
|
287 b = 0; |
|
288 |
|
289 /* parse name=value params */ |
|
290 for(;i<line.length;i++) { |
|
291 if(line.ptr[i] == '>') { |
|
292 break; |
|
293 } |
|
294 |
|
295 /* get name */ |
|
296 name.ptr = line.ptr + i; |
|
297 for(;i<line.length;i++) { |
|
298 if(line.ptr[i] == '=') { |
|
299 b = 1; |
|
300 i++; |
|
301 break; |
|
302 } |
|
303 } |
|
304 if(!b) { |
|
305 printf("2\n"); |
|
306 return NULL; |
|
307 } |
|
308 name.length = line.ptr + i - name.ptr - 1; |
|
309 |
|
310 if(line.ptr[i] == '\"') { |
|
311 i++; // TODO: Bug wenn end of line - wird nicht erkannt! |
|
312 } |
|
313 value.ptr = line.ptr + i; |
|
314 for(;i<line.length;i++) { |
|
315 char c = line.ptr[i]; |
|
316 if(c < 33 || c == '\"' || c == '>') { |
|
317 b = 1; |
|
318 break; |
|
319 } |
|
320 } |
|
321 if(!b) { |
|
322 printf("3\n"); |
|
323 return NULL; |
|
324 } |
|
325 value.length = line.ptr + i - value.ptr; |
|
326 |
|
327 if(sstrcmp(name, sstrn("name", 4)) == 0) { |
|
328 obj_name = sstrdub(value).ptr; |
|
329 } else if (sstrcmp(name, sstrn("ppath", 5)) == 0) { |
|
330 obj_ppath = sstrdub(value).ptr; |
|
331 } |
|
332 |
|
333 /* |
|
334 printf("name: [%d]{", name.length); |
|
335 fwrite(name.ptr, 1, name.length, stdout); |
|
336 printf("}\n"); |
|
337 printf("value: [%d]{", value.length); |
|
338 fwrite(value.ptr, 1, value.length, stdout); |
|
339 printf("}\n"); |
|
340 */ |
|
341 |
|
342 char c = line.ptr[i]; |
|
343 if(c == '>') { |
|
344 break; |
|
345 } else { |
|
346 i++; |
|
347 } |
|
348 } |
|
349 |
|
350 if(obj_name != NULL || obj_ppath != NULL) { |
|
351 httpd_object *o = object_new(obj_name); |
|
352 o->path = obj_ppath; |
|
353 return o; |
|
354 } |
|
355 |
|
356 return NULL; |
|
357 } |
|
358 |
|
359 directive* parse_directive(pool_handle_t *pool, sstr_t line, sstr_t *type) { |
|
360 int i = 0; |
|
361 int b = 0; |
|
362 |
|
363 sstr_t directive_type = line; |
|
364 |
|
365 directive *directive = malloc(sizeof(directive)); |
|
366 directive->cond = NULL; |
|
367 directive->param = pblock_create_pool(pool, 8); |
|
368 |
|
369 for(;i<line.length;i++) { |
|
370 if(line.ptr[i] < 33) { |
|
371 b = 1; |
|
372 directive_type.length = i; |
|
373 if(directive_type.length <= 0) { |
|
374 fprintf(stderr, "parse error: cannot parse directive\n"); |
|
375 return NULL; |
|
376 } |
|
377 } else if(b == 1) { |
|
378 break; |
|
379 } |
|
380 } |
|
381 |
|
382 /* parse name=value params */ |
|
383 sstr_t name; |
|
384 sstr_t value; |
|
385 for(;i<line.length;i++) { |
|
386 /* get name */ |
|
387 name.ptr = line.ptr + i; |
|
388 for(;i<line.length;i++) { |
|
389 if(line.ptr[i] == '=') { |
|
390 b = 1; |
|
391 i++; |
|
392 break; |
|
393 } |
|
394 } |
|
395 if(!b) { |
|
396 printf("2\n"); |
|
397 return NULL; |
|
398 } |
|
399 name.length = line.ptr + i - name.ptr - 1; |
|
400 |
|
401 if(line.ptr[i] == '\"') { |
|
402 i++; // TODO: Bug wenn end of line - wird nicht erkannt! |
|
403 } |
|
404 value.ptr = line.ptr + i; |
|
405 for(;i<line.length;i++) { |
|
406 char c = line.ptr[i]; |
|
407 if(c < 33 || c == '\"') { |
|
408 b = 1; |
|
409 break; |
|
410 } |
|
411 } |
|
412 if(!b) { |
|
413 printf("3\n"); |
|
414 return NULL; |
|
415 } |
|
416 value.length = line.ptr + i - value.ptr; |
|
417 |
|
418 name = string_trim(name); |
|
419 value = string_trim(value); |
|
420 |
|
421 /* insert name and value into directive pblock */ |
|
422 pblock_nvlinsert( |
|
423 name.ptr, |
|
424 name.length, |
|
425 value.ptr, |
|
426 value.length, |
|
427 directive->param); |
|
428 } |
|
429 |
|
430 /* get function */ |
|
431 char *func_name = pblock_findval("fn", directive->param); |
|
432 directive->func = get_function(func_name); |
|
433 |
|
434 *type = directive_type; |
|
435 return directive; |
|
436 } |
|
437 |
|
438 int get_directive_type_from_string(sstr_t type) { |
|
439 /* get nsapi function type */ |
|
440 int dt = -1; |
|
441 if(sstrcmp(type, sstr("AuthTrans")) == 0) { |
|
442 dt = 0; |
|
443 } else if(sstrcmp(type, sstr("NameTrans")) == 0) { |
|
444 dt = 1; |
|
445 } else if(sstrcmp(type, sstr("PathCheck")) == 0) { |
|
446 dt = 2; |
|
447 } else if(sstrcmp(type, sstr("ObjectType")) == 0) { |
|
448 dt = 3; |
|
449 } else if(sstrcmp(type, sstr("Service")) == 0) { |
|
450 dt = 4; |
|
451 } else if(sstrcmp(type, sstr("AddLog")) == 0) { |
|
452 dt = 5; |
|
453 } |
|
454 return dt; |
|
455 } |