From 5b58e5e47e6c69c1453ae009fa31d3013dadbd94 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sat, 26 Oct 2019 22:34:08 -0400 Subject: [PATCH] [mod_ssi] use config_plugin_values_init() --- src/mod_ssi.c | 206 ++++++++++++++++++++++++-------------------------- src/mod_ssi.h | 12 +-- 2 files changed, 103 insertions(+), 115 deletions(-) diff --git a/src/mod_ssi.c b/src/mod_ssi.c index b5ee50a2..dc200cf8 100644 --- a/src/mod_ssi.c +++ b/src/mod_ssi.c @@ -55,11 +55,11 @@ static void handler_ctx_free(handler_ctx *hctx) { /* The newest modified time of included files for include statement */ static volatile time_t include_file_last_mtime = 0; -/* init the plugin data */ INIT_FUNC(mod_ssi_init) { plugin_data *p; p = calloc(1, sizeof(*p)); + force_assert(p); p->timefmt = buffer_init(); p->stat_fn = buffer_init(); @@ -70,88 +70,120 @@ INIT_FUNC(mod_ssi_init) { return p; } -/* detroy the plugin data */ FREE_FUNC(mod_ssi_free) { plugin_data *p = p_d; UNUSED(srv); if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - size_t i; - for (i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - - if (NULL == s) continue; - - array_free(s->ssi_extension); - buffer_free(s->content_type); - - free(s); - } - free(p->config_storage); - } - array_free(p->ssi_vars); array_free(p->ssi_cgi_env); buffer_free(p->timefmt); buffer_free(p->stat_fn); + free(p->cvlist); free(p); return HANDLER_GO_ON; } -/* handle plugin config and check values */ - -SETDEFAULTS_FUNC(mod_ssi_set_defaults) { - plugin_data *p = p_d; - size_t i = 0; - - config_values_t cv[] = { - { "ssi.extension", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ - { "ssi.content-type", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */ - { "ssi.conditional-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */ - { "ssi.exec", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 3 */ - { "ssi.recursion-max", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 4 */ - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - if (!p) return HANDLER_ERROR; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); - - for (i = 0; i < srv->config_context->used; i++) { - data_config const* config = (data_config const*)srv->config_context->data[i]; - plugin_config *s; - - s = calloc(1, sizeof(plugin_config)); - s->ssi_extension = array_init(); - s->content_type = buffer_init(); - s->conditional_requests = 0; - s->ssi_exec = 1; - s->ssi_recursion_max = 0; - - cv[0].destination = s->ssi_extension; - cv[1].destination = s->content_type; - cv[2].destination = &(s->conditional_requests); - cv[3].destination = &(s->ssi_exec); - cv[4].destination = &(s->ssi_recursion_max); - - p->config_storage[i] = s; +static void mod_ssi_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* ssi.extension */ + pconf->ssi_extension = cpv->v.a; + break; + case 1: /* ssi.content-type */ + pconf->content_type = cpv->v.b; + break; + case 2: /* ssi.conditional-requests */ + pconf->conditional_requests = cpv->v.u; + break; + case 3: /* ssi.exec */ + pconf->ssi_exec = cpv->v.u; + break; + case 4: /* ssi.recursion-max */ + pconf->ssi_recursion_max = cpv->v.shrt; + break; + default:/* should not happen */ + return; + } +} - if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; - } +static void mod_ssi_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_ssi_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - if (!array_is_vlist(s->ssi_extension)) { - log_error_write(srv, __FILE__, __LINE__, "s", - "unexpected value for ssi.extension; expected list of \"ext\""); - return HANDLER_ERROR; - } - } +static void mod_ssi_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_ssi_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); + } +} - return HANDLER_GO_ON; +SETDEFAULTS_FUNC(mod_ssi_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("ssi.extension"), + T_CONFIG_ARRAY, + T_CONFIG_SCOPE_CONNECTION } + ,{ CONST_STR_LEN("ssi.content-type"), + T_CONFIG_STRING, + T_CONFIG_SCOPE_CONNECTION } + ,{ CONST_STR_LEN("ssi.conditional-requests"), + T_CONFIG_BOOL, + T_CONFIG_SCOPE_CONNECTION } + ,{ CONST_STR_LEN("ssi.exec"), + T_CONFIG_BOOL, + T_CONFIG_SCOPE_CONNECTION } + ,{ CONST_STR_LEN("ssi.recursion-max"), + T_CONFIG_SHORT, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; + + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_ssi")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* ssi.extension */ + if (!array_is_vlist(cpv->v.a)) { + log_error(srv->errh, __FILE__, __LINE__, + "unexpected value for %s; " + "expected list of \"ext\"", cpk[cpv->k_id].k); + return HANDLER_ERROR; + } + break; + case 1: /* ssi.content-type */ + case 2: /* ssi.conditional-requests */ + case 3: /* ssi.exec */ + case 4: /* ssi.recursion-max */ + break; + default:/* should not happen */ + break; + } + } + } + + p->defaults.ssi_exec = 1; + + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_ssi_merge_config(&p->defaults, cpv); + } + + return HANDLER_GO_ON; } @@ -1258,54 +1290,15 @@ static int mod_ssi_handle_request(server *srv, connection *con, handler_ctx *p) return 0; } -#define PATCH(x) \ - p->conf.x = s->x; -static int mod_ssi_patch_connection(server *srv, connection *con, plugin_data *p) { - size_t i, j; - plugin_config *s = p->config_storage[0]; - - PATCH(ssi_extension); - PATCH(content_type); - PATCH(conditional_requests); - PATCH(ssi_exec); - PATCH(ssi_recursion_max); - - /* skip the first, the global context */ - for (i = 1; i < srv->config_context->used; i++) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ - - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; - - /* merge config */ - for (j = 0; j < dc->value->used; j++) { - data_unset *du = dc->value->data[j]; - - if (buffer_is_equal_string(&du->key, CONST_STR_LEN("ssi.extension"))) { - PATCH(ssi_extension); - } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("ssi.content-type"))) { - PATCH(content_type); - } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("ssi.conditional-requests"))) { - PATCH(conditional_requests); - } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("ssi.exec"))) { - PATCH(ssi_exec); - } else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("ssi.recursion-max"))) { - PATCH(ssi_recursion_max); - } - } - } - - return 0; -} -#undef PATCH - URIHANDLER_FUNC(mod_ssi_physical_path) { plugin_data *p = p_d; + UNUSED(srv); if (con->mode != DIRECT) return HANDLER_GO_ON; if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON; - mod_ssi_patch_connection(srv, con, p); + mod_ssi_patch_config(con, p); + if (NULL == p->conf.ssi_extension) return HANDLER_GO_ON; if (array_match_value_suffix(p->conf.ssi_extension, con->physical.path)) { con->plugin_ctx[p->id] = handler_ctx_init(p); @@ -1349,7 +1342,6 @@ static handler_t mod_ssi_connection_reset(server *srv, connection *con, void *p_ return HANDLER_GO_ON; } -/* this function is called at dlopen() time and inits the callbacks */ int mod_ssi_plugin_init(plugin *p); int mod_ssi_plugin_init(plugin *p) { diff --git a/src/mod_ssi.h b/src/mod_ssi.h index b4722b29..fb5848af 100644 --- a/src/mod_ssi.h +++ b/src/mod_ssi.h @@ -8,11 +8,9 @@ #include "plugin.h" -/* plugin config for all request/connections */ - typedef struct { - array *ssi_extension; - buffer *content_type; + const array *ssi_extension; + const buffer *content_type; unsigned short conditional_requests; unsigned short ssi_exec; unsigned short ssi_recursion_max; @@ -20,6 +18,8 @@ typedef struct { typedef struct { PLUGIN_DATA; + plugin_config defaults; + plugin_config conf; buffer *timefmt; @@ -27,10 +27,6 @@ typedef struct { array *ssi_vars; array *ssi_cgi_env; - - plugin_config **config_storage; - - plugin_config conf; } plugin_data; typedef struct {