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 apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
321 if (rc == APR_SUCCESS) {
323 if (i && buf[i - 1] == '\n')
334 apr_status_t rc = (cfg->getch) (&ch, cfg->param);
335 if (rc != APR_SUCCESS)
347 typedef struct cr_ctx
350 ap_configfile_t *cfp;
353 char buf[HUGE_STRING_LEN];
357 /* Okay, this deserves a little explaination -- in order for the errors that lua
358 * generates to be 'accuarate', including line numbers, we basically inject
359 * N line number new lines into the 'top' of the chunk reader.....
361 * be happy. this is cool.
364 static const char *lf =
365 "\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";
368 static const char *direct_chunkreader(lua_State *lvm, void *udata,
372 struct cr_ctx *ctx = udata;
374 if (ctx->startline) {
375 *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
376 ctx->startline -= *plen;
379 *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
381 for (p = ctx->buf; isspace(*p); ++p);
382 if (p[0] == '<' && p[1] == '/') {
384 while (i < strlen(ctx->endstr)) {
385 if (tolower(p[i + 2]) != ctx->endstr[i])
392 /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
396 static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
399 luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
403 typedef struct hack_section_baton
406 ap_lua_mapped_handler_spec *spec;
407 } hack_section_baton;
409 /* You can be unhappy now.
413 * When you create a <Section handler in httpd, the only 'easy' way to create
414 * a directory context is to parse the section, and convert it into a 'normal'
415 * Configureation option, and then collapse the entire section, in memory,
416 * back into the parent section -- from which you can then get the new directive
417 * invoked.... anyways. evil. Rici taught me how to do this hack :-)
419 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
422 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
423 ap_directive_t *directive = cmd->directive;
424 hack_section_baton *baton = directive->data;
426 apr_array_header_t *hook_specs =
427 apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
430 apr_array_make(cmd->pool, 2, sizeof(ap_lua_mapped_handler_spec *));
431 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, baton->name),
432 APR_HASH_KEY_STRING, hook_specs);
435 baton->spec->scope = cfg->vm_scope;
437 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
442 static const char *register_named_block_function_hook(const char *name,
447 const char *function;
448 ap_lua_mapped_handler_spec *spec;
450 if (line && line[0] == '>') {
456 word = ap_getword_conf(cmd->temp_pool, &line);
457 wordlen = strlen(word);
458 if (wordlen == 0 || word[wordlen - 1] != '>') {
459 return apr_pstrcat(cmd->pool, cmd->directive->directive,
460 "> takes exactly one argument", NULL);
463 function = apr_pstrndup(cmd->pool, word, wordlen - 1);
467 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
475 ap_directive_t **current;
476 hack_section_baton *baton;
478 apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
480 apr_pstrcat(cmd->pool, cmd->config_file->name, ":", buf, NULL);
482 spec->function_name = (char *) function;
487 spec->code_cache_style = APL_CODE_CACHE_FOREVER;
490 tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
493 ctx.cfp = cmd->config_file;
494 ctx.startline = cmd->config_file->line_number;
496 /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
497 lvm = luaL_newstate();
501 rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
505 apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1),
512 luaL_buffinit(lvm, &b);
513 lua_dump(lvm, ldump_writer, &b);
515 spec->bytecode_len = lua_strlen(lvm, -1);
517 apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
524 /* Here, we have to replace our current config node for the next pass */
526 *current = apr_pcalloc(cmd->pool, sizeof(**current));
529 baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
533 (*current)->filename = cmd->config_file->name;
534 (*current)->line_num = cmd->config_file->line_number;
535 (*current)->directive =
536 apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
537 (*current)->args = NULL;
538 (*current)->data = baton;
544 static const char *register_named_file_function_hook(const char *name,
548 const char *function)
550 ap_lua_mapped_handler_spec *spec;
551 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
553 apr_array_header_t *hook_specs =
554 apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
557 apr_array_make(cmd->pool, 2, sizeof(ap_lua_mapped_handler_spec *));
558 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, name),
559 APR_HASH_KEY_STRING, hook_specs);
562 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
563 spec->file_name = apr_pstrdup(cmd->pool, file);
564 spec->function_name = apr_pstrdup(cmd->pool, function);
565 spec->scope = cfg->vm_scope;
566 spec->code_cache_style = APL_CODE_CACHE_STAT;
568 int code_cache_style;
573 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
577 static int lua_check_user_id_harness(request_rec *r)
579 return lua_request_rec_hook_harness(r, "check_user_id");
582 static int lua_translate_name_harness(request_rec *r)
584 return lua_request_rec_hook_harness(r, "translate_name");
587 static int lua_fixup_harness(request_rec *r)
589 return lua_request_rec_hook_harness(r, "fixups");
592 static int lua_map_to_storage_harness(request_rec *r)
594 return lua_request_rec_hook_harness(r, "map_to_storage");
597 static int lua_type_checker_harness(request_rec *r)
599 return lua_request_rec_hook_harness(r, "type_checker");
602 static int lua_access_checker_harness(request_rec *r)
604 return lua_request_rec_hook_harness(r, "access_checker");
607 static int lua_auth_checker_harness(request_rec *r)
609 return lua_request_rec_hook_harness(r, "auth_checker");
612 static void lua_insert_filter_harness(request_rec *r)
614 /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
617 static int lua_quick_harness(request_rec *r, int lookup)
622 return lua_request_rec_hook_harness(r, "quick");
625 static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
627 const char *function)
629 return register_named_file_function_hook("translate_name", cmd, _cfg,
633 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
636 return register_named_block_function_hook("translate_name", cmd, _cfg,
641 static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
643 const char *function)
645 return register_named_file_function_hook("fixups", cmd, _cfg, file,
648 static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
651 return register_named_block_function_hook("fixups", cmd, _cfg, line);
654 static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
656 const char *function)
658 return register_named_file_function_hook("map_to_storage", cmd, _cfg,
661 static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
664 return register_named_block_function_hook("map_to_storage", cmd, _cfg,
668 static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
670 const char *function)
672 return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
675 static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
678 return register_named_block_function_hook("check_user_id", cmd, _cfg,
682 static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
684 const char *function)
686 return register_named_file_function_hook("type_checker", cmd, _cfg, file,
689 static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
692 return register_named_block_function_hook("type_checker", cmd, _cfg,
696 static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
698 const char *function)
700 return register_named_file_function_hook("access_checker", cmd, _cfg,
703 static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
706 return register_named_block_function_hook("access_checker", cmd, _cfg,
710 static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
712 const char *function)
714 return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
717 static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
720 return register_named_block_function_hook("auth_checker", cmd, _cfg,
724 static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
726 const char *function)
728 return "LuaHookInsertFilter not yet implemented";
731 static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
732 const char *file, const char *function)
734 return register_named_file_function_hook("quick", cmd, _cfg, file,
737 static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
740 return "LuaQuickHook in an inline block not yet implemented";
745 static const char *register_package_helper(cmd_parms *cmd, const char *arg,
746 apr_array_header_t *dir_array)
750 ap_lua_server_cfg *server_cfg =
751 ap_get_module_config(cmd->server->module_config, &lua_module);
752 char *fixed_filename;
753 rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, arg,
754 APR_FILEPATH_NOTRELATIVE, cmd->pool);
755 if (rv != APR_SUCCESS) {
756 return apr_psprintf(cmd->pool,
757 "Unable to build full path to file, %s", arg);
760 *(const char **) apr_array_push(dir_array) = fixed_filename;
766 * Called for config directive which looks like
767 * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
769 static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
772 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
774 return register_package_helper(cmd, arg, cfg->package_paths);
778 * Called for config directive which looks like
779 * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
781 static const char *register_package_cdir(cmd_parms *cmd, void *_cfg,
784 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
786 return register_package_helper(cmd, arg, cfg->package_cpaths);
790 * Called for config directive which looks like
793 static const char *register_code_cache(cmd_parms *cmd, void *_cfg,
796 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
797 if (strcmp("stat", arg) == 0) {
798 cfg->code_cache_style = APL_CODE_CACHE_STAT;
800 else if (strcmp("forever", arg) == 0) {
801 cfg->code_cache_style = APL_CODE_CACHE_FOREVER;
803 else if (strcmp("never", arg) == 0) {
804 cfg->code_cache_style = APL_CODE_CACHE_NEVER;
807 return apr_psprintf(cmd->pool,
808 "Invalid value for LuaCodeCache, '%s', acceptable values are %s",
809 arg, "'stat', 'forever', and 'never'");
814 static const char *register_lua_scope(cmd_parms *cmd, void *_cfg,
815 const char *scope, const char *min,
818 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
819 if (strcmp("once", scope) == 0) {
820 cfg->vm_scope = APL_SCOPE_ONCE;
822 else if (strcmp("request", scope) == 0) {
823 cfg->vm_scope = APL_SCOPE_REQUEST;
825 else if (strcmp("conn", scope) == 0) {
826 cfg->vm_scope = APL_SCOPE_CONN;
828 else if (strcmp("server", scope) == 0) {
829 cfg->vm_scope = APL_SCOPE_SERVER;
831 cfg->vm_server_pool_min = atoi(min);
833 cfg->vm_server_pool_max = atoi(max);
836 return apr_psprintf(cmd->pool,
837 "Invalid value for LuaScope, '%s', acceptable values are %s",
838 scope, "'once', 'request', 'conn', and 'server'");
845 * Called for config directive which looks like
846 * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
848 static const char *lua_map_handler(cmd_parms *cmd, void *_cfg,
849 const char *path, const char *file,
850 const char *function)
852 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
854 const char *function_name;
855 function_name = function ? function : "handle";
856 rv = ap_lua_map_handler(cfg, file, function_name, path, "once");
857 if (rv != APR_SUCCESS) {
858 return apr_psprintf(cmd->pool,
859 "Unable to configure a lua handler for path '%s', handler %s#%s",
860 path, file, function_name);
865 static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
868 /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
869 ap_lua_server_cfg *cfg =
870 ap_get_module_config(cmd->server->module_config, &lua_module);
872 cfg->root_path = root;
876 /*******************************/
878 command_rec lua_commands[] = {
880 AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
881 "Specify the base path for resolving relative paths for mod_lua directives"),
884 AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
885 "Add a directory to lua's package.path"),
887 AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
888 "Add a directory to lua's package.cpath"),
890 AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL,
891 "Map a path to a lua handler"),
893 AP_INIT_TAKE2("LuaHookTranslateName", register_translate_name_hook, NULL,
895 "Provide a hook for the translate name phase of request processing"),
896 AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
898 EXEC_ON_READ | OR_ALL,
899 "Provide a hook for the translate name phase of request processing"),
901 AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
902 "Provide a hook for the fixups phase of request processing"),
903 AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
904 EXEC_ON_READ | OR_ALL,
905 "Provide a inline hook for the fixups phase of request processing"),
908 AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
910 "Provide a hook for the map_to_storage phase of request processing"),
911 AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
913 EXEC_ON_READ | OR_ALL,
914 "Provide a hook for the map_to_storage phase of request processing"),
917 AP_INIT_TAKE2("LuaHookCheckUserID", register_check_user_id_hook, NULL,
919 "Provide a hook for the check_user_id phase of request processing"),
920 AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
922 EXEC_ON_READ | OR_ALL,
923 "Provide a hook for the check_user_id phase of request processing"),
926 AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
928 "Provide a hook for the type_checker phase of request processing"),
929 AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
930 EXEC_ON_READ | OR_ALL,
931 "Provide a hook for the type_checker phase of request processing"),
934 AP_INIT_TAKE2("LuaHookAccessChecker", register_access_checker_hook, NULL,
936 "Provide a hook for the access_checker phase of request processing"),
937 AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block,
939 EXEC_ON_READ | OR_ALL,
940 "Provide a hook for the access_checker phase of request processing"),
943 AP_INIT_TAKE2("LuaHookAuthChecker", register_auth_checker_hook, NULL,
945 "Provide a hook for the auth_checker phase of request processing"),
946 AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
947 EXEC_ON_READ | OR_ALL,
948 "Provide a hook for the auth_checker phase of request processing"),
951 AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
953 "Provide a hook for the insert_filter phase of request processing"),
955 AP_INIT_TAKE1("LuaCodeCache", register_code_cache, NULL, OR_ALL,
956 "Configure the compiled code cache. \
957 Default is to stat the file each time, options are stat|forever|never"),
959 AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
960 "One of once, request, conn, server -- default is once"),
962 AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
963 "Provide a hook for the quick handler of request processing"),
964 AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
965 EXEC_ON_READ | OR_ALL,
966 "Provide a hook for the quick handler of request processing"),
968 AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
970 "(internal) Byte code handler"),
975 static void *create_dir_config(apr_pool_t *p, char *dir)
977 ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
978 cfg->package_paths = apr_array_make(p, 2, sizeof(char *));
979 cfg->package_cpaths = apr_array_make(p, 2, sizeof(char *));
980 cfg->mapped_handlers =
981 apr_array_make(p, 1, sizeof(ap_lua_mapped_handler_spec *));
982 cfg->code_cache_style = APL_CODE_CACHE_STAT;
984 cfg->hooks = apr_hash_make(p);
985 cfg->dir = apr_pstrdup(p, dir);
986 cfg->vm_scope = APL_SCOPE_ONCE;
990 static int create_request_config(request_rec *r)
992 ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg));
993 cfg->mapped_request_details = NULL;
994 cfg->request_scoped_vms = apr_hash_make(r->pool);
995 ap_set_module_config(r->request_config, &lua_module, cfg);
999 static void *create_server_config(apr_pool_t *p, server_rec *s)
1002 ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
1003 cfg->code_cache = apr_pcalloc(p, sizeof(ap_lua_code_cache));
1004 apr_thread_rwlock_create(&cfg->code_cache->compiled_files_lock, p);
1005 cfg->code_cache->compiled_files = apr_hash_make(p);
1006 cfg->vm_reslists = apr_hash_make(p);
1007 apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
1008 cfg->code_cache->pool = p;
1009 cfg->root_path = NULL;
1014 static int lua_request_hook(lua_State *L, request_rec *r)
1016 ap_lua_push_request(L, r);
1020 static void lua_register_hooks(apr_pool_t *p)
1022 /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
1023 ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
1024 ap_hook_create_request(create_request_config, NULL, NULL,
1027 /* http_request.h hooks */
1028 ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
1030 ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
1031 ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
1033 ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
1035 ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
1037 ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
1039 ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
1041 ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
1043 ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
1045 /* ap_hook_translate_name(lua_alias_munger, NULL, NULL, APR_HOOK_MIDDLE); */
1046 ap_hook_translate_name(lua_alias_munger, NULL, NULL, APR_HOOK_MIDDLE);
1048 APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
1049 APR_HOOK_REALLY_FIRST);
1051 APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
1052 APR_HOOK_REALLY_FIRST);
1055 AP_DECLARE_MODULE(lua) = {
1056 STANDARD20_MODULE_STUFF,
1057 create_dir_config, /* create per-dir config structures */
1058 NULL, /* merge per-dir config structures */
1059 create_server_config, /* create per-server config structures */
1060 NULL, /* merge per-server config structures */
1061 lua_commands, /* table of config file commands */
1062 lua_register_hooks /* register hooks */