]> granicus.if.org Git - apache/blob - modules/lua/mod_lua.c
Fixup lua_config.c with prefix rename, and make mod_lua.c so it compiles again.
[apache] / modules / lua / mod_lua.c
1 /**
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "mod_lua.h"
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22
23 #include "lua_apr.h"
24 #include "lua_config.h"
25
26 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(apl, AP_LUA, int, lua_open,
27                                     (lua_State *L, apr_pool_t *p),
28                                     (L, p),
29                                     OK, DECLINED)
30
31 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(apl, AP_LUA, int, lua_request,
32                                     (lua_State *L, request_rec *r),
33                                     (L, r),
34                                     OK, DECLINED)
35
36 module AP_MODULE_DECLARE_DATA lua_module;
37
38 /**
39  * error reporting if lua has an error. 
40  * Extracts the error from lua stack and prints
41  */
42 static void report_lua_error(lua_State *L, request_rec *r) {
43     r->status = 500;
44     r->content_type = "text/html";      
45
46     ap_rputs("<b>Error!</b>\n", r);
47     ap_rputs("<p>", r);
48     const char* lua_response = lua_tostring(L, -1);
49     ap_rputs(lua_response, r);            
50     ap_rputs("</p>\n", r);
51     
52     ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, "Lua error: %s", lua_response);
53 }
54
55 static void lua_open_callback(lua_State *L, apr_pool_t *p, void* ctx) {
56     apr_lua_init(L, p);
57     apl_load_apache2_lmodule(L);
58     apl_load_request_lmodule(L, p);
59     apl_load_config_lmodule(L);
60 }
61
62 static int lua_open_hook(lua_State *L, apr_pool_t *p) {
63     lua_open_callback(L, p, NULL);
64     return OK;
65 }
66
67 /*
68 static apr_status_t luahood(ap_filter_t *f, apr_bucket_brigade *bb) {
69     apr_bucket* b;
70     apr_status_t rs;
71     for ( b = APR_BRIGADE_FIRST(bb);
72           b != APR_BRIGADE_SENTINEL(bb);
73           b = APR_BUCKET_NEXT(b)) 
74     {
75         if (APR_BUCKET_IS_EOS(b)) {kl
76             break;
77         }
78         const char *buffer;
79         size_t bytes;
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");
82             return rs;
83         }
84         char *mine = apr_pstrmemdup(f->r->pool, buffer, bytes);
85         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "sending '%s'", mine);
86     }
87     
88     ap_pass_brigade(f->next, bb);
89     
90     return OK;
91 }
92 */
93
94 /**
95  * "main"
96  */
97 static int lua_handler(request_rec *r) {        
98     if (strcmp(r->handler, "lua-script")) {
99         return DECLINED;
100     }
101     
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);
104     
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;
109         if (!d) {
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;
116             d->spec = spec;
117             d->function_name = "handle";
118         }
119         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "request details scope:%u, cache:%u",
120                                                        d->spec->scope,
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,
124                                           d->spec->file,
125                                           cfg->package_paths,
126                                           cfg->package_cpaths,
127                                           &lua_open_callback, NULL); 
128                                           
129         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got a vm!");
130         if (!L) {
131             /* TODO annotate spec with failure reason */
132             r->status = 500;
133             ap_rputs("Unable to compile VM, see logs", r);
134         }
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);
139         }
140     }
141     return OK;
142 }
143
144
145
146 /**
147  * Like mod_alias except for lua handler fun :-) 
148  */
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);
151     
152     int i;
153     ap_regmatch_t matches[AP_MAX_REG_MATCH];
154     
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";
159             
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;
168             }
169             
170             mapped_request_details *d = apr_palloc(r->pool, sizeof(mapped_request_details));
171             
172             d->function_name = ap_pregsub(r->pool, cnd->function_name, r->uri, AP_MAX_REG_MATCH, matches);
173             d->spec = spec;
174             
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;
179             return OK;
180         }
181     }
182     return DECLINED;    
183 }
184
185 /* ---------------- Configury stuff --------------- */
186
187 /** harnesses for magic hooks **/
188
189 static int lua_request_rec_hook_harness(request_rec *r, const char *name) {
190     char *fixed_filename;
191     
192     const apl_dir_cfg* cfg = (apl_dir_cfg*) ap_get_module_config(r->per_dir_config,
193                                                                        &lua_module);
194     apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
195     if (hook_specs) {
196         int i;
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));
201             
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;
208             
209             /*
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,
212                                               d->spec->file,
213                                               cfg->package_paths,
214                                               cfg->package_cpaths,
215                                               &lua_open_callback, NULL);
216             */            
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,
220                                               fixed_filename,
221                                               cfg->package_paths,
222                                               cfg->package_cpaths,
223                                               &lua_open_callback, NULL);
224             
225             
226             
227             if (!L) {
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);
231                 return 500;
232             }
233
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);
240                     return 500;
241                 }
242
243                 apl_run_lua_request(L, r);
244             }
245             else {
246                 apl_run_lua_request(L, r);
247                 
248                 int t = lua_gettop(L);
249                 lua_setglobal(L, "r");
250                 lua_settop(L, t);
251             }
252
253             if (lua_pcall(L, 1, 1, 0)) {
254                 report_lua_error(L, r);
255                 return 500;
256             }
257             apr_status_t rv = DECLINED;
258             if (lua_isnumber(L, -1)) {
259                 rv = lua_tointeger(L, -1);
260             }
261             if (rv != DECLINED) {
262                 return rv;
263             }
264         }
265     }
266     return DECLINED;
267 }
268
269
270 static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf, size_t bufsiz)
271 {
272     apr_size_t i = 0;
273     
274     if (cfg->getstr) {
275         const char *res = (cfg->getstr)(buf, bufsiz, cfg->param);
276         if (res) {
277             i = strlen(buf);
278             if (i && buf[i - 1] == '\n') ++cfg->line_number;
279         }
280         else {
281             buf[0] = '\0';
282             i = 0;
283         }
284     }
285     else {
286         while (i < bufsiz) {
287             int ch = (cfg->getch)(cfg->param);
288             if (ch == EOF) break;
289             buf[i++] = ch;
290             if (ch == '\n') {
291                 ++cfg->line_number;
292                 break;
293             }
294         }
295     }
296     return i;
297 }
298
299 typedef struct cr_ctx {
300     cmd_parms *cmd;
301     ap_configfile_t *cfp;
302     size_t startline;
303     const char *endstr;
304     char buf[HUGE_STRING_LEN];
305 } cr_ctx;
306
307
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..... 
311  *
312  * be happy. this is cool.
313  *
314  */
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";
316 #define N_LF 32
317
318 static const char *direct_chunkreader(lua_State *lvm, void *udata, size_t *plen)
319 {
320     const char *p;
321     struct cr_ctx *ctx = udata;
322     
323     if (ctx->startline) {
324         *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
325         ctx->startline -= *plen;
326         return lf;
327     }
328     *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
329     
330     for (p = ctx->buf; isspace(*p); ++p);
331     if (p[0] == '<' && p[1] == '/') {
332         int i = 0;
333         while (i < strlen(ctx->endstr)) {
334             if (tolower(p[i + 2]) != ctx->endstr[i]) return ctx->buf;
335             ++i;
336         }
337         *plen = 0;
338         return NULL;
339     }
340     /*fprintf(stderr, "buf read: %s\n", ctx->buf);*/
341     return ctx->buf;
342 }
343
344 static int ldump_writer (lua_State *L, const void* b, size_t size, void* B) {
345     (void)L;
346     luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
347     return 0;
348 }
349
350 typedef struct hack_section_baton {
351     const char *name;
352     apl_mapped_handler_spec *spec;
353 } hack_section_baton;
354
355 /* You can be unhappy now.
356  *
357  * This is uncool.
358  *
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 :-)
364  */
365 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg, const char *arg)
366 {
367     apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
368     ap_directive_t *directive = cmd->directive;
369     hack_section_baton* baton = directive->data;
370
371     apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
372     if (!hook_specs) {
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);
375     }
376
377     baton->spec->scope = cfg->vm_scope;
378
379     *(apl_mapped_handler_spec**)apr_array_push(hook_specs) = baton->spec;
380
381     return NULL;
382 }
383
384 static const char *register_named_block_function_hook(const char *name, 
385                                                       cmd_parms *cmd, 
386                                                       void *mconfig, 
387                                                       const char *line)
388 {
389     const char* function;
390
391     if (line && line[0] == '>') {
392         function = NULL;
393     }
394     else {
395         const char *word;
396         apr_size_t wordlen;
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);
401         }
402         else {
403             function = apr_pstrndup(cmd->pool, word, wordlen - 1);
404         }
405     }
406
407     apl_mapped_handler_spec *spec = apr_pcalloc(cmd->pool, sizeof(apl_mapped_handler_spec));
408
409     {
410         cr_ctx ctx;
411         char buf[32];
412         lua_State* lvm;
413         char *tmp;
414         int rv;
415
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);
418         if (function) {
419             spec->function_name = (char*)function;
420         }
421         else {
422             function = NULL;
423         }
424         spec->code_cache_style = APL_CODE_CACHE_FOREVER;
425
426         ctx.cmd = cmd;
427         tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive+1);
428         ap_str_tolower(tmp);
429         ctx.endstr = tmp; 
430         ctx.cfp = cmd->config_file;
431         ctx.startline = cmd->config_file->line_number;
432
433         /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
434         lvm = luaL_newstate();
435
436         lua_settop(lvm, 0);
437
438         rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
439
440         if (rv != 0) {
441             const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1), NULL);
442             lua_close(lvm);
443             return errstr;
444         }
445         else {
446             luaL_Buffer b;
447             luaL_buffinit(lvm, &b);
448             lua_dump(lvm, ldump_writer, &b);
449             luaL_pushresult(&b);
450             spec->bytecode_len = lua_strlen(lvm, -1);
451             spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1), spec->bytecode_len);
452             lua_close(lvm);
453         }
454
455         ap_directive_t **current = mconfig;
456
457         /* Here, we have to replace our current config node for the next pass */
458         if (!*current) {
459             *current = apr_pcalloc(cmd->pool, sizeof(**current));
460         }
461         
462         hack_section_baton *baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
463         baton->name = name;
464         baton->spec = spec;
465
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;
471     }
472
473     return NULL;
474 }
475
476 static const char* register_named_file_function_hook(const char *name, 
477                                                      cmd_parms *cmd, 
478                                                      void *_cfg, 
479                                                      const char *file, 
480                                                      const char *function) {
481     apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
482     
483     apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
484     if (!hook_specs) {
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);
487     }
488
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;
494     /*
495         int code_cache_style;
496         char *function_name;
497         char *file_name;
498         int scope;
499     */
500     *(apl_mapped_handler_spec**)apr_array_push(hook_specs) = spec;
501     return NULL;
502 }
503
504 int lua_check_user_id_harness(request_rec *r) {
505     return lua_request_rec_hook_harness(r, "check_user_id");
506 }
507
508 int lua_translate_name_harness(request_rec *r) {
509     return lua_request_rec_hook_harness(r, "translate_name");
510 }
511
512 int lua_fixup_harness(request_rec *r) {
513     return lua_request_rec_hook_harness(r, "fixups");
514 }
515
516 int lua_map_to_storage_harness(request_rec *r) {
517     return lua_request_rec_hook_harness(r, "map_to_storage");
518 }
519
520 int lua_type_checker_harness(request_rec *r) {
521     return lua_request_rec_hook_harness(r, "type_checker");
522 }
523
524 int lua_access_checker_harness(request_rec *r) {
525     return lua_request_rec_hook_harness(r, "access_checker");
526 }
527
528 int lua_auth_checker_harness(request_rec *r) {
529     return lua_request_rec_hook_harness(r, "auth_checker");
530 }
531
532 void lua_insert_filter_harness(request_rec *r) {
533     /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
534 }
535
536 int lua_quick_harness(request_rec *r, int lookup) {
537     if(lookup) {
538         return DECLINED;
539     }
540     return lua_request_rec_hook_harness(r, "quick");
541 }
542
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);
545 }
546
547 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg, const char *line)
548 {
549     return register_named_block_function_hook("translate_name", cmd, _cfg, line);
550 }
551
552
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);
555 }
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);
558 }
559
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);
562 }
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);
565 }
566
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);
569 }
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);
572 }
573
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);
576 }
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);
579 }
580
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);
583 }
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);
586 }
587
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);
590 }
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);
593 }
594
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";
597 }
598
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);
601 }
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";
604 }
605
606
607
608 static const char* register_package_helper(cmd_parms *cmd, const char *arg, apr_array_header_t *dir_array) {
609     apr_status_t rv;
610         
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);
616     }
617     
618     *(const char**)apr_array_push(dir_array) = fixed_filename;
619     return NULL;
620 }
621
622
623 /**
624  * Called for config directive which looks like
625  * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
626  */
627 const char* register_package_dir(cmd_parms *cmd, void *_cfg, const char *arg) {
628     apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
629     
630     return register_package_helper(cmd, arg, cfg->package_paths);
631 }
632
633 /**
634  * Called for config directive which looks like
635  * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
636  */
637 const char* register_package_cdir(cmd_parms *cmd, void *_cfg, const char *arg) {
638     apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
639     
640     return register_package_helper(cmd, arg, cfg->package_cpaths);
641 }
642
643 /**
644  * Called for config directive which looks like
645  * LuaCodeCache 
646  */
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;
651     }
652     else if (apr_strnatcmp("forever", arg) == 0) {
653         cfg->code_cache_style = APL_CODE_CACHE_FOREVER;
654     }
655     else if (apr_strnatcmp("never", arg) == 0) {
656         cfg->code_cache_style = APL_CODE_CACHE_NEVER;
657     }
658     else {
659         return apr_psprintf(cmd->pool, 
660                "Invalid value for LuaCodeCache, '%s', acceptable values are %s", 
661                arg, "'stat', 'forever', and 'never'");
662     }
663     return NULL;
664 }
665
666 static const char* register_lua_scope(cmd_parms *cmd, void *_cfg, const char *scope,
667                                                                   const char *min,
668                                                                   const char *max) {
669     apl_dir_cfg* cfg = (apl_dir_cfg*)_cfg;
670     if (apr_strnatcmp("once", scope) == 0) {
671         cfg->vm_scope = APL_SCOPE_ONCE;
672     }
673     else if (apr_strnatcmp("request", scope) == 0) {
674         cfg->vm_scope = APL_SCOPE_REQUEST;
675     }
676     else if (apr_strnatcmp("conn", scope) == 0) {
677         cfg->vm_scope = APL_SCOPE_CONN;
678     }
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);
683     }
684     else {
685         return apr_psprintf(cmd->pool, 
686                "Invalid value for LuaScope, '%s', acceptable values are %s", 
687                scope, "'once', 'request', 'conn', and 'server'");
688     }
689     return NULL;
690 }
691
692
693 /**
694  * Called for config directive which looks like
695  * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
696  */
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;
699
700     const char *function_name;
701     function_name = function ? function : "handle";
702     apr_status_t rv;
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);
707     }
708     return NULL;
709 }
710
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);
714     
715     cfg->root_path = root;
716     return NULL;
717 }
718
719 /*******************************/
720
721 command_rec lua_commands[] = {
722
723     AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL, 
724                 "Specify the base path for resolving relative paths for mod_lua directives"),
725
726     
727     AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL, 
728                   "Add a directory to lua's package.path"),
729                   
730     AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL, 
731                   "Add a directory to lua's package.cpath"),
732                   
733     AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL, 
734                   "Map a path to a lua handler"),
735                   
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,
739                      EXEC_ON_READ|OR_ALL,
740                      "Provide a hook for the translate name phase of request processing"),
741
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,
745                      EXEC_ON_READ|OR_ALL,
746                      "Provide a inline hook for the fixups phase of request processing"),
747
748 /* todo: test */
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,
752                      EXEC_ON_READ|OR_ALL,
753                      "Provide a hook for the map_to_storage phase of request processing"),
754
755     /* todo: test */
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,
759                      EXEC_ON_READ|OR_ALL,
760                      "Provide a hook for the check_user_id phase of request processing"),    
761
762     /* todo: test */
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,
766                      EXEC_ON_READ|OR_ALL,
767                      "Provide a hook for the type_checker phase of request processing"),
768
769     /* todo: test */
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,
773                      EXEC_ON_READ|OR_ALL,
774                      "Provide a hook for the access_checker phase of request processing"),
775
776     /* todo: test */
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,
780                      EXEC_ON_READ|OR_ALL,
781                      "Provide a hook for the auth_checker phase of request processing"),
782
783     /* todo: test */
784     AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL, OR_ALL, 
785                   "Provide a hook for the insert_filter phase of request processing"),
786     
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"),
790                    
791     AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
792                   "One of once, request, conn, server -- default is once"),
793
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,
797                      EXEC_ON_READ|OR_ALL,
798                     "Provide a hook for the quick handler of request processing"),
799
800     AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL, OR_ALL, 
801                      "(internal) Byte code handler"),
802     { NULL }
803 };
804
805
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;
812     cfg->pool = p;
813     cfg->hooks = apr_hash_make(p);
814     cfg->dir = apr_pstrdup(p, dir);
815     cfg->vm_scope = APL_SCOPE_ONCE;
816     return cfg;
817 }
818
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);
824     return OK;
825 }
826
827 static void* create_server_config(apr_pool_t *p, server_rec *s) {
828     
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;
837     
838     return cfg;
839 }
840
841 static int lua_request_hook(lua_State *L, request_rec *r) {
842     apl_push_request(L, r);
843     return OK;
844 }
845
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);
850     
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);
861
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);    
864
865     APR_OPTIONAL_HOOK(apl, lua_open, lua_open_hook, NULL, NULL,
866                       APR_HOOK_REALLY_FIRST);
867
868     APR_OPTIONAL_HOOK(apl, lua_request, lua_request_hook, NULL, NULL,
869                       APR_HOOK_REALLY_FIRST); 
870 }
871
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                      */
880 };
881