[mod_evhost] use config_plugin_values_init()
use array of buffers to increase performance (reduce pointer chasing)
This commit is contained in:
parent
28691e6f84
commit
d1fba24469
334
src/mod_evhost.c
334
src/mod_evhost.c
|
@ -9,179 +9,215 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
typedef struct {
|
||||
/* unparsed pieces */
|
||||
buffer *path_pieces_raw;
|
||||
/**
|
||||
*
|
||||
* #
|
||||
* # define a pattern for the host url finding
|
||||
* # %% => % sign
|
||||
* # %0 => domain name + tld
|
||||
* # %1 => tld
|
||||
* # %2 => domain name without tld
|
||||
* # %3 => subdomain 1 name
|
||||
* # %4 => subdomain 2 name
|
||||
* # %_ => fqdn (without port info)
|
||||
* #
|
||||
* evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
|
||||
*
|
||||
*/
|
||||
|
||||
/* pieces for path creation */
|
||||
size_t len;
|
||||
buffer **path_pieces;
|
||||
typedef struct {
|
||||
/* pieces for path creation */
|
||||
const buffer *path_pieces;
|
||||
} plugin_config;
|
||||
|
||||
typedef struct {
|
||||
PLUGIN_DATA;
|
||||
plugin_config **config_storage;
|
||||
plugin_config conf;
|
||||
buffer *tmp_buf;
|
||||
array *split_vals;
|
||||
PLUGIN_DATA;
|
||||
plugin_config defaults;
|
||||
plugin_config conf;
|
||||
|
||||
buffer tmp_buf;
|
||||
array split_vals;
|
||||
} plugin_data;
|
||||
|
||||
INIT_FUNC(mod_evhost_init) {
|
||||
plugin_data *p;
|
||||
return calloc(1, sizeof(plugin_data));
|
||||
}
|
||||
|
||||
p = calloc(1, sizeof(*p));
|
||||
static void mod_evhost_free_path_pieces(const buffer *path_pieces) {
|
||||
buffer *b;
|
||||
*(const buffer **)&b = path_pieces;
|
||||
for (; path_pieces->ptr; ++path_pieces) free(path_pieces->ptr);
|
||||
free(b);
|
||||
}
|
||||
|
||||
p->tmp_buf = buffer_init();
|
||||
p->split_vals = array_init();
|
||||
|
||||
return p;
|
||||
static void mod_evhost_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) {
|
||||
if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
|
||||
switch (cpv->k_id) {
|
||||
case 0: /* evhost.path-pattern */
|
||||
mod_evhost_free_path_pieces(cpv->v.v);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FREE_FUNC(mod_evhost_free) {
|
||||
plugin_data *p = p_d;
|
||||
plugin_data *p = p_d;
|
||||
if (!p) return HANDLER_GO_ON;
|
||||
UNUSED(srv);
|
||||
|
||||
UNUSED(srv);
|
||||
mod_evhost_free_config(p);
|
||||
|
||||
if (!p) return HANDLER_GO_ON;
|
||||
free(p->tmp_buf.ptr);
|
||||
array_free_data(&p->split_vals);
|
||||
|
||||
if (p->config_storage) {
|
||||
size_t i;
|
||||
for (i = 0; i < srv->config_context->used; i++) {
|
||||
plugin_config *s = p->config_storage[i];
|
||||
free(p->cvlist);
|
||||
free(p);
|
||||
|
||||
if (NULL == s) continue;
|
||||
|
||||
if(s->path_pieces) {
|
||||
size_t j;
|
||||
for (j = 0; j < s->len; j++) {
|
||||
buffer_free(s->path_pieces[j]);
|
||||
}
|
||||
|
||||
free(s->path_pieces);
|
||||
}
|
||||
|
||||
buffer_free(s->path_pieces_raw);
|
||||
|
||||
free(s);
|
||||
}
|
||||
free(p->config_storage);
|
||||
}
|
||||
|
||||
buffer_free(p->tmp_buf);
|
||||
array_free(p->split_vals);
|
||||
|
||||
free(p);
|
||||
|
||||
return HANDLER_GO_ON;
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
static int mod_evhost_parse_pattern(plugin_config *s) {
|
||||
char *ptr = s->path_pieces_raw->ptr,*pos;
|
||||
__attribute_cold__
|
||||
static buffer * mod_evhost_parse_pattern_err(buffer *bptr) {
|
||||
for (; bptr->ptr; ++bptr) free(bptr->ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s->path_pieces = NULL;
|
||||
static buffer * mod_evhost_parse_pattern(const char *ptr) {
|
||||
uint32_t used = 0;
|
||||
const uint32_t sz = 127;
|
||||
const char *pos;
|
||||
buffer bptr[sz+1]; /* (128 elements takes 2k on stack in 64-bit) */
|
||||
memset(bptr, 0, sizeof(bptr));
|
||||
|
||||
for(pos=ptr;*ptr;ptr++) {
|
||||
if(*ptr == '%') {
|
||||
size_t len;
|
||||
s->path_pieces = realloc(s->path_pieces,(s->len+2) * sizeof(*s->path_pieces));
|
||||
s->path_pieces[s->len] = buffer_init();
|
||||
s->path_pieces[s->len+1] = buffer_init();
|
||||
if (used >= sz-1) /* (should not happen) */
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
|
||||
/* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */
|
||||
if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) {
|
||||
len = 2;
|
||||
} else if (ptr[1] == '{') {
|
||||
if (!light_isdigit(ptr[2])) return -1;
|
||||
if (!light_isdigit(ptr[2]))
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
if (ptr[3] == '.') {
|
||||
if (!light_isdigit(ptr[4])) return -1;
|
||||
if (ptr[5] != '}') return -1;
|
||||
if (!light_isdigit(ptr[4]))
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
if (ptr[5] != '}')
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
len = 6;
|
||||
} else if (ptr[3] == '}') {
|
||||
len = 4;
|
||||
} else {
|
||||
return -1;
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
}
|
||||
|
||||
buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
|
||||
buffer_copy_string_len(bptr+used, pos, ptr-pos);
|
||||
pos = ptr + len;
|
||||
|
||||
buffer_copy_string_len(s->path_pieces[s->len+1],ptr,len);
|
||||
buffer_copy_string_len(bptr+used+1, ptr, len);
|
||||
ptr += len - 1; /*(ptr++ in for() loop)*/
|
||||
|
||||
s->len += 2;
|
||||
used += 2;
|
||||
}
|
||||
}
|
||||
|
||||
if(*pos != '\0') {
|
||||
s->path_pieces = realloc(s->path_pieces,(s->len+1) * sizeof(*s->path_pieces));
|
||||
s->path_pieces[s->len] = buffer_init();
|
||||
|
||||
buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
|
||||
|
||||
s->len += 1;
|
||||
if (used >= sz) /* (should not happen) */
|
||||
return mod_evhost_parse_pattern_err(bptr);
|
||||
buffer_copy_string_len(bptr+used, pos, ptr-pos);
|
||||
++used;
|
||||
}
|
||||
|
||||
return 0;
|
||||
buffer * const path_pieces =
|
||||
malloc(sizeof(bptr) + ((used+1) * sizeof(buffer)));
|
||||
force_assert(path_pieces);
|
||||
return memcpy(path_pieces, bptr, (used+1) * sizeof(buffer));
|
||||
}
|
||||
|
||||
static void mod_evhost_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: /* evhost.path-pattern */
|
||||
if (cpv->vtype == T_CONFIG_LOCAL)
|
||||
pconf->path_pieces = cpv->v.v;
|
||||
break;
|
||||
default:/* should not happen */
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void mod_evhost_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
||||
do {
|
||||
mod_evhost_merge_config_cpv(pconf, cpv);
|
||||
} while ((++cpv)->k_id != -1);
|
||||
}
|
||||
|
||||
static void mod_evhost_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_evhost_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
||||
}
|
||||
}
|
||||
|
||||
SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
|
||||
plugin_data *p = p_d;
|
||||
size_t i;
|
||||
static const config_plugin_keys_t cpk[] = {
|
||||
{ CONST_STR_LEN("evhost.path-pattern"),
|
||||
T_CONFIG_STRING,
|
||||
T_CONFIG_SCOPE_CONNECTION }
|
||||
,{ NULL, 0,
|
||||
T_CONFIG_UNSET,
|
||||
T_CONFIG_SCOPE_UNSET }
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* #
|
||||
* # define a pattern for the host url finding
|
||||
* # %% => % sign
|
||||
* # %0 => domain name + tld
|
||||
* # %1 => tld
|
||||
* # %2 => domain name without tld
|
||||
* # %3 => subdomain 1 name
|
||||
* # %4 => subdomain 2 name
|
||||
* # %_ => fqdn (without port info)
|
||||
* #
|
||||
* evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
|
||||
*
|
||||
*/
|
||||
plugin_data * const p = p_d;
|
||||
if (!config_plugin_values_init(srv, p, cpk, "mod_evhost"))
|
||||
return HANDLER_ERROR;
|
||||
|
||||
config_values_t cv[] = {
|
||||
{ "evhost.path-pattern", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
|
||||
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
||||
};
|
||||
/* 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: /* evhost.path-pattern */
|
||||
if (!buffer_string_is_empty(cpv->v.b)) {
|
||||
const char * const ptr = cpv->v.b->ptr;
|
||||
cpv->v.v = mod_evhost_parse_pattern(ptr);
|
||||
if (NULL == cpv->v.v) {
|
||||
log_error(srv->errh, __FILE__, __LINE__,
|
||||
"invalid evhost.path-pattern: %s", ptr);
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
cpv->vtype = T_CONFIG_LOCAL;
|
||||
}
|
||||
break;
|
||||
default:/* should not happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!p) return HANDLER_ERROR;
|
||||
/* 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_evhost_merge_config(&p->defaults, cpv);
|
||||
}
|
||||
|
||||
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->path_pieces_raw = buffer_init();
|
||||
s->path_pieces = NULL;
|
||||
s->len = 0;
|
||||
|
||||
cv[0].destination = s->path_pieces_raw;
|
||||
|
||||
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 (!buffer_string_is_empty(s->path_pieces_raw)) {
|
||||
if (0 != mod_evhost_parse_pattern(s)) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb", "invalid evhost.path-pattern:", s->path_pieces_raw);
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return HANDLER_GO_ON;
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -252,45 +288,12 @@ static void mod_evhost_parse_host(buffer *key, array *host, buffer *authority) {
|
|||
}
|
||||
}
|
||||
|
||||
#define PATCH(x) \
|
||||
p->conf.x = s->x;
|
||||
static int mod_evhost_patch_connection(server *srv, connection *con, plugin_data *p) {
|
||||
size_t i, j;
|
||||
plugin_config *s = p->config_storage[0];
|
||||
|
||||
PATCH(path_pieces);
|
||||
PATCH(len);
|
||||
|
||||
/* 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("evhost.path-pattern"))) {
|
||||
PATCH(path_pieces);
|
||||
PATCH(len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#undef PATCH
|
||||
|
||||
|
||||
static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, buffer **path_pieces, const size_t npieces) {
|
||||
static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, const buffer *path_pieces) {
|
||||
array_reset_data_strings(parsed_host);
|
||||
mod_evhost_parse_host(b, parsed_host, authority);
|
||||
buffer_clear(b);
|
||||
|
||||
for (size_t i = 0; i < npieces; ++i) {
|
||||
const char *ptr = path_pieces[i]->ptr;
|
||||
for (const char *ptr; (ptr = path_pieces->ptr); ++path_pieces) {
|
||||
if (*ptr == '%') {
|
||||
const data_string *ds;
|
||||
|
||||
|
@ -321,13 +324,13 @@ static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer
|
|||
} else {
|
||||
/* unhandled %-sequence */
|
||||
}
|
||||
} else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, CONST_BUF_LEN(path_pieces[i])))) {
|
||||
} else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, CONST_BUF_LEN(path_pieces)))) {
|
||||
buffer_append_string_buffer(b, &ds->value);
|
||||
} else {
|
||||
/* unhandled %-sequence */
|
||||
}
|
||||
} else {
|
||||
buffer_append_string_buffer(b, path_pieces[i]);
|
||||
buffer_append_string_buffer(b, path_pieces);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -341,21 +344,20 @@ static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d)
|
|||
/* not authority set */
|
||||
if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
|
||||
|
||||
mod_evhost_patch_connection(srv, con, p);
|
||||
mod_evhost_patch_config(con, p);
|
||||
|
||||
/* missing even default(global) conf */
|
||||
if (0 == p->conf.len) {
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
if (NULL == p->conf.path_pieces) return HANDLER_GO_ON;
|
||||
|
||||
mod_evhost_build_doc_root_path(p->tmp_buf, p->split_vals, con->uri.authority, p->conf.path_pieces, p->conf.len);
|
||||
buffer * const b = &p->tmp_buf;
|
||||
mod_evhost_build_doc_root_path(b, &p->split_vals, con->uri.authority, p->conf.path_pieces);
|
||||
|
||||
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
|
||||
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, b, &sce)) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), b);
|
||||
} else if(!S_ISDIR(sce->st.st_mode)) {
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf);
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", b);
|
||||
} else {
|
||||
buffer_copy_buffer(con->physical.doc_root, p->tmp_buf);
|
||||
buffer_copy_buffer(con->physical.doc_root, b);
|
||||
}
|
||||
|
||||
return HANDLER_GO_ON;
|
||||
|
@ -372,5 +374,3 @@ int mod_evhost_plugin_init(plugin *p) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* eof */
|
||||
|
|
|
@ -2,26 +2,9 @@
|
|||
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mod_evhost.c"
|
||||
|
||||
static plugin_config * test_mod_evhost_plugin_config_init(void) {
|
||||
plugin_config *s = calloc(1, sizeof(plugin_config));
|
||||
s->path_pieces_raw = buffer_init();
|
||||
s->path_pieces = NULL;
|
||||
s->len = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
static void test_mod_evhost_plugin_config_free(plugin_config *s) {
|
||||
buffer_free(s->path_pieces_raw);
|
||||
for (size_t i = 0; i < s->len; ++i) buffer_free(s->path_pieces[i]);
|
||||
free(s->path_pieces);
|
||||
free(s);
|
||||
}
|
||||
|
||||
struct ttt {
|
||||
const char *pattern;
|
||||
size_t plen;
|
||||
|
@ -32,12 +15,11 @@ struct ttt {
|
|||
static void test_mod_evhost_build_doc_root_path_loop(struct ttt *tt, size_t nelts, buffer *authority, buffer *b, array *a) {
|
||||
for (size_t i = 0; i < nelts; ++i) {
|
||||
struct ttt *t = tt+i;
|
||||
plugin_config *s = test_mod_evhost_plugin_config_init();
|
||||
buffer_copy_string_len(s->path_pieces_raw, t->pattern, t->plen);
|
||||
assert(0 == mod_evhost_parse_pattern(s));
|
||||
mod_evhost_build_doc_root_path(b, a, authority, s->path_pieces, s->len);
|
||||
const buffer *path_pieces = mod_evhost_parse_pattern(t->pattern);
|
||||
assert(NULL != path_pieces);
|
||||
mod_evhost_build_doc_root_path(b, a, authority, path_pieces);
|
||||
assert(buffer_is_equal_string(b, t->expect, t->elen));
|
||||
test_mod_evhost_plugin_config_free(s);
|
||||
mod_evhost_free_path_pieces(path_pieces);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue