[mod_ssi] use config_plugin_values_init()

This commit is contained in:
Glenn Strauss 2019-10-26 22:34:08 -04:00
parent be7eb1083a
commit 5b58e5e47e
2 changed files with 99 additions and 111 deletions

View File

@ -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 */
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;
}
}
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);
}
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]);
}
}
SETDEFAULTS_FUNC(mod_ssi_set_defaults) {
plugin_data *p = p_d;
size_t i = 0;
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 }
};
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 }
};
plugin_data * const p = p_d;
if (!config_plugin_values_init(srv, p, cpk, "mod_ssi"))
return HANDLER_ERROR;
if (!p) 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->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
p->defaults.ssi_exec = 1;
for (i = 0; i < srv->config_context->used; i++) {
data_config const* config = (data_config const*)srv->config_context->data[i];
plugin_config *s;
/* 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);
}
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;
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
return HANDLER_ERROR;
}
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;
}
}
return HANDLER_GO_ON;
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) {

View File

@ -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 {