1
1
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
5
#include "ngx_http_lua_common.h"
4
6
#include "ngx_http_lua_directive.h"
5
7
#include "ngx_http_lua_util.h"
6
8
#include "ngx_http_lua_cache.h"
7
9
#include "ngx_http_lua_conf.h"
8
10
#include "ngx_http_lua_setby.h"
9
11
#include "ngx_http_lua_contentby.h"
12
#include "ngx_http_lua_accessby.h"
13
#include "ngx_http_lua_rewriteby.h"
16
unsigned ngx_http_lua_requires_rewrite = 0;
17
unsigned ngx_http_lua_requires_access = 0;
21
ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
23
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
24
"lua_code_cache is off; this will hurt performance");
26
return ngx_conf_set_flag_slot(cf, cmd, conf);
55
73
ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
59
77
ndk_set_var_t filter;
80
ngx_http_lua_set_var_data_t *filter_data;
62
* args[0] = "set_by_lua"
63
* args[1] = target variable name
64
* args[2] = lua script to be executed
65
* args[3..] = real params
83
* value[0] = "set_by_lua"
84
* value[1] = target variable name
85
* value[2] = lua script to be executed
86
* value[3..] = real params
67
args = cf->args->elts;
70
/* prevent variable appearing in Lua inline script/file path */
73
if (ngx_http_lua_has_inline_var(&args[2])) {
74
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
75
"Lua inline script or file path should not has inline variable: %V",
77
return NGX_CONF_ERROR;
88
value = cf->args->elts;
81
91
filter.type = NDK_SET_VAR_MULTI_VALUE_DATA;
82
92
filter.func = cmd->post;
83
filter.size = cf->args->nelts - 2; /* get number of real params + 1 (lua script) */
84
filter.data = (void*)filter.size;
86
return ndk_set_var_multi_value_core(cf, &target, &args[2], &filter);
93
filter.size = cf->args->nelts - 2; /* get number of real params
96
filter_data = ngx_palloc(cf->pool, sizeof(ngx_http_lua_set_var_data_t));
97
if (filter_data == NULL) {
98
return NGX_CONF_ERROR;
101
filter_data->size = filter.size;
103
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
105
return NGX_CONF_ERROR;
108
filter_data->key = p;
110
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
111
p = ngx_http_lua_digest_hex(p, value[2].data, value[2].len);
114
filter.data = filter_data;
116
return ndk_set_var_multi_value_core(cf, &target, &value[2], &filter);
96
126
ngx_http_lua_main_conf_t *lmcf;
127
ngx_http_lua_loc_conf_t *llcf;
130
ngx_http_lua_set_var_data_t *filter_data = data;
99
132
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
136
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
102
138
/* load Lua inline script (w/ cache) sp = 1 */
103
rc = ngx_http_lua_cache_loadbuffer(L, v[0].data, v[0].len, "set_by_lua_inline", &err);
139
rc = ngx_http_lua_cache_loadbuffer(L, v[0].data, v[0].len,
140
filter_data->key, "set_by_lua_inline", &err,
141
llcf->enable_code_cache);
105
143
if (rc != NGX_OK) {
106
144
if (err == NULL) {
131
169
u_char *script_path;
132
170
ngx_http_lua_main_conf_t *lmcf;
171
ngx_http_lua_loc_conf_t *llcf;
174
ngx_http_lua_set_var_data_t *filter_data = data;
135
176
script_path = ngx_http_lua_rebase_path(r->pool, v[0].data, v[0].len);
137
178
if (script_path == NULL) {
138
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
139
"Failed to allocate memory to store absolute path: raw path='%v'",
142
179
return NGX_ERROR;
145
182
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
186
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
148
188
/* load Lua script file (w/ cache) sp = 1 */
149
rc = ngx_http_lua_cache_loadfile(L, (char *)script_path, &err);
189
rc = ngx_http_lua_cache_loadfile(L, script_path, filter_data->key,
190
&err, llcf->enable_code_cache);
151
192
if (rc != NGX_OK) {
152
193
if (err == NULL) {
213
ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
216
ngx_http_lua_loc_conf_t *llcf = conf;
219
ngx_http_compile_complex_value_t ccv;
223
#if defined(nginx_version) && nginx_version >= 8042 && nginx_version <= 8053
224
return "does not work with " NGINX_VER;
227
/* must specifiy a content handler */
228
if (cmd->post == NULL) {
229
return NGX_CONF_ERROR;
232
if (llcf->rewrite_handler) {
233
return "is duplicate";
236
value = cf->args->elts;
238
if (value[1].len == 0) {
239
/* Oops...Invalid location conf */
240
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
241
"Invalid location config: no runnable Lua code");
243
return NGX_CONF_ERROR;
246
if (cmd->post == ngx_http_lua_rewrite_handler_inline) {
247
/* Don't eval nginx variables for inline lua code */
248
llcf->rewrite_src.value = value[1];
250
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
252
return NGX_CONF_ERROR;
255
llcf->rewrite_src_key = p;
257
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
258
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
262
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
264
ccv.value = &value[1];
265
ccv.complex_value = &llcf->rewrite_src;
267
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
268
return NGX_CONF_ERROR;
271
if (llcf->rewrite_src.lengths == NULL) {
272
/* no variable found */
273
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
275
return NGX_CONF_ERROR;
278
llcf->rewrite_src_key = p;
280
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
281
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
286
llcf->rewrite_handler = cmd->post;
288
if (! ngx_http_lua_requires_rewrite) {
289
ngx_http_lua_requires_rewrite = 1;
297
ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
300
ngx_http_lua_loc_conf_t *llcf = conf;
303
ngx_http_compile_complex_value_t ccv;
307
/* must specifiy a content handler */
308
if (cmd->post == NULL) {
309
return NGX_CONF_ERROR;
312
if (llcf->access_handler) {
313
return "is duplicate";
316
value = cf->args->elts;
318
if (value[1].len == 0) {
319
/* Oops...Invalid location conf */
320
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
321
"Invalid location config: no runnable Lua code");
323
return NGX_CONF_ERROR;
326
if (cmd->post == ngx_http_lua_access_handler_inline) {
327
/* Don't eval nginx variables for inline lua code */
328
llcf->access_src.value = value[1];
330
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
332
return NGX_CONF_ERROR;
335
llcf->access_src_key = p;
337
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
338
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
342
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
344
ccv.value = &value[1];
345
ccv.complex_value = &llcf->access_src;
347
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
348
return NGX_CONF_ERROR;
351
if (llcf->access_src.lengths == NULL) {
352
/* no variable found */
353
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
355
return NGX_CONF_ERROR;
358
llcf->access_src_key = p;
360
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
361
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
366
llcf->access_handler = cmd->post;
368
if (! ngx_http_lua_requires_access) {
369
ngx_http_lua_requires_access = 1;
172
377
ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
175
380
ngx_http_core_loc_conf_t *clcf;
176
381
ngx_http_lua_loc_conf_t *llcf = conf;
384
ngx_http_compile_complex_value_t ccv;
182
390
return NGX_CONF_ERROR;
185
/* update lua script data */
187
* args[0] = "content_by_lua"
188
* args[1] = lua script to be executed
190
args = cf->args->elts;
192
/* prevent variable appearing in Lua inline script/file path */
195
if (ngx_http_lua_has_inline_var(&args[1])) {
196
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
197
"Lua inline script or file path should not has inline variable: %V",
200
return NGX_CONF_ERROR;
393
if (llcf->content_handler) {
394
return "is duplicate";
204
if (args[1].len == 0) {
397
value = cf->args->elts;
399
if (value[1].len == 0) {
205
400
/* Oops...Invalid location conf */
206
401
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
207
402
"Invalid location config: no runnable Lua code");
208
403
return NGX_CONF_ERROR;
406
if (cmd->post == ngx_http_lua_content_handler_inline) {
407
/* Don't eval nginx variables for inline lua code */
408
llcf->content_src.value = value[1];
410
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
412
return NGX_CONF_ERROR;
415
llcf->content_src_key = p;
417
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
418
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
422
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
424
ccv.value = &value[1];
425
ccv.complex_value = &llcf->content_src;
427
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
428
return NGX_CONF_ERROR;
431
if (llcf->content_src.lengths == NULL) {
432
/* no variable found */
433
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
435
return NGX_CONF_ERROR;
438
llcf->content_src_key = p;
440
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
441
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
446
llcf->content_handler = cmd->post;
213
448
/* register location content handler */
214
449
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
215
450
if (clcf == NULL) {
216
451
return NGX_CONF_ERROR;
218
clcf->handler = cmd->post;
454
clcf->handler = ngx_http_lua_content_handler;
220
456
return NGX_CONF_OK;
225
ngx_http_lua_content_handler_inline(ngx_http_request_t *r)
229
ngx_http_lua_main_conf_t *lmcf;
230
ngx_http_lua_loc_conf_t *llcf;
233
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
234
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
237
/* load Lua inline script (w/ cache) sp = 1 */
238
rc = ngx_http_lua_cache_loadbuffer(L, llcf->src.data,
239
llcf->src.len, "content_by_lua", &err);
243
err = "unknown error";
246
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
247
"Failed to load Lua inlined code: %s", err);
249
return NGX_HTTP_INTERNAL_SERVER_ERROR;
252
rc = ngx_http_lua_content_by_chunk(L, r);
254
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
258
if (rc == NGX_DONE) {
262
if (rc == NGX_AGAIN) {
263
#if defined(nginx_version) && nginx_version >= 8011
274
ngx_http_lua_content_handler_file(ngx_http_request_t *r)
279
ngx_http_lua_main_conf_t *lmcf;
280
ngx_http_lua_loc_conf_t *llcf;
283
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
285
script_path = ngx_http_lua_rebase_path(r->pool, llcf->src.data,
288
if (script_path == NULL) {
289
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
290
"Failed to allocate memory to store absolute path: raw path='%v'",
296
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
299
/* load Lua script file (w/ cache) sp = 1 */
300
rc = ngx_http_lua_cache_loadfile(L, (char *) script_path, &err);
304
err = "unknown error";
307
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
308
"Failed to load Lua inlined code: %s", err);
310
return NGX_HTTP_INTERNAL_SERVER_ERROR;
313
/* make sure we have a valid code chunk */
314
assert(lua_isfunction(L, -1));
316
rc = ngx_http_lua_content_by_chunk(L, r);
318
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
322
if (rc == NGX_DONE) {
326
if (rc == NGX_AGAIN) {
327
#if defined(nginx_version) && nginx_version >= 8011