src/server/conf.c

changeset 14
b8bf95b39952
parent 13
1fdbf4170ef4
child 15
cff9c4101dd7
equal deleted inserted replaced
13:1fdbf4170ef4 14:b8bf95b39952
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 "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 "sstring.h"
41
42 #include "httplistener.h"
43 #include "conf.h"
44 #include "func.h"
45
46 #include "vserver.h"
47 #include "pblock.h"
48
49 VirtualServer *default_vs;
50
51 void load_init_conf(char *file) {
52 printf("load_init_conf\n");
53 if(1) {
54 return;
55 }
56
57 pool_handle_t *pool = pool_create();
58
59 FILE *in = fopen("conf/obj.conf", "r");
60 if(in == NULL) {
61 fprintf(stderr, "Cannot open conf/obj.conf\n");
62 return;
63 }
64
65 char buf[512];
66 int len = 512;
67
68 while(!feof(in)) {
69 fgets(buf, len, in);
70
71 if(*buf == 0) {
72 continue;
73 }
74
75 char *ptr;
76 if((ptr = strrchr(buf, '\n'))) {
77 ptr[0] = 0;
78 }
79
80 sstr_t line = string_trim(sstr(buf));
81 if(line.length > 0) {
82 sstr_t type;
83 directive *d = parse_directive(pool, line, &type);
84 if(sstrcmp(type, sstr("Init"))) {
85 d->func->func(d->param, NULL, NULL);
86 }
87 }
88 }
89 }
90
91 void load_server_conf(char *file) {
92 printf("load_server_conf\n");
93
94 ListenerConfig *conf = malloc(sizeof(ListenerConfig));
95 conf->port = 9090;
96 conf->nacceptors = 1;
97 conf->name = "default";
98
99 http_listener_new(conf);
100
101 // virtual server
102 default_vs = vs_new();
103 // load obj.conf
104 default_vs->objects = load_obj_conf("conf/obj.conf");
105 default_vs->default_obj_name = "default";
106
107 // begin objset test
108 /*
109 httpd_objset *objset = default_vs->objset;
110 for(int i=0;i<objset->pos;i++) {
111 httpd_object *obj = objset->obj[i];
112 printf("<object [%s]>\n", obj->name);
113 for(int j=0;j<obj->nd;j++) {
114 dtable *dt;
115 switch(j) {
116 case NSAPIAuthTrans: {
117 printf(" Get AuthTrans Directives\n");
118 dt = object_get_dtable(obj, NSAPIAuthTrans);
119 break;
120 }
121 case NSAPINameTrans: {
122 printf(" Get NameTrans Directives\n");
123 dt = object_get_dtable(obj, NSAPINameTrans);
124 break;
125 }
126 case NSAPIPathCheck: {
127 printf(" Get PathCheck Directives\n");
128 dt = object_get_dtable(obj, NSAPIPathCheck);
129 break;
130 }
131 case NSAPIService: {
132 printf(" Get Service Directives\n");
133 dt = object_get_dtable(obj, NSAPIService);
134 break;
135 }
136 default: {
137 printf("j: %d\n", j);
138 dt = object_get_dtable(obj, j);
139 break;
140 }
141 }
142 if(dt != NULL) {
143 printf(" dtable[%d].length = %d\n", dt, dt->ndir);
144 } else {
145 continue;
146 }
147 for(int k=0;k<dt->ndir;k++) {
148 directive *d = dt->directive[k];
149 if(d == NULL) {
150 printf("d is null\n");
151 } else {
152 printf(" Directive[%d].name = %s\n", d, d->func->name);
153 }
154 }
155 }
156 }
157 */
158 // end objset test
159 }
160
161 VirtualServer* conf_get_default_vs() {
162 return default_vs;
163 }
164
165 HTTPObjectConfig* load_obj_conf(char *file) {
166 printf("load_obj_conf\n");
167
168 /* create object config */
169 ObjectConfParser parser;
170 HTTPObjectConfig *conf = calloc(sizeof(HTTPObjectConfig), 1);
171 conf->pool = pool_create();
172 parser.conf = conf;
173
174 FILE *in = fopen("conf/obj.conf", "r");
175 if(in == NULL) {
176 fprintf(stderr, "Cannot open conf/obj.conf\n");
177 return NULL;
178 }
179
180 char buf[512];
181 int len = 512;
182
183 while(!feof(in)) {
184 fgets(buf, len, in);
185
186 if(*buf == 0) {
187 continue;
188 }
189
190 char *ptr;
191 if((ptr = strrchr(buf, '\n'))) {
192 ptr[0] = 0;
193 }
194
195 sstr_t line = string_trim(sstr(buf));
196 if(line.length > 0) {
197 obj_conf_parse_line(&parser, line);
198 }
199 }
200
201
202
203 return conf;
204 }
205
206 void obj_conf_parse_line(ObjectConfParser *parser, sstr_t line) {
207 //printf("{%s}[%d]\n", line.ptr, line.length);
208 if(line.ptr[0] == '#') {
209 return;
210 }
211
212 if(line.length < 3) {
213 // to short for everything
214 fprintf(stderr, "obj.conf: line to short \"%s\"\n", line.ptr);
215 return;
216 }
217
218 // TODO: ersetzen
219 if(line.ptr[0] == '<') {
220 if(line.ptr[1] == '/') {
221 // end tag
222 if(line.ptr[2] == 'O' && parser->obj != NULL) {
223 // end of Object
224 httpobjconf_add_object(parser->conf, parser->obj);
225 parser->obj = NULL;
226 return;
227 }
228 } else {
229 // new tag
230 httpd_object *obj = parse_new_object_tag(line);
231 parser->obj = obj;
232 }
233 } else {
234 // directive
235 sstr_t dtype;
236 directive *d = parse_directive(parser->conf->pool, line, &dtype);
237 int dt = get_directive_type_from_string(dtype);
238 object_add_directive(parser->obj, d, dt);
239 }
240 }
241
242
243 /* utils */
244
245 sstr_t string_trim(sstr_t string) {
246 sstr_t newstr = string;
247 int nsoff = 0;
248 int l = 1;
249 for(int i=0;i<string.length;i++) {
250 char c = string.ptr[i];
251 if(l) {
252 /* leading whitespace */
253 if(c > 32) {
254 l = 0;
255 nsoff = i;
256 newstr.ptr = &string.ptr[i];
257 newstr.length = string.length - nsoff;
258 }
259 } else {
260 /* trailing whitespace */
261 if(c > 32) {
262 newstr.length = (i - nsoff) + 1;
263 }
264 }
265 }
266 return newstr;
267 }
268
269 httpd_object* parse_new_object_tag(sstr_t line) {
270 int i = 0;
271 int b = 0;
272 sstr_t name;
273 sstr_t value;
274
275 char *obj_name = NULL;
276 char *obj_ppath = NULL;
277
278 for(;i<line.length;i++) {
279 if(line.ptr[i] < 33) {
280 b = 1;
281 } else if(b == 1) {
282 break;
283 }
284 }
285 if(!b || line.ptr[i] < 33) {
286 printf("1\n");
287 return NULL;
288 }
289 b = 0;
290
291 /* parse name=value params */
292 for(;i<line.length;i++) {
293 if(line.ptr[i] == '>') {
294 break;
295 }
296
297 /* get name */
298 name.ptr = line.ptr + i;
299 for(;i<line.length;i++) {
300 if(line.ptr[i] == '=') {
301 b = 1;
302 i++;
303 break;
304 }
305 }
306 if(!b) {
307 printf("2\n");
308 return NULL;
309 }
310 name.length = line.ptr + i - name.ptr - 1;
311
312 if(line.ptr[i] == '\"') {
313 i++; // TODO: Bug wenn end of line - wird nicht erkannt!
314 }
315 value.ptr = line.ptr + i;
316 for(;i<line.length;i++) {
317 char c = line.ptr[i];
318 if(c < 33 || c == '\"' || c == '>') {
319 b = 1;
320 break;
321 }
322 }
323 if(!b) {
324 printf("3\n");
325 return NULL;
326 }
327 value.length = line.ptr + i - value.ptr;
328
329 if(sstrcmp(name, sstrn("name", 4)) == 0) {
330 obj_name = sstrdub(value).ptr;
331 } else if (sstrcmp(name, sstrn("ppath", 5)) == 0) {
332 obj_ppath = sstrdub(value).ptr;
333 }
334
335 /*
336 printf("name: [%d]{", name.length);
337 fwrite(name.ptr, 1, name.length, stdout);
338 printf("}\n");
339 printf("value: [%d]{", value.length);
340 fwrite(value.ptr, 1, value.length, stdout);
341 printf("}\n");
342 */
343
344 char c = line.ptr[i];
345 if(c == '>') {
346 break;
347 } else {
348 i++;
349 }
350 }
351
352 if(obj_name != NULL || obj_ppath != NULL) {
353 httpd_object *o = object_new(obj_name);
354 o->path = obj_ppath;
355 return o;
356 }
357
358 return NULL;
359 }
360
361 directive* parse_directive(pool_handle_t *pool, sstr_t line, sstr_t *type) {
362 int i = 0;
363 int b = 0;
364
365 sstr_t directive_type = line;
366
367 directive *directive = malloc(sizeof(directive));
368 directive->cond = NULL;
369 directive->param = pblock_create_pool(pool, 8);
370
371 for(;i<line.length;i++) {
372 if(line.ptr[i] < 33) {
373 b = 1;
374 directive_type.length = i;
375 if(directive_type.length <= 0) {
376 fprintf(stderr, "parse error: cannot parse directive\n");
377 return NULL;
378 }
379 } else if(b == 1) {
380 break;
381 }
382 }
383
384 /* parse name=value params */
385 sstr_t name;
386 sstr_t value;
387 for(;i<line.length;i++) {
388 /* get name */
389 name.ptr = line.ptr + i;
390 for(;i<line.length;i++) {
391 if(line.ptr[i] == '=') {
392 b = 1;
393 i++;
394 break;
395 }
396 }
397 if(!b) {
398 printf("2\n");
399 return NULL;
400 }
401 name.length = line.ptr + i - name.ptr - 1;
402
403 if(line.ptr[i] == '\"') {
404 i++; // TODO: Bug wenn end of line - wird nicht erkannt!
405 }
406 value.ptr = line.ptr + i;
407 for(;i<line.length;i++) {
408 char c = line.ptr[i];
409 if(c < 33 || c == '\"') {
410 b = 1;
411 break;
412 }
413 }
414 if(!b) {
415 printf("3\n");
416 return NULL;
417 }
418 value.length = line.ptr + i - value.ptr;
419
420 name = string_trim(name);
421 value = string_trim(value);
422
423 /* insert name and value into directive pblock */
424 pblock_nvlinsert(
425 name.ptr,
426 name.length,
427 value.ptr,
428 value.length,
429 directive->param);
430 }
431
432 /* get function */
433 char *func_name = pblock_findval("fn", directive->param);
434 directive->func = get_function(func_name);
435
436 *type = directive_type;
437 return directive;
438 }
439
440 int get_directive_type_from_string(sstr_t type) {
441 /* get nsapi function type */
442 int dt = -1;
443 if(sstrcmp(type, sstr("AuthTrans")) == 0) {
444 dt = 0;
445 } else if(sstrcmp(type, sstr("NameTrans")) == 0) {
446 dt = 1;
447 } else if(sstrcmp(type, sstr("PathCheck")) == 0) {
448 dt = 2;
449 } else if(sstrcmp(type, sstr("ObjectType")) == 0) {
450 dt = 3;
451 } else if(sstrcmp(type, sstr("Service")) == 0) {
452 dt = 4;
453 } else if(sstrcmp(type, sstr("AddLog")) == 0) {
454 dt = 5;
455 }
456 return dt;
457 }

mercurial