static int noit_lua_module_config(noit_module_t *mod, noit_hash_table *options) { LMC_DECL(L, mod); SETUP_CALL(L, "config", return 0); noit_lua_setup_module(L, mod); noit_lua_hash_to_table(L, options); noit_hash_destroy(options, free, free); free(options); lua_pcall(L, 2, 1, 0); /* If rv == 0, the caller will free options. We've * already freed options, that would be bad. fudge -> 1 */ RETURN_INT(L, "config", { rv = (rv == 0) ? 1 : rv; });
static int noit_lua_http_request_querystring(lua_State *L) { noit_hash_table *h; CCALL_DECL(L, noit_http_request, req, 0); h = noit_http_request_querystring_table(req); if(lua_gettop(L) == 1) noit_lua_hash_to_table(L, h); else if(lua_gettop(L) == 2) { const char *key = lua_tostring(L,2), *value; if(key == NULL) lua_pushnil(L); else { if(noit_hash_retr_str(h, key, strlen(key), &value)) lua_pushstring(L, value); else lua_pushnil(L); } } else luaL_error(L, "invalid arguments to noit_http_request:querystring()"); return 1; }
static int noit_lua_http_request_headers(lua_State *L) { noit_hash_table *h; CCALL_DECL(L, noit_http_request, req, 0); h = noit_http_request_headers_table(req); if(lua_gettop(L) == 1) noit_lua_hash_to_table(L, h); else if(lua_gettop(L) == 2) { const char *hdr = lua_tostring(L,2); if(hdr == NULL) lua_pushnil(L); else { char *cp, *lower = alloca(strlen(hdr) + 1); memcpy(lower, hdr, strlen(hdr)+1); for(cp=lower; *cp; cp++) *cp = tolower(*cp); if(noit_hash_retr_str(h, lower, strlen(lower), &hdr)) lua_pushstring(L, hdr); else lua_pushnil(L); } } else luaL_error(L, "invalid arguments to noit_http_request:headers()"); return 1; }
static int noit_check_index_func(lua_State *L) { int n; const char *k; noit_check_t **udata, *check; n = lua_gettop(L); /* number of arguments */ assert(n == 2); if(!luaL_checkudata(L, 1, "noit_check_t")) { luaL_error(L, "metatable error, arg1 not a noit_check_t!"); } udata = lua_touserdata(L, 1); check = *udata; if(!lua_isstring(L, 2)) { luaL_error(L, "metatable error, arg2 not a string!"); } k = lua_tostring(L, 2); switch(*k) { case 'a': if(!strcmp(k, "available")) { lua_pushlightuserdata(L, check); lua_pushinteger(L, NP_AVAILABLE); lua_pushcclosure(L, noit_lua_set_available, 2); } else if(!strcmp(k, "availability")) { lua_pushlightuserdata(L, check); lua_pushcclosure(L, noit_lua_get_available, 1); } else break; return 1; case 'b': if(!strcmp(k, "bad")) { lua_pushlightuserdata(L, check); lua_pushinteger(L, NP_BAD); lua_pushcclosure(L, noit_lua_set_state, 2); } else break; return 1; case 'c': if(!strcmp(k, "config")) noit_lua_hash_to_table(L, check->config); else break; return 1; case 'g': if(!strcmp(k, "good")) { lua_pushlightuserdata(L, check); lua_pushinteger(L, NP_GOOD); lua_pushcclosure(L, noit_lua_set_state, 2); } else break; return 1; case 'm': if(!strcmp(k, "module")) lua_pushstring(L, check->module); #define IF_METRIC_BLOCK(name,type) \ if(!strcmp(k, name)) { \ lua_pushlightuserdata(L, check); \ lua_pushinteger(L, type); \ lua_pushcclosure(L, noit_lua_set_metric, 2); \ } else IF_METRIC_BLOCK("metric", METRIC_GUESS) else IF_METRIC_BLOCK("metric_string", METRIC_STRING) else IF_METRIC_BLOCK("metric_int32", METRIC_INT32) else IF_METRIC_BLOCK("metric_uint32", METRIC_UINT32) else IF_METRIC_BLOCK("metric_int64", METRIC_INT64) else IF_METRIC_BLOCK("metric_uint64", METRIC_UINT64) else IF_METRIC_BLOCK("metric_double", METRIC_DOUBLE) else if(!strcmp(k, "metric_json")) { lua_pushlightuserdata(L, check); lua_pushcclosure(L, noit_lua_set_metric_json, 1); } else break; return 1; case 'n': if(!strcmp(k, "name")) lua_pushstring(L, check->name); else break; return 1; case 'p': if(!strcmp(k, "period")) lua_pushinteger(L, check->period); else break; return 1; case 's': if(!strcmp(k, "state")) { lua_pushlightuserdata(L, check); lua_pushcclosure(L, noit_lua_get_state, 1); } else if(!strcmp(k, "status")) { lua_pushlightuserdata(L, check); lua_pushcclosure(L, noit_lua_set_status, 1); } else break; return 1; case 't': if(!strcmp(k, "target")) lua_pushstring(L, check->target); else if(!strcmp(k, "target_ip")) { if(check->target_ip[0] == '\0') lua_pushnil(L); else lua_pushstring(L, check->target_ip); } else if(!strcmp(k, "timeout")) lua_pushinteger(L, check->timeout); else break; return 1; case 'u': if(!strcmp(k, "unavailable")) { lua_pushlightuserdata(L, check); lua_pushinteger(L, NP_UNAVAILABLE); lua_pushcclosure(L, noit_lua_set_available, 2); } else break; return 1; default: break; } luaL_error(L, "noit_check_t no such element: %s", k); return 0; }