enabled block processing properly.
[httpd-crcsyncproxy.git] / modules / lua / lua_config.c
blob62e6757ad7375aeb171ad8f4c58241dcb6f9bbf9
1 /**
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 static apl_dir_cfg *check_dir_config(lua_State *L, int index)
23 apl_dir_cfg *cfg;
24 luaL_checkudata(L, index, "Apache2.DirConfig");
25 cfg = (apl_dir_cfg *) lua_unboxpointer(L, index);
26 return cfg;
29 static cmd_parms *check_cmd_parms(lua_State *L, int index)
31 cmd_parms *cmd;
32 luaL_checkudata(L, index, "Apache2.CommandParameters");
33 cmd = (cmd_parms *) lua_unboxpointer(L, index);
34 return cmd;
37 static int apl_toscope(const char *name)
39 if (0 == apr_strnatcmp("once", name))
40 return APL_SCOPE_ONCE;
41 if (0 == apr_strnatcmp("request", name))
42 return APL_SCOPE_REQUEST;
43 if (0 == apr_strnatcmp("connection", name))
44 return APL_SCOPE_CONN;
45 if (0 == apr_strnatcmp("conn", name))
46 return APL_SCOPE_CONN;
47 if (0 == apr_strnatcmp("server", name))
48 return APL_SCOPE_SERVER;
49 return APL_SCOPE_ONCE;
52 apr_status_t apl_lua_map_handler(apl_dir_cfg *cfg,
53 const char *file,
54 const char *function,
55 const char *pattern, const char *scope)
57 ap_regex_t *uri_pattern;
58 apr_status_t rv;
59 apl_mapped_handler_spec *handler =
60 apr_palloc(cfg->pool, sizeof(apl_mapped_handler_spec));
61 handler->uri_pattern = NULL;
62 handler->function_name = NULL;
64 uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
65 if ((rv = ap_regcomp(uri_pattern, pattern, 0)) != APR_SUCCESS) {
66 return rv;
68 handler->file_name = apr_pstrdup(cfg->pool, file);
69 handler->uri_pattern = uri_pattern;
70 handler->scope = apl_toscope(scope);
72 handler->function_name = apr_pstrdup(cfg->pool, function);
73 *(const apl_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
74 handler;
75 return APR_SUCCESS;
78 /* Change to use apl_lua_map_handler */
79 static int cfg_lua_map_handler(lua_State *L)
81 apl_dir_cfg *cfg = check_dir_config(L, 1);
82 apl_mapped_handler_spec *handler =
83 apr_palloc(cfg->pool, sizeof(apl_mapped_handler_spec));
84 handler->uri_pattern = NULL;
85 handler->function_name = NULL;
87 luaL_checktype(L, 2, LUA_TTABLE);
88 lua_getfield(L, 2, "file");
89 if (lua_isstring(L, -1)) {
90 const char *file = lua_tostring(L, -1);
91 handler->file_name = apr_pstrdup(cfg->pool, file);
93 lua_pop(L, 1);
95 lua_getfield(L, 2, "pattern");
96 if (lua_isstring(L, -1)) {
97 const char *pattern = lua_tostring(L, -1);
99 ap_regex_t *uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
100 if (ap_regcomp(uri_pattern, pattern, 0) != OK) {
101 return luaL_error(L, "Unable to compile regular expression, '%s'",
102 pattern);
104 handler->uri_pattern = uri_pattern;
106 lua_pop(L, 1);
108 lua_getfield(L, 2, "scope");
109 if (lua_isstring(L, -1)) {
110 const char *scope = lua_tostring(L, -1);
111 handler->scope = apl_toscope(scope);
113 else {
114 handler->scope = APL_SCOPE_ONCE;
116 lua_pop(L, 1);
118 lua_getfield(L, 2, "func");
119 if (lua_isstring(L, -1)) {
120 const char *value = lua_tostring(L, -1);
121 handler->function_name = apr_pstrdup(cfg->pool, value);
123 else {
124 handler->function_name = "handle";
126 lua_pop(L, 1);
129 *(const apl_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
130 handler;
131 return 0;
134 static int cfg_directory(lua_State *L)
136 apl_dir_cfg *cfg = check_dir_config(L, 1);
137 lua_pushstring(L, cfg->dir);
138 return 1;
141 /*static int cfg_root(lua_State *L) {
142 apl_dir_cfg *cfg = check_dir_config(L, 1);
143 lua_pushstring(L, cfg->root_path);
144 return 1;
147 static const struct luaL_Reg cfg_methods[] = {
148 {"match_handler", cfg_lua_map_handler},
149 {"directory", cfg_directory},
150 /* {"root", cfg_root}, */
151 {NULL, NULL}
156 static int cmd_foo(lua_State *L)
158 cmd_parms *cmd = check_cmd_parms(L, 1);
159 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server, "FOO!");
160 return 0;
163 /* helper function for the logging functions below */
164 static int cmd_log_at(lua_State *L, int level)
166 const char *msg;
167 cmd_parms *cmd = check_cmd_parms(L, 1);
168 lua_Debug dbg;
170 lua_getstack(L, 1, &dbg);
171 lua_getinfo(L, "Sl", &dbg);
173 msg = luaL_checkstring(L, 2);
174 ap_log_error(dbg.source, dbg.currentline, level, 0, cmd->server, msg);
175 return 0;
178 /* r:debug(String) and friends which use apache logging */
179 static int cmd_emerg(lua_State *L)
181 cmd_log_at(L, APLOG_EMERG);
182 return 0;
184 static int cmd_alert(lua_State *L)
186 cmd_log_at(L, APLOG_ALERT);
187 return 0;
189 static int cmd_crit(lua_State *L)
191 cmd_log_at(L, APLOG_CRIT);
192 return 0;
194 static int cmd_err(lua_State *L)
196 cmd_log_at(L, APLOG_ERR);
197 return 0;
199 static int cmd_warn(lua_State *L)
201 cmd_log_at(L, APLOG_WARNING);
202 return 0;
204 static int cmd_notice(lua_State *L)
206 cmd_log_at(L, APLOG_NOTICE);
207 return 0;
209 static int cmd_info(lua_State *L)
211 cmd_log_at(L, APLOG_INFO);
212 return 0;
214 static int cmd_debug(lua_State *L)
216 cmd_log_at(L, APLOG_DEBUG);
217 return 0;
221 static const struct luaL_Reg cmd_methods[] = {
222 {"foo", cmd_foo},
224 {"debug", cmd_debug},
225 {"info", cmd_info},
226 {"notice", cmd_notice},
227 {"warn", cmd_warn},
228 {"err", cmd_err},
229 {"crit", cmd_crit},
230 {"alert", cmd_alert},
231 {"emerg", cmd_emerg},
233 {NULL, NULL}
236 void apl_load_config_lmodule(lua_State *L)
238 luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */
239 lua_pushvalue(L, -1);
241 lua_setfield(L, -2, "__index");
242 luaL_register(L, NULL, cfg_methods); /* [metatable] */
245 luaL_newmetatable(L, "Apache2.CommandParameters");
246 lua_pushvalue(L, -1);
248 lua_setfield(L, -2, "__index");
249 luaL_register(L, NULL, cmd_methods); /* [metatable] */