-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ngx_http_lua_headerfilterby.c
306 lines (231 loc) · 7.99 KB
/
ngx_http_lua_headerfilterby.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_headerfilterby.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_cache.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_ndk.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"
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
/**
* Set environment table for the given code closure.
*
* Before:
* | code closure | <- top
* | ... |
*
* After:
* | code closure | <- top
* | ... |
* */
static void
ngx_http_lua_header_filter_by_lua_env(lua_State *L, ngx_http_request_t *r)
{
ngx_http_lua_main_conf_t *lmcf;
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
/* set nginx request pointer to current lua thread's globals table */
lua_pushlightuserdata(L, r);
lua_setglobal(L, GLOBALS_SYMBOL_REQUEST);
/**
* we want to create empty environment for current script
*
* setmetatable({}, {__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.
* */
lua_newtable(L); /* new empty environment aka {} */
#if defined(NDK) && NDK
ngx_http_lua_inject_ndk_api(L);
#endif /* defined(NDK) && NDK */
/* {{{ initialize ngx.* namespace */
lua_createtable(L, 0 /* narr */, 71 /* nrec */); /* ngx.* */
ngx_http_lua_inject_internal_utils(r->connection->log, L);
ngx_http_lua_inject_http_consts(L);
ngx_http_lua_inject_core_consts(L);
ngx_http_lua_inject_log_api(L);
ngx_http_lua_inject_time_api(L);
ngx_http_lua_inject_string_api(L);
#if (NGX_PCRE)
ngx_http_lua_inject_regex_api(L);
#endif
ngx_http_lua_inject_req_api_no_io(r->connection->log, L);
ngx_http_lua_inject_resp_header_api(L);
ngx_http_lua_inject_variable_api(L);
ngx_http_lua_inject_shdict_api(lmcf, L);
ngx_http_lua_inject_misc_api(L);
lua_setfield(L, -2, "ngx");
/* }}} */
/* {{{ make new env inheriting main thread's globals table */
lua_newtable(L); /* the metatable for the new env */
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */
/* }}} */
lua_setfenv(L, -2); /* set new running env for the code closure */
}
ngx_int_t
ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
{
ngx_int_t rc;
u_char *err_msg;
#if (NGX_PCRE)
ngx_pool_t *old_pool;
#endif
/* set Lua VM panic handler */
lua_atpanic(L, ngx_http_lua_atpanic);
/* initialize nginx context in Lua VM, code chunk at stack top sp = 1 */
ngx_http_lua_header_filter_by_lua_env(L, r);
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */
old_pool = ngx_http_lua_pcre_malloc_init(r->pool);
#endif
/* protected call user code */
rc = lua_pcall(L, 0, 1, 0);
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */
ngx_http_lua_pcre_malloc_done(old_pool);
#endif
if (rc != 0) {
/* error occured when running loaded code */
err_msg = (u_char *) lua_tostring(L, -1);
if (err_msg != NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "(lua-error) %s",
err_msg);
lua_settop(L, 0); /* clear remaining elems on stack */
}
return NGX_ERROR;
}
/* clear Lua stack */
lua_settop(L, 0);
return NGX_OK;
}
ngx_int_t
ngx_http_lua_header_filter_inline(ngx_http_request_t *r)
{
lua_State *L;
ngx_int_t rc;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_loc_conf_t *llcf;
char *err;
dd("HERE");
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
L = lmcf->lua;
/* load Lua inline script (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadbuffer(L, llcf->header_filter_src.value.data,
llcf->header_filter_src.value.len, llcf->header_filter_src_key,
"header_filter_by_lua", &err, llcf->enable_code_cache ? 1 : 0);
if (rc != NGX_OK) {
if (err == NULL) {
err = "unknown error";
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Failed to load Lua inlined code: %s", err);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_http_lua_header_filter_by_chunk(L, r);
dd("header filter by chunk returns %d", (int) rc);
if (rc != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_http_lua_header_filter_file(ngx_http_request_t *r)
{
lua_State *L;
ngx_int_t rc;
u_char *script_path;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_loc_conf_t *llcf;
char *err;
ngx_str_t eval_src;
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
/* Eval nginx variables in code path string first */
if (ngx_http_complex_value(r, &llcf->header_filter_src, &eval_src)
!= NGX_OK) {
return NGX_ERROR;
}
script_path = ngx_http_lua_rebase_path(r->pool, eval_src.data,
eval_src.len);
if (script_path == NULL) {
return NGX_ERROR;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
L = lmcf->lua;
/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(L, script_path,
llcf->header_filter_src_key, &err, llcf->enable_code_cache ? 1 : 0);
if (rc != NGX_OK) {
if (err == NULL) {
err = "unknown error";
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Failed to load Lua inlined code: %s", err);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* make sure we have a valid code chunk */
assert(lua_isfunction(L, -1));
rc = ngx_http_lua_header_filter_by_chunk(L, r);
if (rc != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_lua_header_filter(ngx_http_request_t *r)
{
ngx_http_lua_loc_conf_t *llcf;
ngx_http_lua_ctx_t *ctx;
ngx_int_t rc;
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
if (llcf->header_filter_handler == NULL) {
dd("no header filter handler found");
return ngx_http_next_header_filter(r);
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua header filter for header_filter_by_lua , uri \"%V\"", &r->uri);
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
dd("ctx = %p", ctx);
if (ctx == NULL) {
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_lua_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
dd("setting new ctx: ctx = %p", ctx);
ctx->cc_ref = LUA_NOREF;
ctx->ctx_ref = LUA_NOREF;
ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
}
dd("calling header filter handler");
rc = llcf->header_filter_handler(r);
if (rc != NGX_OK) {
dd("calling header filter handler rc %d", (int)rc);
return NGX_ERROR;
}
return ngx_http_next_header_filter(r);
}
ngx_int_t
ngx_http_lua_header_filter_init()
{
dd("calling header filter init");
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_lua_header_filter;
return NGX_OK;
}