Apache HTTPD
lua_config.c
Go to the documentation of this file.
1
18#include "lua_config.h"
19#include "lua_vmprep.h"
20
22
24{
25 ap_lua_dir_cfg *cfg;
26 luaL_checkudata(L, index, "Apache2.DirConfig");
27 cfg = (ap_lua_dir_cfg *) lua_unboxpointer(L, index);
28 return cfg;
29}
30
31static cmd_parms *check_cmd_parms(lua_State *L, int index)
32{
34 luaL_checkudata(L, index, "Apache2.CommandParameters");
35 cmd = (cmd_parms *) lua_unboxpointer(L, index);
36 return cmd;
37}
38
39static int apl_toscope(const char *name)
40{
41 if (0 == strcmp("once", name))
42 return AP_LUA_SCOPE_ONCE;
43 if (0 == strcmp("request", name))
45 if (0 == strcmp("connection", name))
46 return AP_LUA_SCOPE_CONN;
47 if (0 == strcmp("conn", name))
48 return AP_LUA_SCOPE_CONN;
49 if (0 == strcmp("thread", name))
51 return AP_LUA_SCOPE_ONCE;
52}
53
55 const char *file,
56 const char *function,
57 const char *pattern,
58 const char *scope)
59{
60 ap_regex_t *uri_pattern;
61 apr_status_t rv;
64 handler->uri_pattern = NULL;
65 handler->function_name = NULL;
66
67 uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
68 if ((rv = ap_regcomp(uri_pattern, pattern, 0)) != APR_SUCCESS) {
69 return rv;
70 }
71 handler->file_name = apr_pstrdup(cfg->pool, file);
72 handler->uri_pattern = uri_pattern;
73 handler->scope = apl_toscope(scope);
74
75 handler->function_name = apr_pstrdup(cfg->pool, function);
77 handler;
78 return APR_SUCCESS;
79}
80
81/* Change to use ap_lua_map_handler */
83{
87 handler->uri_pattern = NULL;
88 handler->function_name = NULL;
89
91 lua_getfield(L, 2, "file");
92 if (lua_isstring(L, -1)) {
93 const char *file = lua_tostring(L, -1);
94 handler->file_name = apr_pstrdup(cfg->pool, file);
95 }
96 lua_pop(L, 1);
97
98 lua_getfield(L, 2, "pattern");
99 if (lua_isstring(L, -1)) {
100 const char *pattern = lua_tostring(L, -1);
101
102 ap_regex_t *uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
103 if (ap_regcomp(uri_pattern, pattern, 0) != OK) {
104 return luaL_error(L, "Unable to compile regular expression, '%s'",
105 pattern);
106 }
107 handler->uri_pattern = uri_pattern;
108 }
109 lua_pop(L, 1);
110
111 lua_getfield(L, 2, "scope");
112 if (lua_isstring(L, -1)) {
113 const char *scope = lua_tostring(L, -1);
114 handler->scope = apl_toscope(scope);
115 }
116 else {
117 handler->scope = AP_LUA_SCOPE_ONCE;
118 }
119 lua_pop(L, 1);
120
121 lua_getfield(L, 2, "func");
122 if (lua_isstring(L, -1)) {
123 const char *value = lua_tostring(L, -1);
124 handler->function_name = apr_pstrdup(cfg->pool, value);
125 }
126 else {
127 handler->function_name = "handle";
128 }
129 lua_pop(L, 1);
130
131
133 handler;
134 return 0;
135}
136
138{
139 ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
140 lua_pushstring(L, cfg->dir);
141 return 1;
142}
143
144/*static int cfg_root(lua_State *L)
145{
146 ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
147 lua_pushstring(L, cfg->root_path);
148 return 1;
149}*/
150
151static const struct luaL_Reg cfg_methods[] = {
152 {"match_handler", cfg_lua_map_handler},
153 {"directory", cfg_directory},
154 /* {"root", cfg_root}, */
155 {NULL, NULL}
156};
157
158/* helper function for the logging functions below */
159static int cmd_log_at(lua_State *L, int level)
160{
161 const char *msg;
164
165 lua_getstack(L, 1, &dbg);
166 lua_getinfo(L, "Sl", &dbg);
167
168 msg = luaL_checkstring(L, 2);
169 /* Intentional no APLOGNO */
170 ap_log_error(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0,
171 cmd->server, "%s", msg);
172 return 0;
173}
174
175/* r:debug(String) and friends which use apache logging */
176static int cmd_emerg(lua_State *L)
177{
178 return cmd_log_at(L, APLOG_EMERG);
179}
180static int cmd_alert(lua_State *L)
181{
182 return cmd_log_at(L, APLOG_ALERT);
183}
184static int cmd_crit(lua_State *L)
185{
186 return cmd_log_at(L, APLOG_CRIT);
187}
188static int cmd_err(lua_State *L)
189{
190 return cmd_log_at(L, APLOG_ERR);
191}
192static int cmd_warn(lua_State *L)
193{
194 return cmd_log_at(L, APLOG_WARNING);
195}
196static int cmd_notice(lua_State *L)
197{
198 return cmd_log_at(L, APLOG_NOTICE);
199}
200static int cmd_info(lua_State *L)
201{
202 return cmd_log_at(L, APLOG_INFO);
203}
204static int cmd_debug(lua_State *L)
205{
206 return cmd_log_at(L, APLOG_DEBUG);
207}
208static int cmd_trace1(lua_State *L)
209{
210 return cmd_log_at(L, APLOG_TRACE1);
211}
212static int cmd_trace2(lua_State *L)
213{
214 return cmd_log_at(L, APLOG_TRACE2);
215}
216static int cmd_trace3(lua_State *L)
217{
218 return cmd_log_at(L, APLOG_TRACE3);
219}
220static int cmd_trace4(lua_State *L)
221{
222 return cmd_log_at(L, APLOG_TRACE4);
223}
224static int cmd_trace5(lua_State *L)
225{
226 return cmd_log_at(L, APLOG_TRACE5);
227}
228static int cmd_trace6(lua_State *L)
229{
230 return cmd_log_at(L, APLOG_TRACE6);
231}
232static int cmd_trace7(lua_State *L)
233{
234 return cmd_log_at(L, APLOG_TRACE7);
235}
236static int cmd_trace8(lua_State *L)
237{
238 return cmd_log_at(L, APLOG_TRACE8);
239}
240
241static const struct luaL_Reg cmd_methods[] = {
242 {"trace8", cmd_trace8},
243 {"trace7", cmd_trace7},
244 {"trace6", cmd_trace6},
245 {"trace5", cmd_trace5},
246 {"trace4", cmd_trace4},
247 {"trace3", cmd_trace3},
248 {"trace2", cmd_trace2},
249 {"trace1", cmd_trace1},
250 {"debug", cmd_debug},
251 {"info", cmd_info},
252 {"notice", cmd_notice},
253 {"warn", cmd_warn},
254 {"err", cmd_err},
255 {"crit", cmd_crit},
256 {"alert", cmd_alert},
257 {"emerg", cmd_emerg},
258
259 {NULL, NULL}
260};
261
263{
264 luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */
265 lua_pushvalue(L, -1);
266
267 lua_setfield(L, -2, "__index");
268 luaL_setfuncs_compat(L, cfg_methods); /* [metatable] */
269
270
271 luaL_newmetatable(L, "Apache2.CommandParameters");
272 lua_pushvalue(L, -1);
273
274 lua_setfield(L, -2, "__index");
275 luaL_setfuncs_compat(L, cmd_methods); /* [metatable] */
276
277}
const char * pattern
Definition ap_regex.h:243
#define APLOG_USE_MODULE(foo)
#define OK
Definition httpd.h:456
#define APLOG_TRACE8
Definition http_log.h:79
#define APLOG_NOTICE
Definition http_log.h:69
#define APLOG_TRACE4
Definition http_log.h:75
#define APLOG_INFO
Definition http_log.h:70
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_TRACE3
Definition http_log.h:74
#define ap_log_error
Definition http_log.h:370
#define APLOG_WARNING
Definition http_log.h:68
#define APLOG_ALERT
Definition http_log.h:65
#define APLOG_TRACE7
Definition http_log.h:78
#define APLOG_CRIT
Definition http_log.h:66
#define APLOG_EMERG
Definition http_log.h:64
#define APLOG_TRACE2
Definition http_log.h:73
#define APLOG_TRACE1
Definition http_log.h:72
#define APLOG_TRACE5
Definition http_log.h:76
#define APLOG_TRACE6
Definition http_log.h:77
#define APLOG_DEBUG
Definition http_log.h:71
#define APLOG_MODULE_INDEX
Definition http_log.h:168
apr_size_t size
const char * value
Definition apr_env.h:51
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char apr_file_t * file
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
apr_cmdtype_e cmd
void ap_lua_load_config_lmodule(lua_State *L)
Definition lua_config.c:262
static int cmd_trace3(lua_State *L)
Definition lua_config.c:216
static int cmd_alert(lua_State *L)
Definition lua_config.c:180
static int cfg_lua_map_handler(lua_State *L)
Definition lua_config.c:82
static int cmd_trace2(lua_State *L)
Definition lua_config.c:212
static int cmd_debug(lua_State *L)
Definition lua_config.c:204
static const struct luaL_Reg cfg_methods[]
Definition lua_config.c:151
static int cmd_trace8(lua_State *L)
Definition lua_config.c:236
static int cmd_trace6(lua_State *L)
Definition lua_config.c:228
apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg, const char *file, const char *function, const char *pattern, const char *scope)
Definition lua_config.c:54
static int apl_toscope(const char *name)
Definition lua_config.c:39
static int cmd_crit(lua_State *L)
Definition lua_config.c:184
static int cfg_directory(lua_State *L)
Definition lua_config.c:137
static cmd_parms * check_cmd_parms(lua_State *L, int index)
Definition lua_config.c:31
static int cmd_err(lua_State *L)
Definition lua_config.c:188
static int cmd_trace7(lua_State *L)
Definition lua_config.c:232
static int cmd_notice(lua_State *L)
Definition lua_config.c:196
static ap_lua_dir_cfg * check_dir_config(lua_State *L, int index)
Definition lua_config.c:23
static int cmd_trace5(lua_State *L)
Definition lua_config.c:224
static const struct luaL_Reg cmd_methods[]
Definition lua_config.c:241
static int cmd_emerg(lua_State *L)
Definition lua_config.c:176
static int cmd_warn(lua_State *L)
Definition lua_config.c:192
static int cmd_trace1(lua_State *L)
Definition lua_config.c:208
static int cmd_log_at(lua_State *L, int level)
Definition lua_config.c:159
static int cmd_info(lua_State *L)
Definition lua_config.c:200
static int cmd_trace4(lua_State *L)
Definition lua_config.c:220
#define AP_LUA_SCOPE_THREAD
Definition lua_vmprep.h:42
#define AP_LUA_SCOPE_CONN
Definition lua_vmprep.h:41
#define AP_LUA_SCOPE_REQUEST
Definition lua_vmprep.h:40
#define AP_LUA_SCOPE_ONCE
Definition lua_vmprep.h:39
#define luaL_setfuncs_compat(a, b)
Definition mod_lua.h:64
return NULL
Definition mod_so.c:359
char * name
apr_array_header_t * mapped_handlers
Definition mod_lua.h:122
apr_pool_t * pool
Definition mod_lua.h:125
const char * dir
Definition mod_lua.h:138