summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mod_redirect.c231
1 files changed, 125 insertions, 106 deletions
diff --git a/src/mod_redirect.c b/src/mod_redirect.c
index 5b71a428..0d8a5792 100644
--- a/src/mod_redirect.c
+++ b/src/mod_redirect.c
@@ -20,7 +20,7 @@ typedef struct {
typedef struct {
PLUGIN_DATA;
- plugin_config **config_storage;
+ plugin_config defaults;
plugin_config conf;
} plugin_data;
@@ -28,121 +28,140 @@ INIT_FUNC(mod_redirect_init) {
return calloc(1, sizeof(plugin_data));
}
+static void mod_redirect_free_config(plugin_data * const p) {
+ if (NULL == p->cvlist) return;
+ /* (init i to 0 if global context; to 1 to skip empty global context) */
+ for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
+ 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: /* url.redirect */
+ if (cpv->vtype == T_CONFIG_LOCAL)
+ pcre_keyvalue_buffer_free(cpv->v.v);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}
+
FREE_FUNC(mod_redirect_free) {
- plugin_data *p = p_d;
- 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;
- pcre_keyvalue_buffer_free(s->redirect);
- free(s);
- }
- free(p->config_storage);
- }
-
- free(p);
- return HANDLER_GO_ON;
+ plugin_data *p = p_d;
+ if (!p) return HANDLER_GO_ON;
+ UNUSED(srv);
+
+ mod_redirect_free_config(p);
+
+ free(p->cvlist);
+ free(p);
+
+ return HANDLER_GO_ON;
}
-SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
- plugin_data *p = p_d;
- size_t i = 0;
-
- config_values_t cv[] = {
- { "url.redirect", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
- { "url.redirect-code", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
- { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
- };
-
- if (!p) return HANDLER_ERROR;
-
- /* 0 */
- 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;
- size_t j;
- const data_unset *du;
- const data_array *da;
-
- s = calloc(1, sizeof(plugin_config));
- s->redirect = pcre_keyvalue_buffer_init();
- s->redirect_code = 301;
-
- cv[0].destination = s->redirect;
- cv[1].destination = &(s->redirect_code);
-
- 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 (s->redirect_code < 100 || s->redirect_code >= 1000) s->redirect_code = 301;
-
- if (NULL == (du = array_get_element_klen(config->value, CONST_STR_LEN("url.redirect")))) {
- /* no url.redirect defined */
- continue;
- }
-
- da = (const data_array *)du;
-
- if (du->type != TYPE_ARRAY || !array_is_kvstring(&da->value)) {
- log_error_write(srv, __FILE__, __LINE__, "s",
- "unexpected value for url.redirect; expected list of \"regex\" => \"redirect\"");
- return HANDLER_ERROR;
- }
-
- for (j = 0; j < da->value.used; j++) {
- data_string *ds = (data_string *)da->value.data[j];
- if (srv->srvconf.http_url_normalize) {
- pcre_keyvalue_burl_normalize_key(&ds->key, srv->tmp_buf);
- pcre_keyvalue_burl_normalize_value(&ds->value, srv->tmp_buf);
- }
- if (0 != pcre_keyvalue_buffer_append(srv, s->redirect, &ds->key, &ds->value)) {
- log_error_write(srv, __FILE__, __LINE__, "sb",
- "pcre-compile failed for", &ds->key);
- return HANDLER_ERROR;
- }
- }
- }
-
- return HANDLER_GO_ON;
+static void mod_redirect_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv, const int idx) {
+ switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
+ case 0: /* url.redirect */
+ if (cpv->vtype == T_CONFIG_LOCAL) {
+ pconf->redirect = cpv->v.v;
+ pconf->context_ndx = idx;
+ }
+ break;
+ case 1: /* url.redirect-code */
+ pconf->redirect_code = cpv->v.shrt;
+ break;
+ default:/* should not happen */
+ return;
+ }
}
-static int mod_redirect_patch_connection(server *srv, connection *con, plugin_data *p) {
- size_t i, j;
- plugin_config *s = p->config_storage[0];
+static void mod_redirect_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv, const int idx) {
+ do {
+ mod_redirect_merge_config_cpv(pconf, cpv, idx);
+ } while ((++cpv)->k_id != -1);
+}
- p->conf.redirect = s->redirect;
- p->conf.redirect_code = s->redirect_code;
- p->conf.context_ndx = 0;
+static void mod_redirect_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_redirect_merge_config(&p->conf,
+ p->cvlist + p->cvlist[i].v.u2[0], i);
+ }
+}
- /* 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 */
+static pcre_keyvalue_buffer * mod_redirect_parse_list(server *srv, const array *a) {
+ pcre_keyvalue_buffer * const redirect = pcre_keyvalue_buffer_init();
+ for (uint32_t j = 0; j < a->used; j++) {
+ data_string *ds = (data_string *)a->data[j];
+ if (srv->srvconf.http_url_normalize) {
+ pcre_keyvalue_burl_normalize_key(&ds->key, srv->tmp_buf);
+ pcre_keyvalue_burl_normalize_value(&ds->value, srv->tmp_buf);
+ }
+ if (0 != pcre_keyvalue_buffer_append(srv,redirect,&ds->key,&ds->value)){
+ log_error(srv->errh, __FILE__, __LINE__,
+ "pcre-compile failed for %s", ds->key.ptr);
+ pcre_keyvalue_buffer_free(redirect);
+ return NULL;
+ }
+ }
+ return redirect;
+}
- data_config *dc = (data_config *)srv->config_context->data[i];
- s = p->config_storage[i];
+SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
+ static const config_plugin_keys_t cpk[] = {
+ { CONST_STR_LEN("url.redirect"),
+ T_CONFIG_ARRAY,
+ T_CONFIG_SCOPE_CONNECTION }
+ ,{ CONST_STR_LEN("url.redirect-code"),
+ 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_redirect"))
+ 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) {
+ 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: /* url.redirect */
+ if (!array_is_kvstring(cpv->v.a)) {
+ log_error(srv->errh, __FILE__, __LINE__,
+ "unexpected value for %s; "
+ "expected list of \"regex\" => \"redirect\"",
+ cpk[cpv->k_id].k);
+ return HANDLER_ERROR;
+ }
+ cpv->v.v = mod_redirect_parse_list(srv, cpv->v.a);
+ if (NULL == cpv->v.v) return HANDLER_ERROR;
+ cpv->vtype = T_CONFIG_LOCAL;
+ break;
+ case 1: /* url.redirect-code */
+ if (cpv->v.shrt < 100 || cpv->v.shrt >= 1000) cpv->v.shrt = 301;
+ break;
+ default:/* should not happen */
+ break;
+ }
+ }
+ }
- /* merge config */
- for (j = 0; j < dc->value->used; j++) {
- data_unset *du = dc->value->data[j];
+ p->defaults.redirect_code = 301;
- if (0 == strcmp(du->key.ptr, "url.redirect")) {
- p->conf.redirect = s->redirect;
- p->conf.context_ndx = i;
- } else if (0 == strcmp(du->key.ptr, "url.redirect-code")) {
- p->conf.redirect_code = s->redirect_code;
- }
- }
- }
+ /* 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_redirect_merge_config(&p->defaults, cpv, 0);
+ }
- return 0;
+ return HANDLER_GO_ON;
}
URIHANDLER_FUNC(mod_redirect_uri_handler) {
@@ -151,7 +170,7 @@ URIHANDLER_FUNC(mod_redirect_uri_handler) {
pcre_keyvalue_ctx ctx;
handler_t rc;
- mod_redirect_patch_connection(srv, con, p);
+ mod_redirect_patch_config(con, p);
if (!p->conf.redirect->used) return HANDLER_GO_ON;
ctx.cache = p->conf.context_ndx
? &con->cond_cache[p->conf.context_ndx]