Sun, 15 Jan 2012 20:30:45 +0100
Added init.conf parser
--- a/src/server/config/conf.c Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/config/conf.c Sun Jan 15 20:30:45 2012 +0100 @@ -145,17 +145,6 @@ return s; } -/* - * checks if the line contains only a comment or space - */ -int cfg_get_basic_type(sstr_t line) { - if(line.length == 0) { - return LINE_NOCONTENT; - } else if(line.ptr[0] == '#') { - return LINE_NOCONTENT; - } - return LINE_OTHER; -} /* * removes a comment from the line @@ -265,6 +254,62 @@ } /* + * parses a line containing a directive and returns a ConfigDirective object + * or NULL if an error occurs + */ +ConfigDirective* cfg_parse_directive(sstr_t line, UcxMempool *mp) { + if(line.length < 6) { + return NULL; // line too short + } + + sstr_t name; + + int i; + for(i=0;i<line.length;i++) { + if(line.ptr[i] < 33) { + break; + } + } + name.ptr = line.ptr; + name.length = i; + + // create directive object + ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective); + directive->directive_type = sstrdub_mp(mp, name); + directive->type_num = cfg_get_directive_type_num(name); + directive->condition = NULL; // set later by main parsing function + directive->param = NULL; + + sstr_t param_str; + param_str.ptr = name.ptr + i; + param_str.length = line.length - i; + param_str = sstrtrim(param_str); + sstr_t pname; + sstr_t pvalue; + for(;;) { + param_str = cfg_param(param_str, &pname, &pvalue); + if(pname.length <= 0) { + break; + } + + // create param object + ConfigParam *param = OBJ_NEW(mp, ConfigParam); + param->name = sstrdub_mp(mp, pname); + if(pvalue.length > 0) { + param->value = sstrdub_mp(mp, pvalue); + } else { + param->value.ptr = NULL; + param->value.length = 0; + } + + // add param to list + directive->param = ucx_list_append(directive->param, param); + } + + return directive; +} + +/* * gets the directive type number from a type string * valid types are: * AuthTrans 0 @@ -295,3 +340,142 @@ } return dt; } + +/* + * checks if the line contains only a comment or space + */ +int cfg_get_basic_type(sstr_t line) { + if(line.length == 0) { + return LINE_NOCONTENT; + } else if(line.ptr[0] == '#') { + return LINE_NOCONTENT; + } + return LINE_OTHER; +} + +/* + * checks if the line contains a begin/end tag or a directive + */ +int cfg_get_line_type(sstr_t line) { + if(line.length < 3) { + // this line is to short to be correct + return LINE_ERROR; + } + + if(line.ptr[0] == '<') { + // start or end tag + // TODO: check for space between '<' and '/' + if(line.ptr[1] == '/') { + return LINE_END_TAG; + } else { + return LINE_BEGIN_TAG; + } + } else { + return LINE_DIRECTIVE; + } +} + +int cfg_get_tag_type(sstr_t tag) { + if(!sstrcmp(tag, sstr("Object"))) { + return TAG_OBJECT; + } else if(!sstrcmp(tag, sstr("If"))) { + return TAG_IF; + } else if(!sstrcmp(tag, sstr("ElseIf"))) { + return TAG_ELSEIF; + } else if(!sstrcmp(tag, sstr("Else"))) { + return TAG_ELSE; + } else if(!sstrcmp(tag, sstr("Client"))) { + return TAG_CLIENT; + } + return -1; +} + +/* + * returns the name of the ending tag + * on error, this functions returns a zero length string + */ +sstr_t cfg_get_end_tag_name(sstr_t line) { + sstr_t ns; + ns.ptr = NULL; + ns.length = 0; + + if(line.length < 4) { + // minimum of 4 chars: </a> + return ns; + } + + sstr_t name; + name.ptr = line.ptr + 2; + name.length = line.length - 3; + + // check for </ > frame + if(line.ptr[0] != '<' + || line.ptr[1] != '/' + || line.ptr[line.length - 1] != '>') + { + return ns; + } + + return sstrtrim(name); +} + +ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxMempool *mp) { + if(line.length < 4) { + return NULL; // this line can't contain a valid tag + } + + if(line.ptr[0] != '<' || line.ptr[line.length - 1] != '>') { + return NULL; // syntax error + } + + sstr_t name; + name.ptr = line.ptr + 1; + int i; + for(i=1;i<line.length - 1;i++) { + if(line.ptr[i] < 33) { // char is space + name.length = i - 1; + break; + } + } + if(name.length < 1) { + return NULL; // syntax error + } + + // create tag object + ConfigTag *tag = OBJ_NEW(mp, ConfigTag); + tag->name = sstrdub_mp(mp, name); + tag->param = NULL; + + // parse parameters + sstr_t param_str; + param_str.ptr = line.ptr + i; + param_str.length = line.length - name.length - 2; + param_str = sstrtrim(param_str); + if(param_str.length <= 0) { + return tag; // no parameters + } + + sstr_t pname; + sstr_t pvalue; + for(;;) { + param_str = cfg_param(param_str, &pname, &pvalue); + if(pname.length <= 0) { + break; + } + + // create param object + ConfigParam *param = OBJ_NEW(mp, ConfigParam); + param->name = sstrdub_mp(mp, pname); + if(pvalue.length > 0) { + param->value = sstrdub_mp(mp, pvalue); + } else { + param->value.ptr = NULL; + param->value.length = 0; + } + + // add param to list + tag->param = ucx_list_append(tag->param, param); + } + + return tag; +}
--- a/src/server/config/conf.h Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/config/conf.h Sun Jan 15 20:30:45 2012 +0100 @@ -26,8 +26,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#ifndef ACONF_H -#define ACONF_H +#ifndef CFG_CONF_H +#define CFG_CONF_H #include <stdio.h> #include <stdlib.h> @@ -55,6 +55,13 @@ #define LINE_NOCONTENT 5 // only comment or space #define LINE_ERROR 6 // parse error on this line +// tag types +#define TAG_OBJECT 0 +#define TAG_IF 1 +#define TAG_ELSEIF 2 +#define TAG_ELSE 3 +#define TAG_CLIENT 4 + typedef struct _cfg_line { sstr_t line; // raw line string void *object; // pointer to data struct @@ -75,24 +82,56 @@ } ConfigParser; +typedef struct _conf_tag ConfigTag; +struct _conf_tag { + ConfigLine *begin; + ConfigLine *end; + + sstr_t name; + UcxList *param; + ConfigTag *parent; + ConfigTag *iftag; // only used by <ElseIf> and <Else> + int type_num; +}; + +typedef struct _conf_directive { + ConfigLine *begin; + ConfigLine *end; + + sstr_t directive_type; + UcxList *param; + ConfigTag *condition; + int type_num; +} ConfigDirective; + + int cfg_parse_basic_file(ConfigParser *parser, FILE *in); sstr_t cfg_readln(FILE *file); -int cfg_get_basic_type(sstr_t line); - sstr_t cfg_trim_comment(sstr_t line); sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value); sstr_t cfg_param_get(UcxList *list, sstr_t name); +ConfigDirective* cfg_parse_directive(sstr_t line, UcxMempool *mp); + int cfg_get_directive_type_num(sstr_t type); +int cfg_get_basic_type(sstr_t line); + +int cfg_get_line_type(sstr_t line); + +int cfg_get_tag_type(sstr_t tag); + +sstr_t cfg_get_end_tag_name(sstr_t line); + +ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxMempool *mp); #ifdef __cplusplus } #endif -#endif /* ACONF_H */ +#endif /* CFG_CONF_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/config/initconf.c Sun Jan 15 20:30:45 2012 +0100 @@ -0,0 +1,79 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "initconf.h" + +InitConfig *load_init_config(char *file) { + FILE *in = fopen(file, "r"); + if(in == NULL) { + return NULL; + } + + InitConfig *conf = malloc(sizeof(InitConfig)); + conf->parser.parse = initconf_parse; + conf->file = file; + + int r = cfg_parse_basic_file((ConfigParser*)conf, in); + if(r != 0) { + // TODO: free + return NULL; + } + + return conf; +} + +void free_init_config(InitConfig *conf) { + if(conf->directives != NULL) { + ucx_dlist_free(conf->directives); + } + if(conf->parser.lines != NULL) { + ucx_dlist_free(conf->parser.lines); + } + ucx_mempool_free(conf->parser.mp); + free(conf); +} + +int initconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { + InitConfig *conf = p; + + // parse directive + ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp); + d->begin = begin; + d->end = end; + if(d->type_num == 6) { + conf->directives = ucx_dlist_append(conf->directives, d); + } else { + fprintf(stderr, "Warning: Non Init directive in init.conf"); + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/config/initconf.h Sun Jan 15 20:30:45 2012 +0100 @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef INITCONF_H +#define INITCONF_H + +#include "conf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _init_conf { + ConfigParser parser; + char *file; + UcxDlist *directives; +} InitConfig; + +InitConfig *load_init_config(char *file); + +void free_init_config(InitConfig *conf); + +int initconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line); + +#ifdef __cplusplus +} +#endif + +#endif /* INITCONF_H */ +
--- a/src/server/config/objconf.c Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/config/objconf.c Sun Jan 15 20:30:45 2012 +0100 @@ -47,6 +47,7 @@ ObjectConfig *conf = malloc(sizeof(ObjectConfig)); conf->parser.parse = objconf_parse; conf->file = file; + conf->conditions = NULL; int r = cfg_parse_basic_file((ConfigParser*)conf, in); if(r != 0) { @@ -57,46 +58,80 @@ return conf; } +void free_object_config(ObjectConfig *conf) { + // free all objects + UcxDlist *objects = conf->objects; + while(objects != NULL) { + ConfigObject *obj = objects->data; + // free all directive lists + for(int i=0;i<6;i++) { + if(obj->directives[i]) { + ucx_dlist_free(obj->directives[i]); + } + } + + objects = objects->next; + } + if(conf->objects) { + ucx_dlist_free(conf->objects); + } + + // free other lists + if(conf->levels) { + ucx_list_free(conf->levels); + } + if(conf->lines) { + ucx_dlist_free(conf->lines); + } + if(conf->conditions) { + ucx_dlist_free(conf->conditions); + } + + // free mempool + ucx_mempool_free(conf->parser.mp); + free(conf); +} + int objconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { ObjectConfig *conf = p; - begin->type = objconf_get_line_type(line); + begin->type = cfg_get_line_type(line); switch(begin->type) { case LINE_BEGIN_TAG: { - ConfigTag *tag = objconf_parse_begin_tag(line, conf->parser.mp); + ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp); if(tag == NULL) { fprintf(stderr, "Parse error!\n"); exit(-1); // TODO: better error handling } tag->begin = begin; tag->end = end; - tag->type_num = objconf_get_tag_type(tag->name); + tag->type_num = cfg_get_tag_type(tag->name); //printf("line {%s}\n", sstrdub(ll).ptr); if(objconf_on_begin_tag(conf, tag) != 0) { - fprintf(stderr, "1error!\n"); + fprintf(stderr, "1error\n"); exit(-1); } break; } case LINE_END_TAG: { - sstr_t tag = objconf_get_end_tag_name(line); + sstr_t tag = cfg_get_end_tag_name(line); if(objconf_on_end_tag(conf, tag) != 0) { - fprintf(stderr, "2error!\n"); + fprintf(stderr, "2error\n"); exit(-1); } break; } case LINE_DIRECTIVE: { - ConfigDirective *dir = objconf_parse_directive( + ConfigDirective *dir = cfg_parse_directive( line, conf->parser.mp); dir->begin = begin; dir->end = end; if(objconf_on_directive(conf, dir) != 0) { - fprintf(stderr, "3error!\n"); + fprintf(stderr, "3error\n"); exit(-1); } } @@ -118,7 +153,7 @@ ConfigObject *obj = OBJ_NEW_N(conf->parser.mp, ConfigObject); obj->begin = tag->begin; obj->end = tag->end; - + obj->name = cfg_param_get(tag->param, sstr("name")); obj->ppath = cfg_param_get(tag->param, sstr("ppath")); @@ -138,7 +173,10 @@ // create tree level object ConfigParserLevel *last_lvl = conf->levels->data; - ConfigParserLevel *lvl = OBJ_NEW(conf->parser.mp, ConfigParserLevel); + ConfigParserLevel *lvl = OBJ_NEW( + conf->parser.mp, + ConfigParserLevel); + lvl->iftag = NULL; lvl->levelnum = last_lvl->levelnum + 1; lvl->tag = tag; @@ -182,7 +220,7 @@ } int objconf_on_end_tag(ObjectConfig *conf, sstr_t tagname) { - int type = objconf_get_tag_type(tagname); + int type = cfg_get_tag_type(tagname); if(type == -1) { fprintf(stderr, "unknown tag\n"); return 1; @@ -215,185 +253,3 @@ return 0; } -/* - * checks if the line contains a begin/end tag or a directive - */ -int objconf_get_line_type(sstr_t line) { - if(line.length < 3) { - // this line is to short to be correct - return LINE_ERROR; - } - - if(line.ptr[0] == '<') { - // start or end tag - // TODO: check for space between '<' and '/' - if(line.ptr[1] == '/') { - return LINE_END_TAG; - } else { - return LINE_BEGIN_TAG; - } - } else { - return LINE_DIRECTIVE; - } -} - -ConfigTag* objconf_parse_begin_tag(sstr_t line, UcxMempool *mp) { - if(line.length < 4) { - return NULL; // this line can't contain a valid tag - } - - if(line.ptr[0] != '<' || line.ptr[line.length - 1] != '>') { - return NULL; // syntax error - } - - sstr_t name; - name.ptr = line.ptr + 1; - int i; - for(i=1;i<line.length - 1;i++) { - if(line.ptr[i] < 33) { // char is space - name.length = i - 1; - break; - } - } - if(name.length < 1) { - return NULL; // syntax error - } - - // create tag object - ConfigTag *tag = OBJ_NEW(mp, ConfigTag); - tag->name = sstrdub_mp(mp, name); - tag->param = NULL; - - // parse parameters - sstr_t param_str; - param_str.ptr = line.ptr + i; - param_str.length = line.length - name.length - 2; - param_str = sstrtrim(param_str); - if(param_str.length <= 0) { - return tag; // no parameters - } - - sstr_t pname; - sstr_t pvalue; - for(;;) { - param_str = cfg_param(param_str, &pname, &pvalue); - if(pname.length <= 0) { - break; - } - - // create param object - ConfigParam *param = OBJ_NEW(mp, ConfigParam); - param->name = sstrdub_mp(mp, pname); - if(pvalue.length > 0) { - param->value = sstrdub_mp(mp, pvalue); - } else { - param->value.ptr = NULL; - param->value.length = 0; - } - - // add param to list - tag->param = ucx_list_append(tag->param, param); - } - - return tag; -} - -/* - * returns the name of the ending tag - * on error, this functions returns a zero length string - */ -sstr_t objconf_get_end_tag_name(sstr_t line) { - sstr_t ns; - ns.ptr = NULL; - ns.length = 0; - - if(line.length < 4) { - // minimum of 4 chars: </a> - return ns; - } - - sstr_t name; - name.ptr = line.ptr + 2; - name.length = line.length - 3; - - // check for </ > frame - if(line.ptr[0] != '<' - || line.ptr[1] != '/' - || line.ptr[line.length - 1] != '>') - { - return ns; - } - - return sstrtrim(name); -} - -/* - * parses a line containing a directive and returns a ConfigDirective object - * or NULL if an error occurs - */ -ConfigDirective* objconf_parse_directive(sstr_t line, UcxMempool *mp) { - if(line.length < 6) { - return NULL; // line too short - } - - sstr_t name; - - int i; - for(i=0;i<line.length;i++) { - if(line.ptr[i] < 33) { - break; - } - } - name.ptr = line.ptr; - name.length = i; - - // create directive object - ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective); - directive->directive_type = sstrdub_mp(mp, name); - directive->type_num = cfg_get_directive_type_num(name); - directive->condition = NULL; // set later by main parsing function - directive->param = NULL; - - sstr_t param_str; - param_str.ptr = name.ptr + i; - param_str.length = line.length - i; - param_str = sstrtrim(param_str); - sstr_t pname; - sstr_t pvalue; - for(;;) { - param_str = cfg_param(param_str, &pname, &pvalue); - if(pname.length <= 0) { - break; - } - - // create param object - ConfigParam *param = OBJ_NEW(mp, ConfigParam); - param->name = sstrdub_mp(mp, pname); - if(pvalue.length > 0) { - param->value = sstrdub_mp(mp, pvalue); - } else { - param->value.ptr = NULL; - param->value.length = 0; - } - - // add param to list - directive->param = ucx_list_append(directive->param, param); - } - - return directive; -} - -int objconf_get_tag_type(sstr_t tag) { - if(!sstrcmp(tag, sstr("Object"))) { - return TAG_OBJECT; - } else if(!sstrcmp(tag, sstr("If"))) { - return TAG_IF; - } else if(!sstrcmp(tag, sstr("ElseIf"))) { - return TAG_ELSEIF; - } else if(!sstrcmp(tag, sstr("Else"))) { - return TAG_ELSE; - } else if(!sstrcmp(tag, sstr("Client"))) { - return TAG_CLIENT; - } - return -1; -}
--- a/src/server/config/objconf.h Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/config/objconf.h Sun Jan 15 20:30:45 2012 +0100 @@ -35,42 +35,12 @@ extern "C" { #endif -#define TAG_OBJECT 0 -#define TAG_IF 1 -#define TAG_ELSEIF 2 -#define TAG_ELSE 3 -#define TAG_CLIENT 4 - - -typedef struct _conf_tag ConfigTag; -struct _conf_tag { - ConfigLine *begin; - ConfigLine *end; - - sstr_t name; - UcxList *param; - ConfigTag *parent; - ConfigTag *iftag; // only used by <ElseIf> and <Else> - int type_num; -}; - -typedef struct _conf_directive { - ConfigLine *begin; - ConfigLine *end; - - sstr_t directive_type; - UcxList *param; - ConfigTag *condition; - int type_num; -} ConfigDirective; - typedef struct _conf_object { ConfigLine *begin; ConfigLine *end; sstr_t name; sstr_t ppath; - ConfigTag *conditions; // directives UcxDlist *directives[6]; } ConfigObject; @@ -99,6 +69,8 @@ ObjectConfig *load_object_config(char *file); +void free_object_config(ObjectConfig *conf); + int objconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line); int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag); @@ -107,16 +79,6 @@ int objconf_on_directive(ObjectConfig *conf, ConfigDirective *dir); -int objconf_get_line_type(sstr_t line); - -ConfigTag* objconf_parse_begin_tag(sstr_t line, UcxMempool *mp); - -sstr_t objconf_get_end_tag_name(sstr_t line); - -ConfigDirective* objconf_parse_directive(sstr_t line, UcxMempool *mp); - -int objconf_get_tag_type(sstr_t tag); - #ifdef __cplusplus }
--- a/src/server/config/objs.mk Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/config/objs.mk Sun Jan 15 20:30:45 2012 +0100 @@ -32,6 +32,7 @@ CONFOBJ = objconf.o CONFOBJ += conf.o +CONFOBJ += initconf.o CONFOBJS = $(CONFOBJ:%=$(CONF_OBJPRE)%) CONFSOURCE = $(CONFOBJ:%.o=config/%.c)
--- a/src/server/daemon/conf.c Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/daemon/conf.c Sun Jan 15 20:30:45 2012 +0100 @@ -47,45 +47,60 @@ #include "../util/pblock.h" #include "../config/objconf.h" +#include "../config/initconf.h" VirtualServer *default_vs; +pool_handle_t *cfg_pool; + +// TODO: Funktion für ConfigDirective -> directive +// TODO: Funktion für UcxList parameter list -> pblock +// TODO: ConfigurationManager +// TODO: server.conf + void load_init_conf(char *file) { printf("load_init_conf\n"); - pool_handle_t *pool = pool_create(); - - FILE *in = fopen("conf/init.conf", "r"); - if(in == NULL) { - fprintf(stderr, "Cannot open conf/init.conf\n"); + InitConfig *cfg = load_init_config("conf/init.conf"); + if(cfg == NULL) { return; } - char buf[512]; - buf[0] = 0; - int len = 512; + cfg_pool = pool_create(); // one pool for one Configuration + UcxDlist *dirs = cfg->directives; + while(dirs != NULL) { + ConfigDirective *dir = dirs->data; - while(!feof(in)) { - fgets(buf, len, in); + /* create NSAPI directive */ + directive *d = malloc(sizeof(directive)); + d->param = pblock_create_pool(cfg_pool, 8); + UcxList *param = dir->param; + while(param != NULL) { + ConfigParam *p = param->data; + pblock_nvlinsert( + p->name.ptr, + p->name.length, + p->value.ptr, + p->value.length, + d->param); + param = param->next; + } - if(*buf == 0) { + /* get function */ + char *func_name = pblock_findval("fn", d->param); + d->func = get_function(func_name); + if(d->func == NULL) { + free(d); continue; } - char *ptr; - if((ptr = strrchr(buf, '\n'))) { - ptr[0] = 0; - } - - sstr_t line = string_trim(sstr(buf)); - if(line.length > 0) { - sstr_t type; - directive *d = parse_directive(pool, line, &type); - if(sstrcmp(type, sstr("Init"))) { - d->func->func(d->param, NULL, NULL); - } - } + /* execute init directive */ + d->func->func(d->param, NULL, NULL); + + dirs = dirs->next; } + + free_init_config(cfg); } void load_server_conf(char *file) { @@ -103,59 +118,7 @@ // load obj.conf default_vs->objects = load_obj_conf("conf/obj.conf"); default_vs->default_obj_name = "default"; - - // begin objset test - /* - httpd_objset *objset = default_vs->objset; - for(int i=0;i<objset->pos;i++) { - httpd_object *obj = objset->obj[i]; - printf("<object [%s]>\n", obj->name); - for(int j=0;j<obj->nd;j++) { - dtable *dt; - switch(j) { - case NSAPIAuthTrans: { - printf(" Get AuthTrans Directives\n"); - dt = object_get_dtable(obj, NSAPIAuthTrans); - break; - } - case NSAPINameTrans: { - printf(" Get NameTrans Directives\n"); - dt = object_get_dtable(obj, NSAPINameTrans); - break; - } - case NSAPIPathCheck: { - printf(" Get PathCheck Directives\n"); - dt = object_get_dtable(obj, NSAPIPathCheck); - break; - } - case NSAPIService: { - printf(" Get Service Directives\n"); - dt = object_get_dtable(obj, NSAPIService); - break; - } - default: { - printf("j: %d\n", j); - dt = object_get_dtable(obj, j); - break; - } - } - if(dt != NULL) { - printf(" dtable[%d].length = %d\n", dt, dt->ndir); - } else { - continue; - } - for(int k=0;k<dt->ndir;k++) { - directive *d = dt->directive[k]; - if(d == NULL) { - printf("d is null\n"); - } else { - printf(" Directive[%d].name = %s\n", d, d->func->name); - } - } - } - } - */ - // end objset test + } VirtualServer* conf_get_default_vs() { @@ -173,7 +136,7 @@ /* create object config */ HTTPObjectConfig *conf = calloc(sizeof(HTTPObjectConfig), 1); - conf->pool = pool_create(); + conf->pool = cfg_pool; /* convert ObjectConfig to HTTPObjectConfig */ @@ -241,263 +204,7 @@ objlist = objlist->next; } - - - - + free_object_config(cfg); return conf; } - -void obj_conf_parse_line(ObjectConfParser *parser, sstr_t line) { - //printf("{%s}[%d]\n", line.ptr, line.length); - if(line.ptr[0] == '#') { - return; - } - - if(line.length < 3) { - // to short for everything - fprintf(stderr, "obj.conf: line to short \"%s\"\n", line.ptr); - return; - } - - // TODO: ersetzen - if(line.ptr[0] == '<') { - if(line.ptr[1] == '/') { - // end tag - if(line.ptr[2] == 'O' && parser->obj != NULL) { - // end of Object - httpobjconf_add_object(parser->conf, parser->obj); - parser->obj = NULL; - return; - } - } else { - // new tag - httpd_object *obj = parse_new_object_tag(line); - parser->obj = obj; - } - } else { - // directive - sstr_t dtype; - directive *d = parse_directive(parser->conf->pool, line, &dtype); - int dt = get_directive_type_from_string(dtype); - object_add_directive(parser->obj, d, dt); - } -} - - -/* utils */ - -sstr_t string_trim(sstr_t string) { - sstr_t newstr = string; - int nsoff = 0; - int l = 1; - for(int i=0;i<string.length;i++) { - char c = string.ptr[i]; - if(l) { - /* leading whitespace */ - if(c > 32) { - l = 0; - nsoff = i; - newstr.ptr = &string.ptr[i]; - newstr.length = string.length - nsoff; - } - } else { - /* trailing whitespace */ - if(c > 32) { - newstr.length = (i - nsoff) + 1; - } - } - } - return newstr; -} - -httpd_object* parse_new_object_tag(sstr_t line) { - int i = 0; - int b = 0; - sstr_t name; - sstr_t value; - - char *obj_name = NULL; - char *obj_ppath = NULL; - - for(;i<line.length;i++) { - if(line.ptr[i] < 33) { - b = 1; - } else if(b == 1) { - break; - } - } - if(!b || line.ptr[i] < 33) { - printf("1\n"); - return NULL; - } - b = 0; - - /* parse name=value params */ - for(;i<line.length;i++) { - if(line.ptr[i] == '>') { - break; - } - - /* get name */ - name.ptr = line.ptr + i; - for(;i<line.length;i++) { - if(line.ptr[i] == '=') { - b = 1; - i++; - break; - } - } - if(!b) { - printf("2\n"); - return NULL; - } - name.length = line.ptr + i - name.ptr - 1; - - if(line.ptr[i] == '\"') { - i++; // TODO: Bug wenn end of line - wird nicht erkannt! - } - value.ptr = line.ptr + i; - for(;i<line.length;i++) { - char c = line.ptr[i]; - if(c < 33 || c == '\"' || c == '>') { - b = 1; - break; - } - } - if(!b) { - printf("3\n"); - return NULL; - } - value.length = line.ptr + i - value.ptr; - - if(sstrcmp(name, sstrn("name", 4)) == 0) { - obj_name = sstrdub(value).ptr; - } else if (sstrcmp(name, sstrn("ppath", 5)) == 0) { - obj_ppath = sstrdub(value).ptr; - } - - /* - printf("name: [%d]{", name.length); - fwrite(name.ptr, 1, name.length, stdout); - printf("}\n"); - printf("value: [%d]{", value.length); - fwrite(value.ptr, 1, value.length, stdout); - printf("}\n"); - */ - - char c = line.ptr[i]; - if(c == '>') { - break; - } else { - i++; - } - } - - if(obj_name != NULL || obj_ppath != NULL) { - httpd_object *o = object_new(obj_name); - o->path = obj_ppath; - return o; - } - - return NULL; -} - -directive* parse_directive(pool_handle_t *pool, sstr_t line, sstr_t *type) { - int i = 0; - int b = 0; - - sstr_t directive_type = line; - - directive *directive = malloc(sizeof(directive)); - directive->cond = NULL; - directive->param = pblock_create_pool(pool, 8); - - for(;i<line.length;i++) { - if(line.ptr[i] < 33) { - b = 1; - directive_type.length = i; - if(directive_type.length <= 0) { - fprintf(stderr, "parse error: cannot parse directive\n"); - return NULL; - } - } else if(b == 1) { - break; - } - } - - /* parse name=value params */ - sstr_t name; - sstr_t value; - for(;i<line.length;i++) { - /* get name */ - name.ptr = line.ptr + i; - for(;i<line.length;i++) { - if(line.ptr[i] == '=') { - b = 1; - i++; - break; - } - } - if(!b) { - printf("2\n"); - return NULL; - } - name.length = line.ptr + i - name.ptr - 1; - - if(line.ptr[i] == '\"') { - i++; // TODO: Bug wenn end of line - wird nicht erkannt! - } - value.ptr = line.ptr + i; - for(;i<line.length;i++) { - char c = line.ptr[i]; - if(c < 33 || c == '\"') { - b = 1; - break; - } - } - if(!b) { - printf("3\n"); - return NULL; - } - value.length = line.ptr + i - value.ptr; - - name = string_trim(name); - value = string_trim(value); - - /* insert name and value into directive pblock */ - pblock_nvlinsert( - name.ptr, - name.length, - value.ptr, - value.length, - directive->param); - } - - /* get function */ - char *func_name = pblock_findval("fn", directive->param); - directive->func = get_function(func_name); - - *type = directive_type; - return directive; -} - -int get_directive_type_from_string(sstr_t type) { - /* get nsapi function type */ - int dt = -1; - if(sstrcmp(type, sstr("AuthTrans")) == 0) { - dt = 0; - } else if(sstrcmp(type, sstr("NameTrans")) == 0) { - dt = 1; - } else if(sstrcmp(type, sstr("PathCheck")) == 0) { - dt = 2; - } else if(sstrcmp(type, sstr("ObjectType")) == 0) { - dt = 3; - } else if(sstrcmp(type, sstr("Service")) == 0) { - dt = 4; - } else if(sstrcmp(type, sstr("AddLog")) == 0) { - dt = 5; - } - return dt; -}
--- a/src/server/daemon/ws-fn.c Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/daemon/ws-fn.c Sun Jan 15 20:30:45 2012 +0100 @@ -31,9 +31,11 @@ #include "../safs/nametrans.h" #include "../safs/objecttype.h" #include "../safs/service.h" +#include "../safs/init.h" #include "../webdav/webdav.h" struct FuncStruct webserver_funcs[] = { + { "init-test", init_test, NULL, 0 }, { "test-nametrans", test_nametrans, NULL, 0 }, { "assign-name", assign_name, NULL, 0}, { "type-by-extension", object_type_by_extension, NULL, 0},
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/init.c Sun Jan 15 20:30:45 2012 +0100 @@ -0,0 +1,34 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "init.h" + +int init_test(pblock *pb, Session *sn, Request *rq) { + printf("init-test\n"); + return REQ_PROCEED; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/safs/init.h Sun Jan 15 20:30:45 2012 +0100 @@ -0,0 +1,46 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef INIT_H +#define INIT_H + +#include "../public/nsapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int init_test(pblock *pb, Session *sn, Request *rq); + + +#ifdef __cplusplus +} +#endif + +#endif /* INIT_H */ +
--- a/src/server/safs/objecttype.c Sun Jan 15 17:00:16 2012 +0100 +++ b/src/server/safs/objecttype.c Sun Jan 15 20:30:45 2012 +0100 @@ -33,6 +33,9 @@ int object_type_by_extension(pblock *pb, Session *sn, Request *rq) { sstr_t ppath = sstr(pblock_findkeyval(pb_key_ppath, rq->vars)); + + printf("\nobject_type_by_extension: {%s}[%d]\n\n", ppath); + sstr_t ct; if(ppath.ptr[ppath.length - 1] == '/') { /* directory */ @@ -61,6 +64,8 @@ ct = sstr("text/html"); } else if(!sstrcmp(ext, sstrn("xml", 3))) { ct = sstr("text/xml"); + } else { + return REQ_ABORTED; } }