]> granicus.if.org Git - apache/commitdiff
Initial import of mod_wombat to the modules directory.
authorPaul Querna <pquerna@apache.org>
Fri, 5 Dec 2008 06:45:13 +0000 (06:45 +0000)
committerPaul Querna <pquerna@apache.org>
Fri, 5 Dec 2008 06:45:13 +0000 (06:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/wombat-integration@723615 13f79535-47bb-0310-9956-ffa450edef68

29 files changed:
modules/wombat/README [new file with mode: 0644]
modules/wombat/apr_lua.c [new file with mode: 0644]
modules/wombat/apr_lua.h [new file with mode: 0644]
modules/wombat/config.c [new file with mode: 0644]
modules/wombat/config.h [new file with mode: 0644]
modules/wombat/docs/README [new file with mode: 0644]
modules/wombat/docs/basic-configuration.txt [new file with mode: 0644]
modules/wombat/docs/building-from-subversion.txt [new file with mode: 0644]
modules/wombat/docs/running-developer-tests.txt [new file with mode: 0644]
modules/wombat/docs/writing-handlers.txt [new file with mode: 0644]
modules/wombat/mod_wombat.c [new file with mode: 0644]
modules/wombat/mod_wombat.h [new file with mode: 0644]
modules/wombat/request.c [new file with mode: 0644]
modules/wombat/request.h [new file with mode: 0644]
modules/wombat/test/helpers.lua [new file with mode: 0644]
modules/wombat/test/htdocs/config_tests.lua [new file with mode: 0644]
modules/wombat/test/htdocs/filters.lua [new file with mode: 0644]
modules/wombat/test/htdocs/find_me.txt [new file with mode: 0644]
modules/wombat/test/htdocs/hooks.lua [new file with mode: 0644]
modules/wombat/test/htdocs/other.lua [new file with mode: 0644]
modules/wombat/test/htdocs/simple.lua [new file with mode: 0644]
modules/wombat/test/htdocs/test.lua [new file with mode: 0755]
modules/wombat/test/httpd_config.lua [new file with mode: 0644]
modules/wombat/test/lib/kangaroo.lua [new file with mode: 0644]
modules/wombat/test/moonunit.lua [new file with mode: 0644]
modules/wombat/test/test.lua [new file with mode: 0755]
modules/wombat/test/test_httpd.conf [new file with mode: 0755]
modules/wombat/vmprep.c [new file with mode: 0644]
modules/wombat/vmprep.h [new file with mode: 0644]

diff --git a/modules/wombat/README b/modules/wombat/README
new file mode 100644 (file)
index 0000000..1ca9cd6
--- /dev/null
@@ -0,0 +1,77 @@
+-*- mode:org -*-
+* Requirements:
+** lua 5.1 ( http://www.lua.org/ )
+** libapreq2 ( http://httpd.apache.org/apreq/download.cgi )
+** Apache HTTPD 2.2 ( http://httpd.apache.org/ )
+
+* Documentation
+  See docs/README
+
+* Building
+  For now, see docs/building-from-subversion.txt
+
+* To Consider
+  Allow definition of lua_State instances associated with arbitrary
+  pool using the pool's user_data constuct. There would, here, be two
+  types, pooled and singleton. On the other hand, singleton would work
+  fine for almost all cases -- the exception being a process or server
+  pool, and then we could stay singleton anyway and lock around it.
+
+  The current "server scope" behavior could, instead, fall into
+  connection scope, for long-lived connections, really we want thread
+  scope (which Brian Akins knows how to do). Is there a pool
+  associated with a thread? Contention on the pool is a pain in a
+  highly concurrent environment.
+
+  Could use apr_thread_data_(get|set) if I can find a way to hook into
+  thread destruction. Looks like apr threads let you use the standard
+  APR_POOL_DECLARE_ACCESSOR(thread); defined method, just need to look
+  up what form that takes. -- apr_thread_pool_get -- just attach to
+  that pool.
+
+  Given that, we can associate a hash of lua_State instances with
+  arbitrary pools, such as the request pool, thread pool, server pool,
+  etc. We then use the file as key into the hash. Users, able to
+  specify the handler function, can then make use of the same file
+  with different handlers to reuse states.
+
+  
+
+* Task List
+** TODO Use r->file to determine file, doing rewriting in translate_name   
+** TODO Change to controlling lifecycle by passing in a pool?
+   Need to determine how to handle server scoped then!
+** TODO Provide means to get useful output from lua errors in response body
+   Probably have to put it on the vm spec for pre-handler errors, as
+   it is pre-handler, will prolly be on the request_config somewhere,
+   but sometimes cannot put there, so... fun
+** TODO Filters
+** TODO Mapping in the server_rec
+** TODO Connection scoped vms
+** TODO Figure out how reentrancy works regarding filter chain stuff. 
+   Do we need new "threads"?
+** TODO Flesh out apw_*getvm for each flavor we allow
+** TODO Rework apw_sgetvm to use the create_vm stuff like apw_rgetvm
+** TODO apw_rgetvm needs to handle connection scoped vms     
+** TODO options in server scoped vms (ie, min and max vm counts)
+    
+* License
+  Apache License, Version 2.0,
+  
+  http://www.apache.org/licenses/LICENSE-2.0 
+
+  See NOTICE file for more information
+        
+* Problems and Patches:
+  Please use dev@httpd.apache.org for discussing mod_wombat development
+  To subscribe send email to dev-subscribe@httpd.apache.org  
+  Note that this is for development discussion, not user support :-)
+   
+* Contributors Include
+** Brian McCallister
+** Paul Querna
+** Garrett Rooney
+** Martin Traverso
+** Brian Akins
+** Justin Erenkrantz
+** Philip M. Gollucci
diff --git a/modules/wombat/apr_lua.c b/modules/wombat/apr_lua.c
new file mode 100644 (file)
index 0000000..e4d369b
--- /dev/null
@@ -0,0 +1,71 @@
+#include "apr.h"
+#include "apr_tables.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+/**
+ * make a userdata out of a C pointer, and vice versa
+ * instead of using lightuserdata
+ */
+#ifndef lua_boxpointer
+#define lua_boxpointer(L,u) (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
+#define lua_unboxpointer(L,i)  (*(void **)(lua_touserdata(L, i)))
+#endif
+
+
+apr_table_t* check_apr_table(lua_State* L, int index) {
+    luaL_checkudata(L, index, "Apr.Table");
+    apr_table_t* t = (apr_table_t*)lua_unboxpointer(L, index);
+    return t;
+}
+
+
+void apw_push_apr_table(lua_State* L, const char *name, apr_table_t *t) {
+    lua_boxpointer(L, t);    
+    luaL_getmetatable(L, "Apr.Table");
+    lua_setmetatable(L, -2);
+    lua_setfield(L, -2, name);
+}
+
+static int lua_table_set(lua_State* L) {
+    apr_table_t *t = check_apr_table(L, 1);
+    const char* key = luaL_checkstring(L, 2);
+    const char* val = luaL_checkstring(L, 3);
+
+    apr_table_set(t, key, val);
+    return 0;
+}
+
+static int lua_table_get(lua_State* L) {
+    apr_table_t *t = check_apr_table(L, 1);
+    const char* key = luaL_checkstring(L, 2);
+    const char *val = apr_table_get(t, key);
+    lua_pushstring(L, val);
+    return 1;
+}
+
+static const luaL_reg lua_table_methods[] = {
+    {"set", lua_table_set},
+    {"get", lua_table_get},
+    {0, 0}
+};
+
+
+int apr_lua_init(lua_State *L, apr_pool_t *p) {
+    luaL_newmetatable(L, "Apr.Table");
+    luaL_openlib(L, "apr_table", lua_table_methods, 0);
+    lua_pushstring(L, "__index");
+    lua_pushstring(L, "get");
+    lua_gettable(L, 2);
+    lua_settable(L, 1);
+
+    lua_pushstring(L, "__newindex");
+    lua_pushstring(L, "set");
+    lua_gettable(L, 2);
+    lua_settable(L, 1);
+    
+    return 0;
+}
+
diff --git a/modules/wombat/apr_lua.h b/modules/wombat/apr_lua.h
new file mode 100644 (file)
index 0000000..162a65a
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef _APR_LUA_H_
+#define _APR_LUA_H_
+
+int apr_lua_init(lua_State *L, apr_pool_t *p);
+apr_table_t* check_apr_table(lua_State* L, int index);
+void apw_push_apr_table(lua_State* L, const char *name, apr_table_t *t);
+
+#endif
diff --git a/modules/wombat/config.c b/modules/wombat/config.c
new file mode 100644 (file)
index 0000000..c4e1d7e
--- /dev/null
@@ -0,0 +1,197 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "config.h"
+#include "vmprep.h"
+
+static apw_dir_cfg* check_dir_config(lua_State* L, int index) {
+    luaL_checkudata(L, index, "Apache2.DirConfig");
+    apw_dir_cfg *cfg = (apw_dir_cfg*)lua_unboxpointer(L, index);
+    return cfg;    
+}
+
+static cmd_parms* check_cmd_parms(lua_State* L, int index) {
+    luaL_checkudata(L, index, "Apache2.CommandParameters");
+    cmd_parms *cmd = (cmd_parms*)lua_unboxpointer(L, index);
+    return cmd;    
+}
+
+static int apw_toscope(const char *name) {
+    if (0 == apr_strnatcmp("once", name)) return APW_SCOPE_ONCE;
+    if (0 == apr_strnatcmp("request", name)) return APW_SCOPE_REQUEST;
+    if (0 == apr_strnatcmp("connection", name)) return APW_SCOPE_CONN;
+    if (0 == apr_strnatcmp("conn", name)) return APW_SCOPE_CONN;
+    if (0 == apr_strnatcmp("server", name)) return APW_SCOPE_SERVER;
+    return APW_SCOPE_ONCE;
+}
+
+apr_status_t apw_lua_map_handler(apw_dir_cfg *cfg, 
+                                 const char *file, 
+                                 const char *function,
+                                 const char *pattern,
+                                 const char *scope) {
+    apr_status_t rv;
+    apw_mapped_handler_spec *handler = apr_palloc(cfg->pool, sizeof(apw_mapped_handler_spec));
+    handler->uri_pattern = NULL;
+    handler->function_name = NULL;
+    
+    ap_regex_t *uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
+    if ((rv = ap_regcomp(uri_pattern, pattern, 0)) != APR_SUCCESS) {
+        return rv;
+    }
+    handler->file_name = apr_pstrdup(cfg->pool, file);
+    handler->uri_pattern = uri_pattern;
+    handler->scope = apw_toscope(scope);
+    
+    handler->function_name = apr_pstrdup(cfg->pool, function);
+    *(const apw_mapped_handler_spec**)apr_array_push(cfg->mapped_handlers) = handler;    
+    return APR_SUCCESS;
+}
+
+/* Change to use apw_lua_map_handler */
+static int cfg_lua_map_handler(lua_State *L) {
+    apw_dir_cfg *cfg = check_dir_config(L, 1);
+    apw_mapped_handler_spec *handler = apr_palloc(cfg->pool, sizeof(apw_mapped_handler_spec));
+    handler->uri_pattern = NULL;
+    handler->function_name = NULL;
+    
+    luaL_checktype(L, 2, LUA_TTABLE);
+    lua_getfield(L, 2, "file");
+    if (lua_isstring(L, -1)) {
+        const char *file = lua_tostring(L, -1);
+        handler->file_name = apr_pstrdup(cfg->pool, file);
+    }
+    lua_pop(L, 1);
+    
+    lua_getfield(L, 2, "pattern");
+    if (lua_isstring(L, -1)) {
+        const char *pattern = lua_tostring(L, -1);
+        
+        ap_regex_t *uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
+        if (ap_regcomp(uri_pattern, pattern, 0) != OK) {
+            return luaL_error(L, "Unable to compile regular expression, '%s'", pattern);
+        }
+        handler->uri_pattern = uri_pattern;
+    }
+    lua_pop(L, 1);
+    
+    lua_getfield(L, 2, "scope");
+    if (lua_isstring(L, -1)) {
+        const char *scope = lua_tostring(L, -1);
+        handler->scope = apw_toscope(scope);
+    }
+    else {
+        handler->scope = APW_SCOPE_ONCE;
+    }
+    lua_pop(L, 1);
+    
+    lua_getfield(L, 2, "func");
+    if (lua_isstring(L, -1)) {
+        const char *value = lua_tostring(L, -1);
+        handler->function_name = apr_pstrdup(cfg->pool, value);
+    }
+    else {
+        handler->function_name = "handle";
+    }
+    lua_pop(L, 1);
+    
+    
+    *(const apw_mapped_handler_spec**)apr_array_push(cfg->mapped_handlers) = handler;    
+    return 0;
+}
+
+static int cfg_directory(lua_State *L) {
+    apw_dir_cfg *cfg = check_dir_config(L, 1);
+    lua_pushstring(L, cfg->dir);
+    return 1;
+}
+
+/*static int cfg_root(lua_State *L) {
+    apw_dir_cfg *cfg = check_dir_config(L, 1);
+    lua_pushstring(L, cfg->root_path);
+    return 1;
+}*/
+
+static const struct luaL_Reg cfg_methods[] = {
+    {"match_handler", cfg_lua_map_handler},
+    {"directory", cfg_directory},
+   /* {"root", cfg_root}, */
+    {NULL, NULL}
+};
+
+
+
+static int cmd_foo(lua_State *L) {
+    cmd_parms *cmd = check_cmd_parms(L, 1);
+    ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server, "FOO!");
+    return 0;
+}
+
+/* helper function for the logging functions below */
+static int cmd_log_at(lua_State* L, int level) {
+    cmd_parms *cmd = check_cmd_parms(L, 1);
+    lua_Debug dbg;
+    
+    lua_getstack(L, 1, &dbg);
+    lua_getinfo(L, "Sl", &dbg);
+
+    const char* msg = luaL_checkstring(L, 2);
+    ap_log_error(dbg.source, dbg.currentline, level, 0, cmd->server, msg);
+    return 0;
+}
+
+/* r:debug(String) and friends which use apache logging */
+static int cmd_emerg(lua_State* L)  { cmd_log_at(L, APLOG_EMERG); return 0; }
+static int cmd_alert(lua_State* L)  { cmd_log_at(L, APLOG_ALERT); return 0; }
+static int cmd_crit(lua_State* L)   { cmd_log_at(L, APLOG_CRIT); return 0; }
+static int cmd_err(lua_State* L)    { cmd_log_at(L, APLOG_ERR); return 0; }
+static int cmd_warn(lua_State* L)   { cmd_log_at(L, APLOG_WARNING); return 0; }
+static int cmd_notice(lua_State* L) { cmd_log_at(L, APLOG_NOTICE); return 0; }
+static int cmd_info(lua_State* L)   { cmd_log_at(L, APLOG_INFO); return 0; }
+static int cmd_debug(lua_State* L)  { cmd_log_at(L, APLOG_DEBUG); return 0; }
+
+
+static const struct luaL_Reg cmd_methods[] = {    
+    {"foo", cmd_foo},
+    
+    {"debug",   cmd_debug},
+    {"info",    cmd_info},
+    {"notice",  cmd_notice},
+    {"warn",    cmd_warn},
+    {"err",     cmd_err},
+    {"crit",    cmd_crit},
+    {"alert",   cmd_alert},
+    {"emerg",   cmd_emerg},
+
+    {NULL, NULL}
+};
+
+void apw_load_config_lmodule(lua_State *L) {
+    luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */
+    lua_pushvalue(L, -1); 
+
+    lua_setfield(L, -2, "__index"); 
+    luaL_register(L, NULL, cfg_methods); /* [metatable] */
+    
+    
+    luaL_newmetatable(L, "Apache2.CommandParameters");
+    lua_pushvalue(L, -1); 
+
+    lua_setfield(L, -2, "__index"); 
+    luaL_register(L, NULL, cmd_methods); /* [metatable] */
+    
+}
diff --git a/modules/wombat/config.h b/modules/wombat/config.h
new file mode 100644 (file)
index 0000000..0ff12f9
--- /dev/null
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "mod_wombat.h"
+
+#ifndef APW_CONFIG_H
+#define APW_CONFIG_H
+                 
+APR_DECLARE(void) apw_load_config_lmodule(lua_State *L);                                                                 
+
+APR_DECLARE(apr_status_t) apw_lua_map_handler(apw_dir_cfg *cfg, 
+                                              const char *file, 
+                                              const char *function,
+                                              const char *pattern,
+                                              const char *scope);
+
+#endif
+
diff --git a/modules/wombat/docs/README b/modules/wombat/docs/README
new file mode 100644 (file)
index 0000000..632945a
--- /dev/null
@@ -0,0 +1,12 @@
+Index of documents:
+    building-from-subversion.txt
+        Basic build instructions
+        
+    basic-configuration.txt
+        Getting mod_wombat up and running
+        
+    running-developer-tests.txt
+        How to set up and run the developer and regression tests
+        
+    writing-handlers.txt
+        basics on writing handlers in mod_wombat
\ No newline at end of file
diff --git a/modules/wombat/docs/basic-configuration.txt b/modules/wombat/docs/basic-configuration.txt
new file mode 100644 (file)
index 0000000..5e6a6d3
--- /dev/null
@@ -0,0 +1,143 @@
+See sample_httpd.conf for examples
+
+The basic module loading directive is
+    LoadModule apreq_module modules/mod_apreq2.so
+    LoadModule wombat_module modules/mod_wombat.so
+I included the apreq_module in the example as you need it to :-)
+
+The handler name is "lua-script" so you can use the normal
+AddHandler directive, such as "AddHandler lua-script .lua" to
+set anything ending in .lua to use mod_wombat to evaluate
+
+mod_wombat exports several additional directives:
+
+    LuaRoot /path/to/a/directory
+        Specify the base path which will be used to evaluate all
+        relative paths within mod_wombat. If not specified they
+        will be resolved relative to the current working directory,
+        which may not always work well for a server.
+
+    LuaScope once|request|conn|server [max|min max]
+        Specify the lifecycle scope of the Lua interpreter which will
+        be used by handlers in this "Directory." The default is "once"
+        
+        once: use the interpreter once and throw it away.
+        
+        request: use the interpreter to handle anything based on 
+                 the same file within this request, which is also 
+                 request scoped.
+                 
+        conn: Same as request but attached to the connection_rec
+        
+        server: This one is different than others because the
+                server scope is quite long lived, and multiple threads
+                will have the same server_rec. To accommodate this
+                server scoped interpreter are stored in an apr
+                resource list. The min and max arguments are intended
+                to specify the pool size, but are unused at this time.
+
+    LuaMapHandler uri-pattern /path/to/lua/script.lua [function-name]
+        This directive matches a uri pattern to invoke a specific
+        handler function in a specific file. It uses PCRE regular
+        expressions to match the uri, and supports interpolating
+        match groups into both the file path and the function name
+        be careful writing your regular expressions to avoid security
+        issues.
+        
+        Examples:
+            LuaMapHandler /(\w+)/(/w+) /scripts/$1.lua handle_$2
+                This would match uri's such as /photos/show?id=9
+                to the file /scripts/photos.lua and invoke the
+                handler function handle_show on the lua vm after
+                loading that file.
+                
+            LuaMapHandler /bingo /scripts/wombat.lua
+                This would invoke the "handle" function, which
+                is the default if no specific function name is
+                provided.
+    
+    LuaPackagePath /path/to/include/?.lua
+        Add a path to lua's module search path. Follows the same
+        conventions as lua. This just munges the package.path in the 
+        lua vms.
+        
+        Examples:
+            LuaPackagePath /scripts/lib/?.lua
+            LuaPackagePath /scripts/lib/?/init.lua
+
+    LuaPackageCPath /path/to/include/?.soa
+        Add a path to lua's shared library search path. Follows the same
+        conventions as lua. This just munges the package.cpath in the 
+        lua vms.
+        
+        Examples:
+            LuaPackagePath /scripts/lib/?.so
+
+    LuaCodeCache stat|forever|never
+        Specify the behavior of the in-memory code cache. The default
+        is stat, which stats the top level script (not any included
+        ones) each time that file is needed, and reloads it if the
+        modified time indicates it is newer than the one it has
+        already loaded. The other values cause it to keep the file
+        cached forever (don't stat and replace) or to never cache the 
+        file.
+        
+        In general stat or forever is good production and stat or never
+        for deveopment.
+        
+        Examples:
+            LuaCodeCache stat
+            LuaCodeCache forever
+            LuaCodeCache never
+    
+    LuaHookTranslateName  /path/to/lua/script.lua  hook_function_name
+        Add a hook (at APR_HOOK_MIDDLE) to the translate name phase of
+        request processing. The hook function receives a single
+        argument, the request_rec, and should return a status code, 
+        which is either an HTTP error code, or the constants defined
+        in the apache2 module: apache2.OK, apache2.DECLINED, or
+        apache2.DONE. 
+
+        For those new to hooks, basically each hook will be invoked
+        until one of them returns apache2.OK. If your hook doesn't
+        want to do the translation it should just return
+        apache2.DECLINED. If the request should stop processing, then
+        return apache2.DONE.
+
+        Example:
+            LuaHookTranslateName /scripts/conf/hooks.lua silly_mapper
+
+            -- /scripts/conf/hooks.lua --
+            function silly_mapper(r)
+                if r.uri == "/" then
+                    r.file = "/var/www/home.lua"
+                    return apache2.OK
+                else
+                    return apache2.DECLINED
+                end
+            end
+
+    LuaHookFixups  /path/to/lua/script.lua  hook_function_name
+        Just like LuaHookTranslateName, but executed at the fixups phase
+
+    LuaHookMapToStorage  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookCheckUserID  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookTypeChecker  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookAuthChecker  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookAccessChecker  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookAuthChecker  /path/to/lua/script.lua  hook_function_name
+        ...
+
+    LuaHookInsertFilter  /path/to/lua/script.lua  hook_function_name
+        Not Yet Implemented
diff --git a/modules/wombat/docs/building-from-subversion.txt b/modules/wombat/docs/building-from-subversion.txt
new file mode 100644 (file)
index 0000000..98da7bf
--- /dev/null
@@ -0,0 +1,72 @@
+Install Lua 5.1
+    http://www.lua.org/download.html
+    
+    Lua does not use autoconf for compiling. This means that you do not use
+    ./configure. It has good build instructions, though, so hopefully things
+    will go smoothly.
+    
+    I like to change the directory Lua installs to. In order to do this you
+    need to set LUA_TOP in the configuration makefile for Lua. For these 
+    instructions I have set LUA_TOP to /Users/brianm/.opt/lua-5.1.2 -- you
+    will see this directory referred to later.
+    
+    
+Install Apache HTTPD 2.2
+    http://httpd.apache.org/download.cgi
+    
+    You can build apache pretty much any way you like, as long as you enable
+    dynamic module loading (--enable-so) so that mod_wombat can be loaded.
+    
+    You may user (and I encourage you to!) the threaded MPMs -- mod_wombat
+    plays nicely with them.
+
+    I build it with these flags:
+        
+    ./configure --prefix=/Users/brianm/.opt/httpd-2.2.4-worker-wombat \
+                --with-mpm=worker  \
+                --enable-so
+    
+    
+Install libapreq2
+    http://httpd.apache.org/apreq/download.cgi
+        The download link is in the page body, NOT under the "Download!" link
+        in the left hand column.
+    
+    Right now, mod_wombat requires libapreq2 for parsing entity bodies. This
+    dependency will probably be made optional in the near future, but for now
+    you need it.
+    
+    I build it with these flags:
+    
+    ./configure --prefix=/Users/brianm/.opt/libapreq2-2.0.8 \
+      --with-apache2-apxs=/Users/brianm/.opt/httpd-2.2.4-worker-wombat/bin/apxs
+    
+    
+Install mod_wombat from subversion
+    http://svn.apache.org/repos/asf/httpd/mod_wombat/trunk
+    
+    The first step, when building from subversion, is to bootstrap autoconf. 
+    To do this run the bootstrap script:
+    
+    ./bootstrap
+    
+    The bootstrap script may report an error that it cannot find
+    libtoolize or glibtoolize. That is fine as long as it 
+    doesn't report that it cannot find both of them. The script
+    just sets up the autoconf magic. 
+    
+    After that, it is a normal configure and build:
+    
+    ./configure  --with-lua=/Users/brianm/.opt/lua-5.1.2/ \
+      --with-apxs=/Users/brianm/.opt/httpd-2.2.4-worker-wombat/bin/apxs \
+      --with-apreq2=/Users/brianm/.opt/libapreq2-2.0.8/
+      
+    If compiling (make) reports an error that it cannot find the
+    libapreq2 header file, please tell me ( brianm@apache.org )
+    as this occurs under some configurations but we haven't 
+    hammered down the weird things libapreq2 does with its
+    install. If you build libapreq2 with a --prefix configuration
+    option, it always seems to work.
+  
+      
+That is it. To configure mod_wombat, look at the basic-configuration.txt document.
\ No newline at end of file
diff --git a/modules/wombat/docs/running-developer-tests.txt b/modules/wombat/docs/running-developer-tests.txt
new file mode 100644 (file)
index 0000000..e40bf98
--- /dev/null
@@ -0,0 +1,16 @@
+-*- mode:org -*-
+* Building mod_wombat
+  The first step is to build mod_wombat per the instructions in
+  building-from-subversion.txt.
+
+* Build and install LuaSocket
+    http://www.cs.princeton.edu/~diego/professional/luasocket/
+    FreeBSD: /usr/ports/net/luasocket
+
+* Running Tests
+  1. Replace apache's httpd.conf with test/test_httpd.conf
+  2. Customize the new httpd.conf to match your directories
+  3. Finally, to run the tests, start apache and run:
+     $ cd test
+     $ lua ./test.lua
+     FreeBSD: lua-5.1 ./test.lua
diff --git a/modules/wombat/docs/writing-handlers.txt b/modules/wombat/docs/writing-handlers.txt
new file mode 100644 (file)
index 0000000..10cfb5d
--- /dev/null
@@ -0,0 +1,49 @@
+mod_wombat always looks to invoke a function for the handler, rather than
+just evaluating a script body CGI style. A handler function looks
+something like this:
+
+    -- example.lua --
+    require "string"
+
+    function handle_something(r)
+        r.content_type = "text/plain"
+        r:puts("Hello Lua World!\n")
+    
+        if r.method == 'GET' then
+            for k, v in pairs( r:parseargs() ) do
+                r:puts( string.format("%s: %s", k, v) )
+            end
+        elseif r.method == 'POST' then
+            for k, v in pairs( r:parsebody() ) do
+                r:puts( string.format("%s: %s", k, v) )
+            end
+        else
+            r:puts("unknown HTTP method " .. r.method)
+        end 
+    end
+
+This handler function just prints out the uri or form encoded
+arguments to a plaintext page.
+
+This means (and in fact encourages) that you can have multiple
+handlers (or hooks, or filters) in the same script.
+
+Data Structures:
+    request_rec:
+        the request_rec is mapped in as a userdata. It has a metatable
+        which lets you do useful things with it. For the most part it
+        has the same fields as the request_rec struct (see httpd.h 
+        until we get better docs here) many of which are writeable as
+        well as readable, and has (at least) the following methods:
+        
+        r:puts("hello", " world", "!") -- print to response body
+        
+        -- logging functions
+        r:debug("This is a debug log message")
+        r:info("This is an info log message")
+        r:notice("This is an notice log message")
+        r:warn("This is an warn log message")
+        r:err("This is an err log message")
+        r:alert("This is an alert log message")
+        r:crit("This is an crit log message")
+        r:emerg("This is an emerg log message")
diff --git a/modules/wombat/mod_wombat.c b/modules/wombat/mod_wombat.c
new file mode 100644 (file)
index 0000000..8dc9435
--- /dev/null
@@ -0,0 +1,881 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mod_wombat.h"
+#include "config.h"
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#include "apr_lua.h"
+
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(apw, WOMBAT, int, wombat_open,
+                                    (lua_State *L, apr_pool_t *p),
+                                    (L, p),
+                                    OK, DECLINED)
+
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(apw, WOMBAT, int, wombat_request,
+                                    (lua_State *L, request_rec *r),
+                                    (L, r),
+                                    OK, DECLINED)
+
+module AP_MODULE_DECLARE_DATA wombat_module;
+
+/**
+ * error reporting if lua has an error. 
+ * Extracts the error from lua stack and prints
+ */
+static void report_lua_error(lua_State *L, request_rec *r) {
+    r->status = 500;
+    r->content_type = "text/html";      
+
+    ap_rputs("<b>Error!</b>\n", r);
+    ap_rputs("<p>", r);
+    const char* lua_response = lua_tostring(L, -1);
+    ap_rputs(lua_response, r);            
+    ap_rputs("</p>\n", r);
+    
+    ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, "Lua error: %s", lua_response);
+}
+
+static void wombat_open_callback(lua_State *L, apr_pool_t *p, void* ctx) {
+    apr_lua_init(L, p);
+    apw_load_apache2_lmodule(L);
+    apw_load_request_lmodule(L, p);
+    apw_load_config_lmodule(L);
+}
+
+static int wombat_open_hook(lua_State *L, apr_pool_t *p) {
+    wombat_open_callback(L, p, NULL);
+    return OK;
+}
+
+/*
+static apr_status_t wombathood(ap_filter_t *f, apr_bucket_brigade *bb) {
+    apr_bucket* b;
+    apr_status_t rs;
+    for ( b = APR_BRIGADE_FIRST(bb);
+          b != APR_BRIGADE_SENTINEL(bb);
+          b = APR_BUCKET_NEXT(b)) 
+    {
+        if (APR_BUCKET_IS_EOS(b)) {kl
+            break;
+        }
+        const char *buffer;
+        size_t bytes;
+        if (( rs = apr_bucket_read(b, &buffer, &bytes, APR_BLOCK_READ))) {
+            ap_log_rerror(APLOG_MARK, APLOG_WARNING, rs, f->r, "read failure in wombathood");
+            return rs;
+        }
+        char *mine = apr_pstrmemdup(f->r->pool, buffer, bytes);
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "sending '%s'", mine);
+    }
+    
+    ap_pass_brigade(f->next, bb);
+    
+    return OK;
+}
+*/
+
+/**
+ * "main"
+ */
+static int wombat_handler(request_rec *r) {        
+    if (strcmp(r->handler, "lua-script")) {
+        return DECLINED;
+    }
+    
+    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "handling [%s] in mod_wombat", r->filename);
+    apw_dir_cfg *dcfg = ap_get_module_config(r->per_dir_config, &wombat_module);
+    
+    if (!r->header_only) {        
+        apw_request_cfg* rcfg = ap_get_module_config(r->request_config, &wombat_module);
+        mapped_request_details *d = rcfg->mapped_request_details;
+        apw_vm_spec *spec = NULL;
+        if (!d) {
+            d = apr_palloc(r->pool, sizeof(mapped_request_details));
+            spec = apr_pcalloc(r->pool, sizeof(apw_vm_spec));
+            spec->scope = dcfg->vm_scope;
+            spec->pool = r->pool;
+            spec->file = r->filename;
+            spec->code_cache_style = dcfg->code_cache_style;
+            d->spec = spec;
+            d->function_name = "handle";
+        }
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "request details scope:%u, cache:%u",
+                                                       d->spec->scope,
+                                                       d->spec->code_cache_style);
+        const apw_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &wombat_module);
+        lua_State *L =  apw_get_lua_state(r->pool,
+                                          d->spec->file,
+                                          cfg->package_paths,
+                                          cfg->package_cpaths,
+                                          &wombat_open_callback, NULL); 
+                                          
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got a vm!");
+        if (!L) {
+            /* TODO annotate spec with failure reason */
+            r->status = 500;
+            ap_rputs("Unable to compile VM, see logs", r);
+        }
+        lua_getglobal(L, d->function_name);
+        apw_run_wombat_request(L, r);
+        if (lua_pcall(L, 1, 0, 0)) {
+            report_lua_error(L, r);
+        }
+    }
+    return OK;
+}
+
+
+
+/**
+ * Like mod_alias except for lua handler fun :-) 
+ */
+static int apw_alias_munger(request_rec *r) {
+    const apw_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, &wombat_module);
+    
+    int i;
+    ap_regmatch_t matches[AP_MAX_REG_MATCH];
+    
+    for (i = 0; i < cfg->mapped_handlers->nelts; i++) {
+        const apw_mapped_handler_spec *cnd = ((const apw_mapped_handler_spec**)cfg->mapped_handlers->elts)[i];
+        if (OK == ap_regexec(cnd->uri_pattern, r->uri, AP_MAX_REG_MATCH, matches, 0)) {
+            r->handler = "lua-script";
+            
+            apw_vm_spec *spec = apr_pcalloc(r->pool, sizeof(apw_vm_spec));
+            spec->file =  ap_pregsub(r->pool, cnd->file_name, r->uri, AP_MAX_REG_MATCH, matches);
+            spec->scope = cnd->scope;
+            spec->code_cache_style = cnd->code_cache_style;
+            spec->bytecode = cnd->bytecode;
+            spec->bytecode_len = cnd->bytecode_len;
+            if (spec->scope == APW_SCOPE_ONCE) {
+                spec->pool = r->pool;
+            }
+            
+            mapped_request_details *d = apr_palloc(r->pool, sizeof(mapped_request_details));
+            
+            d->function_name = ap_pregsub(r->pool, cnd->function_name, r->uri, AP_MAX_REG_MATCH, matches);
+            d->spec = spec;
+            
+            /* now do replacement on method name where? */
+            r->filename = apr_pstrdup(r->pool, spec->file);
+            apw_request_cfg *rcfg = ap_get_module_config(r->request_config, &wombat_module);
+            rcfg->mapped_request_details = d;
+            return OK;
+        }
+    }
+    return DECLINED;    
+}
+
+/* ---------------- Configury stuff --------------- */
+
+/** harnesses for magic hooks **/
+
+static int wombat_request_rec_hook_harness(request_rec *r, const char *name) {
+    char *fixed_filename;
+    
+    const apw_dir_cfg* cfg = (apw_dir_cfg*) ap_get_module_config(r->per_dir_config,
+                                                                       &wombat_module);
+    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
+    if (hook_specs) {
+        int i;
+        for (i=0; i < hook_specs->nelts; i++) {
+            apw_mapped_handler_spec *hook_spec = ((apw_mapped_handler_spec**)hook_specs->elts)[i];
+            if (hook_spec == NULL) continue;
+            apw_vm_spec *spec = apr_pcalloc(r->pool, sizeof(apw_vm_spec));
+            
+            spec->file = hook_spec->file_name;
+            spec->code_cache_style = hook_spec->code_cache_style;
+            spec->scope = hook_spec->scope;
+            spec->bytecode = hook_spec->bytecode;
+            spec->bytecode_len = hook_spec->bytecode_len;
+            spec->pool = r->pool;
+            
+            /*
+            const apw_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &wombat_module);
+            lua_State *L =  apw_get_lua_state(r->pool,
+                                              d->spec->file,
+                                              cfg->package_paths,
+                                              cfg->package_cpaths,
+                                              &wombat_open_callback, NULL);
+            */            
+            apw_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, &wombat_module);
+            apr_filepath_merge(&fixed_filename, server_cfg->root_path, spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
+            lua_State *L =  apw_get_lua_state(r->pool,
+                                              fixed_filename,
+                                              cfg->package_paths,
+                                              cfg->package_cpaths,
+                                              &wombat_open_callback, NULL);
+            
+            
+            
+            if (!L) {
+                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "wombat: Failed to obtain lua interpreter for %s %s",
+                              hook_spec->function_name,
+                              hook_spec->file_name);
+                return 500;
+            }
+
+            if (hook_spec->function_name != NULL) {
+                lua_getglobal(L, hook_spec->function_name);
+                if (!lua_isfunction(L, -1)) {
+                    ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "wombat: Unable to find function %s in %s",
+                                  hook_spec->function_name,
+                                  hook_spec->file_name);
+                    return 500;
+                }
+
+                apw_run_wombat_request(L, r);
+            }
+            else {
+                apw_run_wombat_request(L, r);
+                
+                int t = lua_gettop(L);
+                lua_setglobal(L, "r");
+                lua_settop(L, t);
+            }
+
+            if (lua_pcall(L, 1, 1, 0)) {
+                report_lua_error(L, r);
+                return 500;
+            }
+            apr_status_t rv = DECLINED;
+            if (lua_isnumber(L, -1)) {
+                rv = lua_tointeger(L, -1);
+            }
+            if (rv != DECLINED) {
+                return rv;
+            }
+        }
+    }
+    return DECLINED;
+}
+
+
+static apr_size_t config_getstr(ap_configfile_t *cfg, char *buf, size_t bufsiz)
+{
+    apr_size_t i = 0;
+    
+    if (cfg->getstr) {
+        const char *res = (cfg->getstr)(buf, bufsiz, cfg->param);
+        if (res) {
+            i = strlen(buf);
+            if (i && buf[i - 1] == '\n') ++cfg->line_number;
+        }
+        else {
+            buf[0] = '\0';
+            i = 0;
+        }
+    }
+    else {
+        while (i < bufsiz) {
+            int ch = (cfg->getch)(cfg->param);
+            if (ch == EOF) break;
+            buf[i++] = ch;
+            if (ch == '\n') {
+                ++cfg->line_number;
+                break;
+            }
+        }
+    }
+    return i;
+}
+
+typedef struct cr_ctx {
+    cmd_parms *cmd;
+    ap_configfile_t *cfp;
+    size_t startline;
+    const char *endstr;
+    char buf[HUGE_STRING_LEN];
+} cr_ctx;
+
+
+/* Okay, this deserves a little explaination -- in order for the errors that lua
+ * generates to be 'accuarate', including line numbers, we basically inject
+ * N line number new lines into the 'top' of the chunk reader..... 
+ *
+ * be happy. this is cool.
+ *
+ */
+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";
+#define N_LF 32
+
+static const char *direct_chunkreader(lua_State *lvm, void *udata, size_t *plen)
+{
+    const char *p;
+    struct cr_ctx *ctx = udata;
+    
+    if (ctx->startline) {
+        *plen = ctx->startline > N_LF ? N_LF : ctx->startline;
+        ctx->startline -= *plen;
+        return lf;
+    }
+    *plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
+    
+    for (p = ctx->buf; isspace(*p); ++p);
+    if (p[0] == '<' && p[1] == '/') {
+        int i = 0;
+        while (i < strlen(ctx->endstr)) {
+            if (tolower(p[i + 2]) != ctx->endstr[i]) return ctx->buf;
+            ++i;
+        }
+        *plen = 0;
+        return NULL;
+    }
+    /*fprintf(stderr, "buf read: %s\n", ctx->buf);*/
+    return ctx->buf;
+}
+
+static int ldump_writer (lua_State *L, const void* b, size_t size, void* B) {
+    (void)L;
+    luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
+    return 0;
+}
+
+typedef struct hack_section_baton {
+    const char *name;
+    apw_mapped_handler_spec *spec;
+} hack_section_baton;
+
+/* You can be unhappy now.
+ *
+ * This is uncool.
+ *
+ * When you create a <Section handler in httpd, the only 'easy' way to create
+ * a directory context is to parse the section, and convert it into a 'normal'
+ * Configureation option, and then collapse the entire section, in memory,
+ * back into the parent section -- from which you can then get the new directive
+ * invoked.... anyways. evil. Rici taught me how to do this hack :-)
+ */
+static const char *hack_section_handler(cmd_parms *cmd, void *_cfg, const char *arg)
+{
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    ap_directive_t *directive = cmd->directive;
+    hack_section_baton* baton = directive->data;
+
+    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, baton->name, APR_HASH_KEY_STRING);
+    if (!hook_specs) {
+        hook_specs = apr_array_make(cmd->pool, 2, sizeof(apw_mapped_handler_spec*));
+        apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, baton->name), APR_HASH_KEY_STRING, hook_specs);
+    }
+
+    baton->spec->scope = cfg->vm_scope;
+
+    *(apw_mapped_handler_spec**)apr_array_push(hook_specs) = baton->spec;
+
+    return NULL;
+}
+
+static const char *register_named_block_function_hook(const char *name, 
+                                                      cmd_parms *cmd, 
+                                                      void *mconfig, 
+                                                      const char *line)
+{
+    const char* function;
+
+    if (line && line[0] == '>') {
+        function = NULL;
+    }
+    else {
+        const char *word;
+        apr_size_t wordlen;
+        word = ap_getword_conf(cmd->pool, &line);
+        wordlen = strlen(word);
+        if (wordlen == 0 || word[wordlen - 1] != '>') {
+            return apr_pstrcat(cmd->pool, cmd->directive->directive, "> takes exactly one argument", NULL);
+        }
+        else {
+            function = apr_pstrndup(cmd->pool, word, wordlen - 1);
+        }
+    }
+
+    apw_mapped_handler_spec *spec = apr_pcalloc(cmd->pool, sizeof(apw_mapped_handler_spec));
+
+    {
+        cr_ctx ctx;
+        char buf[32];
+        lua_State* lvm;
+        char *tmp;
+        int rv;
+
+        apr_snprintf(buf, sizeof(buf), "%u", cmd->config_file->line_number);
+        spec->file_name = apr_pstrcat(cmd->pool, cmd->config_file->name, ":", buf, NULL);
+        if (function) {
+            spec->function_name = (char*)function;
+        }
+        else {
+            function = NULL;
+        }
+        spec->code_cache_style = APW_CODE_CACHE_FOREVER;
+
+        ctx.cmd = cmd;
+        tmp = apr_pstrdup(cmd->pool, cmd->err_directive->directive+1);
+        ap_str_tolower(tmp);
+        ctx.endstr = tmp; 
+        ctx.cfp = cmd->config_file;
+        ctx.startline = cmd->config_file->line_number;
+
+        /* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
+        lvm = luaL_newstate();
+
+        lua_settop(lvm, 0);
+
+        rv = lua_load(lvm, direct_chunkreader, &ctx, spec->file_name);
+
+        if (rv != 0) {
+            const char *errstr = apr_pstrcat(cmd->pool, "Lua Error:", lua_tostring(lvm, -1), NULL);
+            lua_close(lvm);
+            return errstr;
+        }
+        else {
+            luaL_Buffer b;
+            luaL_buffinit(lvm, &b);
+            lua_dump(lvm, ldump_writer, &b);
+            luaL_pushresult(&b);
+            spec->bytecode_len = lua_strlen(lvm, -1);
+            spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1), spec->bytecode_len);
+            lua_close(lvm);
+        }
+
+        ap_directive_t **current = mconfig;
+
+        /* Here, we have to replace our current config node for the next pass */
+        if (!*current) {
+            *current = apr_pcalloc(cmd->pool, sizeof(**current));
+        }
+        
+        hack_section_baton *baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton));
+        baton->name = name;
+        baton->spec = spec;
+
+        (*current)->filename = cmd->config_file->name;
+        (*current)->line_num = cmd->config_file->line_number;
+        (*current)->directive = apr_pstrdup(cmd->pool, "Lua_____ByteCodeHack");
+        (*current)->args = NULL;
+        (*current)->data = baton;
+    }
+
+    return NULL;
+}
+
+static const char* register_named_file_function_hook(const char *name, 
+                                                     cmd_parms *cmd, 
+                                                     void *_cfg, 
+                                                     const char *file, 
+                                                     const char *function) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    
+    apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, name, APR_HASH_KEY_STRING);
+    if (!hook_specs) {
+        hook_specs = apr_array_make(cmd->pool, 2, sizeof(apw_mapped_handler_spec*));
+        apr_hash_set(cfg->hooks, apr_pstrdup(cmd->pool, name), APR_HASH_KEY_STRING, hook_specs);
+    }
+
+    apw_mapped_handler_spec *spec = apr_pcalloc(cmd->pool, sizeof(apw_mapped_handler_spec));
+    spec->file_name = apr_pstrdup(cmd->pool, file);
+    spec->function_name = apr_pstrdup(cmd->pool, function);
+    spec->scope = cfg->vm_scope;
+    spec->code_cache_style = APW_CODE_CACHE_STAT;
+    /*
+        int code_cache_style;
+        char *function_name;
+        char *file_name;
+        int scope;
+    */
+    *(apw_mapped_handler_spec**)apr_array_push(hook_specs) = spec;
+    return NULL;
+}
+
+int wombat_check_user_id_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "check_user_id");
+}
+
+int wombat_translate_name_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "translate_name");
+}
+
+int wombat_fixup_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "fixups");
+}
+
+int wombat_map_to_storage_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "map_to_storage");
+}
+
+int wombat_type_checker_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "type_checker");
+}
+
+int wombat_access_checker_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "access_checker");
+}
+
+int wombat_auth_checker_harness(request_rec *r) {
+    return wombat_request_rec_hook_harness(r, "auth_checker");
+}
+
+void wombat_insert_filter_harness(request_rec *r) {
+    /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
+}
+
+int wombat_quick_harness(request_rec *r, int lookup) {
+    if(lookup) {
+        return DECLINED;
+    }
+    return wombat_request_rec_hook_harness(r, "quick");
+}
+
+static const char* register_translate_name_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("translate_name", cmd, _cfg, file, function);
+}
+
+static const char *register_translate_name_block(cmd_parms *cmd, void *_cfg, const char *line)
+{
+    return register_named_block_function_hook("translate_name", cmd, _cfg, line);
+}
+
+
+static const char* register_fixups_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("fixups", cmd, _cfg, file, function);
+}
+static const char* register_fixups_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("fixups", cmd, _cfg, line);
+}
+
+static const char* register_map_to_storage_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("map_to_storage", cmd, _cfg, file, function);
+}
+static const char* register_map_to_storage_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("map_to_storage", cmd, _cfg, line);
+}
+
+static const char* register_check_user_id_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("check_user_id", cmd, _cfg, file, function);
+}
+static const char* register_check_user_id_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("check_user_id", cmd, _cfg, line);
+}
+
+static const char* register_type_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("type_checker", cmd, _cfg, file, function);
+}
+static const char* register_type_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("type_checker", cmd, _cfg, line);
+}
+
+static const char* register_access_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("access_checker", cmd, _cfg, file, function);
+}
+static const char* register_access_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("access_checker", cmd, _cfg, line);
+}
+
+static const char* register_auth_checker_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("auth_checker", cmd, _cfg, file, function);
+}
+static const char* register_auth_checker_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return register_named_block_function_hook("auth_checker", cmd, _cfg, line);
+}
+
+static const char* register_insert_filter_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return "LuaHookInsertFilter not yet implemented";
+}
+
+static const char* register_quick_hook(cmd_parms *cmd, void *_cfg, const char *file, const char *function) {
+    return register_named_file_function_hook("quick", cmd, _cfg, file, function);
+}
+static const char* register_quick_block(cmd_parms *cmd, void *_cfg, const char *line) {
+    return "LuaQuickHook in an inline block not yet implemented";
+}
+
+
+
+static const char* register_package_helper(cmd_parms *cmd, const char *arg, apr_array_header_t *dir_array) {
+    apr_status_t rv;
+        
+    apw_server_cfg *server_cfg = ap_get_module_config(cmd->server->module_config, &wombat_module);
+    char *fixed_filename;
+    rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, arg, APR_FILEPATH_NOTRELATIVE, cmd->pool);
+    if (rv != APR_SUCCESS) {
+        return apr_psprintf(cmd->pool, "Unable to build full path to file, %s", arg);
+    }
+    
+    *(const char**)apr_array_push(dir_array) = fixed_filename;
+    return NULL;
+}
+
+
+/**
+ * Called for config directive which looks like
+ * LuaPackagePath /lua/package/path/mapped/thing/like/this/?.lua
+ */
+const char* register_package_dir(cmd_parms *cmd, void *_cfg, const char *arg) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    
+    return register_package_helper(cmd, arg, cfg->package_paths);
+}
+
+/**
+ * Called for config directive which looks like
+ * LuaPackageCPath /lua/package/path/mapped/thing/like/this/?.so
+ */
+const char* register_package_cdir(cmd_parms *cmd, void *_cfg, const char *arg) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    
+    return register_package_helper(cmd, arg, cfg->package_cpaths);
+}
+
+/**
+ * Called for config directive which looks like
+ * LuaCodeCache 
+ */
+const char* register_code_cache(cmd_parms *cmd, void *_cfg, const char *arg) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    if (apr_strnatcmp("stat", arg) == 0) {
+        cfg->code_cache_style = APW_CODE_CACHE_STAT;
+    }
+    else if (apr_strnatcmp("forever", arg) == 0) {
+        cfg->code_cache_style = APW_CODE_CACHE_FOREVER;
+    }
+    else if (apr_strnatcmp("never", arg) == 0) {
+        cfg->code_cache_style = APW_CODE_CACHE_NEVER;
+    }
+    else {
+        return apr_psprintf(cmd->pool, 
+               "Invalid value for LuaCodeCache, '%s', acceptable values are %s", 
+               arg, "'stat', 'forever', and 'never'");
+    }
+    return NULL;
+}
+
+static const char* register_lua_scope(cmd_parms *cmd, void *_cfg, const char *scope,
+                                                                  const char *min,
+                                                                  const char *max) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+    if (apr_strnatcmp("once", scope) == 0) {
+        cfg->vm_scope = APW_SCOPE_ONCE;
+    }
+    else if (apr_strnatcmp("request", scope) == 0) {
+        cfg->vm_scope = APW_SCOPE_REQUEST;
+    }
+    else if (apr_strnatcmp("conn", scope) == 0) {
+        cfg->vm_scope = APW_SCOPE_CONN;
+    }
+    else if (apr_strnatcmp("server", scope) == 0) {
+        cfg->vm_scope = APW_SCOPE_SERVER;
+        if (min) cfg->vm_server_pool_min = atoi(min);
+        if (max) cfg->vm_server_pool_max = atoi(max);
+    }
+    else {
+        return apr_psprintf(cmd->pool, 
+               "Invalid value for LuaScope, '%s', acceptable values are %s", 
+               scope, "'once', 'request', 'conn', and 'server'");
+    }
+    return NULL;
+}
+
+
+/**
+ * Called for config directive which looks like
+ * AddLuaHandler /alias /path/to/lua/file.lua [handler_function_name]
+ */
+static const char* lua_map_handler(cmd_parms *cmd, void *_cfg, const char *path, const char *file, const char *function) {
+    apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg;
+
+    const char *function_name;
+    function_name = function ? function : "handle";
+    apr_status_t rv;
+    rv = apw_lua_map_handler(cfg, file, function_name, path, "once");
+    if (rv != APR_SUCCESS) {
+        return apr_psprintf(cmd->pool, "Unable to configure a lua handler for path '%s', handler %s#%s",
+                                       path, file, function_name);
+    }
+    return NULL;
+}
+
+static const char* register_lua_root(cmd_parms *cmd, void *_cfg, const char *root) {
+    /* apw_dir_cfg* cfg = (apw_dir_cfg*)_cfg; */
+    apw_server_cfg* cfg = ap_get_module_config(cmd->server->module_config, &wombat_module);
+    
+    cfg->root_path = root;
+    return NULL;
+}
+
+/*******************************/
+
+command_rec wombat_commands[] = {
+
+    AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL, 
+                "Specify the base path for resolving relative paths for mod_wombat directives"),
+
+    
+    AP_INIT_TAKE1("LuaPackagePath", register_package_dir, NULL, OR_ALL, 
+                  "Add a directory to lua's package.path"),
+                  
+    AP_INIT_TAKE1("LuaPackageCPath", register_package_cdir, NULL, OR_ALL, 
+                  "Add a directory to lua's package.cpath"),
+                  
+    AP_INIT_TAKE23("LuaMapHandler", lua_map_handler, NULL, OR_ALL, 
+                  "Map a path to a lua handler"),
+                  
+    AP_INIT_TAKE2("LuaHookTranslateName", register_translate_name_hook, NULL, OR_ALL, 
+                   "Provide a hook for the translate name phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookTranslateName", register_translate_name_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the translate name phase of request processing"),
+
+    AP_INIT_TAKE2("LuaHookFixups", register_fixups_hook, NULL, OR_ALL, 
+                  "Provide a hook for the fixups phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookFixups", register_fixups_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a inline hook for the fixups phase of request processing"),
+
+/* todo: test */
+    AP_INIT_TAKE2("LuaHookMapToStorage", register_map_to_storage_hook, NULL, OR_ALL, 
+                  "Provide a hook for the map_to_storage phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookMapToStorage", register_map_to_storage_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the map_to_storage phase of request processing"),
+
+    /* todo: test */
+    AP_INIT_TAKE2("LuaHookCheckUserID", register_check_user_id_hook, NULL, OR_ALL, 
+                  "Provide a hook for the check_user_id phase of request processing"),    
+    AP_INIT_RAW_ARGS("<LuaHookCheckUserID", register_check_user_id_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the check_user_id phase of request processing"),    
+
+    /* todo: test */
+    AP_INIT_TAKE2("LuaHookTypeChecker", register_type_checker_hook, NULL, OR_ALL, 
+                  "Provide a hook for the type_checker phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookTypeChecker", register_type_checker_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the type_checker phase of request processing"),
+
+    /* todo: test */
+    AP_INIT_TAKE2("LuaHookAccessChecker", register_access_checker_hook, NULL, OR_ALL, 
+                  "Provide a hook for the access_checker phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookAccessChecker", register_access_checker_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the access_checker phase of request processing"),
+
+    /* todo: test */
+    AP_INIT_TAKE2("LuaHookAuthChecker", register_auth_checker_hook, NULL, OR_ALL, 
+                  "Provide a hook for the auth_checker phase of request processing"),
+    AP_INIT_RAW_ARGS("<LuaHookAuthChecker", register_auth_checker_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                     "Provide a hook for the auth_checker phase of request processing"),
+
+    /* todo: test */
+    AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL, OR_ALL, 
+                  "Provide a hook for the insert_filter phase of request processing"),
+    
+    AP_INIT_TAKE1("LuaCodeCache", register_code_cache, NULL, OR_ALL, 
+                  "Configure the compiled code cache. \
+                   Default is to stat the file each time, options are stat|forever|never"),
+                   
+    AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
+                  "One of once, request, conn, server -- default is once"),
+
+    AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL, 
+                  "Provide a hook for the quick handler of request processing"),
+    AP_INIT_RAW_ARGS("<LuaQuickHandler", register_quick_block, NULL,
+                     EXEC_ON_READ|OR_ALL,
+                    "Provide a hook for the quick handler of request processing"),
+
+    AP_INIT_RAW_ARGS("Lua_____ByteCodeHack", hack_section_handler, NULL, OR_ALL, 
+                     "(internal) Byte code handler"),
+    { NULL }
+};
+
+
+static void* create_dir_config(apr_pool_t *p, char *dir) {
+    apw_dir_cfg* cfg =  apr_pcalloc(p, sizeof(apw_dir_cfg));
+    cfg->package_paths = apr_array_make(p, 2, sizeof(char*));
+    cfg->package_cpaths = apr_array_make(p, 2, sizeof(char*));
+    cfg->mapped_handlers = apr_array_make(p, 1, sizeof(apw_mapped_handler_spec*));
+    cfg->code_cache_style = APW_CODE_CACHE_STAT;
+    cfg->pool = p;
+    cfg->hooks = apr_hash_make(p);
+    cfg->dir = apr_pstrdup(p, dir);
+    cfg->vm_scope = APW_SCOPE_ONCE;
+    return cfg;
+}
+
+static int create_request_config(request_rec *r) {
+    apw_request_cfg *cfg = apr_palloc(r->pool, sizeof(apw_request_cfg));
+    cfg->mapped_request_details = NULL;
+    cfg->request_scoped_vms = apr_hash_make(r->pool);
+    ap_set_module_config(r->request_config, &wombat_module, cfg);
+    return OK;
+}
+
+static void* create_server_config(apr_pool_t *p, server_rec *s) {
+    
+    apw_server_cfg *cfg = apr_pcalloc(p, sizeof(apw_server_cfg));
+    cfg->code_cache = apr_pcalloc(p, sizeof(apw_code_cache));
+    apr_thread_rwlock_create(&cfg->code_cache->compiled_files_lock, p);
+    cfg->code_cache->compiled_files = apr_hash_make(p);
+    cfg->vm_reslists = apr_hash_make(p);
+    apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
+    cfg->code_cache->pool = p;
+    cfg->root_path = NULL;
+    
+    return cfg;
+}
+
+static int wombat_request_hook(lua_State *L, request_rec *r) {
+    apw_push_request(L, r);
+    return OK;
+}
+
+static void wombat_register_hooks(apr_pool_t *p) {
+    /* ap_register_output_filter("wombathood", wombathood, NULL, AP_FTYPE_RESOURCE); */
+    ap_hook_handler(wombat_handler, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_create_request(create_request_config, NULL, NULL, APR_HOOK_MIDDLE);
+    
+    /* http_request.h hooks */
+    ap_hook_translate_name(wombat_translate_name_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_fixups(wombat_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_map_to_storage(wombat_map_to_storage_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_check_user_id(wombat_check_user_id_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_type_checker(wombat_type_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_access_checker(wombat_access_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_auth_checker(wombat_auth_checker_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_insert_filter(wombat_insert_filter_harness, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_quick_handler(wombat_quick_harness, NULL, NULL, APR_HOOK_FIRST);
+
+    /* ap_hook_translate_name(wombat_alias_munger, NULL, NULL, APR_HOOK_MIDDLE); */
+    ap_hook_translate_name(apw_alias_munger, NULL, NULL, APR_HOOK_MIDDLE);    
+
+    APR_OPTIONAL_HOOK(apw, wombat_open, wombat_open_hook, NULL, NULL,
+                      APR_HOOK_REALLY_FIRST);
+
+    APR_OPTIONAL_HOOK(apw, wombat_request, wombat_request_hook, NULL, NULL,
+                     APR_HOOK_REALLY_FIRST); 
+}
+
+module AP_MODULE_DECLARE_DATA wombat_module = {
+    STANDARD20_MODULE_STUFF, 
+    create_dir_config,              /* create per-dir    config structures */
+    NULL,                           /* merge  per-dir    config structures */
+    create_server_config,           /* create per-server config structures */
+    NULL,                           /* merge  per-server config structures */
+    wombat_commands,                /* table of config file commands       */
+    wombat_register_hooks           /* register hooks                      */
+};
+
diff --git a/modules/wombat/mod_wombat.h b/modules/wombat/mod_wombat.h
new file mode 100644 (file)
index 0000000..d7cb105
--- /dev/null
@@ -0,0 +1,148 @@
+#ifndef MOD_WOMBAT_H
+#define MOD_WOMBAT_H
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "httpd.h"
+#include "http_core.h"
+#include "http_config.h"
+#include "http_request.h"
+#include "http_log.h"
+#include "http_protocol.h"
+#include "ap_regex.h"
+
+#include "ap_config.h"
+#include "util_filter.h"
+
+#include "apr_thread_rwlock.h"
+#include "apr_strings.h"
+#include "apr_tables.h"
+#include "apr_hash.h"
+#include "apr_buckets.h"
+#include "apr_file_info.h"
+#include "apr_time.h"
+#include "apr_hooks.h"
+
+#include "apreq_parser.h"
+#include "apreq_param.h"
+#include "apreq2/apreq_module_apache2.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+#include "request.h"
+#include "vmprep.h"
+
+
+/**
+ * make a userdata out of a C pointer, and vice versa
+ * instead of using lightuserdata
+ */
+#ifndef lua_boxpointer
+#define lua_boxpointer(L,u) (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
+#define lua_unboxpointer(L,i)  (*(void **)(lua_touserdata(L, i)))
+#endif
+
+void rstack_dump(lua_State* L, request_rec* r, const char* msg);
+
+typedef struct {
+    apr_array_header_t* package_paths;
+    apr_array_header_t* package_cpaths;
+
+    /**
+     * mapped handlers
+     */
+    apr_array_header_t* mapped_handlers;
+
+    apr_pool_t *pool;
+    
+    /**
+     * CODE_CACHE_STAT | CODE_CACHE_FOREVER | CODE_CACHE_NEVER
+     */ 
+    unsigned int code_cache_style;
+    
+    /** 
+     * APW_SCOPE_ONCE | APW_SCOPE_REQUEST | APW_SCOPE_CONN | APW_SCOPE_SERVER
+     */
+    unsigned int vm_scope;
+    unsigned int vm_server_pool_min;
+    unsigned int vm_server_pool_max;
+    
+    /* info for the hook harnesses */
+    apr_hash_t *hooks; /* <wombat_hook_info> */
+    
+    /* the actual directory being configured */
+    char *dir;
+} apw_dir_cfg;
+
+typedef struct {
+    apw_code_cache *code_cache;
+    apr_hash_t *vm_reslists;
+    apr_thread_rwlock_t *vm_reslists_lock;
+
+    /* value of the LuaRoot directive */
+    const char *root_path;  
+} apw_server_cfg;
+
+typedef struct {
+    char *function_name;
+    apw_vm_spec *spec;
+} mapped_request_details;
+
+typedef struct {
+    mapped_request_details *mapped_request_details;
+    apr_hash_t *request_scoped_vms;
+} apw_request_cfg;
+
+typedef struct {
+    lua_State *L;
+    char *function;
+} apw_filter_ctx;
+
+extern module AP_MODULE_DECLARE_DATA wombat_module;
+/* module wombat_module; */
+
+#if !defined(WIN32)
+#define WOMBAT_DECLARE(type)            type
+#define WOMBAT_DECLARE_NONSTD(type)     type
+#define WOMBAT_DECLARE_DATA
+#elif defined(WOMBAT_DECLARE_STATIC)
+#define WOMBAT_DECLARE(type)            type __stdcall
+#define WOMBAT_DECLARE_NONSTD(type)     type
+#define WOMBAT_DECLARE_DATA
+#elif defined(WOMBAT_DECLARE_EXPORT)
+#define WOMBAT_DECLARE(type)            __declspec(dllexport) type __stdcall
+#define WOMBAT_DECLARE_NONSTD(type)     __declspec(dllexport) type
+#define WOMBAT_DECLARE_DATA             __declspec(dllexport)
+#else
+#define WOMBAT_DECLARE(type)            __declspec(dllimport) type __stdcall
+#define WOMBAT_DECLARE_NONSTD(type)     __declspec(dllimport) type
+#define WOMBAT_DECLARE_DATA             __declspec(dllimport)
+#endif
+
+APR_DECLARE_EXTERNAL_HOOK(apw, WOMBAT, int, wombat_open,
+                          (lua_State *L, apr_pool_t *p));
+
+APR_DECLARE_EXTERNAL_HOOK(apw, WOMBAT, int, wombat_request,
+                          (lua_State *L, request_rec *r));
+
+#endif /* !MOD_WOMBAT_H */
+
diff --git a/modules/wombat/request.c b/modules/wombat/request.c
new file mode 100644 (file)
index 0000000..0c1a997
--- /dev/null
@@ -0,0 +1,562 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mod_wombat.h"
+#include "apr_lua.h"
+
+typedef char* (*req_field_string_f) (request_rec* r);
+typedef int (*req_field_int_f) (request_rec* r);
+
+void rstack_dump(lua_State* L, request_rec* r, const char* msg) {
+    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "Lua Stack Dump: [%s]", msg);
+
+    int i;
+    int top = lua_gettop(L);
+    for (i = 1; i<= top; i++) {
+        int t = lua_type(L, i);
+        switch(t) {
+            case LUA_TSTRING: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  '%s'", i, lua_tostring(L, i));
+                break;
+            }
+            case LUA_TUSERDATA: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d:  userdata", i);                
+                break;
+            }
+            case LUA_TLIGHTUSERDATA: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%d:  lightuserdata", i);
+                break;
+            }
+            case LUA_TNIL: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  NIL", i);
+                break;
+            }
+            case LUA_TNONE: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  None", i);
+                break;
+            }
+            case LUA_TBOOLEAN: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  %s", i,  lua_toboolean(L, i) ? "true" : "false");
+                break;
+            }
+            case LUA_TNUMBER: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  %g", i, lua_tonumber(L, i));
+                break;
+            }
+            case LUA_TTABLE: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  <table>", i);
+                break;
+            }
+            case LUA_TFUNCTION: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  <function>", i);
+                break;
+            }
+            default: {
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
+                              "%d:  unkown: -[%s]-", i, lua_typename(L, i));
+                break;                
+            }
+        }
+    }
+}
+
+/**
+ * Verify that the thing at index is a request_rec wrapping
+ * userdata thingamajig and return it if it is. if it is not
+ * lua will enter its error handling routine.
+ */
+static request_rec* apw_check_request_rec(lua_State* L, int index) {
+    luaL_checkudata(L, index, "Apache2.Request");
+    request_rec* r = (request_rec*)lua_unboxpointer(L, index);
+    return r;    
+}
+
+/* ------------------ request methods -------------------- */
+/* helper callback for req_parseargs */
+static int req_aprtable2luatable_cb(void *l, const char *key, const char *value) {
+    lua_State* L = (lua_State*)l; /* [table<s,t>, table<s,s>] */
+    /* rstack_dump(L, RRR, "start of cb"); */
+    /* L is [table<s,t>, table<s,s>] */
+    /* build complex */
+
+    lua_getfield(L, -1, key); /* [VALUE, table<s,t>, table<s,s>] */
+    /* rstack_dump(L, RRR, "after getfield"); */ 
+    int t = lua_type(L, -1);
+    switch(t) {
+        case LUA_TNIL:
+        case LUA_TNONE: {
+            lua_pop(L, 1); /* [table<s,t>, table<s,s>] */
+            lua_newtable(L); /* [array, table<s,t>, table<s,s>] */
+            lua_pushnumber(L, 1); /* [1, array, table<s,t>, table<s,s>] */
+            lua_pushstring(L, value); /* [string, 1, array, table<s,t>, table<s,s>] */
+            lua_settable(L, -3); /* [array, table<s,t>, table<s,s>]  */
+            lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
+            break;
+        }
+        case LUA_TTABLE: {
+            /* [array, table<s,t>, table<s,s>] */
+            int size = lua_objlen(L, -1);
+            lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */
+            lua_pushstring(L, value); /* [string, #, array, table<s,t>, table<s,s>] */
+            lua_settable(L, -3); /* [array, table<s,t>, table<s,s>] */
+            lua_setfield(L, -2, key); /* [table<s,t>, table<s,s>] */
+            break;
+        }
+    }
+
+    /* L is [table<s,t>, table<s,s>] */
+    /* build simple */
+    lua_getfield(L, -2, key); /* [VALUE, table<s,s>, table<s,t>] */
+    if (lua_isnoneornil(L, -1)) { /* only set if not already set */
+        lua_pop(L, 1); /* [table<s,s>, table<s,t>]] */
+        lua_pushstring(L, value); /* [string, table<s,s>, table<s,t>] */
+        lua_setfield(L, -3, key); /* [table<s,s>, table<s,t>]  */
+    } else { lua_pop(L, 1); }
+    return 1;
+}
+
+/* r:parseargs() returning a lua table */
+static int req_parseargs(lua_State* L) {
+    request_rec* r = apw_check_request_rec(L, 1);
+    apreq_handle_t* h = apreq_handle_apache2(r);
+    lua_newtable(L);
+    lua_newtable(L); /* [table, table] */
+    const apr_table_t* form_table;
+    if (apreq_args(h, &form_table) == APR_SUCCESS) {
+        apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);                
+    }
+    return 2; /* [table<string, string>, table<string, array<string>>] */
+}
+
+/* wrap ap_rputs as r:puts(String) */
+static int req_puts(lua_State* L) {    
+    request_rec* r = apw_check_request_rec(L, 1);
+    
+    int argc = lua_gettop(L);
+    int i;
+    
+    for (i=2;i<=argc;i++) {
+        ap_rputs(luaL_checkstring(L, i), r);
+    }
+    return 0;
+}
+
+/* wrap ap_rwrite as r:write(String) */
+static int req_write(lua_State* L) {
+    request_rec* r = apw_check_request_rec(L, 1);
+    size_t n;
+    const char* buf = luaL_checklstring(L, 2, &n);
+  
+    ap_rwrite((void *)buf, n, r);
+    return 0;
+}
+
+/* r:parsebody() */
+static int req_parsebody(lua_State* L) {
+    request_rec* r = apw_check_request_rec(L, 1);
+    apreq_handle_t* h = apreq_handle_apache2(r);
+    lua_newtable(L);
+    lua_newtable(L);
+    const apr_table_t* form_table;
+    if (apreq_body(h, &form_table) == APR_SUCCESS) {
+        apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL);        
+    }
+    return 2;
+}
+
+/* r:addoutputfilter(name|function) */
+static int req_add_output_filter(lua_State *L) {
+    request_rec* r = apw_check_request_rec(L, 1);    
+    const char *name = luaL_checkstring(L, 2);
+    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "adding output filter %s", name);
+    ap_add_output_filter(name, L, r, r->connection);
+    return 0;
+}
+
+static int req_document_root(lua_State* L) {
+  request_rec* r = apw_check_request_rec(L, 1);
+  char* doc_root = apr_pstrdup(r->pool, ap_document_root(r));
+  lua_pushstring(L, doc_root);
+  return 1;
+}
+
+/* BEGIN dispatch mathods for request_rec fields */
+
+static char* req_uri_field(request_rec* r) {
+    return r->uri;
+}
+
+static const char* req_method_field(request_rec* r) {
+    return r->method;
+}
+
+static const char* req_hostname_field(request_rec* r) {
+    return r->hostname;
+}
+
+static const char* req_args_field(request_rec* r) {
+    return r->args;
+}
+
+static const char* req_path_info_field(request_rec* r) {
+    return r->path_info;
+}
+
+static const char* req_canonical_filename_field(request_rec* r) {
+    return r->canonical_filename;
+}
+
+static const char* req_filename_field(request_rec* r) {
+    return r->filename;
+}
+
+static const char* req_user_field(request_rec* r) {
+    return r->user;
+}
+
+static const char* req_unparsed_uri_field(request_rec* r) {
+    return r->unparsed_uri;
+}
+
+static const char* req_ap_auth_type_field(request_rec* r) {
+    return r->ap_auth_type;
+}
+
+static const char* req_content_encoding_field(request_rec* r) {
+    return r->content_encoding;
+}
+
+static const char* req_content_type_field(request_rec* r) {
+    return r->content_type;
+}
+
+static const char* req_range_field(request_rec* r) {
+    return r->range;
+}
+
+static const char* req_protocol_field(request_rec* r) {
+    return r->protocol;
+}
+
+static const char* req_the_request_field(request_rec* r) {
+    return r->the_request;
+}
+
+static int req_status_field(request_rec* r) {
+    return r->status;
+}
+
+static int req_assbackwards_field(request_rec* r) {
+    return r->assbackwards;
+}
+
+/* END dispatch mathods for request_rec fields */
+
+static int req_dispatch(lua_State* L) {
+    request_rec* r = apw_check_request_rec(L, 1);
+    const char *name = luaL_checkstring(L, 2);
+    lua_pop(L, 2);
+    lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
+    apr_hash_t* dispatch = lua_touserdata(L, 1);
+    lua_pop(L, 1);
+    
+    req_fun_t* rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING);
+    if (rft) {
+        switch(rft->type) {
+            case APW_REQ_FUNTYPE_TABLE: {
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                              "request_rec->dispatching %s -> apr table (NOT IMPLEMENTED YET)", name);
+                return 0;
+            }
+            
+            case APW_REQ_FUNTYPE_LUACFUN: {
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                              "request_rec->dispatching %s -> lua_CFunction", name);
+                lua_CFunction func = rft->fun;
+                lua_pushcfunction(L, func);      
+                return 1;                
+            }
+            case APW_REQ_FUNTYPE_STRING: {
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                              "request_rec->dispatching %s -> string", name);
+                req_field_string_f func = rft->fun;
+                char* rs = (*func)(r);
+                lua_pushstring(L, rs);
+                return 1;                
+            }
+            case APW_REQ_FUNTYPE_INT: {
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                              "request_rec->dispatching %s -> int", name);
+                req_field_int_f func = rft->fun;
+                int rs = (*func)(r);
+                lua_pushnumber(L, rs);
+                return 1;                
+            }
+            case APW_REQ_FUNTYPE_BOOLEAN: {
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                              "request_rec->dispatching %s -> boolean", name);
+                req_field_int_f func = rft->fun;
+                int rs = (*func)(r);
+                lua_pushboolean(L, rs);
+                return 1;                
+            }
+        }
+    }
+    
+    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, 
+                  "nothing for %s", name);  
+    return 0;
+}
+
+/* helper function for the logging functions below */
+static int req_log_at(lua_State* L, int level) {
+    request_rec* r = apw_check_request_rec(L, 1);
+    lua_Debug dbg;
+    
+    lua_getstack(L, 1, &dbg);
+    lua_getinfo(L, "Sl", &dbg);
+
+    const char* msg = luaL_checkstring(L, 2);
+    ap_log_rerror(dbg.source, dbg.currentline, level, 0, r, msg);
+    return 0;
+}
+
+/* r:debug(String) and friends which use apache logging */
+static int req_emerg(lua_State* L)  { req_log_at(L, APLOG_EMERG); return 0; }
+static int req_alert(lua_State* L)  { req_log_at(L, APLOG_ALERT); return 0; }
+static int req_crit(lua_State* L)   { req_log_at(L, APLOG_CRIT); return 0; }
+static int req_err(lua_State* L)    { req_log_at(L, APLOG_ERR); return 0; }
+static int req_warn(lua_State* L)   { req_log_at(L, APLOG_WARNING); return 0; }
+static int req_notice(lua_State* L) { req_log_at(L, APLOG_NOTICE); return 0; }
+static int req_info(lua_State* L)   { req_log_at(L, APLOG_INFO); return 0; }
+static int req_debug(lua_State* L)  { req_log_at(L, APLOG_DEBUG); return 0; }
+
+/* handle r.status = 201 */
+static int req_newindex(lua_State* L) {
+    /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */ 
+    /* const char* key = luaL_checkstring(L, -2); */
+    request_rec* r = apw_check_request_rec(L, 1);
+    rstack_dump(L, r, "req_newindex");
+    const char *key = luaL_checkstring(L, 2);
+    rstack_dump(L, r, "req_newindex");
+    if (0 == apr_strnatcmp("status", key)) {
+        int code = luaL_checkinteger(L, 3);
+        r->status = code;
+        luaL_getmetatable(L, "Apache2.Request");
+        lua_pushinteger(L, code);
+        lua_setfield(L, -2, "status");
+        lua_pop(L, 1);
+        return 0;
+    }
+    
+    if (0 == apr_strnatcmp("content_type", key)) {
+        const char* value = luaL_checkstring(L, 3);
+        r->content_type = apr_pstrdup(r->pool, value);
+        luaL_getmetatable(L, "Apache2.Request");
+        lua_pushstring(L, value);
+        lua_setfield(L, -2, "content_type");
+        lua_pop(L, 1);
+        return 0;
+    }
+    
+    if (0 == apr_strnatcmp("filename", key)) {
+        const char* value = luaL_checkstring(L, 3);
+        r->filename = apr_pstrdup(r->pool, value);
+        luaL_getmetatable(L, "Apache2.Request");
+        lua_pushstring(L, value);
+        lua_setfield(L, -2, "filename");
+        lua_pop(L, 1);
+        return 0;
+    }
+
+    if (0 == apr_strnatcmp("uri", key)) {
+        const char* value = luaL_checkstring(L, 3);
+        r->uri = apr_pstrdup(r->pool, value);
+        luaL_getmetatable(L, "Apache2.Request");
+        lua_pushstring(L, value);
+        lua_setfield(L, -2, "uri");
+        lua_pop(L, 1);
+        return 0;
+    }
+    
+    lua_pushstring(L, apr_psprintf(r->pool, "Property [%s] may not be set on a request_rec", key));
+    lua_error(L);
+    return 0;
+}
+
+static const struct luaL_Reg request_methods[] = {
+    {"__index", req_dispatch},
+    {"__newindex", req_newindex},
+    /*   {"__newindex", req_set_field}, */
+    {NULL, NULL}
+};
+
+
+static const struct luaL_Reg connection_methods[] = {
+    {NULL, NULL}
+};
+
+
+static const struct luaL_Reg server_methods[] = {
+    {NULL, NULL}
+};
+
+
+static req_fun_t* makefun(void* fun, int type, apr_pool_t* pool) {
+    req_fun_t* rft = apr_palloc(pool, sizeof(req_fun_t));
+    rft->fun = fun;
+    rft->type = type;
+    return rft;
+}
+
+void apw_load_request_lmodule(lua_State *L, apr_pool_t *p) {
+    
+    apr_hash_t* dispatch = apr_hash_make(p);
+    
+    apr_hash_set(dispatch, "puts", APR_HASH_KEY_STRING, 
+                 makefun(&req_puts, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING, 
+                 makefun(&req_write, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING, 
+                 makefun(&req_document_root, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING, 
+                 makefun(&req_parseargs, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "parsebody", APR_HASH_KEY_STRING, 
+                 makefun(&req_parsebody, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING, 
+                 makefun(&req_debug, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING, 
+                 makefun(&req_info, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING, 
+                 makefun(&req_notice, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING, 
+                 makefun(req_warn, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING, 
+                 makefun(&req_err, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING, 
+                 makefun(&req_crit, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING, 
+                 makefun(&req_alert, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING, 
+                 makefun(&req_emerg, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING, 
+                 makefun(&req_add_output_filter, APW_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING, 
+                 makefun(&req_assbackwards_field, APW_REQ_FUNTYPE_BOOLEAN, p));
+    apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING, 
+                 makefun(&req_status_field, APW_REQ_FUNTYPE_INT, p));
+    apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING, 
+                 makefun(&req_protocol_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING, 
+                 makefun(&req_range_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING, 
+                 makefun(&req_content_type_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING, 
+                 makefun(&req_content_encoding_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING, 
+                 makefun(&req_ap_auth_type_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING, 
+                 makefun(&req_unparsed_uri_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING, 
+                 makefun(&req_user_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING, 
+                 makefun(&req_filename_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING, 
+                 makefun(&req_canonical_filename_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING, 
+                 makefun(&req_path_info_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING, 
+                 makefun(&req_args_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING, 
+                 makefun(&req_hostname_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING, 
+                 makefun(&req_uri_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING, 
+                 makefun(&req_the_request_field, APW_REQ_FUNTYPE_STRING, p));
+    apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING, 
+                 makefun(&req_method_field, APW_REQ_FUNTYPE_STRING, p));
+    
+    lua_pushlightuserdata(L, dispatch);
+    lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
+    
+    luaL_newmetatable(L, "Apache2.Request"); /* [metatable] */
+    lua_pushvalue(L, -1); 
+
+    lua_setfield(L, -2, "__index"); 
+    luaL_register(L, NULL, request_methods); /* [metatable] */
+
+    lua_pop(L, 2);
+
+    luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */
+    lua_pushvalue(L, -1); 
+
+    lua_setfield(L, -2, "__index"); 
+    luaL_register(L, NULL, connection_methods); /* [metatable] */
+
+    lua_pop(L, 2);
+
+    luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */
+    lua_pushvalue(L, -1); 
+
+    lua_setfield(L, -2, "__index"); 
+    luaL_register(L, NULL, server_methods); /* [metatable] */
+
+    lua_pop(L, 2);
+
+}
+
+void apw_push_connection(lua_State* L, conn_rec* c) {  
+    lua_boxpointer(L, c);
+    luaL_getmetatable(L, "Apache2.Connection");
+    lua_setmetatable(L, -2);
+    luaL_getmetatable(L, "Apache2.Connection");
+
+    apw_push_apr_table(L, "notes", c->notes);
+
+    lua_pushstring(L, c->remote_ip);
+    lua_setfield(L, -2, "remote_ip");    
+
+    lua_pop(L, 1);
+}
+
+
+void apw_push_server(lua_State* L, server_rec* s) {  
+    lua_boxpointer(L, s);
+    luaL_getmetatable(L, "Apache2.Server");
+    lua_setmetatable(L, -2);
+    luaL_getmetatable(L, "Apache2.Server");
+
+    lua_pushstring(L, s->server_hostname);
+    lua_setfield(L, -2, "server_hostname");    
+
+    lua_pop(L, 1);
+}
+
+void apw_push_request(lua_State* L, request_rec* r) {  
+    lua_boxpointer(L, r);
+    luaL_getmetatable(L, "Apache2.Request");
+    lua_setmetatable(L, -2);
+}
+
diff --git a/modules/wombat/request.h b/modules/wombat/request.h
new file mode 100644 (file)
index 0000000..94cf44a
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef REQUEST_H
+#define REQUEST_H
+
+APR_DECLARE(void) apw_push_request(lua_State* L, request_rec* r);
+APR_DECLARE(void) apw_load_request_lmodule(lua_State *L, apr_pool_t *p);
+
+#define APW_REQ_FUNTYPE_STRING      1
+#define APW_REQ_FUNTYPE_INT         2
+#define APW_REQ_FUNTYPE_TABLE       3
+#define APW_REQ_FUNTYPE_LUACFUN     4
+#define APW_REQ_FUNTYPE_BOOLEAN     5
+
+typedef struct {
+    void *fun;
+    int type;
+} req_fun_t;
+
+
+#endif
+
diff --git a/modules/wombat/test/helpers.lua b/modules/wombat/test/helpers.lua
new file mode 100644 (file)
index 0000000..79bd269
--- /dev/null
@@ -0,0 +1,36 @@
+module("helpers", package.seeall)
+
+local io = require("io")
+local http = require("socket.http")
+local string = require("string")
+
+base_url = "http://localhost"
+
+function get(uri)
+  return http.request(base_url .. uri)  
+end
+
+function post(uri, body)
+  local function do_it(body)
+    local flat
+    if (type(body) == "table") then
+      i = 1
+      for k, v in pairs(body) do
+        if i == 1 then 
+          flat = k .. "=" ..v 
+        else
+          flat = flat .. "&" .. k .. "=" .. v
+        end
+        i = i + 1
+      end
+    else
+      flat = body;
+    end
+    return http.request(base_url .. uri, flat) 
+  end
+  if body then
+    return do_it(body)
+  else
+    return do_it
+  end
+end
\ No newline at end of file
diff --git a/modules/wombat/test/htdocs/config_tests.lua b/modules/wombat/test/htdocs/config_tests.lua
new file mode 100644 (file)
index 0000000..698bedf
--- /dev/null
@@ -0,0 +1,37 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+require 'string'
+
+local count = 0
+
+function handle(r)
+    r:puts("success in handle " .. count)
+end
+
+function handle_server_vm(r)
+    r:puts("hello from server scope " .. count)
+    count = count + 1
+end
+
+function handle_request_vm(r)
+    r:puts("hello from request scope " .. count)
+    count = count + 1
+end
+
+function handle_conn_vm(r)
+    r:puts("hello from request scope " .. count)
+    count = count + 1
+end
\ No newline at end of file
diff --git a/modules/wombat/test/htdocs/filters.lua b/modules/wombat/test/htdocs/filters.lua
new file mode 100644 (file)
index 0000000..cad0ca8
--- /dev/null
@@ -0,0 +1,7 @@
+
+local s = require 'string'
+
+function handle_simple(r)
+  -- r:addoutputfilter("wombathood")
+  r:puts("added wombathood")
+end
\ No newline at end of file
diff --git a/modules/wombat/test/htdocs/find_me.txt b/modules/wombat/test/htdocs/find_me.txt
new file mode 100644 (file)
index 0000000..cb96e9e
--- /dev/null
@@ -0,0 +1 @@
+please find me
\ No newline at end of file
diff --git a/modules/wombat/test/htdocs/hooks.lua b/modules/wombat/test/htdocs/hooks.lua
new file mode 100644 (file)
index 0000000..b8a8248
--- /dev/null
@@ -0,0 +1,29 @@
+require 'string'
+require 'apache2'
+
+function translate_name(r)
+    if r.uri == "/translate-name" then
+        r.uri = "/find_me.txt"
+        return apache2.DECLINED
+    end
+    return apache2.DECLINED
+end
+
+function translate_name2(r)
+    if r.uri == "/translate-name2" then
+        r.uri = "/find_me.txt"
+        return apache2.DECLINED
+    end
+    return apache2.DECLINED
+end
+
+function fixups_test(r)
+  -- r:err("KABAZ")
+  if r.uri == "/test_fixupstest" then
+    -- r:err("KABIZ")
+    r.status = 201
+    return apache2.OK
+  end
+  -- r:err("ZIBAK")
+  return apache2.DECLINED
+end
\ No newline at end of file
diff --git a/modules/wombat/test/htdocs/other.lua b/modules/wombat/test/htdocs/other.lua
new file mode 100644 (file)
index 0000000..90c6ed2
--- /dev/null
@@ -0,0 +1,21 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+module("other")
+
+function doit(r)
+  r:debug("doing it...")
+  r:puts("Do It!\n")
+end
diff --git a/modules/wombat/test/htdocs/simple.lua b/modules/wombat/test/htdocs/simple.lua
new file mode 100644 (file)
index 0000000..f949001
--- /dev/null
@@ -0,0 +1,3 @@
+function handle(r)
+   r:puts("Hi!")
+end
diff --git a/modules/wombat/test/htdocs/test.lua b/modules/wombat/test/htdocs/test.lua
new file mode 100755 (executable)
index 0000000..c0b96ab
--- /dev/null
@@ -0,0 +1,129 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+require 'string'
+
+function print_args(r, simple, complex)
+  local s = "    %s: %s\n"
+  r:puts("  simple:\n")
+  for k, v in pairs(simple) do
+    r:puts(s:format(k, v))
+  end
+
+  s = "    %s: "
+  r:puts("  complex:\n")
+  for k, ary in pairs(complex) do
+    r:puts(s:format(k))
+    for i=1, #ary do
+      r:puts(ary[i])
+      if i < #ary then r:puts(", ") end
+    end
+    r:puts("\n")
+  end
+end
+
+function debug_stuff(r)
+  r:debug("This is a debug log message")
+  -- r:info("This is an info log message")
+  -- r:notice("This is an notice log message")
+  -- r:warn("This is an warn log message")
+  -- r:err("This is an err log message")
+  -- r:alert("This is an alert log message")
+  -- r:crit("This is an crit log message")
+  -- r:emerg("This is an emerg log message")
+end
+
+function handle(r)
+  r:puts("hello Lua world\n")
+  r:puts("Query args:\n")
+  
+  print_args(r, r:parseargs());
+  
+  debug_stuff(r)
+    
+  r:puts("HTTP Method:\n  " .. r.method .. "\n")
+
+  if r.method == 'POST' then
+    print_args(r, r:parsebody())
+  end
+
+  require("other")
+  r:puts("loaded relative to script:\n  ")
+  other.doit(r)
+  
+  r:puts("loaded from LuaPackagePath:\n")
+  require("kangaroo");
+  kangaroo.hop(r);
+end
+
+function handle_foo(r)
+  r:puts("Handler FOO!\n")
+  r.status = 201
+  r:debug("set status to 201")
+end
+
+
+function handle_attributes(r)
+  local function pf(name)
+    r:puts(("%s: %s\n"):format(name, tostring(r[name])))
+  end
+
+  pf("status")
+  r.status = 201
+  pf("status")
+  r:puts("\n")
+    
+  pf("content_type")
+  r.content_type = "text/plain?charset=ascii"
+  pf("content_type")
+  r:puts("\n")
+  
+  pf("method")
+  pf("protocol")
+  pf("assbackwards")
+  pf("the_request")
+  pf("range")
+  pf("content_encoding")
+  pf("user")
+  pf("unparsed_uri")
+  pf("ap_auth_type")
+  pf("uri")
+  pf("filename")
+  pf("canonical_filename")
+  pf("path_info")
+  pf("args")
+  
+  r:puts("\n")
+end
+
+function test_headers(r)
+  r:puts("test getting and setting headers here\n")
+end
+
+function handle_quietly(r)
+  r:puts("hello!")
+end
+
+function handle_regex(r)
+  r:puts("matched in handle_regex")
+end
+
+function handle_serverversion(r)
+  r:puts(apache2.version)
+end
+
+function handle_fixupstest(r)
+  r:puts("status is " .. r.status)
+end
\ No newline at end of file
diff --git a/modules/wombat/test/httpd_config.lua b/modules/wombat/test/httpd_config.lua
new file mode 100644 (file)
index 0000000..c4d08f4
--- /dev/null
@@ -0,0 +1,64 @@
+-- require 'string'
+-- require 'apache2.config'
+
+function configure(cmd, dir)  
+    dir:match_handler {
+        pattern = "^/server-says-hi$", 
+        file    = "htdocs/config_tests.lua", 
+        func    = "handle_server_vm",
+        scope   = "server",
+        -- options = {
+        --     minimum_idle = 10,
+        --     maximum_idle = 20
+        -- }
+    }
+    
+    dir:match_handler {
+        pattern = "^/super-basic-config$",
+        file    = "htdocs/config_tests.lua"
+    }
+    --[[
+    LuaMapHandler /basic /Users/brianm/src/wombat/test/htdocs/test.lua
+    LuaMapHandler /filter/simple /Users/brianm/src/wombat/test/htdocs/filters.lua handle_simple
+    LuaMapHandler ^/(\w+)_(\w+)$ /Users/brianm/src/wombat/test/htdocs/$1.lua handle_$2
+    ]]--
+    
+    dir:match_handler {
+        pattern = "^/simple$",
+        file    = "htdocs/simple.lua"
+    }
+    
+    dir:match_handler {
+        pattern = "^/filter/simple$",
+        file    = "htdocs/filters.lua",
+        func    = "handle_simple"
+    }
+    
+    dir:match_handler {
+        pattern = "^/(\\w+)_(\\w+)$",
+        file    = "htdocs/$1.lua",
+        func    = "handle_$2"
+    }
+    
+    dir:match_handler {
+       pattern = "^/request-says-hi$", 
+        file    = "/Users/brianm/src/wombat/test/htdocs/config_tests.lua", 
+        func    = "handle_request_vm",
+       scope   = "request"
+       }
+  
+    dir:match_handler {
+        pattern = "^/connection-says-hi$", 
+       file    = "/Users/brianm/src/wombat/test/htdocs/test.lua", 
+       func    = "handle_conn_vm",
+       scope   = "connection"
+       }
+    
+  -- dir:lua_match_handler {
+  --   pattern = "^/once-says-hi$", 
+  --   file    = "/Users/brianm/src/wombat/test/htdocs/test.lua", 
+  --   func    = "handle_configure_server",
+  --   scope   = "tests"
+  -- }
+end
diff --git a/modules/wombat/test/lib/kangaroo.lua b/modules/wombat/test/lib/kangaroo.lua
new file mode 100644 (file)
index 0000000..00ba3e5
--- /dev/null
@@ -0,0 +1,19 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+module("kangaroo")
+
+function hop(r)
+  r:puts("  hop hop!\n")
+end
diff --git a/modules/wombat/test/moonunit.lua b/modules/wombat/test/moonunit.lua
new file mode 100644 (file)
index 0000000..8537a1e
--- /dev/null
@@ -0,0 +1,52 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+module("moonunit", package.seeall)
+
+TestCase = {}
+
+function TestCase:new(it)
+  it = it or {}
+  setmetatable(it, self)
+  self.__index = self
+  return it
+end
+
+function TestCase:run(args)
+  args = args or arg
+  local function run_test(t, name)
+    local status, err = pcall(t, self)
+    if status then
+      print(("%-39s \27[32mpass\27[39m"):format("[" .. name .. "]"))
+    else
+      print(("%-39s \27[31mFAIL\27[39m %s"):format("[" .. name .. "]", err))
+    end
+  end
+  
+  if (args and #args > 0) then
+    for _, v in ipairs(args) do
+      if type(self[v]) == "function" then
+        run_test(self[v], v)
+      else
+        print(("%-39s FAIL %s"):format("[" .. v .. "]", 
+          "'" .. v .. "' doesn't appear to be a test function"))
+      end
+    end
+  else
+    for k, v in pairs(self) do
+      run_test(v, k)
+    end
+  end
+end
diff --git a/modules/wombat/test/test.lua b/modules/wombat/test/test.lua
new file mode 100755 (executable)
index 0000000..59ef00a
--- /dev/null
@@ -0,0 +1,126 @@
+#!/usr/bin/env lua
+
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+local mu = require "moonunit" 
+local http = require "helpers"
+
+http.base_url = "http://localhost:8000"
+
+local test = mu.TestCase:new{}
+
+function test:document_root()
+  local b, c = http.get "/document_root.lua"
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(b:find("test"), "test not found in document root")
+end
+
+function test:basic_get()
+  local b, c = http.get "/basic"
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(b:find("hello Lua world"), "'hello Lua world' not found in response")
+end
+
+function test:quietly()
+  local b, c = http.get "/test_quietly"
+  assert(200 == c, "unexpected response code " .. c)
+  assert(b == 'hello!', "unexpected response body [" .. b .. "]")
+end
+
+function test.basic_post()
+  local b, c = http.post "/basic" "hello=7&hello=1"
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(b:find("complex:%s+hello: 7, 1\n"), "didn't find complex post parsing")
+  assert(b:find("simple:%s+hello: 7\n"), "didn't find simple post parsing")
+end
+
+function test.basic_post_alt()
+  local b, c = http.post("/test_foo", "hello=7&hello=1")
+  assert(201 == c, "expected status code 200, got " .. c)
+  assert(b:find("Handler FOO!"), "unexpected output!")
+end
+
+function test.post_with_table()
+  local b, c = http.post "/basic" { hello = "7" }
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]")
+  
+  b, c = http.post "/basic" { hello = "7", goodbye = "8" }
+  
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]")
+  assert(b:find("goodbye: 8"), "didn't get expected post data [" .. b .."]")
+end
+
+function test:simple_filter()
+  local b, c = http.get "/filter/simple"
+  assert(200 == c, "expected status code 200, got " .. c)
+end
+
+function test:request_attributes()
+  local r, c = http.get "/test_attributes?yes=no"
+  assert(201 == c, "expected status code 201, got " .. c)
+  
+  assert(r:find("status: 200\nstatus: 201"), "changing status code failed")
+  assert(r:find("method: GET"), "method wasn't reported correctly")
+  assert(r:find("protocol: HTTP/1.1"), "protocol reported incorrectly")
+  assert(r:find("assbackwards: false"), "assbackwards reported incorrectly")
+  assert(r:find("args: yes=no"), "args not reported correctly")
+end
+
+function test:map_regex()
+  local r, c = http.get "/test_regex"
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'")  
+end
+
+function test:map_regex2()
+  local r, c = http.get "/test_regex?a=8"
+  assert(200 == c, "expected status code 200, got " .. c)
+  assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'")  
+end
+
+function test:translate_name_hook()
+  local r, c = http.get "/translate-name"
+  assert(200 == c, "expected 200 got " .. c)
+  assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r)
+end
+
+function test:translate_name_hook2()
+  local r, c = http.get "/translate-name2"
+  assert(200 == c, "expected 200 got " .. c)
+  assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r)
+end
+
+function test:server_version()
+  local r, c = http.get "/test_serverversion"
+  assert(200 == c)
+  assert(r:find("Apache/2"), "version isn't Apache/2, but is " .. r)
+end
+
+function test:fixups_hook()
+  local r, c = http.get "/test_fixupstest"
+  assert(201 == c, "incorrect status code returned, expected 201 got " .. c)
+  assert(r:find("status is 201"), "handler sees incorrect status")
+end
+
+function test:simple()
+    local r, c = http.get "/simple.lua"
+    assert(200 == c, "incorrect status code returned, expected 200 got " .. c)
+    assert(r:find("Hi"), "Didn't find 'Hi'")
+end
+
+test:run()
diff --git a/modules/wombat/test/test_httpd.conf b/modules/wombat/test/test_httpd.conf
new file mode 100755 (executable)
index 0000000..7b236ed
--- /dev/null
@@ -0,0 +1,32 @@
+# Customize these two values for your Apache2 install
+ServerRoot "/Users/brianm/.opt/httpd-2.2.3-worker-for-lua"
+DocumentRoot "/Users/brianm/src/wombat/test/htdocs"
+
+# Customize this value to point to the top of mod_wombat's test dir
+LuaRoot /Users/brianm/src/wombat/test
+
+Listen 8000
+
+LoadModule apreq_module modules/mod_apreq2.so
+LoadModule wombat_module modules/mod_wombat.so
+
+AddHandler lua-script .lua
+
+#LuaConfig httpd_config.lua configure
+
+LuaMapHandler /basic /Users/brianm/src/wombat/test/htdocs/test.lua
+LuaMapHandler /filter/simple /Users/brianm/src/wombat/test/htdocs/filters.lua handle_simple
+LuaMapHandler ^/(\w+)_(\w+)$ /Users/brianm/src/wombat/test/htdocs/$1.lua handle_$2
+
+LuaHookTranslateName htdocs/hooks.lua translate_name
+LuaHookTranslateName htdocs/hooks.lua translate_name2
+
+LuaHookFixups htdocs/hooks.lua fixups_test
+
+LuaPackagePath lib/?.lua
+
+# stat | forever | never
+LuaCodeCache stat
+
+ErrorLog logs/error_log
+LogLevel debug
diff --git a/modules/wombat/vmprep.c b/modules/wombat/vmprep.c
new file mode 100644 (file)
index 0000000..5ebbc83
--- /dev/null
@@ -0,0 +1,717 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "vmprep.h"
+#include "mod_wombat.h"
+#include "http_log.h"
+#include "apr_reslist.h"
+#include "apr_uuid.h"
+#include "config.h"
+#include "apr_file_info.h"
+
+/* forward dec'l from this file */
+// static int load_file(apr_pool_t *working_pool, lua_State* L, const apw_code_cache* cfg, apw_vm_spec *spec);
+
+void pstack_dump(lua_State* L, apr_pool_t* r, int level, const char* msg) {
+    ap_log_perror(APLOG_MARK, level, 0, r, "Lua Stack Dump: [%s]", msg);
+
+    int i;
+    int top = lua_gettop(L);
+    for (i = 1; i<= top; i++) {
+        int t = lua_type(L, i);
+        switch(t) {
+            case LUA_TSTRING: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  '%s'", i, lua_tostring(L, i));
+                break;
+            }
+            case LUA_TUSERDATA: {
+                ap_log_perror(APLOG_MARK, level, 0, r, "%d:  userdata", i);                
+                break;
+            }
+            case LUA_TLIGHTUSERDATA: {
+                ap_log_perror(APLOG_MARK, level, 0, r, "%d:  lightuserdata", i);
+                break;
+            }
+            case LUA_TNIL: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  NIL", i);
+                break;
+            }
+            case LUA_TNONE: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  None", i);
+                break;
+            }
+            case LUA_TBOOLEAN: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  %s", i,  lua_toboolean(L, i) ? "true" : "false");
+                break;
+            }
+            case LUA_TNUMBER: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  %g", i, lua_tonumber(L, i));
+                break;
+            }
+            case LUA_TTABLE: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  <table>", i);
+                break;
+            }
+            case LUA_TTHREAD: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  <thread>", i);
+                break;
+            }
+            case LUA_TFUNCTION: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  <function>", i);
+                break;
+            }
+            default: {
+                ap_log_perror(APLOG_MARK, level, 0, r, 
+                              "%d:  unkown: [%s]", i, lua_typename(L, i));
+                break;                
+            }
+        }
+    }
+}
+
+/* BEGIN modules*/
+
+/* BEGIN apache lmodule  */
+
+void apw_load_apache2_lmodule(lua_State *L) {
+    lua_getglobal(L, "package");
+    lua_getfield(L, -1, "loaded");
+    lua_newtable(L);    
+    lua_setfield(L, -2, "apache2");
+    lua_setglobal(L, "apache2");
+    lua_pop(L, 1); /* empty stack */
+
+    lua_getglobal(L, "apache2");
+    lua_pushinteger(L, OK);
+    lua_setfield(L, -2, "OK");
+
+    lua_pushinteger(L, DECLINED);
+    lua_setfield(L, -2, "DECLINED");
+
+    lua_pushinteger(L, DONE);
+    lua_setfield(L, -2, "DONE");
+   
+    lua_pushstring(L, ap_get_server_banner());
+    lua_setfield(L, -2, "version");
+
+    lua_pushinteger(L, HTTP_MOVED_TEMPORARILY);
+    lua_setfield(L, -2, "HTTP_MOVED_TEMPORARILY");
+    
+    /*
+    lua_pushinteger(L, HTTP_CONTINUE);
+    lua_setfield(L, -2, "HTTP_CONTINUE");
+    lua_pushinteger(L, HTTP_SWITCHING_PROTOCOLS);
+    lua_setfield(L, -2, "HTTP_SWITCHING_PROTOCOLS");
+    lua_pushinteger(L, HTTP_PROCESSING);
+    lua_setfield(L, -2, "HTTP_PROCESSING");
+    lua_pushinteger(L, HTTP_OK);
+    lua_setfield(L, -2, "HTTP_OK");
+    lua_pushinteger(L, HTTP_CREATED);
+    lua_setfield(L, -2, "HTTP_CREATED");
+    lua_pushinteger(L, HTTP_ACCEPTED);
+    lua_setfield(L, -2, "HTTP_ACCEPTED");
+    lua_pushinteger(L, HTTP_NON_AUTHORITATIVE);
+    lua_setfield(L, -2, "HTTP_NON_AUTHORITATIVE");
+    lua_pushinteger(L, HTTP_NO_CONTENT);
+    lua_setfield(L, -2, "HTTP_NO_CONTENT");
+    lua_pushinteger(L, HTTP_RESET_CONTENT);
+    lua_setfield(L, -2, "HTTP_RESET_CONTENT");
+    lua_pushinteger(L, HTTP_PARTIAL_CONTENT);
+    lua_setfield(L, -2, "HTTP_PARTIAL_CONTENT");
+    lua_pushinteger(L, HTTP_MULTI_STATUS);
+    lua_setfield(L, -2, "HTTP_MULTI_STATUS");
+    lua_pushinteger(L, HTTP_MULTIPLE_CHOICES);
+    lua_setfield(L, -2, "HTTP_MULTIPLE_CHOICES");
+    lua_pushinteger(L, HTTP_MOVED_PERMANENTLY);
+    lua_setfield(L, -2, "HTTP_MOVED_PERMANENTLY");
+    lua_pushinteger(L, HTTP_SEE_OTHER);
+    lua_setfield(L, -2, "HTTP_SEE_OTHER");
+    lua_pushinteger(L, HTTP_NOT_MODIFIED);
+    lua_setfield(L, -2, "HTTP_NOT_MODIFIED");
+    lua_pushinteger(L, HTTP_USE_PROXY);
+    lua_setfield(L, -2, "HTTP_USE_PROXY");
+    lua_pushinteger(L, HTTP_TEMPORARY_REDIRECT);
+    lua_setfield(L, -2, "HTTP_TEMPORARY_REDIRECT");
+    lua_pushinteger(L, HTTP_BAD_REQUEST);
+    lua_setfield(L, -2, "HTTP_BAD_REQUEST");
+    lua_pushinteger(L, HTTP_UNAUTHORIZED);
+    lua_setfield(L, -2, "HTTP_UNAUTHORIZED");
+    lua_pushinteger(L, HTTP_PAYMENT_REQUIRED);
+    lua_setfield(L, -2, "HTTP_PAYMENT_REQUIRED");
+    lua_pushinteger(L, HTTP_FORBIDDEN);
+    lua_setfield(L, -2, "HTTP_FORBIDDEN");
+    lua_pushinteger(L, HTTP_NOT_FOUND);
+    lua_setfield(L, -2, "HTTP_NOT_FOUND");
+    lua_pushinteger(L, HTTP_METHOD_NOT_ALLOWED);
+    lua_setfield(L, -2, "HTTP_METHOD_NOT_ALLOWED");
+    lua_pushinteger(L, HTTP_NOT_ACCEPTABLE);
+    lua_setfield(L, -2, "HTTP_NOT_ACCEPTABLE");
+    lua_pushinteger(L, HTTP_PROXY_AUTHENTICATION_REQUIRED);
+    lua_setfield(L, -2, "HTTP_PROXY_AUTHENTICATION_REQUIRED");
+    lua_pushinteger(L, HTTP_REQUEST_TIME_OUT);
+    lua_setfield(L, -2, "HTTP_REQUEST_TIME_OUT");
+    lua_pushinteger(L, HTTP_CONFLICT);
+    lua_setfield(L, -2, "HTTP_CONFLICT");
+    lua_pushinteger(L, HTTP_GONE);
+    lua_setfield(L, -2, "HTTP_GONE");
+    lua_pushinteger(L, HTTP_LENGTH_REQUIRED);
+    lua_setfield(L, -2, "HTTP_LENGTH_REQUIRED");
+    lua_pushinteger(L, HTTP_PRECONDITION_FAILED);
+    lua_setfield(L, -2, "HTTP_PRECONDITION_FAILED");
+    lua_pushinteger(L, HTTP_REQUEST_ENTITY_TOO_LARGE);
+    lua_setfield(L, -2, "HTTP_REQUEST_ENTITY_TOO_LARGE");
+    lua_pushinteger(L, HTTP_REQUEST_URI_TOO_LARGE);
+    lua_setfield(L, -2, "HTTP_REQUEST_URI_TOO_LARGE");
+    lua_pushinteger(L, HTTP_UNSUPPORTED_MEDIA_TYPE);
+    lua_setfield(L, -2, "HTTP_UNSUPPORTED_MEDIA_TYPE");
+    lua_pushinteger(L, HTTP_RANGE_NOT_SATISFIABLE);
+    lua_setfield(L, -2, "HTTP_RANGE_NOT_SATISFIABLE");
+    lua_pushinteger(L, HTTP_EXPECTATION_FAILED);
+    lua_setfield(L, -2, "HTTP_EXPECTATION_FAILED");
+    lua_pushinteger(L, HTTP_UNPROCESSABLE_ENTITY);
+    lua_setfield(L, -2, "HTTP_UNPROCESSABLE_ENTITY");
+    lua_pushinteger(L, HTTP_LOCKED);
+    lua_setfield(L, -2, "HTTP_LOCKED");
+    lua_pushinteger(L, HTTP_FAILED_DEPENDENCY);
+    lua_setfield(L, -2, "HTTP_FAILED_DEPENDENCY");
+    lua_pushinteger(L, HTTP_UPGRADE_REQUIRED);
+    lua_setfield(L, -2, "HTTP_UPGRADE_REQUIRED");
+    lua_pushinteger(L, HTTP_INTERNAL_SERVER_ERROR);
+    lua_setfield(L, -2, "HTTP_INTERNAL_SERVER_ERROR");
+    lua_pushinteger(L, HTTP_NOT_IMPLEMENTED);
+    lua_setfield(L, -2, "HTTP_NOT_IMPLEMENTED");
+    lua_pushinteger(L, HTTP_BAD_GATEWAY);
+    lua_setfield(L, -2, "HTTP_BAD_GATEWAY");
+    lua_pushinteger(L, HTTP_SERVICE_UNAVAILABLE);
+    lua_setfield(L, -2, "HTTP_SERVICE_UNAVAILABLE");
+    lua_pushinteger(L, HTTP_GATEWAY_TIME_OUT);
+    lua_setfield(L, -2, "HTTP_GATEWAY_TIME_OUT");
+    lua_pushinteger(L, HTTP_VERSION_NOT_SUPPORTED);
+    lua_setfield(L, -2, "HTTP_VERSION_NOT_SUPPORTED");
+    lua_pushinteger(L, HTTP_VARIANT_ALSO_VARIES);
+    lua_setfield(L, -2, "HTTP_VARIANT_ALSO_VARIES");
+    lua_pushinteger(L, HTTP_INSUFFICIENT_STORAGE);
+    lua_setfield(L, -2, "HTTP_INSUFFICIENT_STORAGE");
+    lua_pushinteger(L, HTTP_NOT_EXTENDED);
+    lua_setfield(L, -2, "HTTP_NOT_EXTENDED");
+    */
+} 
+
+/* END apache2 lmodule */
+
+/*  END library functions */
+
+/* callback for cleaning up a lua vm when pool is closed */
+static apr_status_t cleanup_lua(void *l) {
+  lua_close((lua_State*) l);
+  return APR_SUCCESS;
+}
+
+static void munge_path(lua_State *L, 
+                       const char *field,
+                       const char *sub_pat, 
+                       const char *rep_pat,
+                       apr_pool_t *pool, 
+                       apr_array_header_t *paths, 
+                       const char *file) {
+  lua_getglobal(L, "package");
+  lua_getfield(L, -1, field);
+  const char* current = lua_tostring(L, -1);
+  const char* parent_dir = ap_make_dirstr_parent(pool, file);
+  const char* pattern = apr_pstrcat(pool, parent_dir, sub_pat, NULL);
+  luaL_gsub(L, current, rep_pat, pattern);
+  lua_setfield(L, -3, field);
+  lua_getfield(L, -2, field);
+  const char* modified = lua_tostring(L, -1);
+  lua_pop(L, 2);
+  
+  char * part = apr_pstrdup(pool, modified);
+  int i;
+  for (i = 0; i < paths->nelts; i++) {
+    const char *new_path = ((const char**)paths->elts)[i];
+    part = apr_pstrcat(pool, part, ";", new_path, NULL);
+  }
+  lua_pushstring(L, part);
+  lua_setfield(L, -2, field);
+  lua_pop(L, 1); /* pop "package" off the stack     */
+}
+
+/**
+ * pool is a working pool
+ */
+// static lua_State* create_vm(apw_vm_spec *spec, 
+//                             apw_code_cache *cache, 
+//                             apr_pool_t *pool) {
+//     lua_State* L =  luaL_newstate();
+//     luaL_openlibs(L);
+//     
+//     apw_run_wombat_open(L, pool);
+// 
+//     munge_path(L, "path", "?.lua", "./?.lua", pool, spec->package_paths, spec->file);
+//     munge_path(L, "cpath", "?.so", "./?.so", pool, spec->package_cpaths, spec->file);
+//     
+//     if (load_file(pool, L, cache, spec)) {
+//         ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, 
+//                      "Unable to compile Lua file '%s' because of '%s'",
+//                      spec->file, luaL_checkstring(L, -1));
+//         return NULL;
+//     }    
+//     return L;
+// }
+
+// typedef struct {
+//     server_rec *server;
+//     apw_vm_spec *spec;
+// } server_vm_params;
+
+
+// static apr_status_t server_vm_ctor(void **resource, void *_params, apr_pool_t *pool) {
+//     server_vm_params *params = _params;
+//     apw_server_cfg *cfg = ap_get_module_config(params->server->module_config, &wombat_module);    
+//     lua_State *L = create_vm(params->spec, cfg->code_cache, pool);
+//     *resource = L;
+//     /* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, params->server, "L is %d", (int)L); */
+//     return OK;
+// }
+// 
+// static apr_status_t server_vm_dtor(void *resource, void *_params, apr_pool_t *pool) {
+//     return OK;
+// }
+// 
+// typedef struct {
+//     apr_reslist_t *reslist;
+//     lua_State *L;
+// } server_release_t;
+// 
+// static apr_status_t release_server_vm(void *l) {
+//     server_release_t *srt = l;
+//     apr_reslist_release(srt->reslist, srt->L);
+//     return APR_SUCCESS;
+// }
+
+/* Initially we will just use a resource list keyed to the file name */
+// static lua_State* get_server_vm(server_rec *server, apw_vm_spec *spec) {
+//     apr_status_t rv;
+//     apw_server_cfg *cfg = ap_get_module_config(server->module_config, &wombat_module);
+//         
+//     apr_thread_rwlock_rdlock(cfg->vm_reslists_lock);
+//     apr_reslist_t *rlist = apr_hash_get(cfg->vm_reslists, spec->file, APR_HASH_KEY_STRING);
+//     apr_thread_rwlock_unlock(cfg->vm_reslists_lock);
+//     if (!rlist) {
+//         apr_thread_rwlock_wrlock(cfg->vm_reslists_lock);    
+//         /* double checked lock (works in C :-) */
+//         rlist = apr_hash_get(cfg->vm_reslists, spec->file, APR_HASH_KEY_STRING);
+//         if (!rlist) {
+//             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Creating RESLIST");
+//     
+//             server_vm_params *params = apr_palloc(server->process->pconf, sizeof(server_vm_params));
+//             
+//             params->server = server;
+//             params->spec = apr_pcalloc(server->process->pconf, sizeof(apw_vm_spec));
+//             params->spec->file = apr_pstrdup(server->process->pconf, spec->file);
+//             params->spec->code_cache_style = spec->code_cache_style;
+//             params->spec->scope = APW_SCOPE_SERVER;
+//             params->spec->package_paths = spec->package_paths;
+//             params->spec->package_cpaths = spec->package_cpaths;
+//             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Creating reslist for %s", spec->file);
+//             rv = apr_reslist_create(&rlist,      /* the list */
+//                                     10, 100, 100, /* min, soft max, hard max */
+//                                     0,           /* TTL */
+//                                     server_vm_ctor,
+//                                     server_vm_dtor,
+//                                     params,
+//                                     server->process->pconf);
+// 
+//             apr_hash_set(cfg->vm_reslists, params->spec->file, APR_HASH_KEY_STRING, (void*)rlist);
+//         }
+//         apr_thread_rwlock_unlock(cfg->vm_reslists_lock);
+//     }
+//     lua_State *L;
+//     apr_reslist_acquire(rlist, (void*)&L);
+//     
+//     server_release_t *srt = apr_palloc(spec->pool, sizeof(server_release_t));
+//     srt->reslist = rlist;
+//     srt->L = L;
+//     apr_pool_cleanup_register(spec->pool, srt, release_server_vm, apr_pool_cleanup_null);
+//     
+//     /* apr_pool_cleanup_register(r->pool, L, cleanup_lua, apr_pool_cleanup_null); */
+//     
+//     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Acquired lua_State %p", L);
+//     return L;
+// }
+
+/* Initially we will just use a resource list keyed to the file name */
+// static lua_State* get_request_vm(request_rec *r, apw_vm_spec *spec) {
+//     /* apr_status_t rv; */
+//     apw_request_cfg *cfg = ap_get_module_config(r->request_config, &wombat_module);
+//     apw_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, &wombat_module);    
+//     
+//     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "got module config! %p", cfg);
+//     lua_State *L = apr_hash_get(cfg->request_scoped_vms, spec->file, APR_HASH_KEY_STRING);
+//     if (!L) {
+//         L = create_vm(spec, server_cfg->code_cache, r->pool);
+//         apr_hash_set(cfg->request_scoped_vms, spec->file, APR_HASH_KEY_STRING, L);
+//     }
+//     return L;
+// }
+
+// lua_State* apw_rgetvm(request_rec *r, apw_vm_spec *spec) {
+//     apr_status_t rv;
+//     const apw_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &wombat_module);
+//     apw_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, &wombat_module);
+//     char *fixed_filename;
+//     rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
+//     if (rv != APR_SUCCESS) {
+//         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "Unable to build full path to file, %s", spec->file);
+//         return NULL;
+//     }
+//     spec->file = fixed_filename;
+//     lua_State* L;
+//     switch (spec->scope) {            
+//         case APW_SCOPE_REQUEST: 
+//             spec->package_paths = cfg->package_paths;
+//             spec->package_cpaths = cfg->package_cpaths;
+//             spec->pool = r->pool;
+//             L = get_request_vm(r, spec);           
+//             return L;
+//             return NULL;
+// 
+//         case APW_SCOPE_CONN:
+//             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Conn Scoped Lua VMs not implemented yet");
+//             return NULL;
+// 
+//         case APW_SCOPE_SERVER:
+//             spec->package_paths = cfg->package_paths;
+//             spec->package_cpaths = cfg->package_cpaths;
+//             spec->pool = r->pool;
+//             L = get_server_vm(r->server, spec);           
+//             return L;
+// 
+//         default:
+//             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Unknown Lua VM scope specified, using 'once'");
+//             /* fall through on purpose */
+//         case APW_SCOPE_ONCE:
+//             spec->package_paths = cfg->package_paths;
+//             spec->package_cpaths = cfg->package_cpaths;
+//             L =  create_vm(spec, server_cfg->code_cache, r->pool);
+//             if (spec->pool == NULL) {
+//                 apr_pool_cleanup_register(r->pool, L, cleanup_lua, apr_pool_cleanup_null);                
+//             }
+//             apr_pool_cleanup_register(spec->pool, L, cleanup_lua, apr_pool_cleanup_null);
+//             break;
+//     }
+//     
+//     return L;
+// }
+
+/* returns NULL if the spec requires a request scope */
+// lua_State* apw_cgetvm(conn_rec *conn, apw_vm_spec *spec) {
+//     
+//     return NULL;
+// }
+
+/**
+ * TODO Redo to make use of the create_vm
+ */
+// lua_State* apw_sgetvm(server_rec *server, apw_vm_spec *spec) {
+//     apr_status_t rv;
+//     if (spec->scope == APW_SCOPE_REQUEST || spec->scope == APW_SCOPE_CONN) {
+//         return NULL;
+//     }
+//     
+//     apw_server_cfg *server_cfg = ap_get_module_config(server->module_config, &wombat_module);
+//     char *fixed_filename;
+//     rv = apr_filepath_merge(&fixed_filename, server_cfg->root_path, spec->file, APR_FILEPATH_NOTRELATIVE, 
+//                             server->process->pconf);
+//     if (rv != APR_SUCCESS) {
+//         ap_log_error(APLOG_MARK, APLOG_ERR, rv, server, "Unable to build full path to file, %s", spec->file);
+//         return NULL;
+//     }
+//     spec->file = fixed_filename;
+//     
+//     apr_pool_t *pool = NULL;
+//     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "ALLOCATING A LUA");
+//     /* TODO change to use load_file */
+//     lua_State* L = luaL_newstate();
+//     luaL_openlibs(L);
+//     apw_load_apache2_lmodule(L);
+//     apw_load_config_lmodule(L);
+//     
+//     if (luaL_loadfile(L, spec->file)) {
+//         ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, 
+//                      "Unable to compile Lua file '%s' because of '%s'",
+//                      spec->file, luaL_checkstring(L, -1));
+//         return NULL;
+//     }
+//     
+//     if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
+//         ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, 
+//                      "Unable to compile Lua file '%s' because of '%s'",
+//                      spec->file, luaL_checkstring(L, -1));
+//         return NULL;
+//     }
+//     
+//     switch (spec->scope) {
+//         case APW_SCOPE_ONCE:
+//             if (spec->pool == NULL) {
+//                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, 
+//                              "You must provide a pool for APW_SCOPE_ONCE");                
+//                 lua_close(L);
+//                 return NULL;
+//             }
+//             pool = spec->pool;
+//             break;
+//             
+//         case APW_SCOPE_REQUEST: 
+//             break;
+// 
+//         case APW_SCOPE_CONN: 
+//             break;
+// 
+//         case APW_SCOPE_SERVER: 
+//             lua_close(L);
+//             ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, 
+//                          "Server Scoped Lua VMs not implemented yet");
+//             return NULL;
+// 
+//         default:
+//             pool = spec->pool;
+//             ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, 
+//                          "Unknown Lua VM scope specified, using 'once'");
+//     }
+//     
+//     apr_pool_cleanup_register(pool, L, cleanup_lua, apr_pool_cleanup_null);
+//     return L;
+// }
+
+/* represents a cache entry */
+// typedef struct {
+//     apr_array_header_t *parts; /* <part_t> */
+//     apr_time_t mtime;
+//     apr_pool_t *pool;
+// } code_cache_entry;
+// 
+// typedef struct {
+//     apr_pool_t *pool;
+//     apr_array_header_t *parts; /* <part_t> */
+// } dumper_t;
+// 
+// typedef struct {
+//     apr_array_header_t* parts;
+//     int idx;
+//     request_rec* r;
+// } loader_t;
+// 
+// typedef struct {
+//     const void* chunk;
+//     size_t sz;
+// } part_t;
+// 
+// static int wlua_dumper(lua_State *L, const void* p, size_t sz, void* ud) {
+//     dumper_t* d = (dumper_t*)ud;    
+//     part_t* part = apr_palloc(d->pool, sizeof(part_t));
+//     void* mine = apr_palloc(d->pool, sz);
+//     memcpy(mine, p, sz);
+//     part->chunk = mine;
+//     part->sz = sz;
+//     *(const part_t**)apr_array_push(d->parts) = part;
+//     return 0;
+// }
+// 
+// static const char* wlua_loader(lua_State* L, void* data, size_t* size) {
+//     loader_t* l = (loader_t*) data;
+//     /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, l->r, "part count %d", l->parts->nelts); */
+//     if (l->idx == l->parts->nelts) {
+//         return NULL;
+//     }
+//     part_t* part = ((part_t**)l->parts->elts)[l->idx++];
+//     *size = part->sz;
+//     /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, l->r, "got part of size %lu", *size); */
+//     return part->chunk;
+// }
+
+// static int load_file(apr_pool_t *working_pool, lua_State* L, const apw_code_cache* cfg, apw_vm_spec *spec) {
+//     int rs;
+// 
+//     if (spec->bytecode_len != 0) {
+//         rs = luaL_loadbuffer(L, spec->bytecode, spec->bytecode_len, spec->file);
+//         if (rs) {
+//             ap_log_perror(APLOG_MARK, APLOG_DEBUG, rs, working_pool, "Unable to load %s from buffer", spec->file);
+//             return rs;
+//         }
+//     }
+//     else if (spec->code_cache_style != APW_CODE_CACHE_NEVER) {
+//         /* start code caching magic */
+//         apr_thread_rwlock_rdlock(cfg->compiled_files_lock);
+//         code_cache_entry *cache = apr_hash_get(cfg->compiled_files, spec->file, APR_HASH_KEY_STRING);
+//         apr_thread_rwlock_unlock(cfg->compiled_files_lock);
+// 
+//         int stale = 0;
+//         apr_finfo_t *finfo = NULL;
+//         if (cache == NULL || spec->code_cache_style == APW_CODE_CACHE_STAT) {
+//             ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, working_pool, "stating %s", spec->file);
+// 
+//             finfo = apr_palloc(working_pool, sizeof(apr_finfo_t));
+//             apr_stat(finfo, spec->file, APR_FINFO_MTIME, working_pool);
+// 
+//             /* has the file been modified or is this the first time we load the file? */
+//             if (cache == NULL || finfo->mtime > cache->mtime) {
+//                 /* we're expired */
+//                 ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, working_pool, "file is stale: %s ", spec->file);
+//                 stale = 1;
+//             }
+//         }    
+// 
+//         if (!stale) {
+//             ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, working_pool, "loading from cache: %s", spec->file);
+// 
+//             loader_t* l = apr_palloc(working_pool, sizeof(loader_t));
+//             apr_thread_rwlock_rdlock(cfg->compiled_files_lock);
+//             l->parts = cache->parts;
+//             l->idx = 0;
+//             if ((rs = lua_load(L, wlua_loader, l, spec->file))) {
+//                 apr_thread_rwlock_unlock(cfg->compiled_files_lock);                
+//                 switch (rs) {
+//                     case LUA_ERRSYNTAX: {
+//                         ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, working_pool, 
+//                             "syntax error on compiled [%s] from cache", spec->file);
+//                         return rs;
+//                     }
+//                     case LUA_ERRMEM: {
+//                         ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, working_pool, 
+//                             "memory error on compiled [%s] from cache", spec->file);
+//                         return rs;
+//                     }
+//                     default: {
+//                         ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, working_pool, 
+//                             "other error, %d, on compiled [%s] from cache", rs, spec->file);
+//                         return rs;
+//                     }
+//                 }
+//                 return rs;
+//             }
+//             else {
+//                 apr_thread_rwlock_unlock(cfg->compiled_files_lock);
+//             }
+//         }
+//         else {
+//             ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, working_pool, "loading & caching: %s", spec->file);
+//         
+//             if ((rs = luaL_loadfile(L, spec->file))) {
+//                 return rs;
+//             }
+//             
+//             int is_new = 0;
+//             if (!cache) { 
+//                 /* allocate a new code_cache_entry from the cfg pool. Since entries are reused
+//                  * when files are re-loaded and we don't evict entries from the cache, 
+//                  * we don't need to care about de-allocation. 
+//                  */
+//                 cache = apr_palloc(cfg->pool, sizeof(code_cache_entry));
+//                 is_new = 1;
+//             }
+//             
+//             
+//             apr_pool_t *mp;
+//             apr_pool_create(&mp, cfg->pool);  /* pool from which everything in this code_cache_entry
+//                                                * will be allocated */
+// 
+//             dumper_t* d = apr_palloc(working_pool, sizeof(dumper_t));
+//             d->pool = mp;
+//             d->parts = apr_array_make(mp, 250, sizeof(part_t*));
+//             lua_dump(L, wlua_dumper, d);
+// 
+//             apr_thread_rwlock_wrlock(cfg->compiled_files_lock);
+//             
+//             if (is_new) {
+//                 /* we copy the filename into a string allocated from the cfg pool. apr_hash keeps
+//                  * pointers to keys and values, and we need the key to survive beyond the request lifetime
+//                  */
+//                 const char* key = apr_pstrdup(cfg->pool, spec->file);
+//                 apr_hash_set(cfg->compiled_files, key, APR_HASH_KEY_STRING, cache);
+//             }
+//             else {
+//                 apr_pool_clear(cache->pool);
+//             }
+// 
+//             cache->parts = d->parts;
+//             cache->pool = d->pool; 
+//             cache->mtime = finfo->mtime;
+//             
+//             apr_thread_rwlock_unlock(cfg->compiled_files_lock);
+// 
+//             /* end code caching magic             */
+//         }
+//     }
+//     else { /* CODE_CACHE_NEVER */
+//         ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, working_pool, "loading: %s", spec->file);
+//         
+//         if ((rs = luaL_loadfile(L, spec->file))) {
+//             return rs;
+//         }
+//     }
+// 
+//     return 0;
+// }
+
+/* BEGIN NEW STYLE lua_State MANAGEMENT */
+
+lua_State* apw_get_lua_state(apr_pool_t* lifecycle_pool, 
+                            char* file, 
+                            apr_array_header_t* package_paths, 
+                            apr_array_header_t* package_cpaths,
+                            apw_lua_state_open_callback cb,
+                            void* btn) {
+    
+    lua_State* L;
+    ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, lifecycle_pool, "obtaining lua_State");
+    if (!apr_pool_userdata_get((void**)&L, file, lifecycle_pool)) {
+        ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, lifecycle_pool, "creating lua_State with file %s", file);
+        /* not available, so create */
+        L =  luaL_newstate();
+        luaL_openlibs(L);        
+        if (package_paths) 
+            munge_path(L, "path", "?.lua", "./?.lua", lifecycle_pool, package_paths, file);
+        if (package_cpaths) 
+            munge_path(L, "cpath", "?.so", "./?.so", lifecycle_pool, package_cpaths, file);
+        
+        if (cb) {
+            cb(L, lifecycle_pool, btn);
+        }
+        
+        luaL_loadfile(L, file);
+        lua_pcall(L, 0, LUA_MULTRET, 0);
+        apr_pool_userdata_set(L, file, &cleanup_lua, lifecycle_pool);
+        
+        lua_pushlightuserdata(L, lifecycle_pool);
+        lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool");  
+    }
+    return L;
+}
+
+
+
+
+
+
diff --git a/modules/wombat/vmprep.h b/modules/wombat/vmprep.h
new file mode 100644 (file)
index 0000000..97f0937
--- /dev/null
@@ -0,0 +1,135 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+#include "httpd.h"
+
+#include "apr_thread_rwlock.h"
+#include "apr_strings.h"
+#include "apr_tables.h"
+#include "apr_hash.h"
+#include "apr_buckets.h"
+#include "apr_file_info.h"
+#include "apr_time.h"
+#include "apr_pools.h"
+
+
+#ifndef VMPREP_H
+#define VMPREP_H
+
+#define APW_CODE_CACHE_STAT     1
+#define APW_CODE_CACHE_FOREVER  2
+#define APW_CODE_CACHE_NEVER    3
+
+#define APW_SCOPE_ONCE          1
+#define APW_SCOPE_REQUEST       2
+#define APW_SCOPE_CONN          3
+#define APW_SCOPE_SERVER        4
+
+/**
+ * Specification for a lua virtual machine
+ */ 
+typedef struct {
+
+    /* NEED TO ADD ADDITIONAL PACKAGE PATHS AS PART OF SPEC INSTEAD OF DIR CONFIG */
+    apr_array_header_t* package_paths;
+    apr_array_header_t* package_cpaths;
+    
+    /* name of base file to load in the vm */
+    char *file;             
+
+    /* APW_CODE_CACHE_STAT | APW_CODE_CACHE_FOREVER | APW_CODE_CACHE_NEVER */
+    int code_cache_style;
+
+    /* APW_SCOPE_ONCE | APW_SCOPE_REQUEST | APW_SCOPE_CONN | APW_SCOPE_SERVER */
+    int scope;
+    
+    /* pool to use for lifecycle if APW_SCOPE_ONCE is set, otherwise unused */
+    apr_pool_t *pool;
+
+    const char *bytecode;
+    apr_size_t bytecode_len;
+} apw_vm_spec;
+
+typedef struct {
+    int code_cache_style;
+    char *function_name;
+    char *file_name;
+    int scope;
+    ap_regex_t *uri_pattern;
+    const char *bytecode;
+    apr_size_t bytecode_len;
+} apw_mapped_handler_spec;
+
+typedef struct {
+    apr_pool_t *pool;
+    apr_hash_t *compiled_files;
+    apr_thread_rwlock_t* compiled_files_lock;
+} apw_code_cache;
+
+/* remove and make static once out of mod_wombat.c */
+void apw_openlibs(lua_State* L);
+
+/* remove and make static once out of mod_wombat.c */
+void apw_registerlib(lua_State* L, char* name, lua_CFunction f);
+
+/**
+ * Fake out addition of the "apache2" module
+ */
+void apw_load_apache2_lmodule(lua_State *L);
+
+/**
+ * the apw_?getvm family of functions is used to create and/or obtain
+ * a handle to a lua state. If there is not an extant vm matching the
+ * spec then a new one is created.
+ */
+// lua_State* apw_rgetvm(request_rec *r, apw_vm_spec *spec);
+
+/* returns NULL if the spec requires a request scope */
+// lua_State* apw_cgetvm(conn_rec *r, apw_vm_spec *spec);
+
+/* returns NULL if the spec requires a request scope or conn scope */
+// lua_State* apw_sgetvm(server_rec *r, apw_vm_spec *spec);
+
+typedef void (*apw_lua_state_open_callback) (lua_State* L, apr_pool_t* p, void* ctx);
+
+/*
+ * alternate means of getting lua_State (preferred eventually)
+ * Obtain a lua_State which has loaded file and is associated with lifecycle_pool
+ * If one exists, will return extant one, otherwise will create, attach, and return
+ * This does no locking around the lua_State, so if the pool is shared between
+ * threads, locking is up the client.
+ * 
+ * @lifecycle_pool -> pool whose lifeycle controls the lua_State
+ * @file file to be opened, also used as a key for uniquing lua_States
+ * @cb callback for vm initialization called *before* the file is opened
+ * @ctx a baton passed to cb
+ */
+lua_State* apw_get_lua_state(apr_pool_t* lifecycle_pool, 
+                             char* file, 
+                             apr_array_header_t* package_paths, 
+                             apr_array_header_t* package_cpaths,
+                             apw_lua_state_open_callback cb,
+                             void* btn);
+                             
+                             
+
+#endif
+