[multiple] code reuse: using array_*() funcs
This commit is contained in:
parent
2b40854ab9
commit
810109cc34
21
src/array.c
21
src/array.c
|
@ -578,30 +578,15 @@ int array_print(array *a, int depth) {
|
|||
#ifdef DEBUG_ARRAY
|
||||
int main (int argc, char **argv) {
|
||||
array *a;
|
||||
data_string *ds;
|
||||
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
|
||||
a = array_init();
|
||||
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key, CONST_STR_LEN("abc"));
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("alfrag"));
|
||||
|
||||
array_insert_unique(a, (data_unset *)ds);
|
||||
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key, CONST_STR_LEN("abc"));
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("hameplman"));
|
||||
|
||||
array_insert_unique(a, (data_unset *)ds);
|
||||
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key, CONST_STR_LEN("123"));
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("alfrag"));
|
||||
|
||||
array_insert_unique(a, (data_unset *)ds);
|
||||
array_insert_key_value(a, CONST_STR_LEN("abc"), CONST_STR_LEN("alfrag"));
|
||||
array_insert_key_value(a, CONST_STR_LEN("abc"), CONST_STR_LEN("hameplman"));
|
||||
array_insert_key_value(a, CONST_STR_LEN("123"), CONST_STR_LEN("alfrag"));
|
||||
|
||||
array_print(a, 0);
|
||||
|
||||
|
|
|
@ -192,21 +192,12 @@ int config_insert_values_global(server *srv, array *ca, const config_values_t cv
|
|||
data_unset *du;
|
||||
|
||||
for (i = 0; cv[i].key; i++) {
|
||||
data_string *touched;
|
||||
|
||||
if (NULL == (du = array_get_element_klen(ca, cv[i].key, strlen(cv[i].key)))) {
|
||||
/* no found */
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* touched */
|
||||
touched = data_string_init();
|
||||
|
||||
buffer_copy_string_len(touched->value, CONST_STR_LEN(""));
|
||||
buffer_copy_buffer(touched->key, du->key);
|
||||
|
||||
array_insert_unique(srv->config_touched, (data_unset *)touched);
|
||||
array_set_key_value(srv->config_touched, CONST_BUF_LEN(du->key), CONST_STR_LEN(""));
|
||||
}
|
||||
|
||||
return config_insert_values_internal(srv, ca, cv, scope);
|
||||
|
|
|
@ -25,18 +25,16 @@
|
|||
|
||||
|
||||
#if defined(HAVE_MYSQL) || (defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER))
|
||||
static void config_warn_authn_module (server *srv, const char *module) {
|
||||
size_t len = strlen(module);
|
||||
static void config_warn_authn_module (server *srv, const char *module, size_t len) {
|
||||
for (size_t i = 0; i < srv->config_context->used; ++i) {
|
||||
const data_config *config = (data_config const*)srv->config_context->data[i];
|
||||
const data_unset *du = array_get_element(config->value, "auth.backend");
|
||||
if (NULL != du && du->type == TYPE_STRING) {
|
||||
data_string *ds = (data_string *)du;
|
||||
if (buffer_is_equal_string(ds->value, module, len)) {
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_"));
|
||||
buffer_append_string(ds->value, module);
|
||||
array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
|
||||
buffer_copy_string_len(srv->tmp_buf, CONST_STR_LEN("mod_authn_"));
|
||||
buffer_append_string_len(srv->tmp_buf, module, len);
|
||||
array_insert_value(srv->srvconf.modules, CONST_BUF_LEN(srv->tmp_buf));
|
||||
log_error_write(srv, __FILE__, __LINE__, "SSSsSSS", "Warning: please add \"mod_authn_", module, "\" to server.modules list in lighttpd.conf. A future release of lighttpd 1.4.x will not automatically load mod_authn_", module, "and lighttpd will fail to start up since your lighttpd.conf uses auth.backend = \"", module, "\".");
|
||||
return;
|
||||
}
|
||||
|
@ -53,9 +51,7 @@ static void config_warn_openssl_module (server *srv) {
|
|||
data_unset *du = config->value->data[j];
|
||||
if (0 == strncmp(du->key->ptr, "ssl.", sizeof("ssl.")-1)) {
|
||||
/* mod_openssl should be loaded after mod_extforward */
|
||||
data_string *ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_openssl"));
|
||||
array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
|
||||
array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_openssl"));
|
||||
log_error_write(srv, __FILE__, __LINE__, "S", "Warning: please add \"mod_openssl\" to server.modules list in lighttpd.conf. A future release of lighttpd 1.4.x *will not* automatically load mod_openssl and lighttpd *will not* use SSL/TLS where your lighttpd.conf contains ssl.* directives");
|
||||
return;
|
||||
}
|
||||
|
@ -607,14 +603,11 @@ static int config_insert(server *srv) {
|
|||
if (prepend_mod_indexfile) {
|
||||
/* mod_indexfile has to be loaded before mod_fastcgi and friends */
|
||||
array *modules = array_init();
|
||||
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
|
||||
array_insert_unique(modules, (data_unset *)ds);
|
||||
array_insert_value(modules, CONST_STR_LEN("mod_indexfile"));
|
||||
|
||||
for (i = 0; i < srv->srvconf.modules->used; i++) {
|
||||
data_unset *du = srv->srvconf.modules->data[i];
|
||||
array_insert_unique(modules, du->copy(du));
|
||||
ds = (data_string *)srv->srvconf.modules->data[i];
|
||||
array_insert_value(modules, CONST_BUF_LEN(ds->value));
|
||||
}
|
||||
|
||||
array_free(srv->srvconf.modules);
|
||||
|
@ -623,15 +616,11 @@ static int config_insert(server *srv) {
|
|||
|
||||
/* append default modules */
|
||||
if (append_mod_dirlisting) {
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
|
||||
array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
|
||||
array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_dirlisting"));
|
||||
}
|
||||
|
||||
if (append_mod_staticfile) {
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
|
||||
array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
|
||||
array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_staticfile"));
|
||||
}
|
||||
|
||||
if (append_mod_openssl) {
|
||||
|
@ -645,18 +634,16 @@ static int config_insert(server *srv) {
|
|||
* existing lighttpd 1.4.x configs */
|
||||
if (contains_mod_auth) {
|
||||
if (append_mod_authn_file) {
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_file"));
|
||||
array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
|
||||
array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_authn_file"));
|
||||
}
|
||||
if (append_mod_authn_ldap) {
|
||||
#if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
|
||||
config_warn_authn_module(srv, "ldap");
|
||||
config_warn_authn_module(srv, CONST_STR_LEN("ldap"));
|
||||
#endif
|
||||
}
|
||||
if (append_mod_authn_mysql) {
|
||||
#if defined(HAVE_MYSQL)
|
||||
config_warn_authn_module(srv, "mysql");
|
||||
config_warn_authn_module(srv, CONST_STR_LEN("mysql"));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -1513,7 +1500,7 @@ int config_read(server *srv, const char *fn) {
|
|||
config_t context;
|
||||
data_config *dc;
|
||||
data_integer *dpid;
|
||||
data_string *dcwd;
|
||||
buffer *dcwd;
|
||||
int ret;
|
||||
char *pos;
|
||||
buffer *filename;
|
||||
|
@ -1544,14 +1531,11 @@ int config_read(server *srv, const char *fn) {
|
|||
buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
|
||||
array_insert_unique(dc->value, (data_unset *)dpid);
|
||||
|
||||
dcwd = data_string_init();
|
||||
buffer_string_prepare_copy(dcwd->value, 1023);
|
||||
if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
|
||||
buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
|
||||
buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
|
||||
array_insert_unique(dc->value, (data_unset *)dcwd);
|
||||
} else {
|
||||
dcwd->free((data_unset*) dcwd);
|
||||
dcwd = srv->tmp_buf;
|
||||
buffer_string_prepare_copy(dcwd, 4095);
|
||||
if (NULL != getcwd(dcwd->ptr, 4095)) {
|
||||
buffer_commit(dcwd, strlen(dcwd->ptr));
|
||||
array_set_key_value(dc->value, CONST_STR_LEN("var.CWD"), CONST_BUF_LEN(dcwd));
|
||||
}
|
||||
|
||||
filename = buffer_init_string(fn);
|
||||
|
@ -1596,11 +1580,9 @@ int config_set_defaults(server *srv) {
|
|||
}
|
||||
|
||||
if (!srv->srvconf.upload_tempdirs->used) {
|
||||
data_string *ds = data_string_init();
|
||||
const char *tmpdir = getenv("TMPDIR");
|
||||
if (NULL == tmpdir) tmpdir = "/var/tmp";
|
||||
buffer_copy_string(ds->value, tmpdir);
|
||||
array_insert_unique(srv->srvconf.upload_tempdirs, (data_unset *)ds);
|
||||
array_insert_value(srv->srvconf.upload_tempdirs, tmpdir, strlen(tmpdir));
|
||||
}
|
||||
|
||||
if (srv->srvconf.upload_tempdirs->used) {
|
||||
|
|
|
@ -146,15 +146,13 @@ static int mod_auth_require_parse (server *srv, http_auth_require_t * const requ
|
|||
switch ((int)(eq - str)) {
|
||||
case 4:
|
||||
if (0 == memcmp(str, CONST_STR_LEN("user"))) {
|
||||
data_string *ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,str+5,len-5); /*("user=" is 5)*/
|
||||
array_insert_unique(require->user, (data_unset *)ds);
|
||||
/*("user=" is 5)*/
|
||||
array_set_key_value(require->user, str+5, len-5, CONST_STR_LEN(""));
|
||||
continue;
|
||||
}
|
||||
else if (0 == memcmp(str, CONST_STR_LEN("host"))) {
|
||||
data_string *ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,str+5,len-5); /*("host=" is 5)*/
|
||||
array_insert_unique(require->host, (data_unset *)ds);
|
||||
/*("host=" is 5)*/
|
||||
array_set_key_value(require->host, str+5, len-5, CONST_STR_LEN(""));
|
||||
log_error_write(srv, __FILE__, __LINE__, "ssb",
|
||||
"warning parsing auth.require 'require' field: 'host' not implemented;",
|
||||
"field value:", b);
|
||||
|
@ -163,9 +161,8 @@ static int mod_auth_require_parse (server *srv, http_auth_require_t * const requ
|
|||
break; /* to error */
|
||||
case 5:
|
||||
if (0 == memcmp(str, CONST_STR_LEN("group"))) {
|
||||
data_string *ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,str+6,len-6); /*("group=" is 6)*/
|
||||
array_insert_unique(require->group, (data_unset *)ds);
|
||||
/*("group=" is 6)*/
|
||||
array_set_key_value(require->group, str+6, len-6, CONST_STR_LEN(""));
|
||||
#if 0/*(supported by mod_authn_ldap, but not all other backends)*/
|
||||
log_error_write(srv, __FILE__, __LINE__, "ssb",
|
||||
"warning parsing auth.require 'require' field: 'group' not implemented;",
|
||||
|
|
|
@ -191,11 +191,10 @@ SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
|
|||
* - ...
|
||||
*/
|
||||
|
||||
static int mod_evhost_parse_host(connection *con,array *host) {
|
||||
static int mod_evhost_parse_host(connection *con, array *host, buffer *key) {
|
||||
register char *ptr = con->uri.authority->ptr + buffer_string_length(con->uri.authority);
|
||||
char *colon = ptr; /* needed to filter out the colon (if exists) */
|
||||
int first = 1;
|
||||
data_string *ds;
|
||||
int i;
|
||||
|
||||
/* first, find the domain + tld */
|
||||
|
@ -209,14 +208,9 @@ static int mod_evhost_parse_host(connection *con,array *host) {
|
|||
}
|
||||
}
|
||||
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,CONST_STR_LEN("%0"));
|
||||
|
||||
/* if we stopped at a dot, skip the dot */
|
||||
if (*ptr == '.') ptr++;
|
||||
buffer_copy_string_len(ds->value, ptr, colon-ptr);
|
||||
|
||||
array_insert_unique(host,(data_unset *)ds);
|
||||
array_insert_key_value(host, CONST_STR_LEN("%0"), ptr, colon-ptr);
|
||||
|
||||
/* if the : is not the start of the authority, go on parsing the hostname */
|
||||
|
||||
|
@ -225,12 +219,9 @@ static int mod_evhost_parse_host(connection *con,array *host) {
|
|||
if(*ptr == '.') {
|
||||
if (ptr != colon - 1) {
|
||||
/* is something between the dots */
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
|
||||
buffer_append_int(ds->key, i++);
|
||||
buffer_copy_string_len(ds->value,ptr+1,colon-ptr-1);
|
||||
|
||||
array_insert_unique(host,(data_unset *)ds);
|
||||
buffer_copy_string_len(key, CONST_STR_LEN("%"));
|
||||
buffer_append_int(key, i++);
|
||||
array_insert_key_value(host, CONST_BUF_LEN(key), ptr+1, colon-ptr-1);
|
||||
}
|
||||
colon = ptr;
|
||||
}
|
||||
|
@ -238,12 +229,9 @@ static int mod_evhost_parse_host(connection *con,array *host) {
|
|||
|
||||
/* if the . is not the first charactor of the hostname */
|
||||
if (colon != ptr) {
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
|
||||
buffer_append_int(ds->key, i /* ++ */);
|
||||
buffer_copy_string_len(ds->value,ptr,colon-ptr);
|
||||
|
||||
array_insert_unique(host,(data_unset *)ds);
|
||||
buffer_copy_string_len(key, CONST_STR_LEN("%"));
|
||||
buffer_append_int(key, i /* ++ */);
|
||||
array_insert_key_value(host, CONST_BUF_LEN(key), ptr, colon-ptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -303,7 +291,7 @@ static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d)
|
|||
|
||||
parsed_host = array_init();
|
||||
|
||||
mod_evhost_parse_host(con, parsed_host);
|
||||
mod_evhost_parse_host(con, parsed_host, p->tmp_buf);
|
||||
|
||||
/* build document-root */
|
||||
buffer_reset(p->tmp_buf);
|
||||
|
|
|
@ -279,13 +279,8 @@ SETDEFAULTS_FUNC(mod_extforward_set_defaults) {
|
|||
|
||||
/* default to "X-Forwarded-For" or "Forwarded-For" if extforward.headers not specified or empty */
|
||||
if (!s->hap_PROXY && 0 == s->headers->used && (0 == i || NULL != array_get_element(config->value, "extforward.headers"))) {
|
||||
data_string *ds;
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("X-Forwarded-For"));
|
||||
array_insert_unique(s->headers, (data_unset *)ds);
|
||||
ds = data_string_init();
|
||||
buffer_copy_string_len(ds->value, CONST_STR_LEN("Forwarded-For"));
|
||||
array_insert_unique(s->headers, (data_unset *)ds);
|
||||
array_insert_value(s->headers, CONST_STR_LEN("X-Forwarded-For"));
|
||||
array_insert_value(s->headers, CONST_STR_LEN("Forwarded-For"));
|
||||
}
|
||||
|
||||
if (!array_is_kvany(s->opts_params)) {
|
||||
|
@ -423,15 +418,6 @@ static int mod_extforward_patch_connection(server *srv, connection *con, plugin_
|
|||
#undef PATCH
|
||||
|
||||
|
||||
static void put_string_into_array_len(array *ary, const char *str, int len)
|
||||
{
|
||||
data_string *tempdata;
|
||||
if (len == 0)
|
||||
return;
|
||||
tempdata = data_string_init();
|
||||
buffer_copy_string_len(tempdata->value,str,len);
|
||||
array_insert_unique(ary,(data_unset *)tempdata);
|
||||
}
|
||||
/*
|
||||
extract a forward array from the environment
|
||||
*/
|
||||
|
@ -446,7 +432,7 @@ static array *extract_forward_array(buffer *pbuffer)
|
|||
if (in_str) {
|
||||
if ((*curr > '9' || *curr < '0') && *curr != '.' && *curr != ':' && (*curr < 'a' || *curr > 'f') && (*curr < 'A' || *curr > 'F')) {
|
||||
/* found an separator , insert value into result array */
|
||||
put_string_into_array_len(result, base, curr - base);
|
||||
array_insert_value(result, base, curr - base);
|
||||
/* change state to not in string */
|
||||
in_str = 0;
|
||||
}
|
||||
|
@ -460,7 +446,7 @@ static array *extract_forward_array(buffer *pbuffer)
|
|||
}
|
||||
/* if breaking out while in str, we got to the end of string, so add it */
|
||||
if (in_str) {
|
||||
put_string_into_array_len(result, base, curr - base);
|
||||
array_insert_value(result, base, curr - base);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
Loading…
Reference in New Issue