7
#include "ngx_http_lua_ndk.h"
10
#if defined(NDK) && NDK
13
static ndk_set_var_value_pt ngx_http_lookup_ndk_set_var_directive(u_char *name,
15
static int ngx_http_lua_ndk_set_var_get(lua_State *L);
16
static int ngx_http_lua_ndk_set_var_set(lua_State *L);
17
static int ngx_http_lua_run_set_var_directive(lua_State *L);
21
ngx_http_lua_ndk_set_var_get(lua_State *L)
23
ndk_set_var_value_pt func;
27
p = (u_char *) luaL_checklstring(L, 2, &len);
29
dd("ndk.set_var metatable __index: %s", p);
31
func = ngx_http_lookup_ndk_set_var_directive(p, len);
34
return luaL_error(L, "ndk.set_var: directive \"%s\" not found "
35
"or does not use ndk_set_var_value",
40
lua_pushvalue(L, -1); /* table key key */
41
lua_pushvalue(L, -1); /* table key key key */
43
lua_pushlightuserdata(L, func); /* table key key key func */
45
lua_pushcclosure(L, ngx_http_lua_run_set_var_directive, 2);
46
/* table key key closure */
48
lua_rawset(L, 1); /* table key */
50
lua_rawget(L, 1); /* table closure */
57
ngx_http_lua_ndk_set_var_set(lua_State *L)
59
return luaL_error(L, "Not allowed");
64
ngx_http_lua_run_set_var_directive(lua_State *L)
67
ndk_set_var_value_pt func;
69
ngx_http_variable_value_t arg;
72
ngx_http_request_t *r;
74
if (lua_gettop(L) != 1) {
75
return luaL_error(L, "expecting one argument");
78
arg.data = (u_char *) luaL_checklstring(L, 1, &len);
81
lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
82
r = lua_touserdata(L, -1);
86
return luaL_error(L, "no request object found");
89
p = (u_char *) luaL_checklstring(L, lua_upvalueindex(1), &len);
91
dd("calling set_var func for %s", p);
93
func = (ndk_set_var_value_pt) lua_touserdata(L, lua_upvalueindex(2));
95
rc = func(r, &res, &arg);
98
return luaL_error(L, "calling directive %s failed with code %d",
102
lua_pushlstring(L, (char *) res.data, res.len);
108
static ndk_set_var_value_pt
109
ngx_http_lookup_ndk_set_var_directive(u_char *name,
112
ndk_set_var_t *filter;
114
ngx_module_t *module;
117
for (i = 0; ngx_modules[i]; i++) {
118
module = ngx_modules[i];
119
if (module->type != NGX_HTTP_MODULE) {
123
cmd = ngx_modules[i]->commands;
128
for ( /* void */ ; cmd->name.len; cmd++) {
129
if (cmd->set != ndk_set_var_value) {
134
if (filter == NULL) {
138
if (cmd->name.len != name_len
139
|| ngx_strncmp(cmd->name.data, name, name_len) != 0)
144
return (ndk_set_var_value_pt)(filter->func);
153
ngx_http_lua_inject_ndk_api(lua_State *L)
155
lua_createtable(L, 0, 1); /* ndk.* */
157
lua_newtable(L); /* .set_var */
159
lua_createtable(L, 0, 2); /* metatable for .set_var */
160
lua_pushcfunction(L, ngx_http_lua_ndk_set_var_get);
161
lua_setfield(L, -2, "__index");
162
lua_pushcfunction(L, ngx_http_lua_ndk_set_var_set);
163
lua_setfield(L, -2, "__newindex");
164
lua_setmetatable(L, -2);
166
lua_setfield(L, -2, "set_var");
168
lua_getglobal(L, "package"); /* ndk package */
169
lua_getfield(L, -1, "loaded"); /* ndk package loaded */
170
lua_pushvalue(L, -3); /* ndk package loaded ndk */
171
lua_setfield(L, -2, "ndk"); /* ndk package loaded */
174
lua_setglobal(L, "ndk");
178
#endif /* defined(NDK) && NDK */