-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_configureby.c
248 lines (176 loc) · 5.54 KB
/
ngx_http_lua_configureby.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
/*
* Copyright (C) Yichun Zhang (agentzh)
*
* Author: Thibault Charbonnier (thibaultcha)
* I hereby assign copyright in this code to the lua-nginx-module project,
* to be licensed under the same terms as the rest of the code.
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_configureby.h"
#include "ngx_http_lua_directive.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_api.h"
static ngx_conf_t *cfp;
char *
ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *rv;
ngx_conf_t save;
save = *cf;
cf->handler = ngx_http_lua_configure_by_lua;
cf->handler_conf = conf;
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd);
*cf = save;
return rv;
}
char *
ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
u_char *name;
ngx_str_t *value;
ngx_http_lua_main_conf_t *lmcf = conf;
if (cmd->post == NULL) {
return NGX_CONF_ERROR;
}
if (lmcf->configure_handler) {
return "is duplicate";
}
value = cf->args->elts;
if (value[1].len == 0) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
"invalid location config: no runnable Lua code");
return NGX_CONF_ERROR;
}
lmcf->configure_handler = (ngx_http_lua_configure_handler_pt) cmd->post;
if (cmd->post == ngx_http_lua_configure_handler_file) {
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
value[1].len);
if (name == NULL) {
return NGX_CONF_ERROR;
}
lmcf->configure_src.data = name;
lmcf->configure_src.len = ngx_strlen(name);
} else {
lmcf->configure_src = value[1];
}
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_lua_configure_handler_inline(ngx_conf_t *cf,
ngx_http_lua_main_conf_t *lmcf)
{
int status;
lua_State *L = lmcf->lua;
cfp = cf;
status = luaL_loadbuffer(L, (char *) lmcf->configure_src.data,
lmcf->configure_src.len, "=configure_by_lua")
|| ngx_http_lua_do_call(cf->log, L);
cfp = NULL;
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua");
}
ngx_int_t
ngx_http_lua_configure_handler_file(ngx_conf_t *cf,
ngx_http_lua_main_conf_t *lmcf)
{
int status;
lua_State *L = lmcf->lua;
cfp = cf;
status = luaL_loadfile(L, (char *) lmcf->configure_src.data)
|| ngx_http_lua_do_call(cf->log, L);
cfp = NULL;
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua_file");
}
ngx_uint_t
ngx_http_lua_is_configure_phase()
{
return cfp != NULL;
}
#ifndef NGX_LUA_NO_FFI_API
unsigned int
ngx_http_lua_ffi_is_configure_phase()
{
return ngx_http_lua_is_configure_phase();
}
int
ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size,
u_char *errstr, size_t *err_len)
{
ngx_int_t rc;
ssize_t ssize;
lua_State *L;
ngx_http_lua_main_conf_t *lmcf;
ngx_conf_t *cf = cfp;
ngx_shm_zone_t **zone;
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
ssize = ngx_parse_size(size);
if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) {
*err_len = ngx_snprintf(errstr, *err_len,
"invalid lua shared dict size \"%s\"",
size->data)
- errstr;
return NGX_DECLINED;
}
rc = ngx_http_lua_shared_dict_add(cf, name, ssize);
if (rc != NGX_OK) {
if (rc == NGX_DECLINED) {
*err_len = ngx_snprintf(errstr, *err_len,
"lua_shared_dict \"%V\" is already defined"
" as \"%V\"", name, name)
- errstr;
}
return rc;
}
zone = lmcf->shdict_zones->elts;
L = lmcf->lua;
lua_getglobal(L, "ngx");
lua_getfield(L, -1, "shared");
ngx_http_lua_create_shdict_mt(L);
/* ngx ngx.shared shmt */
ngx_http_lua_attach_shdict(L, name, zone[lmcf->shdict_zones->nelts - 1]);
lua_pop(L, 3); /* pop: ngx ngx.shared shmt */
return NGX_OK;
}
void
ngx_http_lua_ffi_configure_max_pending_timers(int n_timers)
{
ngx_http_lua_main_conf_t *lmcf;
ngx_conf_t *cf = cfp;
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
lmcf->max_pending_timers = (ngx_int_t) n_timers;
}
void
ngx_http_lua_ffi_configure_max_running_timers(int n_timers)
{
ngx_http_lua_main_conf_t *lmcf;
ngx_conf_t *cf = cfp;
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
lmcf->max_running_timers = (ngx_int_t) n_timers;
}
int
ngx_http_lua_ffi_configure_env(u_char *value, size_t name_len, size_t len)
{
ngx_core_conf_t *ccf;
ngx_str_t *var;
ngx_conf_t *cf = cfp;
ccf = (ngx_core_conf_t *) ngx_get_conf(cf->cycle->conf_ctx,
ngx_core_module);
var = ngx_array_push(&ccf->env);
if (var == NULL) {
return NGX_ERROR;
}
var->data = ngx_pnalloc(cf->pool, len);
if (var->data == NULL) {
return NGX_ERROR;
}
(void) ngx_copy(var->data, value, len);
var->len = name_len;
return NGX_OK;
}
#endif
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */