forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_lua_setby.c
216 lines (161 loc) · 5.35 KB
/
ngx_http_lua_setby.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_setby.h"
#include "ngx_http_lua_exception.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_regex.h"
#include "ngx_http_lua_variable.h"
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_util.h"
static void ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r,
size_t nargs, ngx_http_variable_value_t *args);
/* keys in Lua thread for fetching args and nargs in set_by_lua* */
#define ngx_http_lua_nargs_key "__ngx_nargs"
#define ngx_http_lua_args_key "__ngx_args"
ngx_int_t
ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r, ngx_str_t *val,
ngx_http_variable_value_t *args, size_t nargs, ngx_str_t *script)
{
size_t i;
ngx_int_t rc;
u_char *err_msg;
size_t len;
u_char *data;
#if (NGX_PCRE)
ngx_pool_t *old_pool;
#endif
dd("nargs: %d", (int) nargs);
dd("set Lua VM panic handler");
lua_atpanic(L, ngx_http_lua_atpanic);
NGX_LUA_EXCEPTION_TRY {
dd("initialize nginx context in Lua VM, code chunk at "
"stack top sp = 1");
ngx_http_lua_set_by_lua_env(L, r, nargs, args);
/* passing directive arguments to the user code */
for (i = 0; i < nargs; i++) {
lua_pushlstring(L, (const char *) args[i].data, args[i].len);
}
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */
old_pool = ngx_http_lua_pcre_malloc_init(r->pool);
#endif
lua_pushcfunction(L, ngx_http_lua_traceback);
lua_insert(L, 1); /* put it under chunk and args */
dd("protected call user code");
rc = lua_pcall(L, nargs, 1, 1);
dd("after protected call user code");
lua_remove(L, 1); /* remove traceback function */
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */
ngx_http_lua_pcre_malloc_done(old_pool);
#endif
if (rc != 0) {
/* error occurred when running loaded code */
err_msg = (u_char *) lua_tolstring(L, -1, &len);
if (err_msg == NULL) {
err_msg = (u_char *) "unknown reason";
len = sizeof("unknown reason") - 1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to run set_by_lua*: %*s", len, err_msg);
lua_settop(L, 0); /* clear remaining elems on stack */
return NGX_ERROR;
}
data = (u_char *) lua_tolstring(L, -1, &len);
if (data) {
val->data = ngx_palloc(r->pool, len);
if (val->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(val->data, data, len);
val->len = len;
} else {
val->data = NULL;
val->len = 0;
}
} NGX_LUA_EXCEPTION_CATCH {
dd("nginx execution restored");
return NGX_ERROR;
}
/* clear Lua stack */
lua_settop(L, 0);
return NGX_OK;
}
int
ngx_http_lua_setby_param_get(lua_State *L)
{
int idx;
int n;
ngx_http_variable_value_t *v;
idx = luaL_checkint(L, 2);
idx--;
/* get number of args from globals */
lua_getglobal(L, ngx_http_lua_nargs_key);
n = (int) lua_tointeger(L, -1);
/* get args from globals */
lua_getglobal(L, ngx_http_lua_args_key);
v = lua_touserdata(L, -1);
if (idx < 0 || idx > n - 1) {
lua_pushnil(L);
} else {
lua_pushlstring(L, (const char *) (v[idx].data), v[idx].len);
}
return 1;
}
/**
* Set environment table for the given code closure.
*
* Before:
* | code closure | <- top
* | ... |
*
* After:
* | code closure | <- top
* | ... |
* */
static void
ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs,
ngx_http_variable_value_t *args)
{
/* set nginx request pointer to current lua thread's globals table */
ngx_http_lua_set_req(L, r);
lua_pushinteger(L, nargs);
lua_setglobal(L, ngx_http_lua_nargs_key);
lua_pushlightuserdata(L, args);
lua_setglobal(L, ngx_http_lua_args_key);
/**
* we want to create empty environment for current script
*
* newt = {}
* newt["_G"] = newt
* setmetatable(newt, {__index = _G})
*
* if a function or symbol is not defined in our env, __index will lookup
* in the global env.
*
* all variables created in the script-env will be thrown away at the end
* of the script run.
* */
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);
/* {{{ make new env inheriting main thread's globals table */
/* the metatable for the new env */
lua_createtable(L, 0 /* narr */, 1 /* nrec */);
ngx_http_lua_get_globals_table(L);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); /* setmetatable(newt, {__index = _G}) */
/* }}} */
lua_setfenv(L, -2); /* set new running env for the code closure */
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */