From 64ba5fddce602b0ffe54149599c6bfd918b50621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sun, 24 Jan 2010 14:00:29 +0100 Subject: [PATCH] Cleanup value pointer extractions --- include/lighttpd/options.h | 4 ---- include/lighttpd/plugin.h | 2 +- include/lighttpd/value.h | 9 +++++++ src/main/config_parser.rl | 12 +++++----- src/main/options.c | 13 ---------- src/main/plugin_core.c | 40 +++++++++++++++---------------- src/main/value.c | 30 +++++++++++++++++++++++ src/modules/mod_auth.c | 2 +- src/modules/mod_cache_disk_etag.c | 2 +- src/modules/mod_lua.c | 4 ++-- src/modules/mod_vhost.c | 6 ++--- 11 files changed, 73 insertions(+), 51 deletions(-) diff --git a/include/lighttpd/options.h b/include/lighttpd/options.h index 67e3ae6..73e5466 100644 --- a/include/lighttpd/options.h +++ b/include/lighttpd/options.h @@ -27,8 +27,4 @@ struct liOptionSet { /* Extract content from value, value set to none */ LI_API liOptionValue li_value_extract(liValue *val); -LI_API gpointer li_value_extract_ptr(liValue *val); -LI_API gint64 li_value_extract_number(liValue *val); -LI_API gboolean li_value_extract_bool(liValue *val); - #endif diff --git a/include/lighttpd/plugin.h b/include/lighttpd/plugin.h index b28ff11..cd6e98c 100644 --- a/include/lighttpd/plugin.h +++ b/include/lighttpd/plugin.h @@ -84,7 +84,7 @@ struct liServerOption { liPlugin *p; /** the value is freed with li_value_free after the parse call, so you - * probably want to extract the content via li_value_extract* + * probably want to extract pointers via li_value_extract_* * val is zero to get the global default value if nothing is specified * save result in value * diff --git a/include/lighttpd/value.h b/include/lighttpd/value.h index b8954bc..f891fe2 100644 --- a/include/lighttpd/value.h +++ b/include/lighttpd/value.h @@ -44,4 +44,13 @@ LI_API GString *li_value_to_string(liValue *val); LI_API void li_value_list_free(GArray *vallist); +/* extracts the pointer of a; set val->type to none (so a free on the value doesn't free the previous content) + * returns NULL (and doesn't modify val->type) if the type doesn't match + */ +LI_API GString* li_value_extract_string(liValue *val); +LI_API GArray* li_value_extract_list(liValue *val); +LI_API GHashTable* li_value_extract_hash(liValue *val); +LI_API liAction* li_value_extract_action(liValue *val); +LI_API liCondition* li_value_extract_condition(liValue *val); + #endif diff --git a/src/main/config_parser.rl b/src/main/config_parser.rl index e13fdf6..452db3d 100644 --- a/src/main/config_parser.rl +++ b/src/main/config_parser.rl @@ -538,7 +538,7 @@ /* assignment vor user defined variable, insert into hashtable */ gpointer old_key; gpointer old_val; - GString *str = li_value_extract(name).string; + GString *str = li_value_extract_string(name); /* free old key and value if we are overwriting it */ if (g_hash_table_lookup_extended(ctx->uservars, str, &old_key, &old_val)) { @@ -864,14 +864,14 @@ WARNING(srv, "%s", "header conditional needs a key"); return FALSE; } - lvalue = li_condition_lvalue_new(LI_COMP_REQUEST_HEADER, li_value_extract(k).string); + lvalue = li_condition_lvalue_new(LI_COMP_REQUEST_HEADER, li_value_extract_string(k)); } else if (g_str_equal(str, "environment") || g_str_equal(str, "env")) { if (k == NULL) { WARNING(srv, "%s", "environment conditional needs a key"); return FALSE; } - lvalue = li_condition_lvalue_new(LI_COMP_ENVIRONMENT, li_value_extract(k).string); + lvalue = li_condition_lvalue_new(LI_COMP_ENVIRONMENT, li_value_extract_string(k)); } else { WARNING(srv, "unkown lvalue for condition: %s", n->data.string->str); @@ -922,7 +922,7 @@ WARNING(srv, "%s", "header conditional needs a key"); return FALSE; } - lvalue = li_condition_lvalue_new(LI_COMP_RESPONSE_HEADER, li_value_extract(k).string); + lvalue = li_condition_lvalue_new(LI_COMP_RESPONSE_HEADER, li_value_extract_string(k)); } else { WARNING(srv, "unkown lvalue for condition: %s", n->data.string->str); @@ -936,10 +936,10 @@ if (ctx->condition_nonbool) { if (v->type == LI_VALUE_STRING) { - cond = li_condition_new_string(srv, ctx->op, lvalue, li_value_extract(v).string); + cond = li_condition_new_string(srv, ctx->op, lvalue, li_value_extract_string(v)); } else if (v->type == LI_VALUE_NUMBER) - cond = li_condition_new_int(srv, ctx->op, lvalue, li_value_extract_number(v)); + cond = li_condition_new_int(srv, ctx->op, lvalue, v->data.number); else { cond = NULL; } diff --git a/src/main/options.c b/src/main/options.c index 25ad7fa..7ff8e3f 100644 --- a/src/main/options.c +++ b/src/main/options.c @@ -34,16 +34,3 @@ liOptionValue li_value_extract(liValue *val) { val->type = LI_VALUE_NONE; return oval; } - -gpointer li_value_extract_ptr(liValue *val) { - liOptionValue oval = li_value_extract(val); - return oval.ptr; -} -gint64 li_value_extract_number(liValue *val) { - liOptionValue oval = li_value_extract(val); - return oval.number; -} -gboolean li_value_extract_bool(liValue *val) { - liOptionValue oval = li_value_extract(val); - return oval.boolean; -} diff --git a/src/main/plugin_core.c b/src/main/plugin_core.c index b37bf8d..5adfe5f 100644 --- a/src/main/plugin_core.c +++ b/src/main/plugin_core.c @@ -188,7 +188,7 @@ static liAction* core_docroot(liServer *srv, liPlugin* p, liValue *val, gpointer return NULL; } - return li_action_new_function(core_handle_docroot, NULL, core_docroot_free, li_value_extract(val).string); + return li_action_new_function(core_handle_docroot, NULL, core_docroot_free, li_value_extract_string(val)); } typedef struct { @@ -264,8 +264,8 @@ static liAction* core_alias(liServer *srv, liPlugin* p, liValue *val, gpointer u vl = val->data.list; if (vl->len == 2 && g_array_index(vl, liValue*, 0)->type == LI_VALUE_STRING && g_array_index(vl, liValue*, 1)->type == LI_VALUE_STRING) { a = g_array_sized_new(FALSE, TRUE, sizeof(core_alias_config), 1); - ac.prefix = li_value_extract_ptr(g_array_index(vl, liValue*, 0)); - ac.path = li_value_extract_ptr(g_array_index(vl, liValue*, 1)); + ac.prefix = li_value_extract_string(g_array_index(vl, liValue*, 0)); + ac.path = li_value_extract_string(g_array_index(vl, liValue*, 1)); g_array_append_val(a, ac); } else { guint i; @@ -277,8 +277,8 @@ static liAction* core_alias(liServer *srv, liPlugin* p, liValue *val, gpointer u } vl1 = g_array_index(vl, liValue*, i)->data.list; if (g_array_index(vl1, liValue*, 0)->type == LI_VALUE_STRING && g_array_index(vl1, liValue*, 1)->type == LI_VALUE_STRING) { - ac.prefix = li_value_extract_ptr(g_array_index(vl1, liValue*, 0)); - ac.path = li_value_extract_ptr(g_array_index(vl1, liValue*, 1)); + ac.prefix = li_value_extract_string(g_array_index(vl1, liValue*, 0)); + ac.path = li_value_extract_string(g_array_index(vl1, liValue*, 1)); g_array_append_val(a, ac); } else { ERROR(srv, "%s", "unexpected entry in parameter for alias action"); @@ -411,7 +411,7 @@ static liAction* core_index(liServer *srv, liPlugin* p, liValue *val, gpointer u } } - return li_action_new_function(core_handle_index, NULL, core_index_free, li_value_extract(val).list); + return li_action_new_function(core_handle_index, NULL, core_index_free, li_value_extract_list(val)); } @@ -687,7 +687,7 @@ static liAction* core_status(liServer *srv, liPlugin* p, liValue *val, gpointer return NULL; } - ptr = GINT_TO_POINTER((gint) li_value_extract(val).number); + ptr = GINT_TO_POINTER((gint) val->data.number); return li_action_new_function(core_handle_status, NULL, NULL, ptr); } @@ -715,7 +715,7 @@ static liAction* core_log_write(liServer *srv, liPlugin* p, liValue *val, gpoint return NULL; } - return li_action_new_function(core_handle_log_write, NULL, core_log_write_free, li_value_extract(val).string); + return li_action_new_function(core_handle_log_write, NULL, core_log_write_free, li_value_extract_string(val)); } @@ -809,7 +809,7 @@ static gboolean core_module_load(liServer *srv, liPlugin* p, liValue *val, gpoin if (val->type == LI_VALUE_STRING) { /* load only one module */ - liValue *name = li_value_new_string(li_value_extract(val).string); + liValue *name = li_value_new_string(li_value_extract_string(val)); g_array_append_val(mods->data.list, name); } else if (val->type == LI_VALUE_LIST) { /* load a list of modules */ @@ -822,7 +822,7 @@ static gboolean core_module_load(liServer *srv, liPlugin* p, liValue *val, gpoin } } g_array_free(mods->data.list, TRUE); - mods->data.list = li_value_extract(val).list; + mods->data.list = li_value_extract_ist(val); } else { ERROR(srv, "module_load takes either a string or a list of strings as parameter, %s given", li_value_type_string(val->type)); return FALSE; @@ -858,7 +858,7 @@ static gboolean core_io_timeout(liServer *srv, liPlugin* p, liValue *val, gpoint return FALSE; } - srv->io_timeout = li_value_extract(val).number; + srv->io_timeout = val->data.number; return TRUE; } @@ -871,7 +871,7 @@ static gboolean core_stat_cache_ttl(liServer *srv, liPlugin* p, liValue *val, gp return FALSE; } - srv->stat_cache_ttl = (gdouble)li_value_extract(val).number; + srv->stat_cache_ttl = (gdouble)val->data.number; return TRUE; } @@ -956,7 +956,7 @@ static gboolean core_option_log_timestamp_parse(liServer *srv, liPlugin *p, size UNUSED(ndx); if (!val) return TRUE; - oval->ptr = li_log_timestamp_new(srv, li_value_extract(val).string); + oval->ptr = li_log_timestamp_new(srv, li_value_extract_string(val)); return TRUE; } @@ -987,7 +987,7 @@ static gboolean core_option_static_exclude_exts_parse(liServer *srv, liPlugin *p } /* everything ok */ - oval->list = li_value_extract(val).list; + oval->list = li_value_extract_list(val); return TRUE; } @@ -1030,7 +1030,7 @@ static gboolean core_option_mime_types_parse(liServer *srv, liPlugin *p, size_t } /* everything ok */ - oval->list = li_value_extract(val).list; + oval->list = li_value_extract_list(val); return TRUE; } @@ -1125,7 +1125,7 @@ static liAction* core_header_add(liServer *srv, liPlugin* p, liValue *val, gpoin return NULL; } - return li_action_new_function(core_handle_header_add, NULL, core_header_free, li_value_extract(val).list); + return li_action_new_function(core_handle_header_add, NULL, core_header_free, li_value_extract_list(val)); } @@ -1162,7 +1162,7 @@ static liAction* core_header_append(liServer *srv, liPlugin* p, liValue *val, gp return NULL; } - return li_action_new_function(core_handle_header_append, NULL, core_header_free, li_value_extract(val).list); + return li_action_new_function(core_handle_header_append, NULL, core_header_free, li_value_extract_list(val)); } @@ -1199,7 +1199,7 @@ static liAction* core_header_overwrite(liServer *srv, liPlugin* p, liValue *val, return NULL; } - return li_action_new_function(core_handle_header_overwrite, NULL, core_header_free, li_value_extract(val).list); + return li_action_new_function(core_handle_header_overwrite, NULL, core_header_free, li_value_extract_list(val)); } static void core_header_remove_free(liServer *srv, gpointer param) { @@ -1225,7 +1225,7 @@ static liAction* core_header_remove(liServer *srv, liPlugin* p, liValue *val, gp return NULL; } - return li_action_new_function(core_handle_header_remove, NULL, core_header_remove_free, li_value_extract(val).string); + return li_action_new_function(core_handle_header_remove, NULL, core_header_remove_free, li_value_extract_string(val)); } /* chunkqueue memory limits */ @@ -1386,7 +1386,7 @@ static liAction* core_throttle_pool(liServer *srv, liPlugin* p, liValue *val, gp return NULL; } - pool = li_throttle_pool_new(srv, li_value_extract(g_array_index(val->data.list, liValue*, 0)).string, (guint)rate); + pool = li_throttle_pool_new(srv, li_value_extract_string(g_array_index(val->data.list, liValue*, 0)), (guint)rate); g_array_append_val(srv->throttle_pools, pool); } diff --git a/src/main/value.c b/src/main/value.c index 1625572..2f59af7 100644 --- a/src/main/value.c +++ b/src/main/value.c @@ -235,3 +235,33 @@ GString *li_value_to_string(liValue *val) { return str; } + +GString* li_value_extract_string(liValue *val) { + if (val->type != LI_VALUE_STRING) return NULL; + val->type = LI_VALUE_NONE; + return val->data.string; +} + +GArray* li_value_extract_list(liValue *val) { + if (val->type != LI_VALUE_LIST) return NULL; + val->type = LI_VALUE_NONE; + return val->data.list; +} + +GHashTable* li_value_extract_hash(liValue *val) { + if (val->type != LI_VALUE_HASH) return NULL; + val->type = LI_VALUE_NONE; + return val->data.hash; +} + +liAction* li_value_extract_action(liValue *val) { + if (val->type != LI_VALUE_ACTION) return NULL; + val->type = LI_VALUE_NONE; + return val->data.val_action.action; +} + +liCondition* li_value_extract_condition(liValue *val) { + if (val->type != LI_VALUE_CONDITION) return NULL; + val->type = LI_VALUE_NONE; + return val->data.val_cond.cond; +} diff --git a/src/modules/mod_auth.c b/src/modules/mod_auth.c index 183514c..74f6cbe 100644 --- a/src/modules/mod_auth.c +++ b/src/modules/mod_auth.c @@ -435,7 +435,7 @@ static liAction* auth_generic_create(liServer *srv, liPlugin* p, liValue *val, c bdata = g_slice_new(AuthBasicData); bdata->p = p; - bdata->realm = li_value_extract(realm).string; + bdata->realm = li_value_extract_string(realm); bdata->backend = basic_action; bdata->data = afd; diff --git a/src/modules/mod_cache_disk_etag.c b/src/modules/mod_cache_disk_etag.c index 61f4c03..a56a8bf 100644 --- a/src/modules/mod_cache_disk_etag.c +++ b/src/modules/mod_cache_disk_etag.c @@ -315,7 +315,7 @@ static liAction* cache_etag_create(liServer *srv, liPlugin* p, liValue *val, gpo } ctx = g_slice_new0(cache_etag_context); - ctx->path = li_value_extract_ptr(val); + ctx->path = li_value_extract_string(val); return li_action_new_function(cache_etag_handle, cache_etag_cleanup, cache_etag_free, ctx); } diff --git a/src/modules/mod_lua.c b/src/modules/mod_lua.c index 1324645..b2951a9 100644 --- a/src/modules/mod_lua.c +++ b/src/modules/mod_lua.c @@ -204,7 +204,7 @@ static liAction* lua_handler_create(liServer *srv, liPlugin* p, liValue *val, gp } } - conf = lua_config_new(li_value_extract_ptr(v_filename), ttl); + conf = lua_config_new(li_value_extract_string(v_filename), ttl); return li_action_new_function(lua_handle, NULL, lua_config_free, conf); @@ -533,7 +533,7 @@ static gboolean lua_plugin(liServer *srv, liPlugin *p, liValue *val, gpointer us } } - return lua_plugin_load(srv, p, li_value_extract_ptr(v_filename)); + return lua_plugin_load(srv, p, li_value_extract_string(v_filename)); } static const liPluginOption options[] = { diff --git a/src/modules/mod_vhost.c b/src/modules/mod_vhost.c index f0947b5..8133e50 100644 --- a/src/modules/mod_vhost.c +++ b/src/modules/mod_vhost.c @@ -235,7 +235,7 @@ static liAction* vhost_simple_create(liServer *srv, liPlugin* p, liValue *val, g return NULL; } - *setting = li_value_extract(v).string; + *setting = li_value_extract_string(v); } if (!sd->server_root || !sd->docroot || !sd->default_vhost) { @@ -303,7 +303,7 @@ static liAction* vhost_map_create(liServer *srv, liPlugin* p, liValue *val, gpoi md = g_slice_new0(vhost_map_data); md->plugin = p; - md->hash = li_value_extract(val).hash; + md->hash = li_value_extract_hash(val); str = g_string_new_len(CONST_STR_LEN("default")); md->default_action = g_hash_table_lookup(md->hash, str); g_string_free(str, TRUE); @@ -598,7 +598,7 @@ static liAction* vhost_pattern_create(liServer *srv, liPlugin* p, liValue *val, return NULL; } - str = li_value_extract(val).string; + str = li_value_extract_string(val); pd = g_slice_new0(vhost_pattern_data); pd->parts = g_array_sized_new(FALSE, TRUE, sizeof(vhost_pattern_part), 6);