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(apl, AP_LUA, int, lua_open,
27 (lua_State *L, apr_pool_t *p),
31 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(apl, AP_LUA, int, lua_request,
32 (lua_State *L, request_rec *r),
36 module AP_MODULE_DECLARE_DATA lua_module;
39 * error reporting if lua has an error.
40 * Extracts the error from lua stack and prints
42 static void report_lua_error(lua_State *L, request_rec *r) {
44 r->content_type = "text/html";
46 ap_rputs("<b>Error!</b>\n", r);
48 const char* 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", lua_response);
55 static void lua_open_callback(lua_State *L, apr_pool_t *p, void* ctx) {
57 apl_load_apache2_lmodule(L);
58 apl_load_request_lmodule(L, p);
59 apl_load_config_lmodule(L);
62 static int lua_open_hook(lua_State *L, apr_pool_t *p) {
63 lua_open_callback(L, p, NULL);
68 static apr_status_t luahood(ap_filter_t *f, apr_bucket_brigade *bb) {
71 for ( b = APR_BRIGADE_FIRST(bb);
72 b != APR_BRIGADE_SENTINEL(bb);
73 b = APR_BUCKET_NEXT(b))
75 if (APR_BUCKET_IS_EOS(b)) {kl
80 if (( rs = apr_bucket_read(b, &buffer, &bytes, APR_BLOCK_READ))) {
81 ap_log_rerror(APLOG_MARK, APLOG_WARNING, rs, f->r, "read failure in luahood");
84 char *mine = apr_pstrmemdup(f->r->pool, buffer, bytes);
85 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "sending '%s'", mine);
88 ap_pass_brigade(f->next, bb);
97 static int lua_handler(request_rec *r) {
98 if (strcmp(r->handler, "lua-script")) {
102 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "handling [%s] in mod_lua", r->filename);
103 apl_dir_cfg *dcfg = ap_get_module_config(r->per_dir_config, &lua_module);
105 if (!r->header_only) {
106 apl_request_cfg* rcfg = ap_get_module_config(r->request_config, &lua_module);
107 mapped_request_details *d = rcfg->mapped_request_details;
108 apl_vm_spec *spec = NULL;
110 d = apr_palloc(r->pool, sizeof(mapped_request_details));
111 spec = apr_pcalloc(r->pool, sizeof(apl_vm_spec));
112 spec->scope = dcfg->vm_scope;
113 spec->pool = r->pool;
114 spec->file = r->filename;
115 spec->code_cache_style = dcfg->code_cache_style;
117 d->function_name = "handle";
119 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "request details scope:%u, cache:%u",
121 d->spec->code_cache_style);
122 const apl_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &lua_module);
123 lua_State *L = apl_get_lua_state(r->pool,
127 &lua_open_callback, NULL);
129 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got a vm!");
131 /* TODO annotate spec with failure reason */
133 ap_rputs("Unable to compile VM, see logs", r);
135 lua_getglobal(L, d->function_name);
136 apl_run_lua_request(L, r);
137 if (lua_pcall(L, 1, 0, 0)) {
138 report_lua_error(L, r);
147 * Like mod_alias except for lua handler fun :-)
149 static int apl_alias_munger(request_rec *r) {
150 const apl_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, &lua_module);
153 ap_regmatch_t matches[AP_MAX_REG_MATCH];
155 for (i = 0; i < cfg->mapped_handlers->nelts; i++) {
156 const apl_mapped_handler_spec *cnd = ((const apl_mapped_handler_spec**)cfg->mapped_handlers->elts)[i];
157 if (OK == ap_regexec(cnd->uri_pattern, r->uri, AP_MAX_REG_MATCH, matches, 0)) {
158 r->handler = "lua-script";
160 apl_vm_spec *spec = apr_pcalloc(r->pool, sizeof(apl_vm_spec));
161 spec->file = ap_pregsub(r->pool, cnd->file_name, r->uri, AP_MAX_REG_MATCH, matches);
162 spec->scope = cnd->scope;
163 spec->code_cache_style = cnd->code_cache_style;
164 spec->bytecode = cnd->bytecode;
165 spec->bytecode_len = cnd->bytecode_len;
166 if (spec->scope == APL_SCOPE_ONCE) {
167 spec->pool = r->pool;
170 mapped_request_details *d = apr_palloc(r->pool, sizeof(mapped_request_details));
172 d->function_name = ap_pregsub(r->pool, cnd->function_name, r->uri, AP_MAX_REG_MATCH, matches);
175 /* now do replacement on method name where? */
176 r->filename = apr_pstrdup(r->pool, spec->file);
177 apl_request_cfg *rcfg = ap_get_module_config(r->request_config, &lua_module);
178 rcfg->mapped_request_details = d;
185 /* ---------------- Configury stuff --------------- */
187 /** harnesses for magic hooks **/
189 static int lua_request_rec_hook_harness(request_rec *r, const char *name) {
190 char *fixed_filename;
192 const apl_dir_cfg* cfg = (apl_dir_cfg*) ap_get_module_config(r->per_dir_config,
194 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
197 for (i=0; i < hook_specs->nelts; i++) {
198 apl_mapped_handler_spec *hook_spec = ((apl_mapped_handler_spec**)hook_specs->elts)[i];
199 if (hook_spec == NULL) continue;
200 apl_vm_spec *spec = apr_pcalloc(r->pool, sizeof(apl_vm_spec));
202 spec->file = hook_spec->file_name;
203 spec->code_cache_style = hook_spec->code_cache_style;
204 spec->scope = hook_spec->scope;
205 spec->bytecode = hook_spec->bytecode;
206 spec->bytecode_len = hook_spec->bytecode_len;
207 spec->pool = r->pool;
210 const apl_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &lua_module);
211 lua_State *L = apl_get_lua_state(r->pool,
215 &lua_open_callback, NULL);
217 apl_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, &lua_module);
218 apr_filepath_merge(&fixed_filename, server_cfg->root_path, spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
219 lua_State *L = apl_get_lua_state(r->pool,
223 &lua_open_callback, NULL);
228 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "lua: Failed to obtain lua interpreter for %s %s",
229 hook_spec->function_name,
230 hook_spec->file_name);
234 if (hook_spec->function_name != NULL) {
235 lua_getglobal(L, hook_spec->function_name);
236 if (!lua_isfunction(L, -1)) {
237 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "lua: Unable to find function %s in %s",
238 hook_spec->function_name,
239 hook_spec->file_name);
243 apl_run_lua_request(L, r);
246 apl_run_lua_request(L, r);
248 int t = lua_gettop(L);
249 lua_setglobal(L, "r");
253 if (lua_pcall(L, 1, 1, 0)) {
254 report_lua_error(L, r);
257 apr_status_t rv = DECLINED;
258 if (lua_isnumber(L, -1)) {
259 rv = lua_tointeger(L, -1);
261 if (rv != DECLINED) {
270 static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf, size_t bufsiz)
275 const char *res = (cfg->getstr)(buf, bufsiz, cfg->param);
278 if (i && buf[i - 1] == '\n') ++cfg->line_number;
287 int ch = (cfg->getch)(cfg->param);
288 if (ch == EOF) break;
299 typedef struct cr_ctx {
301 ap_configfile_t *cfp;
304 char buf[HUGE_STRING_LEN];
308 /* Okay, this deserves a little explaination -- in order for the errors that lua
309 * generates to be 'accuarate', including line numbers, we basically inject
310 * N line number new lines into the 'top' of the chunk reader.....
312 * be happy. this is cool.
315 static const char *lf = "\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";
318 static const char *direct_chunkreader(lua_State *lvm, void *udata, size_t *plen)
321 struct cr_ctx *ctx = udata;
323 if (ctx->startline) {
324 *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
325 ctx->startline -= *plen;
328 *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
330 for (p = ctx->buf; isspace(*p); ++p);
331 if (p[0] == '<' && p[1] == '/') {
333 while (i < strlen(ctx->endstr)) {
334 if (tolower(p[i + 2]) != ctx->endstr[i]) return ctx->buf;
340 /*fprintf(stderr, "buf read: %s\n", ctx->buf);*/
344 static int ldump_writer (lua_State *L, const void* b, size_t size, void* B) {
346 luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
350 typedef struct hack_section_baton {
352 apl_mapped_handler_spec *spec;
353 } hack_section_baton;
355 /* You can be unhappy now.
359 * When you create a <Section handler in httpd, the only 'easy' way to create
360 * a directory context is to parse the section, and convert it into a 'normal'
361 * Configureation option, and then collapse the entire section, in memory,
362 * back into the parent section -- from which you can then get the new directive
363 * invoked.... anyways. evil. Rici taught me how to do this hack :-)
365 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg, const char *arg)
367 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
368 ap_directive_t *directive = cmd->directive;
369 hack_section_baton* baton = directive->data;
371 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
373 hook_specs = apr_array_make(cmd->pool, 2, sizeof(apl_mapped_handler_spec*));
374 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, baton->name), APR_HASH_KEY_STRING, hook_specs);
377 baton->spec->scope = cfg->vm_scope;
379 *(apl_mapped_handler_spec**)apr_array_push(hook_specs) = baton->spec;
384 static const char *register_named_block_function_hook(const char *name,
389 const char* function;
391 if (line && line[0] == '>') {
397 word = ap_getword_conf(cmd->pool, &line);
398 wordlen = strlen(word);
399 if (wordlen == 0 || word[wordlen - 1] != '>') {
400 return apr_pstrcat(cmd->pool, cmd->directive->directive, "> takes exactly one argument", NULL);
403 function = apr_pstrndup(cmd->pool, word, wordlen - 1);
407 apl_mapped_handler_spec *spec = apr_pcalloc(cmd->pool, sizeof(apl_mapped_handler_spec));
416 apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
417 spec->file_name = apr_pstrcat(cmd->pool, cmd->config_file->name, ":", buf, NULL);
419 spec->function_name = (char*)function;
424 spec->code_cache_style = APL_CODE_CACHE_FOREVER;
427 tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive+1);
430 ctx.cfp = cmd->config_file;
431 ctx.startline = cmd->config_file->line_number;
433 /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
434 lvm = luaL_newstate();
438 rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
441 const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1), NULL);
447 luaL_buffinit(lvm, &b);
448 lua_dump(lvm, ldump_writer, &b);
450 spec->bytecode_len = lua_strlen(lvm, -1);
451 spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1), spec->bytecode_len);
455 ap_directive_t **current = mconfig;
457 /* Here, we have to replace our current config node for the next pass */
459 *current = apr_pcalloc(cmd->pool, sizeof(**current));
462 hack_section_baton *baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
466 (*current)->filename = cmd->config_file->name;
467 (*current)->line_num = cmd->config_file->line_number;
468 (*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
469 (*current)->args = NULL;
470 (*current)->data = baton;
476 static const char* register_named_file_function_hook(const char *name,
480 const char *function) {
481 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
483 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
485 hook_specs = apr_array_make(cmd->pool, 2, sizeof(apl_mapped_handler_spec*));
486 apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, name), APR_HASH_KEY_STRING, hook_specs);
489 apl_mapped_handler_spec *spec = apr_pcalloc(cmd->pool, sizeof(apl_mapped_handler_spec));
490 spec->file_name = apr_pstrdup(cmd->pool, file);
491 spec->function_name = apr_pstrdup(cmd->pool, function);
492 spec->scope = cfg->vm_scope;
493 spec->code_cache_style = APL_CODE_CACHE_STAT;
495 int code_cache_style;
500 *(apl_mapped_handler_spec**)apr_array_push(hook_specs) = spec;
504 int lua_check_user_id_harness(request_rec *r) {
505 return lua_request_rec_hook_harness(r, "check_user_id");
508 int lua_translate_name_harness(request_rec *r) {
509 return lua_request_rec_hook_harness(r, "translate_name");
512 int lua_fixup_harness(request_rec *r) {
513 return lua_request_rec_hook_harness(r, "fixups");
516 int lua_map_to_storage_harness(request_rec *r) {
517 return lua_request_rec_hook_harness(r, "map_to_storage");
520 int lua_type_checker_harness(request_rec *r) {
521 return lua_request_rec_hook_harness(r, "type_checker");
524 int lua_access_checker_harness(request_rec *r) {
525 return lua_request_rec_hook_harness(r, "access_checker");
528 int lua_auth_checker_harness(request_rec *r) {
529 return lua_request_rec_hook_harness(r, "auth_checker");
532 void lua_insert_filter_harness(request_rec *r) {
533 /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
536 int lua_quick_harness(request_rec *r, int lookup) {
540 return lua_request_rec_hook_harness(r, "quick");
543 static const char* register_translate_name_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
544 return register_named_file_function_hook("translate_name", cmd, _cfg, file, function);
547 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg, const char *line)
549 return register_named_block_function_hook("translate_name", cmd, _cfg, line);
553 static const char* register_fixups_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
554 return register_named_file_function_hook("fixups", cmd, _cfg, file, function);
556 static const char* register_fixups_block(cmd_parms *cmd, void *_cfg, const char *line) {
557 return register_named_block_function_hook("fixups", cmd, _cfg, line);
560 static const char* register_map_to_storage_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
561 return register_named_file_function_hook("map_to_storage", cmd, _cfg, file, function);
563 static const char* register_map_to_storage_block(cmd_parms *cmd, void *_cfg, const char *line) {
564 return register_named_block_function_hook("map_to_storage", cmd, _cfg, line);
567 static const char* register_check_user_id_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
568 return register_named_file_function_hook("check_user_id", cmd, _cfg, file, function);
570 static const char* register_check_user_id_block(cmd_parms *cmd, void *_cfg, const char *line) {
571 return register_named_block_function_hook("check_user_id", cmd, _cfg, line);
574 static const char* register_type_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
575 return register_named_file_function_hook("type_checker", cmd, _cfg, file, function);
577 static const char* register_type_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
578 return register_named_block_function_hook("type_checker", cmd, _cfg, line);
581 static const char* register_access_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
582 return register_named_file_function_hook("access_checker", cmd, _cfg, file, function);
584 static const char* register_access_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
585 return register_named_block_function_hook("access_checker", cmd, _cfg, line);
588 static const char* register_auth_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
589 return register_named_file_function_hook("auth_checker", cmd, _cfg, file, function);
591 static const char* register_auth_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
592 return register_named_block_function_hook("auth_checker", cmd, _cfg, line);
595 static const char* register_insert_filter_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
596 return "LuaHookInsertFilter not yet implemented";
599 static const char* register_quick_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
600 return register_named_file_function_hook("quick", cmd, _cfg, file, function);
602 static const char* register_quick_block(cmd_parms *cmd, void *_cfg, const char *line) {
603 return "LuaQuickHook in an inline block not yet implemented";
608 static const char* register_package_helper(cmd_parms *cmd, const char *arg, apr_array_header_t *dir_array) {
611 apl_server_cfg *server_cfg = ap_get_module_config(cmd->server->module_config, &lua_module);
612 char *fixed_filename;
613 rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, arg, APR_FILEPATH_NOTRELATIVE, cmd->pool);
614 if (rv != APR_SUCCESS) {
615 return apr_psprintf(cmd->pool, "Unable to build full path to file, %s", arg);
618 *(const char**)apr_array_push(dir_array) = fixed_filename;
624 * Called for config directive which looks like
625 * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
627 const char* register_package_dir(cmd_parms *cmd, void *_cfg, const char *arg) {
628 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
630 return register_package_helper(cmd, arg, cfg->package_paths);
634 * Called for config directive which looks like
635 * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
637 const char* register_package_cdir(cmd_parms *cmd, void *_cfg, const char *arg) {
638 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
640 return register_package_helper(cmd, arg, cfg->package_cpaths);
644 * Called for config directive which looks like
647 const char* register_code_cache(cmd_parms *cmd, void *_cfg, const char *arg) {
648 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
649 if (apr_strnatcmp("stat", arg) == 0) {
650 cfg->code_cache_style = APL_CODE_CACHE_STAT;
652 else if (apr_strnatcmp("forever", arg) == 0) {
653 cfg->code_cache_style = APL_CODE_CACHE_FOREVER;
655 else if (apr_strnatcmp("never", arg) == 0) {
656 cfg->code_cache_style = APL_CODE_CACHE_NEVER;
659 return apr_psprintf(cmd->pool,
660 "Invalid value for LuaCodeCache, '%s', acceptable values are %s",
661 arg, "'stat', 'forever', and 'never'");
666 static const char* register_lua_scope(cmd_parms *cmd, void *_cfg, const char *scope,
669 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
670 if (apr_strnatcmp("once", scope) == 0) {
671 cfg->vm_scope = APL_SCOPE_ONCE;
673 else if (apr_strnatcmp("request", scope) == 0) {
674 cfg->vm_scope = APL_SCOPE_REQUEST;
676 else if (apr_strnatcmp("conn", scope) == 0) {
677 cfg->vm_scope = APL_SCOPE_CONN;
679 else if (apr_strnatcmp("server", scope) == 0) {
680 cfg->vm_scope = APL_SCOPE_SERVER;
681 if (min) cfg->vm_server_pool_min = atoi(min);
682 if (max) cfg->vm_server_pool_max = atoi(max);
685 return apr_psprintf(cmd->pool,
686 "Invalid value for LuaScope, '%s', acceptable values are %s",
687 scope, "'once', 'request', 'conn', and 'server'");
694 * Called for config directive which looks like
695 * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
697 static const char* lua_map_handler(cmd_parms *cmd, void *_cfg, const char *path, const char *file, const char *function) {
698 apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
700 const char *function_name;
701 function_name = function ? function : "handle";
703 rv = apl_lua_map_handler(cfg, file, function_name, path, "once");
704 if (rv != APR_SUCCESS) {
705 return apr_psprintf(cmd->pool, "Unable to configure a lua handler for path '%s', handler %s#%s",
706 path, file, function_name);
711 static const char* register_lua_root(cmd_parms *cmd, void *_cfg, const char *root) {
712 /* apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg; */
713 apl_server_cfg* cfg = ap_get_module_config(cmd->server->module_config, &lua_module);
715 cfg->root_path = root;
719 /*******************************/
721 command_rec lua_commands[] = {
723 AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
724 "Specify the base path for resolving relative paths for mod_lua directives"),
727 AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
728 "Add a directory to lua's package.path"),
730 AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
731 "Add a directory to lua's package.cpath"),
733 AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL,
734 "Map a path to a lua handler"),
736 AP_INIT_TAKE2("LuaHookTranslateName", register_translate_name_hook, NULL, OR_ALL,
737 "Provide a hook for the translate name phase of request processing"),
738 AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block, NULL,
740 "Provide a hook for the translate name phase of request processing"),
742 AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
743 "Provide a hook for the fixups phase of request processing"),
744 AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
746 "Provide a inline hook for the fixups phase of request processing"),
749 AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL, OR_ALL,
750 "Provide a hook for the map_to_storage phase of request processing"),
751 AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block, NULL,
753 "Provide a hook for the map_to_storage phase of request processing"),
756 AP_INIT_TAKE2("LuaHookCheckUserID", register_check_user_id_hook, NULL, OR_ALL,
757 "Provide a hook for the check_user_id phase of request processing"),
758 AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block, NULL,
760 "Provide a hook for the check_user_id phase of request processing"),
763 AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL, OR_ALL,
764 "Provide a hook for the type_checker phase of request processing"),
765 AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
767 "Provide a hook for the type_checker phase of request processing"),
770 AP_INIT_TAKE2("LuaHookAccessChecker", register_access_checker_hook, NULL, OR_ALL,
771 "Provide a hook for the access_checker phase of request processing"),
772 AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block, NULL,
774 "Provide a hook for the access_checker phase of request processing"),
777 AP_INIT_TAKE2("LuaHookAuthChecker", register_auth_checker_hook, NULL, OR_ALL,
778 "Provide a hook for the auth_checker phase of request processing"),
779 AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
781 "Provide a hook for the auth_checker phase of request processing"),
784 AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL, OR_ALL,
785 "Provide a hook for the insert_filter phase of request processing"),
787 AP_INIT_TAKE1("LuaCodeCache", register_code_cache, NULL, OR_ALL,
788 "Configure the compiled code cache. \
789 Default is to stat the file each time, options are stat|forever|never"),
791 AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
792 "One of once, request, conn, server -- default is once"),
794 AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
795 "Provide a hook for the quick handler of request processing"),
796 AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
798 "Provide a hook for the quick handler of request processing"),
800 AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL, OR_ALL,
801 "(internal) Byte code handler"),
806 static void* create_dir_config(apr_pool_t *p, char *dir) {
807 apl_dir_cfg* cfg = apr_pcalloc(p, sizeof(apl_dir_cfg));
808 cfg->package_paths = apr_array_make(p, 2, sizeof(char*));
809 cfg->package_cpaths = apr_array_make(p, 2, sizeof(char*));
810 cfg->mapped_handlers = apr_array_make(p, 1, sizeof(apl_mapped_handler_spec*));
811 cfg->code_cache_style = APL_CODE_CACHE_STAT;
813 cfg->hooks = apr_hash_make(p);
814 cfg->dir = apr_pstrdup(p, dir);
815 cfg->vm_scope = APL_SCOPE_ONCE;
819 static int create_request_config(request_rec *r) {
820 apl_request_cfg *cfg = apr_palloc(r->pool, sizeof(apl_request_cfg));
821 cfg->mapped_request_details = NULL;
822 cfg->request_scoped_vms = apr_hash_make(r->pool);
823 ap_set_module_config(r->request_config, &lua_module, cfg);
827 static void* create_server_config(apr_pool_t *p, server_rec *s) {
829 apl_server_cfg *cfg = apr_pcalloc(p, sizeof(apl_server_cfg));
830 cfg->code_cache = apr_pcalloc(p, sizeof(apl_code_cache));
831 apr_thread_rwlock_create(&cfg->code_cache->compiled_files_lock, p);
832 cfg->code_cache->compiled_files = apr_hash_make(p);
833 cfg->vm_reslists = apr_hash_make(p);
834 apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
835 cfg->code_cache->pool = p;
836 cfg->root_path = NULL;
841 static int lua_request_hook(lua_State *L, request_rec *r) {
842 apl_push_request(L, r);
846 static void lua_register_hooks(apr_pool_t *p) {
847 /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
848 ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
849 ap_hook_create_request(create_request_config, NULL, NULL, APR_HOOK_MIDDLE);
851 /* http_request.h hooks */
852 ap_hook_translate_name(lua_translate_name_harness, NULL, NULL, APR_HOOK_MIDDLE);
853 ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
854 ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL, APR_HOOK_MIDDLE);
855 ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL, APR_HOOK_MIDDLE);
856 ap_hook_type_checker(lua_type_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
857 ap_hook_access_checker(lua_access_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
858 ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
859 ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL, APR_HOOK_MIDDLE);
860 ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
862 /* ap_hook_translate_name(lua_alias_munger, NULL, NULL, APR_HOOK_MIDDLE); */
863 ap_hook_translate_name(apl_alias_munger, NULL, NULL, APR_HOOK_MIDDLE);
865 APR_OPTIONAL_HOOK(apl, lua_open, lua_open_hook, NULL, NULL,
866 APR_HOOK_REALLY_FIRST);
868 APR_OPTIONAL_HOOK(apl, lua_request, lua_request_hook, NULL, NULL,
869 APR_HOOK_REALLY_FIRST);
872 module AP_MODULE_DECLARE_DATA lua_module = {
873 STANDARD20_MODULE_STUFF,
874 create_dir_config, /* create per-dir config structures */
875 NULL, /* merge per-dir config structures */
876 create_server_config, /* create per-server config structures */
877 NULL, /* merge per-server config structures */
878 lua_commands, /* table of config file commands */
879 lua_register_hooks /* register hooks */