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.
24 #include "lua_config.h"
26 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
27 (lua_State *L, apr_pool_t *p),
30 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
31 (lua_State *L, request_rec *r),
34 module AP_MODULE_DECLARE_DATA lua_module;
37 * error reporting if lua has an error.
38 * Extracts the error from lua stack and prints
40 static void report_lua_error(lua_State *L, request_rec *r)
42 const char *lua_response;
43 r->status = HTTP_INTERNAL_SERVER_ERROR;
44 r->content_type = "text/html";
46 ap_rputs("<b>Error!</b>\n", r);
48 lua_response = lua_tostring(L, -1);
49 ap_rputs(lua_response, r);
50 ap_rputs("</p>\n", r);
52 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, "Lua error: %s",
56 static void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
59 ap_lua_load_apache2_lmodule(L);
60 ap_lua_load_request_lmodule(L, p);
61 ap_lua_load_config_lmodule(L);
64 static int lua_open_hook(lua_State *L, apr_pool_t *p)
66 lua_open_callback(L, p, NULL);
71 static apr_status_t luahood(ap_filter_t *f, apr_bucket_brigade *bb) {
74 for ( b = APR_BRIGADE_FIRST(bb);
75 b != APR_BRIGADE_SENTINEL(bb);
76 b = APR_BUCKET_NEXT(b))
78 if (APR_BUCKET_IS_EOS(b)) {kl
83 if (( rs = apr_bucket_read(b, &buffer, &bytes, APR_BLOCK_READ))) {
84 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rs, f->r, "read failure in luahood");
87 char *mine = apr_pstrmemdup(f->r->pool, buffer, bytes);
88 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "sending '%s'", mine);
91 ap_pass_brigade(f->next, bb);
100 static int lua_handler(request_rec *r)
102 ap_lua_dir_cfg *dcfg;
103 if (strcmp(r->handler, "lua-script")) {
107 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "handling [%s] in mod_lua",
109 dcfg = ap_get_module_config(r->per_dir_config, &lua_module);
111 if (!r->header_only) {
113 const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
115 ap_lua_request_cfg *rcfg =
116 ap_get_module_config(r->request_config, &lua_module);
117 mapped_request_details *d = rcfg->mapped_request_details;
118 ap_lua_vm_spec *spec = NULL;
121 d = apr_palloc(r->pool, sizeof(mapped_request_details));
122 spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
123 spec->scope = dcfg->vm_scope;
124 spec->pool = spec->scope==APL_SCOPE_SERVER ? cfg->pool : r->pool;
125 spec->file = r->filename;
126 spec->code_cache_style = dcfg->code_cache_style;
127 spec->package_paths = cfg->package_paths;
128 spec->package_cpaths = cfg->package_cpaths;
129 spec->vm_server_pool_min = cfg->vm_server_pool_min;
130 spec->vm_server_pool_max = cfg->vm_server_pool_max;
131 spec->cb = &lua_open_callback;
134 d->function_name = "handle";
137 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
138 "request details scope:%u, cache:%u, filename:%s, function:%s",
140 d->spec->code_cache_style,
143 L = ap_lua_get_lua_state(r->pool,
147 /* TODO annotate spec with failure reason */
148 r->status = HTTP_INTERNAL_SERVER_ERROR;
149 ap_rputs("Unable to compile VM, see logs", r);
150 return HTTP_INTERNAL_SERVER_ERROR;
152 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got a vm!");
153 lua_getglobal(L, d->function_name);
154 if (!lua_isfunction(L, -1)) {
155 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
156 "lua: Unable to find function %s in %s",
159 return HTTP_INTERNAL_SERVER_ERROR;
161 ap_lua_run_lua_request(L, r);
162 if (lua_pcall(L, 1, 0, 0)) {
163 report_lua_error(L, r);
172 * Like mod_alias except for lua handler fun :-)
174 static int lua_alias_munger(request_rec *r)
176 ap_lua_vm_spec *spec;
177 ap_lua_request_cfg *rcfg = ap_get_module_config(r->request_config,
179 const ap_lua_dir_cfg *cfg =
180 ap_get_module_config(r->per_dir_config, &lua_module);
182 ap_regmatch_t matches[AP_MAX_REG_MATCH];
184 for (i = 0; i < cfg->mapped_handlers->nelts; i++) {
185 const ap_lua_mapped_handler_spec *cnd =
186 ((const ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[i];
189 ap_regexec(cnd->uri_pattern, r->uri, AP_MAX_REG_MATCH, matches,
191 mapped_request_details *d;
192 r->handler = "lua-script";
194 spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
196 ap_pregsub(r->pool, cnd->file_name, r->uri, AP_MAX_REG_MATCH,
198 spec->scope = cnd->scope;
199 spec->code_cache_style = cnd->code_cache_style;
200 spec->bytecode = cnd->bytecode;
201 spec->bytecode_len = cnd->bytecode_len;
202 if (spec->scope == APL_SCOPE_ONCE) {
203 spec->pool = r->pool;
206 d = apr_palloc(r->pool, sizeof(mapped_request_details));
209 ap_pregsub(r->pool, cnd->function_name, r->uri,
210 AP_MAX_REG_MATCH, matches);
213 /* now do replacement on method name where? */
214 r->filename = apr_pstrdup(r->pool, spec->file);
215 rcfg->mapped_request_details = d;
222 /* ---------------- Configury stuff --------------- */
224 /** harnesses for magic hooks **/
226 static int lua_request_rec_hook_harness(request_rec *r, const char *name)
230 ap_lua_vm_spec *spec;
231 ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
233 const ap_lua_dir_cfg *cfg =
234 (ap_lua_dir_cfg *) ap_get_module_config(r->per_dir_config,
236 apr_array_header_t *hook_specs =
237 apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
240 for (i = 0; i < hook_specs->nelts; i++) {
241 ap_lua_mapped_handler_spec *hook_spec =
242 ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
244 if (hook_spec == NULL) {
247 spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
249 spec->file = hook_spec->file_name;
250 spec->code_cache_style = hook_spec->code_cache_style;
251 spec->scope = hook_spec->scope;
252 spec->vm_server_pool_min = cfg->vm_server_pool_min;
253 spec->vm_server_pool_max = cfg->vm_server_pool_max;
254 spec->bytecode = hook_spec->bytecode;
255 spec->bytecode_len = hook_spec->bytecode_len;
256 spec->pool = spec->scope==APL_SCOPE_SERVER ? cfg->pool : r->pool;
257 spec->package_paths = cfg->package_paths;
258 spec->package_cpaths = cfg->package_cpaths;
259 spec->cb = &lua_open_callback;
262 apr_filepath_merge(&spec->file, server_cfg->root_path,
263 spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
264 L = ap_lua_get_lua_state(r->pool,
270 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
271 "lua: Failed to obtain lua interpreter for %s %s",
272 hook_spec->function_name, hook_spec->file_name);
273 return HTTP_INTERNAL_SERVER_ERROR;
276 if (hook_spec->function_name != NULL) {
277 lua_getglobal(L, hook_spec->function_name);
278 if (!lua_isfunction(L, -1)) {
279 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
280 "lua: Unable to find function %s in %s",
281 hook_spec->function_name,
282 hook_spec->file_name);
283 return HTTP_INTERNAL_SERVER_ERROR;
286 ap_lua_run_lua_request(L, r);
290 ap_lua_run_lua_request(L, r);
293 lua_setglobal(L, "r");
297 if (lua_pcall(L, 1, 1, 0)) {
298 report_lua_error(L, r);
299 return HTTP_INTERNAL_SERVER_ERROR;
302 if (lua_isnumber(L, -1)) {
303 rc = lua_tointeger(L, -1);
305 if (rc != DECLINED) {
314 static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
320 const char *res = (cfg->getstr) (buf, bufsiz, cfg->param);
323 if (i && buf[i - 1] == '\n')
333 int ch = (cfg->getch) (cfg->param);
346 typedef struct cr_ctx
349 ap_configfile_t *cfp;
352 char buf[HUGE_STRING_LEN];
356 /* Okay, this deserves a little explaination -- in order for the errors that lua
357 * generates to be 'accuarate', including line numbers, we basically inject
358 * N line number new lines into the 'top' of the chunk reader.....
360 * be happy. this is cool.
363 static const char *lf =
364 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
367 static const char *direct_chunkreader(lua_State *lvm, void *udata,
371 struct cr_ctx *ctx = udata;
373 if (ctx->startline) {
374 *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
375 ctx->startline -= *plen;
378 *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
380 for (p = ctx->buf; isspace(*p); ++p);
381 if (p[0] == '<' && p[1] == '/') {
383 while (i < strlen(ctx->endstr)) {
384 if (tolower(p[i + 2]) != ctx->endstr[i])
391 /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
395 static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
398 luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
402 typedef struct hack_section_baton
405 ap_lua_mapped_handler_spec *spec;
406 } hack_section_baton;
408 /* You can be unhappy now.
412 * When you create a <Section handler in httpd, the only 'easy' way to create
413 * a directory context is to parse the section, and convert it into a 'normal'
414 * Configureation option, and then collapse the entire section, in memory,
415 * back into the parent section -- from which you can then get the new directive
416 * invoked.... anyways. evil. Rici taught me how to do this hack :-)
418 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
421 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
422 ap_directive_t *directive = cmd->directive;
423 hack_section_baton *baton = directive->data;
425 apr_array_header_t *hook_specs =
426 apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
429 apr_array_make(cmd->pool, 2, sizeof(ap_lua_mapped_handler_spec *));
430 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, baton->name),
431 APR_HASH_KEY_STRING, hook_specs);
434 baton->spec->scope = cfg->vm_scope;
436 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
441 static const char *register_named_block_function_hook(const char *name,
446 const char *function;
447 ap_lua_mapped_handler_spec *spec;
449 if (line && line[0] == '>') {
455 word = ap_getword_conf(cmd->temp_pool, &line);
456 wordlen = strlen(word);
457 if (wordlen == 0 || word[wordlen - 1] != '>') {
458 return apr_pstrcat(cmd->pool, cmd->directive->directive,
459 "> takes exactly one argument", NULL);
462 function = apr_pstrndup(cmd->pool, word, wordlen - 1);
466 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
474 ap_directive_t **current;
475 hack_section_baton *baton;
477 apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
479 apr_pstrcat(cmd->pool, cmd->config_file->name, ":", buf, NULL);
481 spec->function_name = (char *) function;
486 spec->code_cache_style = APL_CODE_CACHE_FOREVER;
489 tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
492 ctx.cfp = cmd->config_file;
493 ctx.startline = cmd->config_file->line_number;
495 /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
496 lvm = luaL_newstate();
500 rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
504 apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1),
511 luaL_buffinit(lvm, &b);
512 lua_dump(lvm, ldump_writer, &b);
514 spec->bytecode_len = lua_strlen(lvm, -1);
516 apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
523 /* Here, we have to replace our current config node for the next pass */
525 *current = apr_pcalloc(cmd->pool, sizeof(**current));
528 baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
532 (*current)->filename = cmd->config_file->name;
533 (*current)->line_num = cmd->config_file->line_number;
534 (*current)->directive =
535 apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
536 (*current)->args = NULL;
537 (*current)->data = baton;
543 static const char *register_named_file_function_hook(const char *name,
547 const char *function)
549 ap_lua_mapped_handler_spec *spec;
550 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
552 apr_array_header_t *hook_specs =
553 apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
556 apr_array_make(cmd->pool, 2, sizeof(ap_lua_mapped_handler_spec *));
557 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, name),
558 APR_HASH_KEY_STRING, hook_specs);
561 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
562 spec->file_name = apr_pstrdup(cmd->pool, file);
563 spec->function_name = apr_pstrdup(cmd->pool, function);
564 spec->scope = cfg->vm_scope;
565 spec->code_cache_style = APL_CODE_CACHE_STAT;
567 int code_cache_style;
572 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
576 static int lua_check_user_id_harness(request_rec *r)
578 return lua_request_rec_hook_harness(r, "check_user_id");
581 static int lua_translate_name_harness(request_rec *r)
583 return lua_request_rec_hook_harness(r, "translate_name");
586 static int lua_fixup_harness(request_rec *r)
588 return lua_request_rec_hook_harness(r, "fixups");
591 static int lua_map_to_storage_harness(request_rec *r)
593 return lua_request_rec_hook_harness(r, "map_to_storage");
596 static int lua_type_checker_harness(request_rec *r)
598 return lua_request_rec_hook_harness(r, "type_checker");
601 static int lua_access_checker_harness(request_rec *r)
603 return lua_request_rec_hook_harness(r, "access_checker");
606 static int lua_auth_checker_harness(request_rec *r)
608 return lua_request_rec_hook_harness(r, "auth_checker");
611 static void lua_insert_filter_harness(request_rec *r)
613 /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
616 static int lua_quick_harness(request_rec *r, int lookup)
621 return lua_request_rec_hook_harness(r, "quick");
624 static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
626 const char *function)
628 return register_named_file_function_hook("translate_name", cmd, _cfg,
632 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
635 return register_named_block_function_hook("translate_name", cmd, _cfg,
640 static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
642 const char *function)
644 return register_named_file_function_hook("fixups", cmd, _cfg, file,
647 static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
650 return register_named_block_function_hook("fixups", cmd, _cfg, line);
653 static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
655 const char *function)
657 return register_named_file_function_hook("map_to_storage", cmd, _cfg,
660 static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
663 return register_named_block_function_hook("map_to_storage", cmd, _cfg,
667 static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
669 const char *function)
671 return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
674 static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
677 return register_named_block_function_hook("check_user_id", cmd, _cfg,
681 static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
683 const char *function)
685 return register_named_file_function_hook("type_checker", cmd, _cfg, file,
688 static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
691 return register_named_block_function_hook("type_checker", cmd, _cfg,
695 static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
697 const char *function)
699 return register_named_file_function_hook("access_checker", cmd, _cfg,
702 static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
705 return register_named_block_function_hook("access_checker", cmd, _cfg,
709 static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
711 const char *function)
713 return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
716 static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
719 return register_named_block_function_hook("auth_checker", cmd, _cfg,
723 static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
725 const char *function)
727 return "LuaHookInsertFilter not yet implemented";
730 static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
731 const char *file, const char *function)
733 return register_named_file_function_hook("quick", cmd, _cfg, file,
736 static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
739 return "LuaQuickHook in an inline block not yet implemented";
744 static const char *register_package_helper(cmd_parms *cmd, const char *arg,
745 apr_array_header_t *dir_array)
749 ap_lua_server_cfg *server_cfg =
750 ap_get_module_config(cmd->server->module_config, &lua_module);
751 char *fixed_filename;
752 rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, arg,
753 APR_FILEPATH_NOTRELATIVE, cmd->pool);
754 if (rv != APR_SUCCESS) {
755 return apr_psprintf(cmd->pool,
756 "Unable to build full path to file, %s", arg);
759 *(const char **) apr_array_push(dir_array) = fixed_filename;
765 * Called for config directive which looks like
766 * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
768 static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
771 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
773 return register_package_helper(cmd, arg, cfg->package_paths);
777 * Called for config directive which looks like
778 * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
780 static const char *register_package_cdir(cmd_parms *cmd, void *_cfg,
783 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
785 return register_package_helper(cmd, arg, cfg->package_cpaths);
789 * Called for config directive which looks like
792 static const char *register_code_cache(cmd_parms *cmd, void *_cfg,
795 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
796 if (strcmp("stat", arg) == 0) {
797 cfg->code_cache_style = APL_CODE_CACHE_STAT;
799 else if (strcmp("forever", arg) == 0) {
800 cfg->code_cache_style = APL_CODE_CACHE_FOREVER;
802 else if (strcmp("never", arg) == 0) {
803 cfg->code_cache_style = APL_CODE_CACHE_NEVER;
806 return apr_psprintf(cmd->pool,
807 "Invalid value for LuaCodeCache, '%s', acceptable values are %s",
808 arg, "'stat', 'forever', and 'never'");
813 static const char *register_lua_scope(cmd_parms *cmd, void *_cfg,
814 const char *scope, const char *min,
817 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
818 if (strcmp("once", scope) == 0) {
819 cfg->vm_scope = APL_SCOPE_ONCE;
821 else if (strcmp("request", scope) == 0) {
822 cfg->vm_scope = APL_SCOPE_REQUEST;
824 else if (strcmp("conn", scope) == 0) {
825 cfg->vm_scope = APL_SCOPE_CONN;
827 else if (strcmp("server", scope) == 0) {
828 cfg->vm_scope = APL_SCOPE_SERVER;
830 cfg->vm_server_pool_min = atoi(min);
832 cfg->vm_server_pool_max = atoi(max);
835 return apr_psprintf(cmd->pool,
836 "Invalid value for LuaScope, '%s', acceptable values are %s",
837 scope, "'once', 'request', 'conn', and 'server'");
844 * Called for config directive which looks like
845 * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
847 static const char *lua_map_handler(cmd_parms *cmd, void *_cfg,
848 const char *path, const char *file,
849 const char *function)
851 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
853 const char *function_name;
854 function_name = function ? function : "handle";
855 rv = ap_lua_map_handler(cfg, file, function_name, path, "once");
856 if (rv != APR_SUCCESS) {
857 return apr_psprintf(cmd->pool,
858 "Unable to configure a lua handler for path '%s', handler %s#%s",
859 path, file, function_name);
864 static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
867 /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
868 ap_lua_server_cfg *cfg =
869 ap_get_module_config(cmd->server->module_config, &lua_module);
871 cfg->root_path = root;
875 /*******************************/
877 command_rec lua_commands[] = {
879 AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
880 "Specify the base path for resolving relative paths for mod_lua directives"),
883 AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
884 "Add a directory to lua's package.path"),
886 AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
887 "Add a directory to lua's package.cpath"),
889 AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL,
890 "Map a path to a lua handler"),
892 AP_INIT_TAKE2("LuaHookTranslateName", register_translate_name_hook, NULL,
894 "Provide a hook for the translate name phase of request processing"),
895 AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
897 EXEC_ON_READ | OR_ALL,
898 "Provide a hook for the translate name phase of request processing"),
900 AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
901 "Provide a hook for the fixups phase of request processing"),
902 AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
903 EXEC_ON_READ | OR_ALL,
904 "Provide a inline hook for the fixups phase of request processing"),
907 AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
909 "Provide a hook for the map_to_storage phase of request processing"),
910 AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
912 EXEC_ON_READ | OR_ALL,
913 "Provide a hook for the map_to_storage phase of request processing"),
916 AP_INIT_TAKE2("LuaHookCheckUserID", register_check_user_id_hook, NULL,
918 "Provide a hook for the check_user_id phase of request processing"),
919 AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
921 EXEC_ON_READ | OR_ALL,
922 "Provide a hook for the check_user_id phase of request processing"),
925 AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
927 "Provide a hook for the type_checker phase of request processing"),
928 AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
929 EXEC_ON_READ | OR_ALL,
930 "Provide a hook for the type_checker phase of request processing"),
933 AP_INIT_TAKE2("LuaHookAccessChecker", register_access_checker_hook, NULL,
935 "Provide a hook for the access_checker phase of request processing"),
936 AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block,
938 EXEC_ON_READ | OR_ALL,
939 "Provide a hook for the access_checker phase of request processing"),
942 AP_INIT_TAKE2("LuaHookAuthChecker", register_auth_checker_hook, NULL,
944 "Provide a hook for the auth_checker phase of request processing"),
945 AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
946 EXEC_ON_READ | OR_ALL,
947 "Provide a hook for the auth_checker phase of request processing"),
950 AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
952 "Provide a hook for the insert_filter phase of request processing"),
954 AP_INIT_TAKE1("LuaCodeCache", register_code_cache, NULL, OR_ALL,
955 "Configure the compiled code cache. \
956 Default is to stat the file each time, options are stat|forever|never"),
958 AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
959 "One of once, request, conn, server -- default is once"),
961 AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
962 "Provide a hook for the quick handler of request processing"),
963 AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
964 EXEC_ON_READ | OR_ALL,
965 "Provide a hook for the quick handler of request processing"),
967 AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
969 "(internal) Byte code handler"),
974 static void *create_dir_config(apr_pool_t *p, char *dir)
976 ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
977 cfg->package_paths = apr_array_make(p, 2, sizeof(char *));
978 cfg->package_cpaths = apr_array_make(p, 2, sizeof(char *));
979 cfg->mapped_handlers =
980 apr_array_make(p, 1, sizeof(ap_lua_mapped_handler_spec *));
981 cfg->code_cache_style = APL_CODE_CACHE_STAT;
983 cfg->hooks = apr_hash_make(p);
984 cfg->dir = apr_pstrdup(p, dir);
985 cfg->vm_scope = APL_SCOPE_ONCE;
989 static int create_request_config(request_rec *r)
991 ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg));
992 cfg->mapped_request_details = NULL;
993 cfg->request_scoped_vms = apr_hash_make(r->pool);
994 ap_set_module_config(r->request_config, &lua_module, cfg);
998 static void *create_server_config(apr_pool_t *p, server_rec *s)
1001 ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
1002 cfg->code_cache = apr_pcalloc(p, sizeof(ap_lua_code_cache));
1003 apr_thread_rwlock_create(&cfg->code_cache->compiled_files_lock, p);
1004 cfg->code_cache->compiled_files = apr_hash_make(p);
1005 cfg->vm_reslists = apr_hash_make(p);
1006 apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
1007 cfg->code_cache->pool = p;
1008 cfg->root_path = NULL;
1013 static int lua_request_hook(lua_State *L, request_rec *r)
1015 ap_lua_push_request(L, r);
1019 static void lua_register_hooks(apr_pool_t *p)
1021 /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
1022 ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
1023 ap_hook_create_request(create_request_config, NULL, NULL,
1026 /* http_request.h hooks */
1027 ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
1029 ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
1030 ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
1032 ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
1034 ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
1036 ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
1038 ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
1040 ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
1042 ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
1044 /* ap_hook_translate_name(lua_alias_munger, NULL, NULL, APR_HOOK_MIDDLE); */
1045 ap_hook_translate_name(lua_alias_munger, NULL, NULL, APR_HOOK_MIDDLE);
1047 APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
1048 APR_HOOK_REALLY_FIRST);
1050 APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
1051 APR_HOOK_REALLY_FIRST);
1054 AP_DECLARE_MODULE(lua) = {
1055 STANDARD20_MODULE_STUFF,
1056 create_dir_config, /* create per-dir config structures */
1057 NULL, /* merge per-dir config structures */
1058 create_server_config, /* create per-server config structures */
1059 NULL, /* merge per-server config structures */
1060 lua_commands, /* table of config file commands */
1061 lua_register_hooks /* register hooks */