From f387d898702c05eac22045437c04562e00d466bb Mon Sep 17 00:00:00 2001 From: mOo Date: Tue, 23 Aug 2005 14:35:01 +0000 Subject: [PATCH] fix merging of auto-indexing element, and make autoload modules hack to use array merging git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-merge-1.4.x@616 152afb58-edef-0310-8abb-c4023f1b3aa9 --- src/array.c | 15 +++------ src/array.h | 2 +- src/configfile-glue.c | 2 +- src/configfile.c | 71 +++++++++++++++++++++++++++---------------- src/configfile.h | 1 + src/configparser.y | 30 ++++++++++++------ src/data_array.c | 1 + src/data_count.c | 1 + src/data_fastcgi.c | 1 + src/data_integer.c | 1 + src/data_string.c | 1 + src/mod_staticfile.c | 2 ++ 12 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/array.c b/src/array.c index 7605fcdd..d372be66 100644 --- a/src/array.c +++ b/src/array.c @@ -28,7 +28,6 @@ array *array_init_array(array *src) { a->size = src->size; a->next_power_of_2 = src->next_power_of_2; a->unique_ndx = src->unique_ndx; - a->is_array = src->is_array; a->data = malloc(sizeof(*src->data) * src->size); for (i = 0; i < src->size; i++) { @@ -167,9 +166,9 @@ int array_insert_unique(array *a, data_unset *str) { size_t j; /* generate unique index if neccesary */ - if (str->key->used == 0) { + if (str->key->used == 0 || str->is_index_key) { buffer_copy_long(str->key, a->unique_ndx++); - a->is_array = 1; + str->is_index_key = 1; } /* try to find the string */ @@ -251,12 +250,6 @@ size_t array_get_max_key_length(array *a) { return maxlen; } -static inline int str_int_equal(const char *str, int i) { - char buf[16]; - snprintf(buf, sizeof(buf), "%d", i); - return strcmp(str, buf) == 0; -} - int array_print(array *a, int depth) { size_t i; size_t maxlen; @@ -267,7 +260,7 @@ int array_print(array *a, int depth) { } for (i = 0; i < a->used && oneline; i++) { data_unset *du = a->data[i]; - if (!str_int_equal(du->key->ptr, i)) { + if (!du->is_index_key) { oneline = 0; break; } @@ -299,7 +292,7 @@ int array_print(array *a, int depth) { for (i = 0; i < a->used; i++) { data_unset *du = a->data[i]; array_print_indent(depth + 1); - if (!str_int_equal(du->key->ptr, i)) { + if (!du->is_index_key) { int j; if (i && (i % 5) == 0) { diff --git a/src/array.h b/src/array.h index c072b540..7720c685 100644 --- a/src/array.h +++ b/src/array.h @@ -14,6 +14,7 @@ typedef enum { TYPE_UNSET, TYPE_STRING, TYPE_COUNT, TYPE_ARRAY, TYPE_INTEGER, TY #define DATA_UNSET \ data_type_t type; \ buffer *key; \ + int is_index_key; /* 1 if key is a array index (autogenerated keys) */ \ struct data_unset *(*copy)(const struct data_unset *src); \ void (* free)(struct data_unset *p); \ void (* reset)(struct data_unset *p); \ @@ -33,7 +34,6 @@ typedef struct { size_t size; size_t unique_ndx; - int is_array; /* 0 if it is a hash, 1 for array (autogenerated keys) */ size_t next_power_of_2; int is_weakref; /* data is weakref, don't bother the data */ diff --git a/src/configfile-glue.c b/src/configfile-glue.c index 2f400476..99f002c9 100644 --- a/src/configfile-glue.c +++ b/src/configfile-glue.c @@ -42,7 +42,7 @@ int config_insert_values_internal(server *srv, array *ca, const config_values_t data_string *ds = data_string_init(); buffer_copy_string_buffer(ds->value, ((data_string *)(da->value->data[j]))->value); - if (!da->value->is_array) { + if (!da->is_index_key) { /* the id's were generated automaticly, as we copy now we might have to renumber them * this is used to prepend server.modules by mod_indexfiles as it has to be loaded * before mod_fastcgi and friends */ diff --git a/src/configfile.c b/src/configfile.c index 03d08f6a..27c860f6 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -26,7 +26,6 @@ static int config_insert(server *srv) { size_t i; int ret = 0; buffer *stat_cache_string; - data_string *ds; config_values_t cv[] = { { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */ @@ -117,13 +116,6 @@ static int config_insert(server *srv) { assert(srv->config_storage); - /* prepend default modules */ - if (NULL == array_get_element(srv->srvconf.modules, "mod_indexfile")) { - ds = data_string_init(); - buffer_copy_string(ds->value, "mod_indexfile"); - array_insert_unique(srv->srvconf.modules, (data_unset *)ds); - } - for (i = 0; i < srv->config_context->used; i++) { specific_config *s; @@ -211,21 +203,6 @@ static int config_insert(server *srv) { buffer_free(stat_cache_string); - srv->srvconf.modules->unique_ndx = srv->srvconf.modules->used; - - /* append default modules */ - if (NULL == array_get_element(srv->srvconf.modules, "mod_dirlisting")) { - ds = data_string_init(); - buffer_copy_string(ds->value, "mod_dirlisting"); - array_insert_unique(srv->srvconf.modules, (data_unset *)ds); - } - - if (NULL == array_get_element(srv->srvconf.modules, "mod_staticfile")) { - ds = data_string_init(); - buffer_copy_string(ds->value, "mod_staticfile"); - array_insert_unique(srv->srvconf.modules, (data_unset *)ds); - } - return ret; } @@ -923,6 +900,7 @@ int config_read(server *srv, const char *fn) { data_config *dc; int ret; char *pos; + data_array *modules; context_init(srv, &context); context.all_configs = srv->config_context; @@ -960,16 +938,55 @@ int config_read(server *srv, const char *fn) { return ret; } - if (0 != config_insert(srv)) { - return -1; - } - if (NULL != (dc = (data_config *)array_get_element(srv->config_context, "global"))) { srv->config = dc->value; } else { return -1; } + if (NULL != (modules = (data_array *)array_get_element(srv->config, "server.modules"))) { + data_string *ds; + data_array *prepends; + + if (modules->type != TYPE_ARRAY) { + fprintf(stderr, "server.modules must be an array"); + return -1; + } + + prepends = data_array_init(); + + /* prepend default modules */ + if (NULL == array_get_element(modules->value, "mod_indexfile")) { + ds = data_string_init(); + buffer_copy_string(ds->value, "mod_indexfile"); + array_insert_unique(prepends->value, (data_unset *)ds); + } + + prepends = (data_array *)configparser_merge_data((data_unset *)prepends, (data_unset *)modules); + buffer_copy_string_buffer(prepends->key, modules->key); + array_replace(srv->config, (data_unset *)prepends); + modules->free((data_unset *)modules); + modules = prepends; + + /* append default modules */ + if (NULL == array_get_element(modules->value, "mod_dirlisting")) { + ds = data_string_init(); + buffer_copy_string(ds->value, "mod_dirlisting"); + array_insert_unique(modules->value, (data_unset *)ds); + } + + if (NULL == array_get_element(modules->value, "mod_staticfile")) { + ds = data_string_init(); + buffer_copy_string(ds->value, "mod_staticfile"); + array_insert_unique(modules->value, (data_unset *)ds); + } + } + + + if (0 != config_insert(srv)) { + return -1; + } + return 0; } diff --git a/src/configfile.h b/src/configfile.h index a58a257d..600297f6 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -19,5 +19,6 @@ void configparserFree(void *p, void (*freeProc)(void*)); void configparser(void *yyp, int yymajor, buffer *yyminor, config_t *ctx); int config_parse_file(server *srv, config_t *context, const char *fn); int config_parse_cmd(server *srv, config_t *context, const char *cmd); +data_unset *configparser_merge_data(data_unset *op1, const data_unset *op2); #endif diff --git a/src/configparser.y b/src/configparser.y index 06fa6955..7b717294 100644 --- a/src/configparser.y +++ b/src/configparser.y @@ -70,7 +70,7 @@ static data_unset *configparser_get_variable(config_t *ctx, const buffer *key) { /* op1 is to be eat/return by this function, op1->key is not cared op2 is left untouch, unreferenced */ -data_unset *configparser_merge_data(config_t *ctx, data_unset *op1, const data_unset *op2) { +data_unset *configparser_merge_data(data_unset *op1, const data_unset *op2) { /* type mismatch */ if (op1->type != op2->type) { if (op1->type == TYPE_STRING && op2->type == TYPE_INTEGER) { @@ -85,7 +85,6 @@ data_unset *configparser_merge_data(config_t *ctx, data_unset *op1, const data_u return (data_unset *)ds; } else { fprintf(stderr, "data type mismatch, cannot be merge\n"); - ctx->ok = 0; op1->free(op1); return NULL; } @@ -179,13 +178,23 @@ varline ::= key(A) APPEND expression(B). { if (NULL != (du = array_get_element(vars, A->ptr))) { /* exists in current block */ - du = configparser_merge_data(ctx, du, B); - buffer_copy_string_buffer(du->key, A); - array_replace(vars, du); + du = configparser_merge_data(du, B); + if (NULL == du) { + ctx->ok = 0; + } + else { + buffer_copy_string_buffer(du->key, A); + array_replace(vars, du); + } } else if (NULL != (du = configparser_get_variable(ctx, A))) { - du = configparser_merge_data(ctx, du, B); - buffer_copy_string_buffer(du->key, A); - array_insert_unique(ctx->current->value, du); + du = configparser_merge_data(du, B); + if (NULL == du) { + ctx->ok = 0; + } + else { + buffer_copy_string_buffer(du->key, A); + array_insert_unique(ctx->current->value, du); + } } else { fprintf(stderr, "Undefined config variable in conditional 1 %s: %s\n", ctx->current->key->ptr, A->ptr); @@ -210,7 +219,10 @@ key(A) ::= LKEY(B). { } expression(A) ::= expression(B) PLUS value(C). { - A = configparser_merge_data(ctx, B, C); + A = configparser_merge_data(B, C); + if (NULL == A) { + ctx->ok = 0; + } B = NULL; C->free(C); C = NULL; diff --git a/src/data_array.c b/src/data_array.c index d5a2564a..9dfa6fdf 100644 --- a/src/data_array.c +++ b/src/data_array.c @@ -11,6 +11,7 @@ static data_unset *data_array_copy(const data_unset *s) { buffer_copy_string_buffer(ds->key, src->key); array_free(ds->value); ds->value = array_init_array(src->value); + ds->is_index_key = src->is_index_key; return (data_unset *)ds; } diff --git a/src/data_count.c b/src/data_count.c index d727a106..ca51f67c 100644 --- a/src/data_count.c +++ b/src/data_count.c @@ -10,6 +10,7 @@ static data_unset *data_count_copy(const data_unset *s) { buffer_copy_string_buffer(ds->key, src->key); ds->count = src->count; + ds->is_index_key = src->is_index_key; return (data_unset *)ds; } diff --git a/src/data_fastcgi.c b/src/data_fastcgi.c index da0b8435..714b2905 100644 --- a/src/data_fastcgi.c +++ b/src/data_fastcgi.c @@ -11,6 +11,7 @@ static data_unset *data_fastcgi_copy(const data_unset *s) { buffer_copy_string_buffer(ds->key, src->key); buffer_copy_string_buffer(ds->host, src->host); + ds->is_index_key = src->is_index_key; return (data_unset *)ds; } diff --git a/src/data_integer.c b/src/data_integer.c index 48e8c9b9..96d1d0af 100644 --- a/src/data_integer.c +++ b/src/data_integer.c @@ -9,6 +9,7 @@ static data_unset *data_integer_copy(const data_unset *s) { data_integer *ds = data_integer_init(); buffer_copy_string_buffer(ds->key, src->key); + ds->is_index_key = src->is_index_key; ds->value = src->value; return (data_unset *)ds; } diff --git a/src/data_string.c b/src/data_string.c index 37f108d1..d9325da7 100644 --- a/src/data_string.c +++ b/src/data_string.c @@ -11,6 +11,7 @@ static data_unset *data_string_copy(const data_unset *s) { buffer_copy_string_buffer(ds->key, src->key); buffer_copy_string_buffer(ds->value, src->value); + ds->is_index_key = src->is_index_key; return (data_unset *)ds; } diff --git a/src/mod_staticfile.c b/src/mod_staticfile.c index 8a11d717..dee9834e 100644 --- a/src/mod_staticfile.c +++ b/src/mod_staticfile.c @@ -2,6 +2,8 @@ #include #include #include +#define _XOPEN_SOURCE /* glibc 2.0 */ +#define __USE_XOPEN /* glibc 2.3 */ #include #include "base.h"