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
)
24 luaL_checkudata(L
, index
, "Apache2.DirConfig");
25 cfg
= (apl_dir_cfg
*) lua_unboxpointer(L
, index
);
29 static cmd_parms
*check_cmd_parms(lua_State
*L
, int index
)
32 luaL_checkudata(L
, index
, "Apache2.CommandParameters");
33 cmd
= (cmd_parms
*) lua_unboxpointer(L
, index
);
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
,
55 const char *pattern
, const char *scope
)
57 ap_regex_t
*uri_pattern
;
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
) {
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
) =
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
);
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'",
104 handler
->uri_pattern
= uri_pattern
;
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
);
114 handler
->scope
= APL_SCOPE_ONCE
;
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
);
124 handler
->function_name
= "handle";
129 *(const apl_mapped_handler_spec
**) apr_array_push(cfg
->mapped_handlers
) =
134 static int cfg_directory(lua_State
*L
)
136 apl_dir_cfg
*cfg
= check_dir_config(L
, 1);
137 lua_pushstring(L
, cfg
->dir
);
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);
147 static const struct luaL_Reg cfg_methods
[] = {
148 {"match_handler", cfg_lua_map_handler
},
149 {"directory", cfg_directory
},
150 /* {"root", cfg_root}, */
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!");
163 /* helper function for the logging functions below */
164 static int cmd_log_at(lua_State
*L
, int level
)
167 cmd_parms
*cmd
= check_cmd_parms(L
, 1);
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
);
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
);
184 static int cmd_alert(lua_State
*L
)
186 cmd_log_at(L
, APLOG_ALERT
);
189 static int cmd_crit(lua_State
*L
)
191 cmd_log_at(L
, APLOG_CRIT
);
194 static int cmd_err(lua_State
*L
)
196 cmd_log_at(L
, APLOG_ERR
);
199 static int cmd_warn(lua_State
*L
)
201 cmd_log_at(L
, APLOG_WARNING
);
204 static int cmd_notice(lua_State
*L
)
206 cmd_log_at(L
, APLOG_NOTICE
);
209 static int cmd_info(lua_State
*L
)
211 cmd_log_at(L
, APLOG_INFO
);
214 static int cmd_debug(lua_State
*L
)
216 cmd_log_at(L
, APLOG_DEBUG
);
221 static const struct luaL_Reg cmd_methods
[] = {
224 {"debug", cmd_debug
},
226 {"notice", cmd_notice
},
230 {"alert", cmd_alert
},
231 {"emerg", cmd_emerg
},
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] */