]> granicus.if.org Git - apache/blob - modules/lua/mod_lua.c
bf74c3974ca177a77837ea7fdcc5b4e84e3471fc
[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(ap_lua, AP_LUA, int, lua_open,
27                                     (lua_State *L, apr_pool_t *p),
28                                     (L, p), OK, DECLINED)
29
30 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request,
31                                     (lua_State *L, request_rec *r),
32                                     (L, r), OK, DECLINED)
33
34      module AP_MODULE_DECLARE_DATA lua_module;
35
36 /**
37  * error reporting if lua has an error.
38  * Extracts the error from lua stack and prints
39  */
40 static void report_lua_error(lua_State *L, request_rec *r)
41 {
42     const char *lua_response;
43     r->status = HTTP_INTERNAL_SERVER_ERROR;
44     r->content_type = "text/html";
45
46     ap_rputs("<b>Error!</b>\n", r);
47     ap_rputs("<p>", r);
48     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",
53                   lua_response);
54 }
55
56 static void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx)
57 {
58     ap_lua_init(L, p);
59     ap_lua_load_apache2_lmodule(L);
60     ap_lua_load_request_lmodule(L, p);
61     ap_lua_load_config_lmodule(L);
62 }
63
64 static int lua_open_hook(lua_State *L, apr_pool_t *p)
65 {
66     lua_open_callback(L, p, NULL);
67     return OK;
68 }
69
70 /*
71 static apr_status_t luahood(ap_filter_t *f, apr_bucket_brigade *bb) {
72     apr_bucket* b;
73     apr_status_t rs;
74     for ( b = APR_BRIGADE_FIRST(bb);
75           b != APR_BRIGADE_SENTINEL(bb);
76           b = APR_BUCKET_NEXT(b))
77     {
78         if (APR_BUCKET_IS_EOS(b)) {kl
79             break;
80         }
81         const char *buffer;
82         size_t bytes;
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");
85             return rs;
86         }
87         char *mine = apr_pstrmemdup(f->r->pool, buffer, bytes);
88         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "sending '%s'", mine);
89     }
90
91     ap_pass_brigade(f->next, bb);
92
93     return OK;
94 }
95 */
96
97 /**
98  * "main"
99  */
100 static int lua_handler(request_rec *r)
101 {
102     ap_lua_dir_cfg *dcfg;
103     if (strcmp(r->handler, "lua-script")) {
104         return DECLINED;
105     }
106
107     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "handling [%s] in mod_lua",
108                   r->filename);
109     dcfg = ap_get_module_config(r->per_dir_config, &lua_module);
110
111     if (!r->header_only) {
112         lua_State *L;
113         const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
114                                                       &lua_module);
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;
119
120         if (!d) {
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;
132             spec->cb_arg = NULL;
133             d->spec = spec;
134             d->function_name = "handle";
135         }
136
137         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
138                       "request details scope:%u, cache:%u, filename:%s, function:%s",
139                       d->spec->scope,
140                       d->spec->code_cache_style,
141                       d->spec->file,
142                       d->function_name);
143         L = ap_lua_get_lua_state(r->pool,
144                               d->spec);
145
146         if (!L) {
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;
151         }
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",
157                           d->function_name,
158                           d->spec->file);
159             return HTTP_INTERNAL_SERVER_ERROR;
160         }
161         ap_lua_run_lua_request(L, r);
162         if (lua_pcall(L, 1, 0, 0)) {
163             report_lua_error(L, r);
164         }
165     }
166     return OK;
167 }
168
169
170
171 /**
172  * Like mod_alias except for lua handler fun :-)
173  */
174 static int lua_alias_munger(request_rec *r)
175 {
176     ap_lua_vm_spec *spec;
177     ap_lua_request_cfg *rcfg = ap_get_module_config(r->request_config,
178                                                  &lua_module);
179     const ap_lua_dir_cfg *cfg =
180         ap_get_module_config(r->per_dir_config, &lua_module);
181     int i;
182     ap_regmatch_t matches[AP_MAX_REG_MATCH];
183
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];
187
188         if (OK ==
189             ap_regexec(cnd->uri_pattern, r->uri, AP_MAX_REG_MATCH, matches,
190                        0)) {
191             mapped_request_details *d;
192             r->handler = "lua-script";
193
194             spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
195             spec->file =
196                 ap_pregsub(r->pool, cnd->file_name, r->uri, AP_MAX_REG_MATCH,
197                            matches);
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;
204             }
205
206             d = apr_palloc(r->pool, sizeof(mapped_request_details));
207
208             d->function_name =
209                 ap_pregsub(r->pool, cnd->function_name, r->uri,
210                            AP_MAX_REG_MATCH, matches);
211             d->spec = spec;
212
213             /* now do replacement on method name where? */
214             r->filename = apr_pstrdup(r->pool, spec->file);
215             rcfg->mapped_request_details = d;
216             return OK;
217         }
218     }
219     return DECLINED;
220 }
221
222 /* ---------------- Configury stuff --------------- */
223
224 /** harnesses for magic hooks **/
225
226 static int lua_request_rec_hook_harness(request_rec *r, const char *name)
227 {
228     int rc;
229     lua_State *L;
230     ap_lua_vm_spec *spec;
231     ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config,
232                                                       &lua_module);
233     const ap_lua_dir_cfg *cfg =
234         (ap_lua_dir_cfg *) ap_get_module_config(r->per_dir_config,
235                                              &lua_module);
236     apr_array_header_t *hook_specs =
237         apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
238     if (hook_specs) {
239         int i;
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];
243
244             if (hook_spec == NULL) {
245                 continue;
246             }
247             spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
248
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;
260             spec->cb_arg = NULL;
261
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,
265                                   spec);
266
267
268
269             if (!L) {
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;
274             }
275
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;
284                 }
285
286                 ap_lua_run_lua_request(L, r);
287             }
288             else {
289                 int t;
290                 ap_lua_run_lua_request(L, r);
291
292                 t = lua_gettop(L);
293                 lua_setglobal(L, "r");
294                 lua_settop(L, t);
295             }
296
297             if (lua_pcall(L, 1, 1, 0)) {
298                 report_lua_error(L, r);
299                 return HTTP_INTERNAL_SERVER_ERROR;
300             }
301             rc = DECLINED;
302             if (lua_isnumber(L, -1)) {
303                 rc = lua_tointeger(L, -1);
304             }
305             if (rc != DECLINED) {
306                 return rc;
307             }
308         }
309     }
310     return DECLINED;
311 }
312
313
314 static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf,
315                                 size_t bufsiz)
316 {
317     apr_size_t i = 0;
318
319     if (cfg->getstr) {
320         apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param);
321         if (rc == APR_SUCCESS) {
322             i = strlen(buf);
323             if (i && buf[i - 1] == '\n')
324                 ++cfg->line_number;
325         }
326         else {
327             buf[0] = '\0';
328             i = 0;
329         }
330     }
331     else {
332         while (i < bufsiz) {
333             char ch;
334             apr_status_t rc = (cfg->getch) (&ch, cfg->param);
335             if (rc != APR_SUCCESS)
336                 break;
337             buf[i++] = ch;
338             if (ch == '\n') {
339                 ++cfg->line_number;
340                 break;
341             }
342         }
343     }
344     return i;
345 }
346
347 typedef struct cr_ctx
348 {
349     cmd_parms *cmd;
350     ap_configfile_t *cfp;
351     size_t startline;
352     const char *endstr;
353     char buf[HUGE_STRING_LEN];
354 } cr_ctx;
355
356
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.....
360  *
361  * be happy. this is cool.
362  *
363  */
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";
366 #define N_LF 32
367
368 static const char *direct_chunkreader(lua_State *lvm, void *udata,
369                                       size_t *plen)
370 {
371     const char *p;
372     struct cr_ctx *ctx = udata;
373
374     if (ctx->startline) {
375         *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
376         ctx->startline -= *plen;
377         return lf;
378     }
379     *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
380
381     for (p = ctx->buf; isspace(*p); ++p);
382     if (p[0] == '<' && p[1] == '/') {
383         apr_size_t i = 0;
384         while (i < strlen(ctx->endstr)) {
385             if (tolower(p[i + 2]) != ctx->endstr[i])
386                 return ctx->buf;
387             ++i;
388         }
389         *plen = 0;
390         return NULL;
391     }
392     /*fprintf(stderr, "buf read: %s\n", ctx->buf); */
393     return ctx->buf;
394 }
395
396 static int ldump_writer(lua_State *L, const void *b, size_t size, void *B)
397 {
398     (void) L;
399     luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
400     return 0;
401 }
402
403 typedef struct hack_section_baton
404 {
405     const char *name;
406     ap_lua_mapped_handler_spec *spec;
407 } hack_section_baton;
408
409 /* You can be unhappy now.
410  *
411  * This is uncool.
412  *
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 :-)
418  */
419 static const char *hack_section_handler(cmd_parms *cmd, void *_cfg,
420                                         const char *arg)
421 {
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;
425
426     apr_array_header_t *hook_specs =
427         apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
428     if (!hook_specs) {
429         hook_specs =
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);
433     }
434
435     baton->spec->scope = cfg->vm_scope;
436
437     *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = baton->spec;
438
439     return NULL;
440 }
441
442 static const char *register_named_block_function_hook(const char *name,
443                                                       cmd_parms *cmd,
444                                                       void *mconfig,
445                                                       const char *line)
446 {
447     const char *function;
448     ap_lua_mapped_handler_spec *spec;
449
450     if (line && line[0] == '>') {
451         function = NULL;
452     }
453     else {
454         const char *word;
455         apr_size_t wordlen;
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);
461         }
462         else {
463             function = apr_pstrndup(cmd->pool, word, wordlen - 1);
464         }
465     }
466
467     spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec));
468
469     {
470         cr_ctx ctx;
471         char buf[32];
472         lua_State *lvm;
473         char *tmp;
474         int rv;
475         ap_directive_t **current;
476         hack_section_baton *baton;
477
478         apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
479         spec->file_name =
480             apr_pstrcat(cmd->pool, cmd->config_file->name, ":", buf, NULL);
481         if (function) {
482             spec->function_name = (char *) function;
483         }
484         else {
485             function = NULL;
486         }
487         spec->code_cache_style = APL_CODE_CACHE_FOREVER;
488
489         ctx.cmd = cmd;
490         tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive + 1);
491         ap_str_tolower(tmp);
492         ctx.endstr = tmp;
493         ctx.cfp = cmd->config_file;
494         ctx.startline = cmd->config_file->line_number;
495
496         /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
497         lvm = luaL_newstate();
498
499         lua_settop(lvm, 0);
500
501         rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
502
503         if (rv != 0) {
504             const char *errstr =
505                 apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1),
506                             NULL);
507             lua_close(lvm);
508             return errstr;
509         }
510         else {
511             luaL_Buffer b;
512             luaL_buffinit(lvm, &b);
513             lua_dump(lvm, ldump_writer, &b);
514             luaL_pushresult(&b);
515             spec->bytecode_len = lua_strlen(lvm, -1);
516             spec->bytecode =
517                 apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
518                                spec->bytecode_len);
519             lua_close(lvm);
520         }
521
522         current = mconfig;
523
524         /* Here, we have to replace our current config node for the next pass */
525         if (!*current) {
526             *current = apr_pcalloc(cmd->pool, sizeof(**current));
527         }
528
529         baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
530         baton->name = name;
531         baton->spec = spec;
532
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;
539     }
540
541     return NULL;
542 }
543
544 static const char *register_named_file_function_hook(const char *name,
545                                                      cmd_parms *cmd,
546                                                      void *_cfg,
547                                                      const char *file,
548                                                      const char *function)
549 {
550     ap_lua_mapped_handler_spec *spec;
551     ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
552
553     apr_array_header_t *hook_specs =
554         apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
555     if (!hook_specs) {
556         hook_specs =
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);
560     }
561
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;
567     /*
568        int code_cache_style;
569        char *function_name;
570        char *file_name;
571        int scope;
572      */
573     *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec;
574     return NULL;
575 }
576
577 static int lua_check_user_id_harness(request_rec *r)
578 {
579     return lua_request_rec_hook_harness(r, "check_user_id");
580 }
581
582 static int lua_translate_name_harness(request_rec *r)
583 {
584     return lua_request_rec_hook_harness(r, "translate_name");
585 }
586
587 static int lua_fixup_harness(request_rec *r)
588 {
589     return lua_request_rec_hook_harness(r, "fixups");
590 }
591
592 static int lua_map_to_storage_harness(request_rec *r)
593 {
594     return lua_request_rec_hook_harness(r, "map_to_storage");
595 }
596
597 static int lua_type_checker_harness(request_rec *r)
598 {
599     return lua_request_rec_hook_harness(r, "type_checker");
600 }
601
602 static int lua_access_checker_harness(request_rec *r)
603 {
604     return lua_request_rec_hook_harness(r, "access_checker");
605 }
606
607 static int lua_auth_checker_harness(request_rec *r)
608 {
609     return lua_request_rec_hook_harness(r, "auth_checker");
610 }
611
612 static void lua_insert_filter_harness(request_rec *r)
613 {
614     /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
615 }
616
617 static int lua_quick_harness(request_rec *r, int lookup)
618 {
619     if (lookup) {
620         return DECLINED;
621     }
622     return lua_request_rec_hook_harness(r, "quick");
623 }
624
625 static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg,
626                                                 const char *file,
627                                                 const char *function)
628 {
629     return register_named_file_function_hook("translate_name", cmd, _cfg,
630                                              file, function);
631 }
632
633 static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg,
634                                                  const char *line)
635 {
636     return register_named_block_function_hook("translate_name", cmd, _cfg,
637                                               line);
638 }
639
640
641 static const char *register_fixups_hook(cmd_parms *cmd, void *_cfg,
642                                         const char *file,
643                                         const char *function)
644 {
645     return register_named_file_function_hook("fixups", cmd, _cfg, file,
646                                              function);
647 }
648 static const char *register_fixups_block(cmd_parms *cmd, void *_cfg,
649                                          const char *line)
650 {
651     return register_named_block_function_hook("fixups", cmd, _cfg, line);
652 }
653
654 static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
655                                                 const char *file,
656                                                 const char *function)
657 {
658     return register_named_file_function_hook("map_to_storage", cmd, _cfg,
659                                              file, function);
660 }
661 static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
662                                                  const char *line)
663 {
664     return register_named_block_function_hook("map_to_storage", cmd, _cfg,
665                                               line);
666 }
667
668 static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
669                                                const char *file,
670                                                const char *function)
671 {
672     return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
673                                              function);
674 }
675 static const char *register_check_user_id_block(cmd_parms *cmd, void *_cfg,
676                                                 const char *line)
677 {
678     return register_named_block_function_hook("check_user_id", cmd, _cfg,
679                                               line);
680 }
681
682 static const char *register_type_checker_hook(cmd_parms *cmd, void *_cfg,
683                                               const char *file,
684                                               const char *function)
685 {
686     return register_named_file_function_hook("type_checker", cmd, _cfg, file,
687                                              function);
688 }
689 static const char *register_type_checker_block(cmd_parms *cmd, void *_cfg,
690                                                const char *line)
691 {
692     return register_named_block_function_hook("type_checker", cmd, _cfg,
693                                               line);
694 }
695
696 static const char *register_access_checker_hook(cmd_parms *cmd, void *_cfg,
697                                                 const char *file,
698                                                 const char *function)
699 {
700     return register_named_file_function_hook("access_checker", cmd, _cfg,
701                                              file, function);
702 }
703 static const char *register_access_checker_block(cmd_parms *cmd, void *_cfg,
704                                                  const char *line)
705 {
706     return register_named_block_function_hook("access_checker", cmd, _cfg,
707                                               line);
708 }
709
710 static const char *register_auth_checker_hook(cmd_parms *cmd, void *_cfg,
711                                               const char *file,
712                                               const char *function)
713 {
714     return register_named_file_function_hook("auth_checker", cmd, _cfg, file,
715                                              function);
716 }
717 static const char *register_auth_checker_block(cmd_parms *cmd, void *_cfg,
718                                                const char *line)
719 {
720     return register_named_block_function_hook("auth_checker", cmd, _cfg,
721                                               line);
722 }
723
724 static const char *register_insert_filter_hook(cmd_parms *cmd, void *_cfg,
725                                                const char *file,
726                                                const char *function)
727 {
728     return "LuaHookInsertFilter not yet implemented";
729 }
730
731 static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
732                                        const char *file, const char *function)
733 {
734     return register_named_file_function_hook("quick", cmd, _cfg, file,
735                                              function);
736 }
737 static const char *register_quick_block(cmd_parms *cmd, void *_cfg,
738                                         const char *line)
739 {
740     return "LuaQuickHook in an inline block not yet implemented";
741 }
742
743
744
745 static const char *register_package_helper(cmd_parms *cmd, const char *arg,
746                                            apr_array_header_t *dir_array)
747 {
748     apr_status_t rv;
749
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);
758     }
759
760     *(const char **) apr_array_push(dir_array) = fixed_filename;
761     return NULL;
762 }
763
764
765 /**
766  * Called for config directive which looks like
767  * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
768  */
769 static const char *register_package_dir(cmd_parms *cmd, void *_cfg,
770                                         const char *arg)
771 {
772     ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
773
774     return register_package_helper(cmd, arg, cfg->package_paths);
775 }
776
777 /**
778  * Called for config directive which looks like
779  * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
780  */
781 static const char *register_package_cdir(cmd_parms *cmd, void *_cfg,
782                                          const char *arg)
783 {
784     ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
785
786     return register_package_helper(cmd, arg, cfg->package_cpaths);
787 }
788
789 /**
790  * Called for config directive which looks like
791  * LuaCodeCache
792  */
793 static const char *register_code_cache(cmd_parms *cmd, void *_cfg,
794                                        const char *arg)
795 {
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;
799     }
800     else if (strcmp("forever", arg) == 0) {
801         cfg->code_cache_style = APL_CODE_CACHE_FOREVER;
802     }
803     else if (strcmp("never", arg) == 0) {
804         cfg->code_cache_style = APL_CODE_CACHE_NEVER;
805     }
806     else {
807         return apr_psprintf(cmd->pool,
808                             "Invalid value for LuaCodeCache, '%s', acceptable values are %s",
809                             arg, "'stat', 'forever', and 'never'");
810     }
811     return NULL;
812 }
813
814 static const char *register_lua_scope(cmd_parms *cmd, void *_cfg,
815                                       const char *scope, const char *min,
816                                       const char *max)
817 {
818     ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
819     if (strcmp("once", scope) == 0) {
820         cfg->vm_scope = APL_SCOPE_ONCE;
821     }
822     else if (strcmp("request", scope) == 0) {
823         cfg->vm_scope = APL_SCOPE_REQUEST;
824     }
825     else if (strcmp("conn", scope) == 0) {
826         cfg->vm_scope = APL_SCOPE_CONN;
827     }
828     else if (strcmp("server", scope) == 0) {
829         cfg->vm_scope = APL_SCOPE_SERVER;
830         if (min)
831             cfg->vm_server_pool_min = atoi(min);
832         if (max)
833             cfg->vm_server_pool_max = atoi(max);
834     }
835     else {
836         return apr_psprintf(cmd->pool,
837                             "Invalid value for LuaScope, '%s', acceptable values are %s",
838                             scope, "'once', 'request', 'conn', and 'server'");
839     }
840     return NULL;
841 }
842
843
844 /**
845  * Called for config directive which looks like
846  * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
847  */
848 static const char *lua_map_handler(cmd_parms *cmd, void *_cfg,
849                                    const char *path, const char *file,
850                                    const char *function)
851 {
852     ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
853     apr_status_t rv;
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);
861     }
862     return NULL;
863 }
864
865 static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
866                                      const char *root)
867 {
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);
871
872     cfg->root_path = root;
873     return NULL;
874 }
875
876 /*******************************/
877
878 command_rec lua_commands[] = {
879
880     AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL,
881                   "Specify the base path for resolving relative paths for mod_lua directives"),
882
883
884     AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL,
885                   "Add a directory to lua's package.path"),
886
887     AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL,
888                   "Add a directory to lua's package.cpath"),
889
890     AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL,
891                    "Map a path to a lua handler"),
892
893     AP_INIT_TAKE2("LuaHookTranslateName", register_translate_name_hook, NULL,
894                   OR_ALL,
895                   "Provide a hook for the translate name phase of request processing"),
896     AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block,
897                      NULL,
898                      EXEC_ON_READ | OR_ALL,
899                      "Provide a hook for the translate name phase of request processing"),
900
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"),
906
907 /* todo: test */
908     AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL,
909                   OR_ALL,
910                   "Provide a hook for the map_to_storage phase of request processing"),
911     AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block,
912                      NULL,
913                      EXEC_ON_READ | OR_ALL,
914                      "Provide a hook for the map_to_storage phase of request processing"),
915
916     /* todo: test */
917     AP_INIT_TAKE2("LuaHookCheckUserID", register_check_user_id_hook, NULL,
918                   OR_ALL,
919                   "Provide a hook for the check_user_id phase of request processing"),
920     AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block,
921                      NULL,
922                      EXEC_ON_READ | OR_ALL,
923                      "Provide a hook for the check_user_id phase of request processing"),
924
925     /* todo: test */
926     AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL,
927                   OR_ALL,
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"),
932
933     /* todo: test */
934     AP_INIT_TAKE2("LuaHookAccessChecker", register_access_checker_hook, NULL,
935                   OR_ALL,
936                   "Provide a hook for the access_checker phase of request processing"),
937     AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block,
938                      NULL,
939                      EXEC_ON_READ | OR_ALL,
940                      "Provide a hook for the access_checker phase of request processing"),
941
942     /* todo: test */
943     AP_INIT_TAKE2("LuaHookAuthChecker", register_auth_checker_hook, NULL,
944                   OR_ALL,
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"),
949
950     /* todo: test */
951     AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
952                   OR_ALL,
953                   "Provide a hook for the insert_filter phase of request processing"),
954
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"),
958
959     AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
960                     "One of once, request, conn, server -- default is once"),
961
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"),
967
968     AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL,
969                      OR_ALL,
970                      "(internal) Byte code handler"),
971     {NULL}
972 };
973
974
975 static void *create_dir_config(apr_pool_t *p, char *dir)
976 {
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;
983     cfg->pool = p;
984     cfg->hooks = apr_hash_make(p);
985     cfg->dir = apr_pstrdup(p, dir);
986     cfg->vm_scope = APL_SCOPE_ONCE;
987     return cfg;
988 }
989
990 static int create_request_config(request_rec *r)
991 {
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);
996     return OK;
997 }
998
999 static void *create_server_config(apr_pool_t *p, server_rec *s)
1000 {
1001
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;
1010
1011     return cfg;
1012 }
1013
1014 static int lua_request_hook(lua_State *L, request_rec *r)
1015 {
1016     ap_lua_push_request(L, r);
1017     return OK;
1018 }
1019
1020 static void lua_register_hooks(apr_pool_t *p)
1021 {
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,
1025                            APR_HOOK_MIDDLE);
1026
1027     /* http_request.h hooks */
1028     ap_hook_translate_name(lua_translate_name_harness, NULL, NULL,
1029                            APR_HOOK_MIDDLE);
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,
1032                            APR_HOOK_MIDDLE);
1033     ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
1034                           APR_HOOK_MIDDLE);
1035     ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
1036                          APR_HOOK_MIDDLE);
1037     ap_hook_access_checker(lua_access_checker_harness, NULL, NULL,
1038                            APR_HOOK_MIDDLE);
1039     ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL,
1040                          APR_HOOK_MIDDLE);
1041     ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL,
1042                           APR_HOOK_MIDDLE);
1043     ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
1044
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);
1047
1048     APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
1049                       APR_HOOK_REALLY_FIRST);
1050
1051     APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
1052                       APR_HOOK_REALLY_FIRST);
1053 }
1054
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                      */
1063 };