static liAction* vhost_map_create(liServer *srv, liWorker *wrk, liPlugin* p, liValue *val, gpointer userdata) { vhost_map_data *md; UNUSED(wrk); UNUSED(userdata); val = li_value_get_single_argument(val); if (NULL == (val = li_value_to_key_value_list(val))) { ERROR(srv, "%s", "vhost.map expects a hashtable/key-value list as parameter"); return NULL; } md = g_slice_new0(vhost_map_data); md->plugin = p; md->hash = li_value_new_hashtable(); LI_VALUE_FOREACH(entry, val) liValue *entryKey = li_value_list_at(entry, 0); liValue *entryValue = li_value_list_at(entry, 1); GString *entryKeyStr; if (LI_VALUE_ACTION != li_value_type(entryValue)) { ERROR(srv, "vhost.map expects a hashtable/key-value list with action values as parameter, %s value given", li_value_type_string(entryValue)); vhost_map_free(srv, md); return NULL; } /* we now own the key string: free it in case of failure */ entryKeyStr = li_value_extract_string(entryKey); if (NULL != entryKeyStr && g_str_equal(entryKeyStr->str, "default")) { WARNING(srv, "%s", "vhost.map: found entry with string key \"default\". please convert the parameter to a key-value list and use the keyword default instead."); /* TODO: remove support for "default" (LI_VALUE_HASH) */ g_string_free(entryKeyStr, TRUE); entryKeyStr = NULL; } if (NULL == entryKeyStr) { if (NULL != md->default_action) { ERROR(srv, "%s", "vhost.map: already have a default action"); /* key string is NULL, nothing to free */ vhost_map_free(srv, md); return NULL; } md->default_action = li_value_extract(entryValue); } else { if (NULL != g_hash_table_lookup(md->hash, entryKeyStr)) { ERROR(srv, "vhost.map: duplicate entry for '%s'", entryKeyStr->str); g_string_free(entryKeyStr, TRUE); vhost_map_free(srv, md); return NULL; } g_hash_table_insert(md->hash, entryKeyStr, li_value_extract(entryValue)); } LI_VALUE_END_FOREACH() return li_action_new_function(vhost_map, NULL, vhost_map_free, md); }
static liAction* lua_handler_create(liServer *srv, liWorker *wrk, liPlugin* p, liValue *val, gpointer userdata) { liValue *v_filename = NULL, *v_options = NULL, *v_args = NULL; lua_config *conf; guint ttl = 0; UNUSED(wrk); UNUSED(userdata); if (LI_VALUE_STRING == li_value_type(val)) { v_filename = val; } else if (LI_VALUE_LIST == li_value_type(val)) { switch (li_value_list_len(val)) { case 3: v_args = li_value_list_at(val, 2); /* fall through */ case 2: v_options = li_value_list_at(val, 1); /* fall through */ case 1: v_filename = li_value_list_at(val, 0); /* fall through */ case 0: break; default: ERROR(srv, "%s", "lua.handler expects at most 3 arguments"); return NULL; } } if (LI_VALUE_STRING != li_value_type(v_filename)) { ERROR(srv, "%s", "lua.handler expects at least a filename, or a filename and some options"); return NULL; } if (NULL != v_options) { if (NULL == (v_options = li_value_to_key_value_list(v_options))) { ERROR(srv, "%s", "lua.handler expects options in a hash/key-value list"); return NULL; } LI_VALUE_FOREACH(entry, v_options) liValue *entryKey = li_value_list_at(entry, 0); liValue *entryValue = li_value_list_at(entry, 1); GString *entryKeyStr; if (LI_VALUE_STRING != li_value_type(entryKey)) { ERROR(srv, "%s", "lua.handler doesn't take default keys"); return NULL; } entryKeyStr = entryKey->data.string; /* keys are either NONE or STRING */ if (g_string_equal(entryKeyStr, &lon_ttl)) { if (LI_VALUE_NUMBER != li_value_type(entryValue) || entryValue->data.number <= 0) { ERROR(srv, "lua.handler option '%s' expects positive integer as parameter", entryKeyStr->str); return NULL; } ttl = entryValue->data.number; } else { ERROR(srv, "unknown option for lua.handler '%s'", entryKeyStr->str); return NULL; } LI_VALUE_END_FOREACH() } conf = lua_config_new(srv, p, li_value_extract_string(v_filename), ttl, li_value_extract(v_args)); return li_action_new_function(lua_handle, NULL, lua_config_free, conf); }
void li_value_wrap_in_list(liValue *val) { liValue *item; assert(NULL != val); item = li_value_extract(val); val->type = LI_VALUE_LIST; val->data.list = g_array_new(FALSE, TRUE, sizeof(liValue*)); g_array_append_val(val->data.list, item); }
void li_value_wrap_in_list(liValue *val) { liValue *item; LI_FORCE_ASSERT(NULL != val); item = li_value_extract(val); val->type = LI_VALUE_LIST; val->data.list = g_ptr_array_new(); g_ptr_array_add(val->data.list, item); }
static liAction* vhost_map_regex_create(liServer *srv, liWorker *wrk, liPlugin* p, liValue *val, gpointer userdata) { vhost_map_regex_data *mrd; UNUSED(wrk); UNUSED(userdata); val = li_value_get_single_argument(val); if (NULL == (val = li_value_to_key_value_list(val))) { ERROR(srv, "%s", "vhost.map_regex expects a hashtable/key-value list as parameter"); return NULL; } mrd = g_slice_new0(vhost_map_regex_data); mrd->plugin = p; mrd->list = g_array_new(FALSE, FALSE, sizeof(vhost_map_regex_entry)); LI_VALUE_FOREACH(entry, val) liValue *entryKey = li_value_list_at(entry, 0); liValue *entryValue = li_value_list_at(entry, 1); GString *entryKeyStr; if (LI_VALUE_ACTION != li_value_type(entryValue)) { ERROR(srv, "vhost.map_regex expects a hashtable/key-value list with action values as parameter, %s value given", li_value_type_string(entryValue)); vhost_map_free(srv, mrd); return NULL; } /* we now own the key string: free it in case of failure */ entryKeyStr = li_value_extract_string(entryKey); if (NULL != entryKeyStr && g_str_equal(entryKeyStr->str, "default")) { WARNING(srv, "%s", "vhost.map_regex: found entry with string key \"default\". please convert the parameter to a key-value list and use the keyword default instead."); /* TODO: remove support for "default" (LI_VALUE_HASH) */ g_string_free(entryKeyStr, TRUE); entryKeyStr = NULL; } if (NULL == entryKeyStr) { if (NULL != mrd->default_action) { ERROR(srv, "%s", "vhost.map_regex: already have a default action"); vhost_map_free(srv, mrd); return NULL; } mrd->default_action = li_value_extract(entryValue); } else { GError *err = NULL; vhost_map_regex_entry map_entry; map_entry.regex = g_regex_new(entryKeyStr->str, G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &err); g_string_free(entryKeyStr, TRUE); if (NULL == map_entry.regex) { assert(NULL != err); vhost_map_regex_free(srv, mrd); ERROR(srv, "vhost.map_regex: error compiling regex \"%s\": %s", entryKeyStr->str, err->message); g_error_free(err); return NULL; } assert(NULL == err); map_entry.action = li_value_extract(entryValue); g_array_append_val(mrd->list, map_entry); } LI_VALUE_END_FOREACH() return li_action_new_function(vhost_map_regex, NULL, vhost_map_regex_free, mrd); }