2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "lua_config.h"
19 #include "lua_vmprep.h"
21 APLOG_USE_MODULE(lua);
23 static ap_lua_dir_cfg *check_dir_config(lua_State *L, int index)
26 luaL_checkudata(L, index, "Apache2.DirConfig");
27 cfg = (ap_lua_dir_cfg *) lua_unboxpointer(L, index);
31 static cmd_parms *check_cmd_parms(lua_State *L, int index)
34 luaL_checkudata(L, index, "Apache2.CommandParameters");
35 cmd = (cmd_parms *) lua_unboxpointer(L, index);
39 static int apl_toscope(const char *name)
41 if (0 == strcmp("once", name))
42 return AP_LUA_SCOPE_ONCE;
43 if (0 == strcmp("request", name))
44 return AP_LUA_SCOPE_REQUEST;
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))
50 return AP_LUA_SCOPE_THREAD;
51 return AP_LUA_SCOPE_ONCE;
54 apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg,
60 ap_regex_t *uri_pattern;
62 ap_lua_mapped_handler_spec *handler =
63 apr_pcalloc(cfg->pool, sizeof(ap_lua_mapped_handler_spec));
64 handler->uri_pattern = NULL;
65 handler->function_name = NULL;
67 uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
68 if ((rv = ap_regcomp(uri_pattern, pattern, 0)) != APR_SUCCESS) {
71 handler->file_name = apr_pstrdup(cfg->pool, file);
72 handler->uri_pattern = uri_pattern;
73 handler->scope = apl_toscope(scope);
75 handler->function_name = apr_pstrdup(cfg->pool, function);
76 *(const ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
81 /* Change to use ap_lua_map_handler */
82 static int cfg_lua_map_handler(lua_State *L)
84 ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
85 ap_lua_mapped_handler_spec *handler =
86 apr_pcalloc(cfg->pool, sizeof(ap_lua_mapped_handler_spec));
87 handler->uri_pattern = NULL;
88 handler->function_name = NULL;
90 luaL_checktype(L, 2, LUA_TTABLE);
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);
98 lua_getfield(L, 2, "pattern");
99 if (lua_isstring(L, -1)) {
100 const char *pattern = lua_tostring(L, -1);
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'",
107 handler->uri_pattern = uri_pattern;
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);
117 handler->scope = AP_LUA_SCOPE_ONCE;
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);
127 handler->function_name = "handle";
132 *(const ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
137 static int cfg_directory(lua_State *L)
139 ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
140 lua_pushstring(L, cfg->dir);
144 /*static int cfg_root(lua_State *L)
146 ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
147 lua_pushstring(L, cfg->root_path);
151 static const struct luaL_Reg cfg_methods[] = {
152 {"match_handler", cfg_lua_map_handler},
153 {"directory", cfg_directory},
154 /* {"root", cfg_root}, */
158 /* helper function for the logging functions below */
159 static int cmd_log_at(lua_State *L, int level)
162 cmd_parms *cmd = check_cmd_parms(L, 1);
165 lua_getstack(L, 1, &dbg);
166 lua_getinfo(L, "Sl", &dbg);
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);
175 /* r:debug(String) and friends which use apache logging */
176 static int cmd_emerg(lua_State *L)
178 return cmd_log_at(L, APLOG_EMERG);
180 static int cmd_alert(lua_State *L)
182 return cmd_log_at(L, APLOG_ALERT);
184 static int cmd_crit(lua_State *L)
186 return cmd_log_at(L, APLOG_CRIT);
188 static int cmd_err(lua_State *L)
190 return cmd_log_at(L, APLOG_ERR);
192 static int cmd_warn(lua_State *L)
194 return cmd_log_at(L, APLOG_WARNING);
196 static int cmd_notice(lua_State *L)
198 return cmd_log_at(L, APLOG_NOTICE);
200 static int cmd_info(lua_State *L)
202 return cmd_log_at(L, APLOG_INFO);
204 static int cmd_debug(lua_State *L)
206 return cmd_log_at(L, APLOG_DEBUG);
208 static int cmd_trace1(lua_State *L)
210 return cmd_log_at(L, APLOG_TRACE1);
212 static int cmd_trace2(lua_State *L)
214 return cmd_log_at(L, APLOG_TRACE2);
216 static int cmd_trace3(lua_State *L)
218 return cmd_log_at(L, APLOG_TRACE3);
220 static int cmd_trace4(lua_State *L)
222 return cmd_log_at(L, APLOG_TRACE4);
224 static int cmd_trace5(lua_State *L)
226 return cmd_log_at(L, APLOG_TRACE5);
228 static int cmd_trace6(lua_State *L)
230 return cmd_log_at(L, APLOG_TRACE6);
232 static int cmd_trace7(lua_State *L)
234 return cmd_log_at(L, APLOG_TRACE7);
236 static int cmd_trace8(lua_State *L)
238 return cmd_log_at(L, APLOG_TRACE8);
241 static 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},
252 {"notice", cmd_notice},
256 {"alert", cmd_alert},
257 {"emerg", cmd_emerg},
262 void ap_lua_load_config_lmodule(lua_State *L)
264 luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */
265 lua_pushvalue(L, -1);
267 lua_setfield(L, -2, "__index");
268 luaL_register(L, NULL, cfg_methods); /* [metatable] */
271 luaL_newmetatable(L, "Apache2.CommandParameters");
272 lua_pushvalue(L, -1);
274 lua_setfield(L, -2, "__index");
275 luaL_register(L, NULL, cmd_methods); /* [metatable] */