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"
25 #include "apr_optional.h"
28 #ifdef APR_HAS_THREADS
29 #include "apr_thread_proc.h"
32 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
33 (lua_State *L, apr_pool_t *p),
36 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
37 (lua_State *L, request_rec *r),
39 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *lua_ssl_val = NULL;
40 static APR_OPTIONAL_FN_TYPE(ssl_is_https) *lua_ssl_is_https = NULL;
42 module AP_MODULE_DECLARE_DATA lua_module;
44 #define AP_LUA_HOOK_FIRST (APR_HOOK_FIRST - 1)
45 #define AP_LUA_HOOK_LAST (APR_HOOK_LAST + 1)
48 * error reporting if lua has an error.
49 * Extracts the error from lua stack and prints
51 static void report_lua_error(lua_State *L, request_rec *r)
53 const char *lua_response;
54 r->status = HTTP_INTERNAL_SERVER_ERROR;
55 r->content_type = "text/html";
57 ap_rputs("<b>Error!</b>\n", r);
59 lua_response = lua_tostring(L, -1);
60 ap_rputs(lua_response, r);
61 ap_rputs("</p>\n", r);
63 ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, "Lua error: %s",
67 static void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
70 ap_lua_load_apache2_lmodule(L);
71 ap_lua_load_request_lmodule(L, p);
72 ap_lua_load_config_lmodule(L);
75 static int lua_open_hook(lua_State *L, apr_pool_t *p)
77 lua_open_callback(L, p, NULL);
85 static int lua_handler(request_rec *r)
89 if (strcmp(r->handler, "lua-script")) {
93 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "handling [%s] in mod_lua",
95 dcfg = ap_get_module_config(r->per_dir_config, &lua_module);
97 if (!r->header_only) {
99 const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
101 ap_lua_vm_spec *spec = NULL;
103 spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
104 spec->scope = dcfg->vm_scope;
105 spec->pool = r->pool;
106 spec->file = r->filename;
107 spec->package_paths = cfg->package_paths;
108 spec->package_cpaths = cfg->package_cpaths;
109 spec->cb = &lua_open_callback;
112 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
113 "request details scope:%u, filename:%s, function:%s",
118 switch (dcfg->vm_scope) {
119 case AP_LUA_SCOPE_ONCE:
120 apr_pool_create(&pool, r->pool);
122 case AP_LUA_SCOPE_REQUEST:
125 case AP_LUA_SCOPE_CONN:
126 pool = r->connection->pool;
128 case AP_LUA_SCOPE_THREAD:
130 pool = apr_thread_pool_get(r->connection->current_thread);
135 L = ap_lua_get_lua_state(pool,
139 /* TODO annotate spec with failure reason */
140 r->status = HTTP_INTERNAL_SERVER_ERROR;
141 ap_rputs("Unable to compile VM, see logs", r);
142 return HTTP_INTERNAL_SERVER_ERROR;
144 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got a vm!");
145 lua_getglobal(L, "handle");
146 if (!lua_isfunction(L, -1)) {
147 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
148 "lua: Unable to find function %s in %s",
151 return HTTP_INTERNAL_SERVER_ERROR;
153 ap_lua_run_lua_request(L, r);
154 if (lua_pcall(L, 1, 0, 0)) {
155 report_lua_error(L, r);
164 /* ---------------- Configury stuff --------------- */
166 /** harnesses for magic hooks **/
168 static int lua_request_rec_hook_harness(request_rec *r, const char *name, int apr_hook_when)
172 ap_lua_vm_spec *spec;
173 ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
175 const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
177 const char *key = apr_psprintf(r->pool, "%s_%d", name, apr_hook_when);
178 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
179 APR_HASH_KEY_STRING);
182 for (i = 0; i < hook_specs->nelts; i++) {
183 ap_lua_mapped_handler_spec *hook_spec =
184 ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
186 if (hook_spec == NULL) {
189 spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
191 spec->file = hook_spec->file_name;
192 spec->scope = hook_spec->scope;
193 spec->bytecode = hook_spec->bytecode;
194 spec->bytecode_len = hook_spec->bytecode_len;
195 spec->pool = r->pool;
196 spec->package_paths = cfg->package_paths;
197 spec->package_cpaths = cfg->package_cpaths;
198 spec->cb = &lua_open_callback;
201 apr_filepath_merge(&spec->file, server_cfg->root_path,
202 spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
203 L = ap_lua_get_lua_state(r->pool, spec);
206 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
207 "lua: Failed to obtain lua interpreter for %s %s",
208 hook_spec->function_name, hook_spec->file_name);
209 return HTTP_INTERNAL_SERVER_ERROR;
212 if (hook_spec->function_name != NULL) {
213 lua_getglobal(L, hook_spec->function_name);
214 if (!lua_isfunction(L, -1)) {
215 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
216 "lua: Unable to find function %s in %s",
217 hook_spec->function_name,
218 hook_spec->file_name);
219 return HTTP_INTERNAL_SERVER_ERROR;
222 ap_lua_run_lua_request(L, r);
226 ap_lua_run_lua_request(L, r);
229 lua_setglobal(L, "r");
233 if (lua_pcall(L, 1, 1, 0)) {
234 report_lua_error(L, r);
235 return HTTP_INTERNAL_SERVER_ERROR;
238 if (lua_isnumber(L, -1)) {
239 rc = lua_tointeger(L, -1);
241 if (rc != DECLINED) {
250 static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
256 apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
257 if (rc == APR_SUCCESS) {
259 if (i && buf[i - 1] == '\n')
270 apr_status_t rc = (cfg->getch) (&ch, cfg->param);
271 if (rc != APR_SUCCESS)
283 typedef struct cr_ctx
286 ap_configfile_t *cfp;
289 char buf[HUGE_STRING_LEN];
293 /* Okay, this deserves a little explaination -- in order for the errors that lua
294 * generates to be 'accuarate', including line numbers, we basically inject
295 * N line number new lines into the 'top' of the chunk reader.....
297 * be happy. this is cool.
300 static const char *lf =
301 "\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";
304 static const char *direct_chunkreader(lua_State *lvm, void *udata,
308 struct cr_ctx *ctx = udata;
310 if (ctx->startline) {
311 *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
312 ctx->startline -= *plen;
315 *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
317 for (p = ctx->buf; isspace(*p); ++p);
318 if (p[0] == '<' && p[1] == '/') {
320 while (i < strlen(ctx->endstr)) {
321 if (tolower(p[i + 2]) != ctx->endstr[i])
328 /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
332 static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
335 luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
339 typedef struct hack_section_baton
342 ap_lua_mapped_handler_spec *spec;
344 } hack_section_baton;
346 /* You can be unhappy now.
350 * When you create a <Section handler in httpd, the only 'easy' way to create
351 * a directory context is to parse the section, and convert it into a 'normal'
352 * Configureation option, and then collapse the entire section, in memory,
353 * back into the parent section -- from which you can then get the new directive
354 * invoked.... anyways. evil. Rici taught me how to do this hack :-)
356 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
359 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
360 ap_directive_t *directive = cmd->directive;
361 hack_section_baton *baton = directive->data;
362 const char *key = apr_psprintf(cmd->pool, "%s_%d", baton->name, baton->apr_hook_when);
364 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
365 APR_HASH_KEY_STRING);
367 hook_specs = apr_array_make(cmd->pool, 2,
368 sizeof(ap_lua_mapped_handler_spec *));
369 apr_hash_set(cfg->hooks, key,
370 APR_HASH_KEY_STRING, hook_specs);
373 baton->spec->scope = cfg->vm_scope;
375 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
380 static const char *register_named_block_function_hook(const char *name,
385 const char *function = NULL;
386 ap_lua_mapped_handler_spec *spec;
387 int when = APR_HOOK_MIDDLE;
388 const char *endp = ap_strrchr_c(line, '>');
391 return apr_pstrcat(cmd->pool, cmd->cmd->name,
392 "> directive missing closing '>'", NULL);
395 line = apr_pstrndup(cmd->temp_pool, line, endp - line);
399 word = ap_getword_conf(cmd->temp_pool, &line);
401 function = apr_pstrdup(cmd->pool, word);
403 word = ap_getword_conf(cmd->temp_pool, &line);
405 if (!strcasecmp("early", word)) {
406 when = AP_LUA_HOOK_FIRST;
408 else if (!strcasecmp("late", word)) {
409 when = AP_LUA_HOOK_LAST;
412 return apr_pstrcat(cmd->pool, cmd->cmd->name,
413 "> 2nd argument must be 'early' or 'late'", NULL);
418 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
426 ap_directive_t **current;
427 hack_section_baton *baton;
429 apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
430 spec->file_name = apr_pstrcat(cmd->pool, cmd->config_file->name, ":",
433 spec->function_name = (char *) function;
440 tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
443 ctx.cfp = cmd->config_file;
444 ctx.startline = cmd->config_file->line_number;
446 /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
447 lvm = luaL_newstate();
451 rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
454 const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:",
455 lua_tostring(lvm, -1), NULL);
461 luaL_buffinit(lvm, &b);
462 lua_dump(lvm, ldump_writer, &b);
464 spec->bytecode_len = lua_strlen(lvm, -1);
465 spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
472 /* Here, we have to replace our current config node for the next pass */
474 *current = apr_pcalloc(cmd->pool, sizeof(**current));
477 baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
480 baton->apr_hook_when = when;
482 (*current)->filename = cmd->config_file->name;
483 (*current)->line_num = cmd->config_file->line_number;
484 (*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
485 (*current)->args = NULL;
486 (*current)->data = baton;
492 static const char *register_named_file_function_hook(const char *name,
496 const char *function,
499 ap_lua_mapped_handler_spec *spec;
500 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
501 const char *key = apr_psprintf(cmd->pool, "%s_%d", name, apr_hook_when);
502 apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key,
503 APR_HASH_KEY_STRING);
506 hook_specs = apr_array_make(cmd->pool, 2,
507 sizeof(ap_lua_mapped_handler_spec *));
508 apr_hash_set(cfg->hooks, key, APR_HASH_KEY_STRING, hook_specs);
511 spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
512 spec->file_name = apr_pstrdup(cmd->pool, file);
513 spec->function_name = apr_pstrdup(cmd->pool, function);
514 spec->scope = cfg->vm_scope;
516 *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
520 static int lua_check_user_id_harness_first(request_rec *r)
522 return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST);
524 static int lua_check_user_id_harness(request_rec *r)
526 return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE);
528 static int lua_check_user_id_harness_last(request_rec *r)
530 return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST);
533 static int lua_translate_name_harness_first(request_rec *r)
535 return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_FIRST);
537 static int lua_translate_name_harness(request_rec *r)
539 return lua_request_rec_hook_harness(r, "translate_name", APR_HOOK_MIDDLE);
541 static int lua_translate_name_harness_last(request_rec *r)
543 return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_LAST);
546 static int lua_fixup_harness(request_rec *r)
548 return lua_request_rec_hook_harness(r, "fixups", APR_HOOK_MIDDLE);
551 static int lua_map_to_storage_harness(request_rec *r)
553 return lua_request_rec_hook_harness(r, "map_to_storage", APR_HOOK_MIDDLE);
556 static int lua_type_checker_harness(request_rec *r)
558 return lua_request_rec_hook_harness(r, "type_checker", APR_HOOK_MIDDLE);
561 static int lua_access_checker_harness_first(request_rec *r)
563 return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_FIRST);
565 static int lua_access_checker_harness(request_rec *r)
567 return lua_request_rec_hook_harness(r, "access_checker", APR_HOOK_MIDDLE);
569 static int lua_access_checker_harness_last(request_rec *r)
571 return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_LAST);
574 static int lua_auth_checker_harness_first(request_rec *r)
576 return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_FIRST);
578 static int lua_auth_checker_harness(request_rec *r)
580 return lua_request_rec_hook_harness(r, "auth_checker", APR_HOOK_MIDDLE);
582 static int lua_auth_checker_harness_last(request_rec *r)
584 return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_LAST);
586 static void lua_insert_filter_harness(request_rec *r)
588 /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
591 static int lua_quick_harness(request_rec *r, int lookup)
596 return lua_request_rec_hook_harness(r, "quick", APR_HOOK_MIDDLE);
599 static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
601 const char *function,
604 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
606 int apr_hook_when = APR_HOOK_MIDDLE;
612 if (!strcasecmp(when, "early")) {
613 apr_hook_when = AP_LUA_HOOK_FIRST;
615 else if (!strcasecmp(when, "late")) {
616 apr_hook_when = AP_LUA_HOOK_LAST;
619 return "Third argument must be 'early' or 'late'";
623 return register_named_file_function_hook("translate_name", cmd, _cfg,
624 file, function, apr_hook_when);
627 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
630 return register_named_block_function_hook("translate_name", cmd, _cfg,
635 static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
637 const char *function)
639 return register_named_file_function_hook("fixups", cmd, _cfg, file,
640 function, APR_HOOK_MIDDLE);
642 static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
645 return register_named_block_function_hook("fixups", cmd, _cfg, line);
648 static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
650 const char *function)
652 return register_named_file_function_hook("map_to_storage", cmd, _cfg,
653 file, function, APR_HOOK_MIDDLE);
655 static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
658 return register_named_block_function_hook("map_to_storage", cmd, _cfg,
662 static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
664 const char *function,
667 int apr_hook_when = APR_HOOK_MIDDLE;
670 if (!strcasecmp(when, "early")) {
671 apr_hook_when = AP_LUA_HOOK_FIRST;
673 else if (!strcasecmp(when, "late")) {
674 apr_hook_when = AP_LUA_HOOK_LAST;
677 return "Third argument must be 'early' or 'late'";
681 return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
682 function, apr_hook_when);
684 static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
687 return register_named_block_function_hook("check_user_id", cmd, _cfg,
691 static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
693 const char *function)
695 return register_named_file_function_hook("type_checker", cmd, _cfg, file,
696 function, APR_HOOK_MIDDLE);
698 static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
701 return register_named_block_function_hook("type_checker", cmd, _cfg,
705 static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
707 const char *function,
710 int apr_hook_when = APR_HOOK_MIDDLE;
713 if (!strcasecmp(when, "early")) {
714 apr_hook_when = AP_LUA_HOOK_FIRST;
716 else if (!strcasecmp(when, "late")) {
717 apr_hook_when = AP_LUA_HOOK_LAST;
720 return "Third argument must be 'early' or 'late'";
724 return register_named_file_function_hook("access_checker", cmd, _cfg,
725 file, function, apr_hook_when);
727 static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
731 return register_named_block_function_hook("access_checker", cmd, _cfg,
735 static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
737 const char *function,
740 int apr_hook_when = APR_HOOK_MIDDLE;
743 if (!strcasecmp(when, "early")) {
744 apr_hook_when = AP_LUA_HOOK_FIRST;
746 else if (!strcasecmp(when, "late")) {
747 apr_hook_when = AP_LUA_HOOK_LAST;
750 return "Third argument must be 'early' or 'late'";
754 return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
755 function, apr_hook_when);
757 static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
760 return register_named_block_function_hook("auth_checker", cmd, _cfg,
764 static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
766 const char *function)
768 return "LuaHookInsertFilter not yet implemented";
771 static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
772 const char *file, const char *function)
774 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES|
779 return register_named_file_function_hook("quick", cmd, _cfg, file,
780 function, APR_HOOK_MIDDLE);
782 static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
785 return register_named_block_function_hook("quick", cmd, _cfg,
791 static const char *register_package_helper(cmd_parms *cmd,
793 apr_array_header_t *dir_array)
797 ap_lua_server_cfg *server_cfg =
798 ap_get_module_config(cmd->server->module_config, &lua_module);
800 char *fixed_filename;
801 rv = apr_filepath_merge(&fixed_filename,
802 server_cfg->root_path,
804 APR_FILEPATH_NOTRELATIVE,
807 if (rv != APR_SUCCESS) {
808 return apr_psprintf(cmd->pool,
809 "Unable to build full path to file, %s", arg);
812 *(const char **) apr_array_push(dir_array) = fixed_filename;
818 * Called for config directive which looks like
819 * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
821 static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
824 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
826 return register_package_helper(cmd, arg, cfg->package_paths);
830 * Called for config directive which looks like
831 * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
833 static const char *register_package_cdir(cmd_parms *cmd,
837 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
839 return register_package_helper(cmd, arg, cfg->package_cpaths);
843 static const char *register_lua_scope(cmd_parms *cmd,
849 ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
850 if (strcmp("once", scope) == 0) {
851 cfg->vm_scope = AP_LUA_SCOPE_ONCE;
853 else if (strcmp("request", scope) == 0) {
854 cfg->vm_scope = AP_LUA_SCOPE_REQUEST;
856 else if (strcmp("conn", scope) == 0) {
857 cfg->vm_scope = AP_LUA_SCOPE_CONN;
859 else if (strcmp("thread", scope) == 0) {
860 cfg->vm_scope = AP_LUA_SCOPE_THREAD;
863 return apr_psprintf(cmd->pool,
864 "Invalid value for LuaScope, '%s', acceptable "
865 "values are 'once', 'request', 'conn', and "
874 static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
877 /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
878 ap_lua_server_cfg *cfg = ap_get_module_config(cmd->server->module_config,
881 cfg->root_path = root;
884 AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var)
887 return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
892 AP_LUA_DECLARE(int) ap_lua_ssl_is_https(conn_rec *c)
894 return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
897 /*******************************/
899 command_rec lua_commands[] = {
901 AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
902 "Specify the base path for resolving relative paths for mod_lua directives"),
904 AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
905 "Add a directory to lua's package.path"),
907 AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
908 "Add a directory to lua's package.cpath"),
911 AP_INIT_TAKE23("LuaHookTranslateName", register_translate_name_hook, NULL,
913 "Provide a hook for the translate name phase of request processing"),
915 AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
917 EXEC_ON_READ | OR_ALL,
918 "Provide a hook for the translate name phase of request processing"),
920 AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL,
921 "Provide a hook for the fixups phase of request processing"),
922 AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
923 EXEC_ON_READ | OR_ALL,
924 "Provide a inline hook for the fixups phase of request processing"),
927 AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
929 "Provide a hook for the map_to_storage phase of request processing"),
930 AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
932 EXEC_ON_READ | OR_ALL,
933 "Provide a hook for the map_to_storage phase of request processing"),
936 AP_INIT_TAKE23("LuaHookCheckUserID", register_check_user_id_hook, NULL,
938 "Provide a hook for the check_user_id phase of request processing"),
939 AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
941 EXEC_ON_READ | OR_ALL,
942 "Provide a hook for the check_user_id phase of request processing"),
945 AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
947 "Provide a hook for the type_checker phase of request processing"),
948 AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
949 EXEC_ON_READ | OR_ALL,
950 "Provide a hook for the type_checker phase of request processing"),
953 AP_INIT_TAKE23("LuaHookAccessChecker", register_access_checker_hook, NULL,
955 "Provide a hook for the access_checker phase of request processing"),
956 AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block,
958 EXEC_ON_READ | OR_ALL,
959 "Provide a hook for the access_checker phase of request processing"),
962 AP_INIT_TAKE23("LuaHookAuthChecker", register_auth_checker_hook, NULL,
964 "Provide a hook for the auth_checker phase of request processing"),
965 AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
966 EXEC_ON_READ | OR_ALL,
967 "Provide a hook for the auth_checker phase of request processing"),
970 AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
972 "Provide a hook for the insert_filter phase of request processing"),
974 AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
975 "One of once, request, conn, server -- default is once"),
977 AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
978 "Provide a hook for the quick handler of request processing"),
979 AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
980 EXEC_ON_READ | OR_ALL,
981 "Provide a hook for the quick handler of request processing"),
982 AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
984 "(internal) Byte code handler"),
989 static void *create_dir_config(apr_pool_t *p, char *dir)
991 ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_dir_cfg));
992 cfg->package_paths = apr_array_make(p, 2, sizeof(char *));
993 cfg->package_cpaths = apr_array_make(p, 2, sizeof(char *));
994 cfg->mapped_handlers =
995 apr_array_make(p, 1, sizeof(ap_lua_mapped_handler_spec *));
997 cfg->hooks = apr_hash_make(p);
998 cfg->dir = apr_pstrdup(p, dir);
999 cfg->vm_scope = AP_LUA_SCOPE_ONCE;
1003 static int create_request_config(request_rec *r)
1005 ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg));
1006 cfg->mapped_request_details = NULL;
1007 cfg->request_scoped_vms = apr_hash_make(r->pool);
1008 ap_set_module_config(r->request_config, &lua_module, cfg);
1012 static void *create_server_config(apr_pool_t *p, server_rec *s)
1015 ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
1016 cfg->vm_reslists = apr_hash_make(p);
1017 apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
1018 cfg->root_path = NULL;
1023 static int lua_request_hook(lua_State *L, request_rec *r)
1025 ap_lua_push_request(L, r);
1029 static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1030 apr_pool_t *ptemp, server_rec *s)
1032 lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
1033 lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
1037 static void lua_register_hooks(apr_pool_t *p)
1039 /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
1040 ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE);
1041 ap_hook_create_request(create_request_config, NULL, NULL,
1044 /* http_request.h hooks */
1045 ap_hook_translate_name(lua_translate_name_harness_first, NULL, NULL,
1047 ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
1049 ap_hook_translate_name(lua_translate_name_harness_last, NULL, NULL,
1052 ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
1053 ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
1056 ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
1058 ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
1060 ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
1063 ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
1066 ap_hook_access_checker(lua_access_checker_harness_first, NULL, NULL,
1068 ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
1070 ap_hook_access_checker(lua_access_checker_harness_last, NULL, NULL,
1072 ap_hook_auth_checker(lua_auth_checker_harness_first, NULL, NULL,
1074 ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
1076 ap_hook_auth_checker(lua_auth_checker_harness_last, NULL, NULL,
1079 ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
1081 ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
1083 ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1085 APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
1086 APR_HOOK_REALLY_FIRST);
1088 APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
1089 APR_HOOK_REALLY_FIRST);
1092 AP_DECLARE_MODULE(lua) = {
1093 STANDARD20_MODULE_STUFF,
1094 create_dir_config, /* create per-dir config structures */
1095 NULL, /* merge per-dir config structures */
1096 create_server_config, /* create per-server config structures */
1097 NULL, /* merge per-server config structures */
1098 lua_commands, /* table of config file commands */
1099 lua_register_hooks /* register hooks */