diff options
Diffstat (limited to '')
39 files changed, 10065 insertions, 0 deletions
diff --git a/modules/lua/Makefile.in b/modules/lua/Makefile.in new file mode 100644 index 0000000..7c5c149 --- /dev/null +++ b/modules/lua/Makefile.in @@ -0,0 +1,3 @@ +# a modules Makefile has no explicit targets -- they will be defined by +# whatever modules are enabled. just grab special.mk to deal with this. +include $(top_srcdir)/build/special.mk diff --git a/modules/lua/NWGNUmakefile b/modules/lua/NWGNUmakefile new file mode 100644 index 0000000..24f2703 --- /dev/null +++ b/modules/lua/NWGNUmakefile @@ -0,0 +1,287 @@ +# +# Declare the sub-directories to be built here +# + +SUBDIRS = \ + $(EOLIST) + +# +# Get the 'head' of the build environment. This includes default targets and +# paths to tools +# + +include $(AP_WORK)/build/NWGNUhead.inc + +# +# build this level's files + +# +# Make sure all needed macro's are defined +# + +# +# These directories will be at the beginning of the include list, followed by +# INCDIRS +# +XINCDIRS += \ + $(APR)/include \ + $(APRUTIL)/include \ + $(SRC)/include \ + $(STDMOD)/database \ + $(STDMOD)/http \ + $(STDMOD)/ssl \ + $(NWOS) \ + $(LUASRC)/src \ + $(EOLIST) + +# +# These flags will come after CFLAGS +# +XCFLAGS += \ + -opt nointrinsics \ + $(EOLIST) + +# +# These defines will come after DEFINES +# +XDEFINES += \ + -DLUA_COMPAT_ALL \ + -DLUA_COMPAT_5_2 \ + -DLUA_COMPAT_5_1 \ + -DLUA_COMPAT_MODULE \ + $(EOLIST) + +# +# These flags will be added to the link.opt file +# +XLFLAGS += \ + -L$(OBJDIR) \ + $(EOLIST) + +# +# These values will be appended to the correct variables based on the value of +# RELEASE +# +ifeq "$(RELEASE)" "debug" +XINCDIRS += \ + $(EOLIST) + +XCFLAGS += \ + $(EOLIST) + +XDEFINES += \ + $(EOLIST) + +XLFLAGS += \ + $(EOLIST) +endif + +ifeq "$(RELEASE)" "noopt" +XINCDIRS += \ + $(EOLIST) + +XCFLAGS += \ + $(EOLIST) + +XDEFINES += \ + $(EOLIST) + +XLFLAGS += \ + $(EOLIST) +endif + +ifeq "$(RELEASE)" "release" +XINCDIRS += \ + $(EOLIST) + +XCFLAGS += \ + $(EOLIST) + +XDEFINES += \ + $(EOLIST) + +XLFLAGS += \ + $(EOLIST) +endif + +# +# These are used by the link target if an NLM is being generated +# This is used by the link 'name' directive to name the nlm. If left blank +# TARGET_nlm (see below) will be used. +# +NLM_NAME = mod_lua + +# +# This is used by the link '-desc ' directive. +# If left blank, NLM_NAME will be used. +# +NLM_DESCRIPTION = Apache $(VERSION_STR) LUA Module + +# +# This is used by the '-threadname' directive. If left blank, +# NLM_NAME Thread will be used. +# +NLM_THREAD_NAME = LUA Module + +# +# If this is specified, it will override VERSION value in +# $(AP_WORK)/build/NWGNUenvironment.inc +# +NLM_VERSION = + +# +# If this is specified, it will override the default of 64K +# +NLM_STACK_SIZE = 131072 + + +# +# If this is specified it will be used by the link '-entry' directive +# +NLM_ENTRY_SYM = + +# +# If this is specified it will be used by the link '-exit' directive +# +NLM_EXIT_SYM = + +# +# If this is specified it will be used by the link '-check' directive +# +NLM_CHECK_SYM = + +# +# If these are specified it will be used by the link '-flags' directive +# +NLM_FLAGS = + +# +# If this is specified it will be linked in with the XDCData option in the def +# file instead of the default of $(NWOS)/apache.xdc. XDCData can be disabled +# by setting APACHE_UNIPROC in the environment +# +XDCDATA = + +# +# If there is an NLM target, put it here +# +TARGET_nlm = \ + $(OBJDIR)/$(NLM_NAME).nlm \ + $(EOLIST) + +# +# If there is an LIB target, put it here +# +TARGET_lib = \ + $(OBJDIR)/lua.lib \ + $(EOLIST) + +# +# These are the OBJ files needed to create the NLM target above. +# Paths must all use the '/' character +# +FILES_nlm_objs = \ + $(OBJDIR)/mod_lua.o \ + $(OBJDIR)/lua_apr.o \ + $(OBJDIR)/lua_config.o \ + $(OBJDIR)/lua_passwd.o \ + $(OBJDIR)/lua_request.o \ + $(OBJDIR)/lua_vmprep.o \ + $(OBJDIR)/lua_dbd.o \ + $(OBJDIR)/libprews.o \ + $(EOLIST) + +# +# These are the LIB files needed to create the NLM target above. +# These will be added as a library command in the link.opt file. +# +FILES_nlm_libs = \ + $(PRELUDE) \ + $(OBJDIR)/lua.lib \ + $(EOLIST) + +# +# These are the modules that the above NLM target depends on to load. +# These will be added as a module command in the link.opt file. +# +FILES_nlm_modules = \ + aprlib \ + libc \ + $(EOLIST) + +# +# If the nlm has a msg file, put it's path here +# +FILE_nlm_msg = + +# +# If the nlm has a hlp file put it's path here +# +FILE_nlm_hlp = + +# +# If this is specified, it will override $(NWOS)\copyright.txt. +# +FILE_nlm_copyright = + +# +# Any additional imports go here +# +FILES_nlm_Ximports = \ + @aprlib.imp \ + @httpd.imp \ + @libc.imp \ + $(EOLIST) + +# Don't link with Winsock if standard sockets are being used +ifndef USE_STDSOCKETS +FILES_nlm_Ximports += @ws2nlm.imp \ + $(EOLIST) +endif + +# +# Any symbols exported to here +# +FILES_nlm_exports = \ + lua_module \ + $(EOLIST) + +# +# These are the OBJ files needed to create the LIB target above. +# Paths must all use the '/' character +# +UNWANTED = $(LUASRC)/src/lua.c $(LUASRC)/src/luac.c $(LUASRC)/src/print.c + +FILES_lib_objs = \ + $(patsubst $(LUASRC)/src/%.c,$(OBJDIR)/%.o, $(filter-out $(UNWANTED), $(wildcard $(LUASRC)/src/*.c))) \ + $(EOLIST) + +# +# implement targets and dependancies (leave this section alone) +# + +libs :: $(OBJDIR) $(TARGET_lib) + +nlms :: libs $(TARGET_nlm) + +# +# Updated this target to create necessary directories and copy files to the +# correct place. (See $(AP_WORK)/build/NWGNUhead.inc for examples) +# +install :: nlms FORCE + $(call COPY,$(OBJDIR)/*.nlm, $(INSTALLBASE)/modules/) + +# +# Any specialized rules here +# + +vpath %.c $(LUASRC)/src ../arch/netware + +# +# Include the 'tail' makefile that has targets that depend on variables defined +# in this makefile +# + +include $(APBUILD)/NWGNUtail.inc + + diff --git a/modules/lua/README b/modules/lua/README new file mode 100644 index 0000000..e0106e8 --- /dev/null +++ b/modules/lua/README @@ -0,0 +1,54 @@ +-*- mode:org -*- +* Requirements: +** lua 5.1, 5.2, 5.3 ( http://www.lua.org/ ) or LuaJIT 2.x ( http://www.luajit.org/ ) +** Apache HTTPD 2.4 ( http://httpd.apache.org/ ) or higher + +* Documentation + See docs/README + +* Building + For now, see docs/building-from-subversion.txt + + +* Task List +** TODO Use r->file to determine file, doing rewriting in translate_name +** 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 Mapping in the server_rec +** TODO Figure out how reentrancy works regarding filter chain stuff. + Do we need new "threads"? +** TODO: Flatten LuaHook* to LuaHook phase file fn ? +** TODO: document or remove block sections +** TODO: test per-dir behavior of block sections +** TODO: Suppress internal details (fs path to scripts, etc) in error responses +** TODO: Check whether we can tighten the mode flag in lua_load(), + luaL_loadfile() an dluaL_loadbuffer() from NULL (="bt") + to e.g. "t". + +* 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_lua 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 +** Stefan Fritsch +** Eric Covener +** Daniel Gruno +** Günter Knauf +** Jim Jagielski diff --git a/modules/lua/config.m4 b/modules/lua/config.m4 new file mode 100644 index 0000000..40ae6f0 --- /dev/null +++ b/modules/lua/config.m4 @@ -0,0 +1,130 @@ + +APACHE_MODPATH_INIT(lua) + +dnl CHECK_LUA_PATH(PREFIX, INCLUDE-PATH, LIB-PATH, LIB-NAME) +dnl +dnl Checks for a specific version of the Lua libraries. Use CHECK_LUA instead, +dnl which will call this macro. +dnl +dnl Sets LUA_CFLAGS and LUA_LIBS, and breaks from its containing loop, if the +dnl check succeeds. +AC_DEFUN([CHECK_LUA_PATH], [dnl + AC_MSG_CHECKING([for lua.h in $1/$2]) + if test -f $1/$2/lua.h; then + AC_MSG_RESULT([yes]) + save_CFLAGS=$CFLAGS + save_LDFLAGS=$LDFLAGS + CFLAGS="$CFLAGS" + LDFLAGS="-L$1/$3 $LDFLAGS $lib_m" + AC_CHECK_LIB($4, luaL_newstate, [ + LUA_LIBS="-L$1/$3 -l$4 $lib_m" + if test "x$ap_platform_runtime_link_flag" != "x"; then + APR_ADDTO(LUA_LIBS, [$ap_platform_runtime_link_flag$1/$3]) + fi + LUA_CFLAGS="-I$1/$2" + ]) + CFLAGS=$save_CFLAGS + LDFLAGS=$save_LDFLAGS + + if test -n "${LUA_LIBS}"; then + break + fi + else + AC_MSG_RESULT([no]) + fi +]) + +dnl Check for Lua Libraries +dnl CHECK_LUA(ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]) +dnl Sets: +dnl LUA_CFLAGS +dnl LUA_LIBS +AC_DEFUN([CHECK_LUA], +[dnl + +AC_ARG_WITH( + lua, + [AC_HELP_STRING([--with-lua=PATH],[Path to the Lua installation prefix])], + lua_path="$withval", + :) + +dnl # Determine lua lib directory +if test -z "$lua_path"; then + test_paths=". /usr/local /usr" +else + test_paths="${lua_path}" +fi + +for pklua in lua lua5.4 lua5.3 lua5.2 lua5.1; do + if test -n "$PKGCONFIG" -a -z "$lua_path" \ + && $PKGCONFIG --atleast-version=5.1 $pklua; then + LUA_LIBS="`$PKGCONFIG --libs $pklua`" + LUA_CFLAGS="`$PKGCONFIG --cflags $pklua`" + LUA_VERSION="`$PKGCONFIG --modversion $pklua`" + AC_MSG_NOTICE([using Lua $LUA_VERSION configuration from pkg-config]) + break + fi +done + +if test -z "$LUA_VERSION"; then + AC_CHECK_LIB(m, pow, lib_m="-lm") + AC_CHECK_LIB(m, sqrt, lib_m="-lm") + for x in $test_paths ; do + CHECK_LUA_PATH([${x}], [include/lua-5.4], [lib/lua-5.4], [lua-5.4]) + CHECK_LUA_PATH([${x}], [include/lua5.4], [lib], [lua5.4]) + CHECK_LUA_PATH([${x}], [include/lua54], [lib/lua54], [lua]) + + CHECK_LUA_PATH([${x}], [include/lua-5.3], [lib/lua-5.3], [lua-5.3]) + CHECK_LUA_PATH([${x}], [include/lua5.3], [lib], [lua5.3]) + CHECK_LUA_PATH([${x}], [include/lua53], [lib/lua53], [lua]) + + CHECK_LUA_PATH([${x}], [include], [lib], [lua]) + + CHECK_LUA_PATH([${x}], [include/lua-5.2], [lib/lua-5.2], [lua-5.2]) + CHECK_LUA_PATH([${x}], [include/lua5.2], [lib], [lua5.2]) + CHECK_LUA_PATH([${x}], [include/lua52], [lib/lua52], [lua]) + + CHECK_LUA_PATH([${x}], [include/lua-5.1], [lib/lua-5.1], [lua-5.1]) + CHECK_LUA_PATH([${x}], [include/lua5.1], [lib], [lua5.1]) + CHECK_LUA_PATH([${x}], [include/lua51], [lib/lua51], [lua]) + done +fi + +AC_SUBST(LUA_LIBS) +AC_SUBST(LUA_CFLAGS) + +if test -z "${LUA_LIBS}"; then + AC_MSG_WARN([*** Lua 5.4 5.3 5.2 or 5.1 library not found.]) + ifelse([$2], , + enable_lua="no" + if test -z "${lua_path}"; then + AC_MSG_WARN([Lua 5.4 5.3 5.2 or 5.1 library is required]) + else + AC_MSG_ERROR([Lua 5.4 5.3 5.2 or 5.1 library is required]) + fi, + $2) +else + AC_MSG_NOTICE([using '${LUA_LIBS}' for Lua Library]) + AC_ARG_ENABLE(luajit,APACHE_HELP_STRING(--enable-luajit,Enable LuaJit Support), + [ + if test "$enableval" = "yes"; then + APR_ADDTO(MOD_CPPFLAGS, ["-DAP_ENABLE_LUAJIT"]) + fi + ]) + ifelse([$1], , , $1) +fi +]) + +lua_objects="lua_apr.lo lua_config.lo mod_lua.lo lua_request.lo lua_vmprep.lo lua_dbd.lo lua_passwd.lo" + +APACHE_MODULE(lua, Apache Lua Framework, $lua_objects, , , [ + CHECK_LUA() + if test "x$enable_lua" != "xno" ; then + APR_ADDTO(MOD_INCLUDES, [$LUA_CFLAGS]) + APR_ADDTO(MOD_LUA_LDADD, [$LUA_LIBS $CRYPT_LIBS]) + fi +]) + +APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current]) + +APACHE_MODPATH_FINISH diff --git a/modules/lua/docs/README b/modules/lua/docs/README new file mode 100644 index 0000000..632945a --- /dev/null +++ b/modules/lua/docs/README @@ -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/lua/docs/basic-configuration.txt b/modules/lua/docs/basic-configuration.txt new file mode 100644 index 0000000..b837698 --- /dev/null +++ b/modules/lua/docs/basic-configuration.txt @@ -0,0 +1,141 @@ +See sample_httpd.conf for examples + +The basic module loading directive is + LoadModule lua_module modules/mod_lua.so + +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_lua to evaluate + +mod_lua exports several additional directives: + + LuaRoot /path/to/a/directory + Specify the base path which will be used to evaluate all + relative paths within mod_lua. 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 development. + + 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/lua/docs/building-from-subversion.txt b/modules/lua/docs/building-from-subversion.txt new file mode 100644 index 0000000..98da7bf --- /dev/null +++ b/modules/lua/docs/building-from-subversion.txt @@ -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/lua/docs/running-developer-tests.txt b/modules/lua/docs/running-developer-tests.txt new file mode 100644 index 0000000..59eb990 --- /dev/null +++ b/modules/lua/docs/running-developer-tests.txt @@ -0,0 +1,16 @@ +-*- mode:org -*- +* Building mod_lua + The first step is to build mod_lua 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/lua/docs/writing-handlers.txt b/modules/lua/docs/writing-handlers.txt new file mode 100644 index 0000000..2f18c41 --- /dev/null +++ b/modules/lua/docs/writing-handlers.txt @@ -0,0 +1,49 @@ +mod_lua 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/lua/lua_apr.c b/modules/lua/lua_apr.c new file mode 100644 index 0000000..9590fd6 --- /dev/null +++ b/modules/lua/lua_apr.c @@ -0,0 +1,110 @@ +/** + * 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_lua.h" +#include "lua_apr.h" +APLOG_USE_MODULE(lua); + +req_table_t *ap_lua_check_apr_table(lua_State *L, int index) +{ + req_table_t* t; + luaL_checkudata(L, index, "Apr.Table"); + t = lua_unboxpointer(L, index); + return t; +} + + +void ap_lua_push_apr_table(lua_State *L, req_table_t *t) +{ + lua_boxpointer(L, t); + luaL_getmetatable(L, "Apr.Table"); + lua_setmetatable(L, -2); +} + +static int lua_table_set(lua_State *L) +{ + req_table_t *t = ap_lua_check_apr_table(L, 1); + const char *key = luaL_checkstring(L, 2); + const char *val = luaL_optlstring(L, 3, NULL, NULL); + + if (!val) { + apr_table_unset(t->t, key); + return 0; + } + + /* Unless it's the 'notes' table, check for newline chars */ + /* t->r will be NULL in case of the connection notes, but since + we aren't going to check anything called 'notes', we can safely + disregard checking whether t->r is defined. + */ + if (strcmp(t->n, "notes") && ap_strchr_c(val, '\n')) { + char *badchar; + char *replacement = apr_pstrdup(t->r->pool, val); + badchar = replacement; + while ( (badchar = ap_strchr(badchar, '\n')) ) { + *badchar = ' '; + } + ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, t->r, APLOGNO(02614) + "mod_lua: Value for '%s' in table '%s' contains newline!", + key, t->n); + apr_table_set(t->t, key, replacement); + } + else { + apr_table_set(t->t, key, val); + } + return 0; +} + +static int lua_table_get(lua_State *L) +{ + req_table_t *t = ap_lua_check_apr_table(L, 1); + const char *key = luaL_checkstring(L, 2); + const char *val = apr_table_get(t->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 ap_lua_init(lua_State *L, apr_pool_t *p) +{ + luaL_newmetatable(L, "Apr.Table"); +#if LUA_VERSION_NUM < 502 + luaL_register(L, "apr_table", lua_table_methods); +#else + luaL_newlib(L, lua_table_methods); +#endif + 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/lua/lua_apr.h b/modules/lua/lua_apr.h new file mode 100644 index 0000000..c194f11 --- /dev/null +++ b/modules/lua/lua_apr.h @@ -0,0 +1,36 @@ +/** + * 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 _LUA_APR_H_ +#define _LUA_APR_H_ + +#include "scoreboard.h" +#include "http_main.h" +#include "ap_mpm.h" +#include "apr_md5.h" +#include "apr_sha1.h" +#include "apr_poll.h" +#include "apr.h" +#include "apr_tables.h" +#include "apr_base64.h" + + +int ap_lua_init(lua_State *L, apr_pool_t * p); +req_table_t *ap_lua_check_apr_table(lua_State *L, int index); +void ap_lua_push_apr_table(lua_State *L, req_table_t *t); + +#endif /* !_LUA_APR_H_ */ diff --git a/modules/lua/lua_config.c b/modules/lua/lua_config.c new file mode 100644 index 0000000..14674a9 --- /dev/null +++ b/modules/lua/lua_config.c @@ -0,0 +1,277 @@ +/** + * 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_config.h" +#include "lua_vmprep.h" + +APLOG_USE_MODULE(lua); + +static ap_lua_dir_cfg *check_dir_config(lua_State *L, int index) +{ + ap_lua_dir_cfg *cfg; + luaL_checkudata(L, index, "Apache2.DirConfig"); + cfg = (ap_lua_dir_cfg *) lua_unboxpointer(L, index); + return cfg; +} + +static cmd_parms *check_cmd_parms(lua_State *L, int index) +{ + cmd_parms *cmd; + luaL_checkudata(L, index, "Apache2.CommandParameters"); + cmd = (cmd_parms *) lua_unboxpointer(L, index); + return cmd; +} + +static int apl_toscope(const char *name) +{ + if (0 == strcmp("once", name)) + return AP_LUA_SCOPE_ONCE; + if (0 == strcmp("request", name)) + return AP_LUA_SCOPE_REQUEST; + if (0 == strcmp("connection", name)) + return AP_LUA_SCOPE_CONN; + if (0 == strcmp("conn", name)) + return AP_LUA_SCOPE_CONN; + if (0 == strcmp("thread", name)) + return AP_LUA_SCOPE_THREAD; + return AP_LUA_SCOPE_ONCE; +} + +apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg, + const char *file, + const char *function, + const char *pattern, + const char *scope) +{ + ap_regex_t *uri_pattern; + apr_status_t rv; + ap_lua_mapped_handler_spec *handler = + apr_pcalloc(cfg->pool, sizeof(ap_lua_mapped_handler_spec)); + handler->uri_pattern = NULL; + handler->function_name = NULL; + + 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 = apl_toscope(scope); + + handler->function_name = apr_pstrdup(cfg->pool, function); + *(const ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = + handler; + return APR_SUCCESS; +} + +/* Change to use ap_lua_map_handler */ +static int cfg_lua_map_handler(lua_State *L) +{ + ap_lua_dir_cfg *cfg = check_dir_config(L, 1); + ap_lua_mapped_handler_spec *handler = + apr_pcalloc(cfg->pool, sizeof(ap_lua_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 = apl_toscope(scope); + } + else { + handler->scope = AP_LUA_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 ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = + handler; + return 0; +} + +static int cfg_directory(lua_State *L) +{ + ap_lua_dir_cfg *cfg = check_dir_config(L, 1); + lua_pushstring(L, cfg->dir); + return 1; +} + +/*static int cfg_root(lua_State *L) +{ + ap_lua_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} +}; + +/* helper function for the logging functions below */ +static int cmd_log_at(lua_State *L, int level) +{ + const char *msg; + cmd_parms *cmd = check_cmd_parms(L, 1); + lua_Debug dbg; + + lua_getstack(L, 1, &dbg); + lua_getinfo(L, "Sl", &dbg); + + msg = luaL_checkstring(L, 2); + /* Intentional no APLOGNO */ + ap_log_error(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0, + cmd->server, "%s", msg); + return 0; +} + +/* r:debug(String) and friends which use apache logging */ +static int cmd_emerg(lua_State *L) +{ + return cmd_log_at(L, APLOG_EMERG); +} +static int cmd_alert(lua_State *L) +{ + return cmd_log_at(L, APLOG_ALERT); +} +static int cmd_crit(lua_State *L) +{ + return cmd_log_at(L, APLOG_CRIT); +} +static int cmd_err(lua_State *L) +{ + return cmd_log_at(L, APLOG_ERR); +} +static int cmd_warn(lua_State *L) +{ + return cmd_log_at(L, APLOG_WARNING); +} +static int cmd_notice(lua_State *L) +{ + return cmd_log_at(L, APLOG_NOTICE); +} +static int cmd_info(lua_State *L) +{ + return cmd_log_at(L, APLOG_INFO); +} +static int cmd_debug(lua_State *L) +{ + return cmd_log_at(L, APLOG_DEBUG); +} +static int cmd_trace1(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE1); +} +static int cmd_trace2(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE2); +} +static int cmd_trace3(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE3); +} +static int cmd_trace4(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE4); +} +static int cmd_trace5(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE5); +} +static int cmd_trace6(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE6); +} +static int cmd_trace7(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE7); +} +static int cmd_trace8(lua_State *L) +{ + return cmd_log_at(L, APLOG_TRACE8); +} + +static const struct luaL_Reg cmd_methods[] = { + {"trace8", cmd_trace8}, + {"trace7", cmd_trace7}, + {"trace6", cmd_trace6}, + {"trace5", cmd_trace5}, + {"trace4", cmd_trace4}, + {"trace3", cmd_trace3}, + {"trace2", cmd_trace2}, + {"trace1", cmd_trace1}, + {"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 ap_lua_load_config_lmodule(lua_State *L) +{ + luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */ + lua_pushvalue(L, -1); + + lua_setfield(L, -2, "__index"); + luaL_setfuncs_compat(L, cfg_methods); /* [metatable] */ + + + luaL_newmetatable(L, "Apache2.CommandParameters"); + lua_pushvalue(L, -1); + + lua_setfield(L, -2, "__index"); + luaL_setfuncs_compat(L, cmd_methods); /* [metatable] */ + +} diff --git a/modules/lua/lua_config.h b/modules/lua/lua_config.h new file mode 100644 index 0000000..8a778ad --- /dev/null +++ b/modules/lua/lua_config.h @@ -0,0 +1,31 @@ +/** + * 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_lua.h" + +#ifndef _APL_CONFIG_H_ +#define _APL_CONFIG_H_ + +void ap_lua_load_config_lmodule(lua_State *L); + +apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg, + const char *file, + const char *function, + const char *pattern, + const char *scope); + +#endif /* !_APL_CONFIG_H_ */ diff --git a/modules/lua/lua_dbd.c b/modules/lua/lua_dbd.c new file mode 100644 index 0000000..1b8d5b7 --- /dev/null +++ b/modules/lua/lua_dbd.c @@ -0,0 +1,843 @@ +/** + * 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_lua.h" +#include "lua_dbd.h" + +APLOG_USE_MODULE(lua); +static APR_OPTIONAL_FN_TYPE(ap_dbd_close) *lua_ap_dbd_close = NULL; +static APR_OPTIONAL_FN_TYPE(ap_dbd_open) *lua_ap_dbd_open = NULL; + + + + +static request_rec *ap_lua_check_request_rec(lua_State *L, int index) +{ + request_rec *r; + luaL_checkudata(L, index, "Apache2.Request"); + r = lua_unboxpointer(L, index); + return r; +} + +static lua_db_handle *lua_get_db_handle(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + lua_rawgeti(L, 1, 0); + luaL_checktype(L, -1, LUA_TUSERDATA); + return (lua_db_handle *) lua_topointer(L, -1); +} + +static lua_db_result_set *lua_get_result_set(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + lua_rawgeti(L, 1, 0); + luaL_checktype(L, -1, LUA_TUSERDATA); + return (lua_db_result_set *) lua_topointer(L, -1); +} + + +/* + ============================================================================= + db:close(): Closes an open database connection. + ============================================================================= + */ +int lua_db_close(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db; + apr_status_t rc = 0; + /*~~~~~~~~~~~~~~~~~~~~*/ + + db = lua_get_db_handle(L); + if (db && db->alive) { + if (db->type == LUA_DBTYPE_APR_DBD) { + rc = apr_dbd_close(db->driver, db->handle); + if (db->pool) apr_pool_destroy(db->pool); + } + else { + lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close); + if (lua_ap_dbd_close != NULL) + if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle); + } + + db->driver = NULL; + db->handle = NULL; + db->alive = 0; + db->pool = NULL; + } + + lua_settop(L, 0); + lua_pushnumber(L, rc); + return 1; +} + +/* + ============================================================================= + db:__gc(): Garbage collecting function. + ============================================================================= + */ +int lua_db_gc(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~*/ + lua_db_handle *db; + /*~~~~~~~~~~~~~~~~~~~~*/ + + db = lua_touserdata(L, 1); + if (db && db->alive) { + if (db->type == LUA_DBTYPE_APR_DBD) { + apr_dbd_close(db->driver, db->handle); + if (db->pool) apr_pool_destroy(db->pool); + } + else { + lua_ap_dbd_close = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_close); + if (lua_ap_dbd_close != NULL) + if (db->dbdhandle) lua_ap_dbd_close(db->server, db->dbdhandle); + } + db->driver = NULL; + db->handle = NULL; + db->alive = 0; + db->pool = NULL; + } + lua_settop(L, 0); + return 0; +} + +/* + ============================================================================= + db:active(): Returns true if the connection to the db is still active. + ============================================================================= + */ +int lua_db_active(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + apr_status_t rc = 0; + /*~~~~~~~~~~~~~~~~~~~~*/ + + db = lua_get_db_handle(L); + if (db && db->alive) { + rc = apr_dbd_check_conn(db->driver, db->pool, db->handle); + if (rc == APR_SUCCESS) { + lua_pushboolean(L, 1); + return 1; + } + } + + lua_pushboolean(L, 0); + return 1; +} + +/* + ============================================================================= + db:query(statement): Executes the given database query and returns the + number of rows affected. If an error is encountered, returns nil as the + first parameter and the error message as the second. + ============================================================================= + */ +int lua_db_query(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + apr_status_t rc = 0; + int x = 0; + const char *statement; + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + luaL_checktype(L, 3, LUA_TSTRING); + statement = lua_tostring(L, 3); + db = lua_get_db_handle(L); + if (db && db->alive) + rc = apr_dbd_query(db->driver, db->handle, &x, statement); + else { + rc = 0; + x = -1; + } + + if (rc == APR_SUCCESS) + lua_pushnumber(L, x); + else { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *err = apr_dbd_error(db->driver, db->handle, rc); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + lua_pushnil(L); + if (err) { + lua_pushstring(L, err); + return 2; + } + } + + return 1; +} + +/* + ============================================================================= + db:escape(string): Escapes a string for safe use in the given database type. + ============================================================================= + */ +int lua_db_escape(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + const char *statement; + const char *escaped = 0; + request_rec *r; + /*~~~~~~~~~~~~~~~~~~~~~*/ + + r = ap_lua_check_request_rec(L, 2); + if (r) { + luaL_checktype(L, 3, LUA_TSTRING); + statement = lua_tostring(L, 3); + db = lua_get_db_handle(L); + if (db && db->alive) { + apr_dbd_init(r->pool); + escaped = apr_dbd_escape(db->driver, r->pool, statement, + db->handle); + if (escaped) { + lua_pushstring(L, escaped); + return 1; + } + } + else { + lua_pushnil(L); + } + return (1); + } + + return 0; +} + +/* + ============================================================================= + resultset(N): Fetches one or more rows from a result set. + ============================================================================= + */ +int lua_db_get_row(lua_State *L) +{ + int row_no,x,alpha = 0; + const char *entry, *rowname; + apr_dbd_row_t *row = 0; + lua_db_result_set *res = lua_get_result_set(L); + + row_no = luaL_optinteger(L, 2, 0); + if (lua_isboolean(L, 3)) { + alpha = lua_toboolean(L, 3); + } + lua_settop(L,0); + + /* Fetch all rows at once? */ + + if (row_no == 0) { + row_no = 1; + lua_newtable(L); + while (apr_dbd_get_row(res->driver, res->pool, res->results, + &row, -1) != -1) + { + lua_pushinteger(L, row_no); + lua_newtable(L); + for (x = 0; x < res->cols; x++) { + entry = apr_dbd_get_entry(res->driver, row, x); + if (entry) { + if (alpha == 1) { + rowname = apr_dbd_get_name(res->driver, + res->results, x); + lua_pushstring(L, rowname ? rowname : "(oob)"); + } + else { + lua_pushinteger(L, x + 1); + } + lua_pushstring(L, entry); + lua_rawset(L, -3); + } + } + lua_rawset(L, -3); + row_no++; + } + return 1; + } + + /* Just fetch a single row */ + if (apr_dbd_get_row(res->driver, res->pool, res->results, + &row, row_no) != -1) + { + + lua_newtable(L); + for (x = 0; x < res->cols; x++) { + entry = apr_dbd_get_entry(res->driver, row, x); + if (entry) { + if (alpha == 1) { + rowname = apr_dbd_get_name(res->driver, + res->results, x); + lua_pushstring(L, rowname ? rowname : "(oob)"); + } + else { + lua_pushinteger(L, x + 1); + } + lua_pushstring(L, entry); + lua_rawset(L, -3); + } + } + return 1; + } + return 0; +} + + +/* + ============================================================================= + db:select(statement): Queries the database for the given statement and + returns the rows/columns found as a table. If an error is encountered, + returns nil as the first parameter and the error message as the second. + ============================================================================= + */ +int lua_db_select(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + apr_status_t rc = 0; + const char *statement; + request_rec *r; + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + r = ap_lua_check_request_rec(L, 2); + if (r) { + luaL_checktype(L, 3, LUA_TSTRING); + statement = lua_tostring(L, 3); + db = lua_get_db_handle(L); + if (db && db->alive) { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + int cols; + apr_dbd_results_t *results = 0; + lua_db_result_set* resultset = NULL; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + rc = apr_dbd_select(db->driver, db->pool, db->handle, + &results, statement, 0); + if (rc == APR_SUCCESS) { + + cols = apr_dbd_num_cols(db->driver, results); + + if (cols > 0) { + lua_newtable(L); + resultset = lua_newuserdata(L, sizeof(lua_db_result_set)); + resultset->cols = cols; + resultset->driver = db->driver; + resultset->pool = db->pool; + resultset->rows = apr_dbd_num_tuples(db->driver, results); + resultset->results = results; + luaL_newmetatable(L, "lua_apr.dbselect"); + lua_pushliteral(L, "__call"); + lua_pushcfunction(L, lua_db_get_row); + lua_rawset(L, -3); + lua_setmetatable(L, -3); + lua_rawseti(L, -2, 0); + return 1; + } + return 0; + } + else { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *err = apr_dbd_error(db->driver, db->handle, rc); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + lua_pushnil(L); + if (err) { + lua_pushstring(L, err); + return 2; + } + } + } + + lua_pushboolean(L, 0); + return 1; + } + + return 0; +} + + + +/* + ============================================================================= + statement:select(var1, var2, var3...): Injects variables into a prepared + statement and returns the number of rows matching the query. + ============================================================================= + */ +int lua_db_prepared_select(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_prepared_statement *st = 0; + apr_status_t rc = 0; + const char **vars; + int x, have; + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + + /* Fetch the prepared statement and the vars passed */ + luaL_checktype(L, 1, LUA_TTABLE); + lua_rawgeti(L, 1, 0); + luaL_checktype(L, -1, LUA_TUSERDATA); + st = (lua_db_prepared_statement*) lua_topointer(L, -1); + + /* Check if we got enough variables passed on to us. + * This, of course, only works for prepared statements made through lua. */ + have = lua_gettop(L) - 2; + if (st->variables != -1 && have < st->variables ) { + lua_pushboolean(L, 0); + lua_pushfstring(L, + "Error in executing prepared statement: Expected %d arguments, got %d.", + st->variables, have); + return 2; + } + vars = apr_pcalloc(st->db->pool, have*sizeof(char *)); + for (x = 0; x < have; x++) { + vars[x] = lua_tostring(L, x + 2); + } + + /* Fire off the query */ + if (st->db && st->db->alive) { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + int cols; + apr_dbd_results_t *results = 0; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + rc = apr_dbd_pselect(st->db->driver, st->db->pool, st->db->handle, + &results, st->statement, 0, have, vars); + if (rc == APR_SUCCESS) { + + /*~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_result_set *resultset; + /*~~~~~~~~~~~~~~~~~~~~~*/ + + cols = apr_dbd_num_cols(st->db->driver, results); + lua_newtable(L); + resultset = lua_newuserdata(L, sizeof(lua_db_result_set)); + resultset->cols = cols; + resultset->driver = st->db->driver; + resultset->pool = st->db->pool; + resultset->rows = apr_dbd_num_tuples(st->db->driver, results); + resultset->results = results; + luaL_newmetatable(L, "lua_apr.dbselect"); + lua_pushliteral(L, "__call"); + lua_pushcfunction(L, lua_db_get_row); + lua_rawset(L, -3); + lua_setmetatable(L, -3); + lua_rawseti(L, -2, 0); + return 1; + + } + else { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *err = apr_dbd_error(st->db->driver, st->db->handle, rc); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + lua_pushnil(L); + if (err) { + lua_pushstring(L, err); + return 2; + } + return 1; + } + } + + lua_pushboolean(L, 0); + lua_pushliteral(L, + "Database connection seems to be closed, please reacquire it."); + return (2); +} + + +/* + ============================================================================= + statement:query(var1, var2, var3...): Injects variables into a prepared + statement and returns the number of rows affected. + ============================================================================= + */ +int lua_db_prepared_query(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_prepared_statement *st = 0; + apr_status_t rc = 0; + const char **vars; + int x, have; + /*~~~~~~~~~~~~~~~~~~~~~~~*/ + + /* Fetch the prepared statement and the vars passed */ + luaL_checktype(L, 1, LUA_TTABLE); + lua_rawgeti(L, 1, 0); + luaL_checktype(L, -1, LUA_TUSERDATA); + st = (lua_db_prepared_statement*) lua_topointer(L, -1); + + /* Check if we got enough variables passed on to us. + * This, of course, only works for prepared statements made through lua. */ + have = lua_gettop(L) - 2; + if (st->variables != -1 && have < st->variables ) { + lua_pushboolean(L, 0); + lua_pushfstring(L, + "Error in executing prepared statement: Expected %d arguments, got %d.", + st->variables, have); + return 2; + } + vars = apr_pcalloc(st->db->pool, have*sizeof(char *)); + for (x = 0; x < have; x++) { + vars[x] = lua_tostring(L, x + 2); + } + + /* Fire off the query */ + if (st->db && st->db->alive) { + + /*~~~~~~~~~~~~~~*/ + int affected = 0; + /*~~~~~~~~~~~~~~*/ + + rc = apr_dbd_pquery(st->db->driver, st->db->pool, st->db->handle, + &affected, st->statement, have, vars); + if (rc == APR_SUCCESS) { + lua_pushinteger(L, affected); + return 1; + } + else { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *err = apr_dbd_error(st->db->driver, st->db->handle, rc); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + lua_pushnil(L); + if (err) { + lua_pushstring(L, err); + return 2; + } + return 1; + } + } + + lua_pushboolean(L, 0); + lua_pushliteral(L, + "Database connection seems to be closed, please reacquire it."); + return (2); +} + +/* + ============================================================================= + db:prepare(statement): Prepares a statement for later query/select. + Returns a table with a :query and :select function, same as the db funcs. + ============================================================================= + */ +int lua_db_prepare(lua_State* L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + apr_status_t rc = 0; + const char *statement, *at; + request_rec *r; + lua_db_prepared_statement* st; + int need = 0; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + r = ap_lua_check_request_rec(L, 2); + if (r) { + apr_dbd_prepared_t *pstatement = NULL; + luaL_checktype(L, 3, LUA_TSTRING); + statement = lua_tostring(L, 3); + + /* Count number of variables in statement */ + at = ap_strchr_c(statement,'%'); + while (at != NULL) { + if (at[1] == '%') { + at++; + } + else { + need++; + } + at = ap_strchr_c(at+1,'%'); + } + + + db = lua_get_db_handle(L); + rc = apr_dbd_prepare(db->driver, r->pool, db->handle, statement, + NULL, &pstatement); + if (rc != APR_SUCCESS) { + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *err = apr_dbd_error(db->driver, db->handle, rc); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + lua_pushnil(L); + if (err) { + lua_pushstring(L, err); + return 2; + } + return 1; + } + + /* Push the prepared statement table */ + lua_newtable(L); + st = lua_newuserdata(L, sizeof(lua_db_prepared_statement)); + st->statement = pstatement; + st->variables = need; + st->db = db; + + lua_pushliteral(L, "select"); + lua_pushcfunction(L, lua_db_prepared_select); + lua_rawset(L, -4); + lua_pushliteral(L, "query"); + lua_pushcfunction(L, lua_db_prepared_query); + lua_rawset(L, -4); + lua_rawseti(L, -2, 0); + return 1; + } + return 0; +} + + + +/* + ============================================================================= + db:prepared(statement): Fetches a prepared statement made through + DBDPrepareSQL. + ============================================================================= + */ +int lua_db_prepared(lua_State* L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + lua_db_handle *db = 0; + const char *tag; + request_rec *r; + lua_db_prepared_statement* st; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + r = ap_lua_check_request_rec(L, 2); + if (r) { + apr_dbd_prepared_t *pstatement = NULL; + db = lua_get_db_handle(L); + luaL_checktype(L, 3, LUA_TSTRING); + tag = lua_tostring(L, 3); + + /* Look for the statement */ + pstatement = apr_hash_get(db->dbdhandle->prepared, tag, + APR_HASH_KEY_STRING); + + if (pstatement == NULL) { + lua_pushnil(L); + lua_pushfstring(L, + "Could not find any prepared statement called %s!", tag); + return 2; + } + + + /* Push the prepared statement table */ + lua_newtable(L); + st = lua_newuserdata(L, sizeof(lua_db_prepared_statement)); + st->statement = pstatement; + st->variables = -1; /* we don't know :( */ + st->db = db; + lua_pushliteral(L, "select"); + lua_pushcfunction(L, lua_db_prepared_select); + lua_rawset(L, -4); + lua_pushliteral(L, "query"); + lua_pushcfunction(L, lua_db_prepared_query); + lua_rawset(L, -4); + lua_rawseti(L, -2, 0); + return 1; + } + return 0; +} + + + +/* lua_push_db_handle: Creates a database table object with database functions + and a userdata at index 0, which will call lua_dbgc when garbage collected. + */ +static lua_db_handle* lua_push_db_handle(lua_State *L, request_rec* r, int type, + apr_pool_t* pool) +{ + lua_db_handle* db; + lua_newtable(L); + db = lua_newuserdata(L, sizeof(lua_db_handle)); + db->alive = 1; + db->pool = pool; + db->type = type; + db->dbdhandle = 0; + db->server = r->server; + luaL_newmetatable(L, "lua_apr.dbacquire"); + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, lua_db_gc); + lua_rawset(L, -3); + lua_setmetatable(L, -2); + lua_rawseti(L, -2, 0); + + lua_pushliteral(L, "escape"); + lua_pushcfunction(L, lua_db_escape); + lua_rawset(L, -3); + + lua_pushliteral(L, "close"); + lua_pushcfunction(L, lua_db_close); + lua_rawset(L, -3); + + lua_pushliteral(L, "select"); + lua_pushcfunction(L, lua_db_select); + lua_rawset(L, -3); + + lua_pushliteral(L, "query"); + lua_pushcfunction(L, lua_db_query); + lua_rawset(L, -3); + + lua_pushliteral(L, "active"); + lua_pushcfunction(L, lua_db_active); + lua_rawset(L, -3); + + lua_pushliteral(L, "prepare"); + lua_pushcfunction(L, lua_db_prepare); + lua_rawset(L, -3); + + lua_pushliteral(L, "prepared"); + lua_pushcfunction(L, lua_db_prepared); + lua_rawset(L, -3); + return db; +} + +/* + ============================================================================= + dbacquire(dbType, dbString): Opens a new connection to a database of type + _dbType_ and with the connection parameters _dbString_. If successful, + returns a table with functions for using the database handle. If an error + occurs, returns nil as the first parameter and the error message as the + second. See the APR_DBD for a list of database types and connection strings + supported. + ============================================================================= + */ +int lua_db_acquire(lua_State *L) +{ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + const char *type; + const char *arguments; + const char *error = 0; + request_rec *r; + lua_db_handle *db = 0; + apr_status_t rc = 0; + ap_dbd_t *dbdhandle = NULL; + apr_pool_t *pool = NULL; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + r = ap_lua_check_request_rec(L, 1); + if (r) { + type = luaL_optstring(L, 2, "mod_dbd"); /* Defaults to mod_dbd */ + + if (!strcmp(type, "mod_dbd")) { + + lua_settop(L, 0); + lua_ap_dbd_open = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_open); + if (lua_ap_dbd_open) + dbdhandle = (ap_dbd_t *) lua_ap_dbd_open( + r->server->process->pool, r->server); + + if (dbdhandle) { + db = lua_push_db_handle(L, r, LUA_DBTYPE_MOD_DBD, dbdhandle->pool); + db->driver = dbdhandle->driver; + db->handle = dbdhandle->handle; + db->dbdhandle = dbdhandle; + return 1; + } + else { + lua_pushnil(L); + if ( lua_ap_dbd_open == NULL ) + lua_pushliteral(L, + "mod_dbd doesn't seem to have been loaded."); + else + lua_pushliteral( + L, + "Could not acquire connection from mod_dbd. If your database is running, this may indicate a permission problem."); + return 2; + } + } + else { + rc = apr_pool_create(&pool, NULL); + if (rc != APR_SUCCESS) { + lua_pushnil(L); + lua_pushliteral(L, "Could not allocate memory for database!"); + return 2; + } + apr_pool_tag(pool, "lua_dbd_pool"); + apr_dbd_init(pool); + dbdhandle = apr_pcalloc(pool, sizeof(ap_dbd_t)); + rc = apr_dbd_get_driver(pool, type, &dbdhandle->driver); + if (rc == APR_SUCCESS) { + luaL_checktype(L, 3, LUA_TSTRING); + arguments = lua_tostring(L, 3); + lua_settop(L, 0); + + if (*arguments) { + rc = apr_dbd_open_ex(dbdhandle->driver, pool, + arguments, &dbdhandle->handle, &error); + if (rc == APR_SUCCESS) { + db = lua_push_db_handle(L, r, LUA_DBTYPE_APR_DBD, pool); + db->driver = dbdhandle->driver; + db->handle = dbdhandle->handle; + db->dbdhandle = dbdhandle; + return 1; + } + else { + lua_pushnil(L); + if (error) { + lua_pushstring(L, error); + return 2; + } + + return 1; + } + } + + lua_pushnil(L); + lua_pushliteral(L, + "No database connection string was specified."); + apr_pool_destroy(pool); + return (2); + } + else { + lua_pushnil(L); + if (APR_STATUS_IS_ENOTIMPL(rc)) { + lua_pushfstring(L, + "driver for %s not available", type); + } + else if (APR_STATUS_IS_EDSOOPEN(rc)) { + lua_pushfstring(L, + "can't find driver for %s", type); + } + else if (APR_STATUS_IS_ESYMNOTFOUND(rc)) { + lua_pushfstring(L, + "driver for %s is invalid or corrupted", + type); + } + else { + lua_pushliteral(L, + "mod_lua not compatible with APR in get_driver"); + } + lua_pushinteger(L, rc); + apr_pool_destroy(pool); + return 3; + } + } + + lua_pushnil(L); + return 1; + } + + return 0; +} + diff --git a/modules/lua/lua_dbd.h b/modules/lua/lua_dbd.h new file mode 100644 index 0000000..566204b --- /dev/null +++ b/modules/lua/lua_dbd.h @@ -0,0 +1,66 @@ +/** + * 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 _LUA_DBD_H_ +#define _LUA_DBD_H_ + +#include "mod_lua.h" +#include "apr.h" +#include "apr_dbd.h" +#include "mod_dbd.h" + +#define LUA_DBTYPE_APR_DBD 0 +#define LUA_DBTYPE_MOD_DBD 1 +typedef struct +{ + apr_dbd_t *handle; + const apr_dbd_driver_t *driver; + int alive; + apr_pool_t *pool; + char type; + ap_dbd_t * dbdhandle; + server_rec *server; +} lua_db_handle; + +typedef struct { + const apr_dbd_driver_t *driver; + int rows; + int cols; + apr_dbd_results_t *results; + apr_pool_t *pool; +} lua_db_result_set; + +typedef struct { + apr_dbd_prepared_t *statement; + int variables; + lua_db_handle *db; +} lua_db_prepared_statement; + +int lua_db_acquire(lua_State* L); +int lua_db_escape(lua_State* L); +int lua_db_close(lua_State* L); +int lua_db_prepare(lua_State* L); +int lua_db_prepared(lua_State* L); +int lua_db_select(lua_State* L); +int lua_db_query(lua_State* L); +int lua_db_prepared_select(lua_State* L); +int lua_db_prepared_query(lua_State* L); +int lua_db_get_row(lua_State* L); +int lua_db_gc(lua_State* L); +int lua_db_active(lua_State* L); + +#endif /* !_LUA_DBD_H_ */ diff --git a/modules/lua/lua_passwd.c b/modules/lua/lua_passwd.c new file mode 100644 index 0000000..ad86536 --- /dev/null +++ b/modules/lua/lua_passwd.c @@ -0,0 +1,178 @@ +/* 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_passwd.h" +#include "apr_strings.h" +#include "apr_errno.h" + +#if APR_HAVE_STDIO_H +#include <stdio.h> +#endif + +#include "apr_md5.h" +#include "apr_sha1.h" + +#if APR_HAVE_TIME_H +#include <time.h> +#endif +#if APR_HAVE_CRYPT_H +#include <crypt.h> +#endif +#if APR_HAVE_STDLIB_H +#include <stdlib.h> +#endif +#if APR_HAVE_STRING_H +#include <string.h> +#endif +#if APR_HAVE_UNISTD_H +#include <unistd.h> +#endif +#if APR_HAVE_IO_H +#include <io.h> +#endif + +static int generate_salt(char *s, size_t size, const char **errstr, + apr_pool_t *pool) +{ + unsigned char rnd[32]; + static const char itoa64[] = + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + apr_size_t n; + unsigned int val = 0, bits = 0; + apr_status_t rv; + + n = (size * 6 + 7)/8; + if (n > sizeof(rnd)) { + *errstr = apr_psprintf(pool, "generate_salt(): BUG: Buffer too small"); + return ERR_RANDOM; + } + rv = apr_generate_random_bytes(rnd, n); + if (rv) { + *errstr = apr_psprintf(pool, "Unable to generate random bytes: %pm", + &rv); + return ERR_RANDOM; + } + n = 0; + while (size > 0) { + if (bits < 6) { + val |= (rnd[n++] << bits); + bits += 8; + } + *s++ = itoa64[val & 0x3f]; + size--; + val >>= 6; + bits -= 6; + } + *s = '\0'; + return 0; +} + +/* + * Make a password record from the given information. A zero return + * indicates success; on failure, ctx->errstr points to the error message. + */ +int mk_password_hash(passwd_ctx *ctx) +{ + char *pw; + char salt[16]; + apr_status_t rv; + int ret = 0; +#if CRYPT_ALGO_SUPPORTED + char *cbuf; +#endif + + pw = ctx->passwd; + switch (ctx->alg) { + case ALG_APSHA: + /* XXX out >= 28 + strlen(sha1) chars - fixed len SHA */ + apr_sha1_base64(pw, strlen(pw), ctx->out); + break; + + case ALG_APMD5: + ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool); + if (ret != 0) { + ret = ERR_GENERAL; + break; + } + rv = apr_md5_encode(pw, salt, ctx->out, ctx->out_len); + if (rv != APR_SUCCESS) { + ctx->errstr = apr_psprintf(ctx->pool, + "could not encode password: %pm", &rv); + ret = ERR_GENERAL; + } + break; + +#if CRYPT_ALGO_SUPPORTED + case ALG_CRYPT: + ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool); + if (ret != 0) + break; + cbuf = crypt(pw, salt); + if (cbuf == NULL) { + rv = APR_FROM_OS_ERROR(errno); + ctx->errstr = apr_psprintf(ctx->pool, "crypt() failed: %pm", &rv); + ret = ERR_PWMISMATCH; + break; + } + + apr_cpystrn(ctx->out, cbuf, ctx->out_len - 1); + if (strlen(pw) > 8) { + char *truncpw = apr_pstrdup(ctx->pool, pw); + truncpw[8] = '\0'; + if (!strcmp(ctx->out, crypt(truncpw, salt))) { + ctx->errstr = apr_psprintf(ctx->pool, + "Warning: Password truncated to 8 " + "characters by CRYPT algorithm."); + } + memset(truncpw, '\0', strlen(pw)); + } + break; +#endif /* CRYPT_ALGO_SUPPORTED */ + +#if BCRYPT_ALGO_SUPPORTED + case ALG_BCRYPT: + rv = apr_generate_random_bytes((unsigned char*)salt, 16); + if (rv != APR_SUCCESS) { + ctx->errstr = apr_psprintf(ctx->pool, "Unable to generate random " + "bytes: %pm", &rv); + ret = ERR_RANDOM; + break; + } + + if (ctx->cost == 0) + ctx->cost = BCRYPT_DEFAULT_COST; + rv = apr_bcrypt_encode(pw, ctx->cost, (unsigned char*)salt, 16, + ctx->out, ctx->out_len); + if (rv != APR_SUCCESS) { + ctx->errstr = apr_psprintf(ctx->pool, "Unable to encode with " + "bcrypt: %pm", &rv); + ret = ERR_PWMISMATCH; + break; + } + break; +#endif /* BCRYPT_ALGO_SUPPORTED */ + + default: + ctx->errstr = apr_psprintf(ctx->pool, + "mk_password_hash(): unsupported algorithm %d", + ctx->alg); + ret = ERR_GENERAL; + } + memset(pw, '\0', strlen(pw)); + return ret; +} + + diff --git a/modules/lua/lua_passwd.h b/modules/lua/lua_passwd.h new file mode 100644 index 0000000..8606cc1 --- /dev/null +++ b/modules/lua/lua_passwd.h @@ -0,0 +1,90 @@ +/* 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 _LUA_PASSWD_H +#define _LUA_PASSWD_H + +#include "apr.h" +#include "apr_lib.h" +#include "apr_strings.h" +#include "apr_errno.h" +#include "apr_file_io.h" +#include "apr_general.h" +#include "apr_version.h" +#if !APR_VERSION_AT_LEAST(2,0,0) +#include "apu_version.h" +#endif + +#define MAX_PASSWD_LEN 256 + +#define ALG_APMD5 0 +#define ALG_APSHA 1 +#define ALG_BCRYPT 2 +#define ALG_CRYPT 3 + +#define BCRYPT_DEFAULT_COST 5 + +#define ERR_FILEPERM 1 +#define ERR_SYNTAX 2 +#define ERR_PWMISMATCH 3 +#define ERR_INTERRUPTED 4 +#define ERR_OVERFLOW 5 +#define ERR_BADUSER 6 +#define ERR_INVALID 7 +#define ERR_RANDOM 8 +#define ERR_GENERAL 9 +#define ERR_ALG_NOT_SUPP 10 + +#if defined(WIN32) || defined(NETWARE) +#define CRYPT_ALGO_SUPPORTED 0 +#define PLAIN_ALGO_SUPPORTED 1 +#else +#define CRYPT_ALGO_SUPPORTED 1 +#define PLAIN_ALGO_SUPPORTED 0 +#endif + +#if APR_VERSION_AT_LEAST(2,0,0) || \ + (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 5) +#define BCRYPT_ALGO_SUPPORTED 1 +#else +#define BCRYPT_ALGO_SUPPORTED 0 +#endif + +typedef struct passwd_ctx passwd_ctx; + +struct passwd_ctx { + apr_pool_t *pool; + const char *errstr; + char *out; + apr_size_t out_len; + char *passwd; + int alg; + int cost; +}; + + +/* + * The following functions return zero on success; otherwise, one of + * the ERR_* codes is returned and an error message is stored in ctx->errstr. + */ + +/* + * Make a password record from the given information. + */ +int mk_password_hash(passwd_ctx *ctx); + +#endif /* _LUA_PASSWD_H */ + diff --git a/modules/lua/lua_request.c b/modules/lua/lua_request.c new file mode 100644 index 0000000..bec8580 --- /dev/null +++ b/modules/lua/lua_request.c @@ -0,0 +1,3043 @@ +/** + * 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_lua.h" +#include "lua_apr.h" +#include "lua_dbd.h" +#include "lua_passwd.h" +#include "scoreboard.h" +#include "util_md5.h" +#include "util_script.h" +#include "util_varbuf.h" +#include "apr_date.h" +#include "apr_pools.h" +#include "apr_thread_mutex.h" +#include "apr_tables.h" +#include "util_cookies.h" + +#define APR_WANT_BYTEFUNC +#include "apr_want.h" + +extern apr_global_mutex_t* lua_ivm_mutex; +extern apr_shm_t *lua_ivm_shm; + +APLOG_USE_MODULE(lua); +#define POST_MAX_VARS 500 + +#ifndef MODLUA_MAX_REG_MATCH +#define MODLUA_MAX_REG_MATCH 25 +#endif + +typedef char *(*req_field_string_f) (request_rec * r); +typedef int (*req_field_int_f) (request_rec * r); +typedef req_table_t *(*req_field_apr_table_f) (request_rec * r); + + +void ap_lua_rstack_dump(lua_State *L, request_rec *r, const char *msg) +{ + int i; + int top = lua_gettop(L); + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01484) "Lua Stack Dump: [%s]", msg); + 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, APLOGNO(03001) + "%d: '%s'", i, lua_tostring(L, i)); + break; + } + case LUA_TUSERDATA:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03002) + "%d: userdata", i); + break; + } + case LUA_TLIGHTUSERDATA:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03003) + "%d: lightuserdata", i); + break; + } + case LUA_TNIL:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03004) + "%d: NIL", i); + break; + } + case LUA_TNONE:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03005) + "%d: None", i); + break; + } + case LUA_TBOOLEAN:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03006) + "%d: %s", i, + lua_toboolean(L, i) ? "true" : "false"); + break; + } + case LUA_TNUMBER:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03007) + "%d: %g", i, lua_tonumber(L, i)); + break; + } + case LUA_TTABLE:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03008) + "%d: <table>", i); + break; + } + case LUA_TFUNCTION:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03009) + "%d: <function>", i); + break; + } + default:{ + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(03010) + "%d: unknown: -[%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 *ap_lua_check_request_rec(lua_State *L, int index) +{ + request_rec *r; + luaL_checkudata(L, index, "Apache2.Request"); + 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) +{ + int t; + 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"); */ + 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_rawlen(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; +} + +/* helper callback for req_parseargs */ +static int req_aprtable2luatable_cb_len(void *l, const char *key, + const char *value, size_t len) +{ + int t; + 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"); */ + 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_pushlstring(L, value, len); /* [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_rawlen(L, -1); + lua_pushnumber(L, size + 1); /* [#, array, table<s,t>, table<s,s>] */ + lua_pushlstring(L, value, len); /* [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_pushlstring(L, value, len); /* [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; +} + + +/* + ======================================================================================================================= + lua_read_body(request_rec *r, const char **rbuf, apr_off_t *size): Reads any additional form data sent in POST/PUT + requests. Used for multipart POST data. + ======================================================================================================================= + */ +static int lua_read_body(request_rec *r, const char **rbuf, apr_off_t *size, + apr_off_t maxsize) +{ + int rc = OK; + + *rbuf = NULL; + *size = 0; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR))) { + return (rc); + } + if (ap_should_client_block(r)) { + + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + apr_off_t len_read = -1; + apr_off_t rpos = 0; + apr_off_t length = r->remaining; + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + + if (maxsize != 0 && length > maxsize) { + return APR_EINCOMPLETE; /* Only room for incomplete data chunk :( */ + } + *rbuf = (const char *) apr_pcalloc(r->pool, (apr_size_t) (length) + 1); + while ((rpos < length) + && (len_read = ap_get_client_block(r, (char *) *rbuf + rpos, + length - rpos)) > 0) { + rpos += len_read; + } + if (len_read < 0) { + return APR_EINCOMPLETE; + } + *size = rpos; + } + else { + rc = DONE; + } + + return (rc); +} + + +/* + * ======================================================================================================================= + * lua_write_body: Reads any additional form data sent in POST/PUT requests + * and writes to a file. + * ======================================================================================================================= + */ +static apr_status_t lua_write_body(request_rec *r, apr_file_t *file, apr_off_t *size) +{ + apr_status_t rc = OK; + + *size = 0; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR))) + return rc; + if (ap_should_client_block(r)) { + char argsbuffer[HUGE_STRING_LEN]; + apr_off_t rsize, + len_read, + rpos = 0; + apr_off_t length = r->remaining; + + *size = length; + while ((len_read = + ap_get_client_block(r, argsbuffer, + sizeof(argsbuffer))) > 0) { + if ((rpos + len_read) > length) + rsize = (apr_size_t) length - rpos; + else + rsize = len_read; + + rc = apr_file_write_full(file, argsbuffer, (apr_size_t) rsize, + NULL); + if (rc != APR_SUCCESS) + return rc; + rpos += rsize; + } + } + else { + rc = DONE; + } + + return rc; +} + +/* expose apr_table as (r/o) lua table */ +static int req_aprtable2luatable(lua_State *L, apr_table_t *t) +{ + lua_newtable(L); + lua_newtable(L); /* [table, table] */ + apr_table_do(req_aprtable2luatable_cb, L, t, NULL); + return 2; /* [table<string, string>, table<string, array<string>>] */ +} + +static int req_headers_in_table(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + return req_aprtable2luatable(L, r->headers_in); +} +static int req_headers_out_table(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + return req_aprtable2luatable(L, r->headers_out); +} +static int req_err_headers_out_table(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + return req_aprtable2luatable(L, r->err_headers_out); +} +static int req_notes_table(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + return req_aprtable2luatable(L, r->notes); +} +static int req_subprocess_env_table(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + return req_aprtable2luatable(L, r->subprocess_env); +} +/* r:parseargs() returning a lua table */ +static int req_parseargs(lua_State *L) +{ + apr_table_t *form_table; + request_rec *r = ap_lua_check_request_rec(L, 1); + lua_newtable(L); + lua_newtable(L); /* [table, table] */ + ap_args_to_table(r, &form_table); + apr_table_do(req_aprtable2luatable_cb, L, form_table, NULL); + return 2; /* [table<string, string>, table<string, array<string>>] */ +} + +/* ap_lua_binstrstr: Binary strstr function for uploaded data with NULL bytes */ +static char* ap_lua_binstrstr (const char * haystack, size_t hsize, const char* needle, size_t nsize) +{ + size_t p; + if (haystack == NULL) return NULL; + if (needle == NULL) return NULL; + if (hsize < nsize) return NULL; + for (p = 0; p <= (hsize - nsize); ++p) { + if (memcmp(haystack + p, needle, nsize) == 0) { + return (char*) (haystack + p); + } + } + return NULL; +} + +/* r:parsebody(): Parses regular (url-enocded) or multipart POST data and returns two tables*/ +static int req_parsebody(lua_State *L) +{ + apr_array_header_t *pairs; + apr_off_t len; + int res; + apr_size_t size; + apr_size_t max_post_size; + char *multipart; + const char *contentType; + request_rec *r = ap_lua_check_request_rec(L, 1); + max_post_size = (apr_size_t) luaL_optinteger(L, 2, MAX_STRING_LEN); + multipart = apr_pcalloc(r->pool, 256); + contentType = apr_table_get(r->headers_in, "Content-Type"); + lua_newtable(L); + lua_newtable(L); /* [table, table] */ + if (contentType != NULL && (sscanf(contentType, "multipart/form-data; boundary=%250c", multipart) == 1)) { + char *buffer, *key, *filename; + char *start = 0, *end = 0, *crlf = 0; + const char *data; + int i; + size_t vlen = 0; + size_t len = 0; + if (lua_read_body(r, &data, (apr_off_t*) &size, max_post_size) != OK) { + return 2; + } + len = strlen(multipart); + i = 0; + for + ( + start = strstr((char *) data, multipart); + start != NULL; + start = end + ) { + i++; + if (i == POST_MAX_VARS) break; + crlf = strstr((char *) start, "\r\n\r\n"); + if (!crlf) break; + end = ap_lua_binstrstr(crlf, (size - (crlf - data)), multipart, len); + if (end == NULL) break; + key = (char *) apr_pcalloc(r->pool, 256); + filename = (char *) apr_pcalloc(r->pool, 256); + if (end - crlf <= 8) break; + vlen = end - crlf - 8; + buffer = (char *) apr_pcalloc(r->pool, vlen+1); + memcpy(buffer, crlf + 4, vlen); + sscanf(start + len + 2, + "Content-Disposition: form-data; name=\"%255[^\"]\"; filename=\"%255[^\"]\"", + key, filename); + if (*key) { + req_aprtable2luatable_cb_len(L, key, buffer, vlen); + } + } + } + else { + char *buffer; + res = ap_parse_form_data(r, NULL, &pairs, -1, max_post_size); + if (res == OK) { + while(pairs && !apr_is_empty_array(pairs)) { + ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs); + apr_brigade_length(pair->value, 1, &len); + size = (apr_size_t) len; + buffer = apr_palloc(r->pool, size + 1); + apr_brigade_flatten(pair->value, buffer, &size); + buffer[len] = 0; + req_aprtable2luatable_cb(L, pair->name, buffer); + } + } + } + return 2; /* [table<string, string>, table<string, array<string>>] */ +} + + +/* + * lua_ap_requestbody; r:requestbody([filename]) - Reads or stores the request + * body + */ +static int lua_ap_requestbody(lua_State *L) +{ + const char *filename; + request_rec *r; + apr_off_t maxSize; + + r = ap_lua_check_request_rec(L, 1); + filename = luaL_optstring(L, 2, 0); + maxSize = (apr_off_t)luaL_optinteger(L, 3, 0); + + if (r) { + apr_off_t size; + if (maxSize > 0 && r->remaining > maxSize) { + lua_pushnil(L); + lua_pushliteral(L, "Request body was larger than the permitted size."); + return 2; + } + if (r->method_number != M_POST && r->method_number != M_PUT) + return (0); + if (!filename) { + const char *data; + + if (lua_read_body(r, &data, &size, maxSize) != OK) + return (0); + + lua_pushlstring(L, data, (size_t) size); + lua_pushinteger(L, (lua_Integer) size); + return (2); + } else { + apr_status_t rc; + apr_file_t *file; + + rc = apr_file_open(&file, filename, APR_CREATE | APR_FOPEN_WRITE, + APR_FPROT_OS_DEFAULT, r->pool); + lua_settop(L, 0); + if (rc == APR_SUCCESS) { + rc = lua_write_body(r, file, &size); + apr_file_close(file); + if (rc != OK) { + lua_pushboolean(L, 0); + return 1; + } + lua_pushinteger(L, (lua_Integer) size); + return (1); + } else + lua_pushboolean(L, 0); + return (1); + } + } + + return (0); +} + +/* wrap ap_rputs as r:puts(String) */ +static int req_puts(lua_State *L) +{ + request_rec *r = ap_lua_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 = ap_lua_check_request_rec(L, 1); + size_t n; + int rv; + const char *buf = luaL_checklstring(L, 2, &n); + + rv = ap_rwrite((void *) buf, n, r); + lua_pushinteger(L, rv); + return 1; +} + +/* r:add_output_filter(name) */ +static int req_add_output_filter(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + const char *name = luaL_checkstring(L, 2); + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01485) "adding output filter %s", + name); + ap_add_output_filter(name, L, r, r->connection); + return 0; +} + +/* wrap ap_construct_url as r:construct_url(String) */ +static int req_construct_url(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + const char *name = luaL_checkstring(L, 2); + lua_pushstring(L, ap_construct_url(r->pool, name, r)); + return 1; +} + +/* wrap ap_escape_html r:escape_html(String) */ +static int req_escape_html(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + const char *s = luaL_checkstring(L, 2); + lua_pushstring(L, ap_escape_html(r->pool, s)); + return 1; +} + +/* wrap optional ssl_var_lookup as r:ssl_var_lookup(String) */ +static int req_ssl_var_lookup(lua_State *L) +{ + request_rec *r = ap_lua_check_request_rec(L, 1); + const char *s = luaL_checkstring(L, 2); + const char *res = ap_lua_ssl_val(r->pool, r->server, r->connection, r, + (char *)s); + lua_pushstring(L, res); + return 1; +} + +/* BEGIN dispatch mathods for request_rec fields */ + +/* not really a field, but we treat it like one */ +static const char *req_document_root(request_rec *r) +{ + return ap_document_root(r); +} + +static const char *req_context_prefix(request_rec *r) +{ + return ap_context_prefix(r); +} + +static const char *req_context_document_root(request_rec *r) +{ + return ap_context_document_root(r); +} + +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_handler_field(request_rec *r) +{ + return r->handler; +} +static const char *req_proxyreq_field(request_rec *r) +{ + switch (r->proxyreq) { + case PROXYREQ_NONE: return "PROXYREQ_NONE"; + case PROXYREQ_PROXY: return "PROXYREQ_PROXY"; + case PROXYREQ_REVERSE: return "PROXYREQ_REVERSE"; + case PROXYREQ_RESPONSE: return "PROXYREQ_RESPONSE"; + default: return NULL; + } +} +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 const char *req_log_id_field(request_rec *r) +{ + return r->log_id; +} + +static const char *req_useragent_ip_field(request_rec *r) +{ + return r->useragent_ip; +} + +static int req_remaining_field(request_rec *r) +{ + return r->remaining; +} + +static int req_status_field(request_rec *r) +{ + return r->status; +} + +static int req_assbackwards_field(request_rec *r) +{ + return r->assbackwards; +} + +static req_table_t* req_headers_in(request_rec *r) +{ + req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t)); + t->r = r; + t->t = r->headers_in; + t->n = "headers_in"; + return t; +} + +static req_table_t* req_headers_out(request_rec *r) +{ + req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t)); + t->r = r; + t->t = r->headers_out; + t->n = "headers_out"; + return t; +} + +static req_table_t* req_err_headers_out(request_rec *r) +{ + req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t)); + t->r = r; + t->t = r->err_headers_out; + t->n = "err_headers_out"; + return t; +} + +static req_table_t* req_subprocess_env(request_rec *r) +{ + req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t)); + t->r = r; + t->t = r->subprocess_env; + t->n = "subprocess_env"; + return t; +} + +static req_table_t* req_notes(request_rec *r) +{ + req_table_t* t = apr_palloc(r->pool, sizeof(req_table_t)); + t->r = r; + t->t = r->notes; + t->n = "notes"; + return t; +} + +static int req_ssl_is_https_field(request_rec *r) +{ + return ap_lua_ssl_is_https(r->connection); +} + +static int req_ap_get_server_port(request_rec *r) +{ + return (int) ap_get_server_port(r); +} + +static int lua_ap_rflush (lua_State *L) { + + int returnValue; + request_rec *r; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + returnValue = ap_rflush(r); + lua_pushboolean(L, (returnValue == 0)); + return 1; +} + + +static const char* lua_ap_options(request_rec* r) +{ + int opts; + opts = ap_allow_options(r); + return apr_psprintf(r->pool, "%s %s %s %s %s %s", (opts&OPT_INDEXES) ? "Indexes" : "", (opts&OPT_INCLUDES) ? "Includes" : "", (opts&OPT_SYM_LINKS) ? "FollowSymLinks" : "", (opts&OPT_EXECCGI) ? "ExecCGI" : "", (opts&OPT_MULTI) ? "MultiViews" : "", (opts&OPT_ALL) == OPT_ALL ? "All" : "" ); +} + +static const char* lua_ap_allowoverrides(request_rec* r) +{ + int opts; + opts = ap_allow_overrides(r); + if ( (opts & OR_ALL) == OR_ALL) { + return "All"; + } + else if (opts == OR_NONE) { + return "None"; + } + return apr_psprintf(r->pool, "%s %s %s %s %s", (opts & OR_LIMIT) ? "Limit" : "", (opts & OR_OPTIONS) ? "Options" : "", (opts & OR_FILEINFO) ? "FileInfo" : "", (opts & OR_AUTHCFG) ? "AuthCfg" : "", (opts & OR_INDEXES) ? "Indexes" : "" ); + +} + +static int lua_ap_started(request_rec* r) +{ + return (int)(ap_scoreboard_image->global->restart_time / 1000000); +} + +static const char* lua_ap_basic_auth_pw(request_rec* r) +{ + const char* pw = NULL; + ap_get_basic_auth_pw(r, &pw); + return pw ? pw : ""; +} + +static int lua_ap_limit_req_body(request_rec* r) +{ + return (int) ap_get_limit_req_body(r); +} + +static int lua_ap_is_initial_req(request_rec *r) +{ + return ap_is_initial_req(r); +} + +static int lua_ap_some_auth_required(request_rec *r) +{ + return ap_some_auth_required(r); +} + +static int lua_ap_sendfile(lua_State *L) +{ + + apr_finfo_t file_info; + const char *filename; + request_rec *r; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + filename = lua_tostring(L, 2); + apr_stat(&file_info, filename, APR_FINFO_MIN, r->pool); + if (file_info.filetype == APR_NOFILE || file_info.filetype == APR_DIR) { + lua_pushboolean(L, 0); + } + else { + apr_size_t sent; + apr_status_t rc; + apr_file_t *file; + + rc = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, + r->pool); + if (rc == APR_SUCCESS) { + ap_send_fd(file, r, 0, (apr_size_t)file_info.size, &sent); + apr_file_close(file); + lua_pushinteger(L, sent); + } + else { + lua_pushboolean(L, 0); + } + } + + return (1); +} + + +/* + * lua_apr_b64encode; r:encode_base64(string) - encodes a string to Base64 + * format + */ +static int lua_apr_b64encode(lua_State *L) +{ + const char *plain; + char *encoded; + size_t plain_len, encoded_len; + request_rec *r; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + plain = lua_tolstring(L, 2, &plain_len); + encoded_len = apr_base64_encode_len(plain_len); + if (encoded_len) { + encoded = apr_palloc(r->pool, encoded_len); + encoded_len = apr_base64_encode(encoded, plain, plain_len); + if (encoded_len > 0 && encoded[encoded_len - 1] == '\0') + encoded_len--; + lua_pushlstring(L, encoded, encoded_len); + return 1; + } + return 0; +} + +/* + * lua_apr_b64decode; r:decode_base64(string) - decodes a Base64 string + */ +static int lua_apr_b64decode(lua_State *L) +{ + const char *encoded; + char *plain; + size_t encoded_len, decoded_len; + request_rec *r; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + encoded = lua_tolstring(L, 2, &encoded_len); + decoded_len = apr_base64_decode_len(encoded); + if (decoded_len) { + plain = apr_palloc(r->pool, decoded_len); + decoded_len = apr_base64_decode(plain, encoded); + if (decoded_len > 0 && plain[decoded_len - 1] == '\0') + decoded_len--; + lua_pushlstring(L, plain, decoded_len); + return 1; + } + return 0; +} + +/* + * lua_ap_unescape; r:unescape(string) - Unescapes an URL-encoded string + */ +static int lua_ap_unescape(lua_State *L) +{ + const char *escaped; + char *plain; + size_t x, + y; + request_rec *r; + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + escaped = lua_tolstring(L, 2, &x); + plain = apr_pstrdup(r->pool, escaped); + y = ap_unescape_urlencoded(plain); + if (!y) { + lua_pushstring(L, plain); + return 1; + } + return 0; +} + +/* + * lua_ap_escape; r:escape(string) - URL-escapes a string + */ +static int lua_ap_escape(lua_State *L) +{ + const char *plain; + char *escaped; + size_t x; + request_rec *r; + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + plain = lua_tolstring(L, 2, &x); + escaped = ap_escape_urlencoded(r->pool, plain); + lua_pushstring(L, escaped); + return 1; +} + +/* + * lua_apr_md5; r:md5(string) - Calculates an MD5 digest of a string + */ +static int lua_apr_md5(lua_State *L) +{ + const char *buffer; + char *result; + size_t len; + request_rec *r; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + buffer = lua_tolstring(L, 2, &len); + result = ap_md5_binary(r->pool, (const unsigned char *)buffer, len); + lua_pushstring(L, result); + return 1; +} + +/* + * lua_apr_sha1; r:sha1(string) - Calculates the SHA1 digest of a string + */ +static int lua_apr_sha1(lua_State *L) +{ + unsigned char digest[APR_SHA1_DIGESTSIZE]; + apr_sha1_ctx_t sha1; + const char *buffer; + char *result; + size_t len; + request_rec *r; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + result = apr_pcalloc(r->pool, sizeof(digest) * 2 + 1); + buffer = lua_tolstring(L, 2, &len); + apr_sha1_init(&sha1); + apr_sha1_update(&sha1, buffer, len); + apr_sha1_final(digest, &sha1); + + ap_bin2hex(digest, sizeof(digest), result); + lua_pushstring(L, result); + return 1; +} + +/* + * lua_apr_htpassword; r:htpassword(string [, algorithm [, cost]]) - Creates + * a htpassword hash from a string + */ +static int lua_apr_htpassword(lua_State *L) +{ + passwd_ctx ctx = { 0 }; + request_rec *r; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + ctx.passwd = apr_pstrdup(r->pool, lua_tostring(L, 2)); + ctx.alg = luaL_optinteger(L, 3, ALG_APMD5); + ctx.cost = luaL_optinteger(L, 4, 0); + ctx.pool = r->pool; + ctx.out = apr_pcalloc(r->pool, MAX_PASSWD_LEN); + ctx.out_len = MAX_PASSWD_LEN; + if (mk_password_hash(&ctx)) { + lua_pushboolean(L, 0); + lua_pushstring(L, ctx.errstr); + return 2; + } else { + lua_pushstring(L, ctx.out); + } + return 1; +} + +/* + * lua_apr_touch; r:touch(string [, time]) - Sets mtime of a file + */ +static int lua_apr_touch(lua_State *L) +{ + request_rec *r; + const char *path; + apr_status_t status; + apr_time_t mtime; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + path = lua_tostring(L, 2); + mtime = (apr_time_t)luaL_optnumber(L, 3, (lua_Number)apr_time_now()); + status = apr_file_mtime_set(path, mtime, r->pool); + lua_pushboolean(L, (status == 0)); + return 1; +} + +/* + * lua_apr_mkdir; r:mkdir(string [, permissions]) - Creates a directory + */ +static int lua_apr_mkdir(lua_State *L) +{ + request_rec *r; + const char *path; + apr_status_t status; + apr_fileperms_t perms; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + path = lua_tostring(L, 2); + perms = luaL_optinteger(L, 3, APR_OS_DEFAULT); + status = apr_dir_make(path, perms, r->pool); + lua_pushboolean(L, (status == 0)); + return 1; +} + +/* + * lua_apr_mkrdir; r:mkrdir(string [, permissions]) - Creates directories + * recursive + */ +static int lua_apr_mkrdir(lua_State *L) +{ + request_rec *r; + const char *path; + apr_status_t status; + apr_fileperms_t perms; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + path = lua_tostring(L, 2); + perms = luaL_optinteger(L, 3, APR_OS_DEFAULT); + status = apr_dir_make_recursive(path, perms, r->pool); + lua_pushboolean(L, (status == 0)); + return 1; +} + +/* + * lua_apr_rmdir; r:rmdir(string) - Removes a directory + */ +static int lua_apr_rmdir(lua_State *L) +{ + request_rec *r; + const char *path; + apr_status_t status; + + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + path = lua_tostring(L, 2); + status = apr_dir_remove(path, r->pool); + lua_pushboolean(L, (status == 0)); + return 1; +} + +/* + * lua_apr_date_parse_rfc; r.date_parse_rfc(string) - Parses a DateTime string + */ +static int lua_apr_date_parse_rfc(lua_State *L) +{ + const char *input; + apr_time_t result; + + luaL_checktype(L, 1, LUA_TSTRING); + input = lua_tostring(L, 1); + result = apr_date_parse_rfc(input); + if (result == 0) + return 0; + lua_pushnumber(L, (lua_Number)(result / APR_USEC_PER_SEC)); + return 1; +} + +/* + * lua_ap_mpm_query; r:mpm_query(info) - Queries for MPM info + */ +static int lua_ap_mpm_query(lua_State *L) +{ + int x, + y; + + x = lua_tointeger(L, 1); + ap_mpm_query(x, &y); + lua_pushinteger(L, y); + return 1; +} + +/* + * lua_ap_expr; r:expr(string) - Evaluates an expr statement. + */ +static int lua_ap_expr(lua_State *L) +{ + request_rec *r; + int x = 0; + const char *expr, + *err; + ap_expr_info_t res; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + expr = lua_tostring(L, 2); + + + res.filename = NULL; + res.flags = 0; + res.line_number = 0; + res.module_index = APLOG_MODULE_INDEX; + + err = ap_expr_parse(r->pool, r->pool, &res, expr, NULL); + if (!err) { + x = ap_expr_exec(r, &res, &err); + lua_pushboolean(L, x); + if (x < 0) { + lua_pushstring(L, err); + return 2; + } + return 1; + } else { + lua_pushboolean(L, 0); + lua_pushstring(L, err); + return 2; + } + lua_pushboolean(L, 0); + return 1; +} + + +/* + * lua_ap_regex; r:regex(string, pattern [, flags]) + * - Evaluates a regex and returns captures if matched + */ +static int lua_ap_regex(lua_State *L) +{ + request_rec *r; + int i, + rv, + flags; + const char *pattern, + *source; + char *err; + ap_regex_t regex; + ap_regmatch_t matches[MODLUA_MAX_REG_MATCH+1]; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + luaL_checktype(L, 3, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + source = lua_tostring(L, 2); + pattern = lua_tostring(L, 3); + flags = luaL_optinteger(L, 4, 0); + + rv = ap_regcomp(®ex, pattern, flags); + if (rv) { + lua_pushboolean(L, 0); + err = apr_palloc(r->pool, 256); + ap_regerror(rv, ®ex, err, 256); + lua_pushstring(L, err); + return 2; + } + + if (regex.re_nsub > MODLUA_MAX_REG_MATCH) { + lua_pushboolean(L, 0); + err = apr_palloc(r->pool, 64); + apr_snprintf(err, 64, + "regcomp found %d matches; only %d allowed.", + regex.re_nsub, MODLUA_MAX_REG_MATCH); + lua_pushstring(L, err); + return 2; + } + + rv = ap_regexec(®ex, source, MODLUA_MAX_REG_MATCH, matches, 0); + if (rv == AP_REG_NOMATCH) { + lua_pushboolean(L, 0); + return 1; + } + + lua_newtable(L); + for (i = 0; i <= regex.re_nsub; i++) { + lua_pushinteger(L, i); + if (matches[i].rm_so >= 0 && matches[i].rm_eo >= 0) + lua_pushstring(L, + apr_pstrndup(r->pool, source + matches[i].rm_so, + matches[i].rm_eo - matches[i].rm_so)); + else + lua_pushnil(L); + lua_settable(L, -3); + + } + return 1; +} + + + + +/* + * lua_ap_scoreboard_process; r:scoreboard_process(a) - returns scoreboard info + */ +static int lua_ap_scoreboard_process(lua_State *L) +{ + int i; + process_score *ps_record; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TNUMBER); + i = lua_tointeger(L, 2); + ps_record = ap_get_scoreboard_process(i); + if (ps_record) { + lua_newtable(L); + + lua_pushstring(L, "connections"); + lua_pushnumber(L, ps_record->connections); + lua_settable(L, -3); + + lua_pushstring(L, "keepalive"); + lua_pushnumber(L, ps_record->keep_alive); + lua_settable(L, -3); + + lua_pushstring(L, "lingering_close"); + lua_pushnumber(L, ps_record->lingering_close); + lua_settable(L, -3); + + lua_pushstring(L, "pid"); + lua_pushnumber(L, ps_record->pid); + lua_settable(L, -3); + + lua_pushstring(L, "suspended"); + lua_pushnumber(L, ps_record->suspended); + lua_settable(L, -3); + + lua_pushstring(L, "write_completion"); + lua_pushnumber(L, ps_record->write_completion); + lua_settable(L, -3); + + lua_pushstring(L, "not_accepting"); + lua_pushnumber(L, ps_record->not_accepting); + lua_settable(L, -3); + + lua_pushstring(L, "quiescing"); + lua_pushnumber(L, ps_record->quiescing); + lua_settable(L, -3); + + return 1; + } + return 0; +} + +/* + * lua_ap_scoreboard_worker; r:scoreboard_worker(proc, thread) - Returns thread + * info + */ +static int lua_ap_scoreboard_worker(lua_State *L) +{ + int i, j; + worker_score *ws_record = NULL; + request_rec *r = NULL; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TNUMBER); + luaL_checktype(L, 3, LUA_TNUMBER); + + r = ap_lua_check_request_rec(L, 1); + if (!r) return 0; + + i = lua_tointeger(L, 2); + j = lua_tointeger(L, 3); + ws_record = apr_palloc(r->pool, sizeof *ws_record); + + ap_copy_scoreboard_worker(ws_record, i, j); + if (ws_record) { + lua_newtable(L); + + lua_pushstring(L, "access_count"); + lua_pushnumber(L, ws_record->access_count); + lua_settable(L, -3); + + lua_pushstring(L, "bytes_served"); + lua_pushnumber(L, (lua_Number) ws_record->bytes_served); + lua_settable(L, -3); + + lua_pushstring(L, "client"); + lua_pushstring(L, ws_record->client); + lua_settable(L, -3); + + lua_pushstring(L, "client64"); + lua_pushstring(L, ws_record->client64); + lua_settable(L, -3); + + lua_pushstring(L, "conn_bytes"); + lua_pushnumber(L, (lua_Number) ws_record->conn_bytes); + lua_settable(L, -3); + + lua_pushstring(L, "conn_count"); + lua_pushnumber(L, ws_record->conn_count); + lua_settable(L, -3); + + lua_pushstring(L, "generation"); + lua_pushnumber(L, ws_record->generation); + lua_settable(L, -3); + + lua_pushstring(L, "last_used"); + lua_pushnumber(L, (lua_Number) ws_record->last_used); + lua_settable(L, -3); + + lua_pushstring(L, "pid"); + lua_pushnumber(L, ws_record->pid); + lua_settable(L, -3); + + lua_pushstring(L, "request"); + lua_pushstring(L, ws_record->request); + lua_settable(L, -3); + + lua_pushstring(L, "start_time"); + lua_pushnumber(L, (lua_Number) ws_record->start_time); + lua_settable(L, -3); + + lua_pushstring(L, "status"); + lua_pushnumber(L, ws_record->status); + lua_settable(L, -3); + + lua_pushstring(L, "stop_time"); + lua_pushnumber(L, (lua_Number) ws_record->stop_time); + lua_settable(L, -3); + + lua_pushstring(L, "tid"); + + lua_pushinteger(L, (lua_Integer) ws_record->tid); + lua_settable(L, -3); + + lua_pushstring(L, "vhost"); + lua_pushstring(L, ws_record->vhost); + lua_settable(L, -3); +#ifdef HAVE_TIMES + lua_pushstring(L, "stimes"); + lua_pushnumber(L, ws_record->times.tms_stime); + lua_settable(L, -3); + + lua_pushstring(L, "utimes"); + lua_pushnumber(L, ws_record->times.tms_utime); + lua_settable(L, -3); +#endif + return 1; + } + return 0; +} + +/* + * lua_ap_clock; r:clock() - Returns timestamp with microsecond precision + */ +static int lua_ap_clock(lua_State *L) +{ + apr_time_t now; + now = apr_time_now(); + lua_pushnumber(L, (lua_Number) now); + return 1; +} + +/* + * lua_ap_add_input_filter; r:add_input_filter(name) - Adds an input filter to + * the chain + */ +static int lua_ap_add_input_filter(lua_State *L) +{ + request_rec *r; + const char *filterName; + ap_filter_rec_t *filter; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + filterName = lua_tostring(L, 2); + filter = ap_get_input_filter_handle(filterName); + if (filter) { + ap_add_input_filter_handle(filter, NULL, r, r->connection); + lua_pushboolean(L, 1); + } else + lua_pushboolean(L, 0); + return 1; +} + + +/* + * lua_ap_module_info; r:module_info(mod_name) - Returns information about a + * loaded module + */ +static int lua_ap_module_info(lua_State *L) +{ + const char *moduleName; + module *mod; + + luaL_checktype(L, 1, LUA_TSTRING); + moduleName = lua_tostring(L, 1); + mod = ap_find_linked_module(moduleName); + if (mod && mod->cmds) { + const command_rec *cmd; + lua_newtable(L); + lua_pushstring(L, "commands"); + lua_newtable(L); + for (cmd = mod->cmds; cmd->name; ++cmd) { + lua_pushstring(L, cmd->name); + lua_pushstring(L, cmd->errmsg); + lua_settable(L, -3); + } + lua_settable(L, -3); + return 1; + } + return 0; +} + +/* + * lua_ap_runtime_dir_relative: r:runtime_dir_relative(file): Returns the + * filename as relative to the runtime dir + */ +static int lua_ap_runtime_dir_relative(lua_State *L) +{ + request_rec *r; + const char *file; + + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + file = luaL_optstring(L, 2, "."); + lua_pushstring(L, ap_runtime_dir_relative(r->pool, file)); + return 1; +} + +/* + * lua_ap_set_document_root; r:set_document_root(path) - sets the current doc + * root for the request + */ +static int lua_ap_set_document_root(lua_State *L) +{ + request_rec *r; + const char *root; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + root = lua_tostring(L, 2); + ap_set_document_root(r, root); + return 0; +} + +/* + * lua_ap_getdir; r:get_direntries(directory) - Gets all entries of a + * directory and returns the directory info as a table + */ +static int lua_ap_getdir(lua_State *L) +{ + request_rec *r; + apr_dir_t *thedir; + apr_finfo_t file_info; + apr_status_t status; + const char *directory; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + directory = lua_tostring(L, 2); + if (apr_dir_open(&thedir, directory, r->pool) == APR_SUCCESS) { + int i = 0; + lua_newtable(L); + do { + status = apr_dir_read(&file_info, APR_FINFO_NAME, thedir); + if (APR_STATUS_IS_INCOMPLETE(status)) { + continue; /* ignore un-stat()able files */ + } + else if (status != APR_SUCCESS) { + break; + } + lua_pushinteger(L, ++i); + lua_pushstring(L, file_info.name); + lua_settable(L, -3); + + } while (1); + apr_dir_close(thedir); + return 1; + } + else { + return 0; + } +} + +/* + * lua_ap_stat; r:stat(filename [, wanted]) - Runs stat on a file and + * returns the file info as a table + */ +static int lua_ap_stat(lua_State *L) +{ + request_rec *r; + const char *filename; + apr_finfo_t file_info; + apr_int32_t wanted; + + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TSTRING); + r = ap_lua_check_request_rec(L, 1); + filename = lua_tostring(L, 2); + wanted = luaL_optinteger(L, 3, APR_FINFO_MIN); + if (apr_stat(&file_info, filename, wanted, r->pool) == OK) { + lua_newtable(L); + if (wanted & APR_FINFO_MTIME) { + lua_pushstring(L, "mtime"); + lua_pushnumber(L, (lua_Number) file_info.mtime); + lua_settable(L, -3); + } + if (wanted & APR_FINFO_ATIME) { + lua_pushstring(L, "atime"); + lua_pushnumber(L, (lua_Number) file_info.atime); + lua_settable(L, -3); + } + if (wanted & APR_FINFO_CTIME) { + lua_pushstring(L, "ctime"); + lua_pushnumber(L, (lua_Number) file_info.ctime); + lua_settable(L, -3); + } + if (wanted & APR_FINFO_SIZE) { + lua_pushstring(L, "size"); + lua_pushnumber(L, (lua_Number) file_info.size); + lua_settable(L, -3); + } + if (wanted & APR_FINFO_TYPE) { + lua_pushstring(L, "filetype"); + lua_pushinteger(L, file_info.filetype); + lua_settable(L, -3); + } + if (wanted & APR_FINFO_PROT) { + lua_pushstring(L, "protection"); + lua_pushinteger(L, file_info.protection); + lua_settable(L, -3); + } + return 1; + } + else { + return 0; + } +} + +/* + * lua_ap_loaded_modules; r:loaded_modules() - Returns a list of loaded modules + */ +static int lua_ap_loaded_modules(lua_State *L) +{ + int i; + lua_newtable(L); + for (i = 0; ap_loaded_modules[i] && ap_loaded_modules[i]->name; i++) { + lua_pushinteger(L, i + 1); + lua_pushstring(L, ap_loaded_modules[i]->name); + lua_settable(L, -3); + } + return 1; +} + +/* + * lua_ap_server_info; r:server_info() - Returns server info, such as the + * executable filename, server root, mpm etc + */ +static int lua_ap_server_info(lua_State *L) +{ + lua_newtable(L); + + lua_pushstring(L, "server_executable"); + lua_pushstring(L, ap_server_argv0); + lua_settable(L, -3); + + lua_pushstring(L, "server_root"); + lua_pushstring(L, ap_server_root); + lua_settable(L, -3); + + lua_pushstring(L, "scoreboard_fname"); + lua_pushstring(L, ap_scoreboard_fname); + lua_settable(L, -3); + + lua_pushstring(L, "server_mpm"); + lua_pushstring(L, ap_show_mpm()); + lua_settable(L, -3); + + return 1; +} + + +/* + * === Auto-scraped functions === + */ + + +/** + * ap_set_context_info: Set context_prefix and context_document_root. + * @param r The request + * @param prefix the URI prefix, without trailing slash + * @param document_root the corresponding directory on disk, without trailing + * slash + * @note If one of prefix of document_root is NULL, the corrsponding + * property will not be changed. + */ +static int lua_ap_set_context_info(lua_State *L) +{ + request_rec *r; + const char *prefix; + const char *document_root; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + prefix = lua_tostring(L, 2); + luaL_checktype(L, 3, LUA_TSTRING); + document_root = lua_tostring(L, 3); + ap_set_context_info(r, prefix, document_root); + return 0; +} + + +/** + * ap_os_escape_path (apr_pool_t *p, const char *path, int partial) + * convert an OS path to a URL in an OS dependant way. + * @param p The pool to allocate from + * @param path The path to convert + * @param partial if set, assume that the path will be appended to something + * with a '/' in it (and thus does not prefix "./") + * @return The converted URL + */ +static int lua_ap_os_escape_path(lua_State *L) +{ + char *returnValue; + request_rec *r; + const char *path; + int partial = 0; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + path = lua_tostring(L, 2); + if (lua_isboolean(L, 3)) + partial = lua_toboolean(L, 3); + returnValue = ap_os_escape_path(r->pool, path, partial); + lua_pushstring(L, returnValue); + return 1; +} + + +/** + * ap_escape_logitem (apr_pool_t *p, const char *str) + * Escape a string for logging + * @param p The pool to allocate from + * @param str The string to escape + * @return The escaped string + */ +static int lua_ap_escape_logitem(lua_State *L) +{ + char *returnValue; + request_rec *r; + const char *str; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + str = lua_tostring(L, 2); + returnValue = ap_escape_logitem(r->pool, str); + lua_pushstring(L, returnValue); + return 1; +} + +/** + * ap_strcmp_match (const char *str, const char *expected) + * Determine if a string matches a pattern containing the wildcards '?' or '*' + * @param str The string to check + * @param expected The pattern to match against + * @param ignoreCase Whether to ignore case when matching + * @return 1 if the two strings match, 0 otherwise + */ +static int lua_ap_strcmp_match(lua_State *L) +{ + int returnValue; + const char *str; + const char *expected; + int ignoreCase = 0; + luaL_checktype(L, 1, LUA_TSTRING); + str = lua_tostring(L, 1); + luaL_checktype(L, 2, LUA_TSTRING); + expected = lua_tostring(L, 2); + if (lua_isboolean(L, 3)) + ignoreCase = lua_toboolean(L, 3); + if (!ignoreCase) + returnValue = ap_strcmp_match(str, expected); + else + returnValue = ap_strcasecmp_match(str, expected); + lua_pushboolean(L, (!returnValue)); + return 1; +} + + +/** + * ap_set_keepalive (request_rec *r) + * Set the keepalive status for this request + * @param r The current request + * @return 1 if keepalive can be set, 0 otherwise + */ +static int lua_ap_set_keepalive(lua_State *L) +{ + int returnValue; + request_rec *r; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + returnValue = ap_set_keepalive(r); + lua_pushboolean(L, returnValue); + return 1; +} + +/** + * ap_make_etag (request_rec *r, int force_weak) + * Construct an entity tag from the resource information. If it's a real + * file, build in some of the file characteristics. + * @param r The current request + * @param force_weak Force the entity tag to be weak - it could be modified + * again in as short an interval. + * @return The entity tag + */ +static int lua_ap_make_etag(lua_State *L) +{ + char *returnValue; + request_rec *r; + int force_weak; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TBOOLEAN); + force_weak = (int)luaL_optinteger(L, 2, 0); + returnValue = ap_make_etag(r, force_weak); + lua_pushstring(L, returnValue); + return 1; +} + + + +/** + * ap_send_interim_response (request_rec *r, int send_headers) + * Send an interim (HTTP 1xx) response immediately. + * @param r The request + * @param send_headers Whether to send&clear headers in r->headers_out + */ +static int lua_ap_send_interim_response(lua_State *L) +{ + request_rec *r; + int send_headers = 0; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + if (lua_isboolean(L, 2)) + send_headers = lua_toboolean(L, 2); + ap_send_interim_response(r, send_headers); + return 0; +} + + +/** + * ap_custom_response (request_rec *r, int status, const char *string) + * Install a custom response handler for a given status + * @param r The current request + * @param status The status for which the custom response should be used + * @param string The custom response. This can be a static string, a file + * or a URL + */ +static int lua_ap_custom_response(lua_State *L) +{ + request_rec *r; + int status; + const char *string; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + luaL_checktype(L, 2, LUA_TNUMBER); + status = lua_tointeger(L, 2); + luaL_checktype(L, 3, LUA_TSTRING); + string = lua_tostring(L, 3); + ap_custom_response(r, status, string); + return 0; +} + + +/** + * ap_exists_config_define (const char *name) + * Check for a definition from the server command line + * @param name The define to check for + * @return 1 if defined, 0 otherwise + */ +static int lua_ap_exists_config_define(lua_State *L) +{ + int returnValue; + const char *name; + luaL_checktype(L, 1, LUA_TSTRING); + name = lua_tostring(L, 1); + returnValue = ap_exists_config_define(name); + lua_pushboolean(L, returnValue); + return 1; +} + +static int lua_ap_get_server_name_for_url(lua_State *L) +{ + const char *servername; + request_rec *r; + luaL_checktype(L, 1, LUA_TUSERDATA); + r = ap_lua_check_request_rec(L, 1); + servername = ap_get_server_name_for_url(r); + lua_pushstring(L, servername); + return 1; +} + +/* ap_state_query (int query_code) item starts a new field */ +static int lua_ap_state_query(lua_State *L) +{ + + int returnValue; + int query_code; + luaL_checktype(L, 1, LUA_TNUMBER); + query_code = lua_tointeger(L, 1); + returnValue = ap_state_query(query_code); + lua_pushinteger(L, returnValue); + return 1; +} + +/* + * lua_ap_usleep; r:usleep(microseconds) + * - Sleep for the specified number of microseconds. + */ +static int lua_ap_usleep(lua_State *L) +{ + apr_interval_time_t msec; + luaL_checktype(L, 1, LUA_TNUMBER); + msec = (apr_interval_time_t)lua_tonumber(L, 1); + apr_sleep(msec); + return 0; +} + +/* END dispatch methods for request_rec fields */ + +static int req_dispatch(lua_State *L) +{ + apr_hash_t *dispatch; + req_fun_t *rft; + request_rec *r = ap_lua_check_request_rec(L, 1); + const char *name = luaL_checkstring(L, 2); + lua_pop(L, 2); + + lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch"); + dispatch = lua_touserdata(L, 1); + lua_pop(L, 1); + + rft = apr_hash_get(dispatch, name, APR_HASH_KEY_STRING); + if (rft) { + switch (rft->type) { + case APL_REQ_FUNTYPE_TABLE:{ + req_table_t *rs; + req_field_apr_table_f func = (req_field_apr_table_f)rft->fun; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01486) + "request_rec->dispatching %s -> apr table", + name); + rs = (*func)(r); + ap_lua_push_apr_table(L, rs); + return 1; + } + + case APL_REQ_FUNTYPE_LUACFUN:{ + lua_CFunction func = (lua_CFunction)rft->fun; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01487) + "request_rec->dispatching %s -> lua_CFunction", + name); + lua_pushcfunction(L, func); + return 1; + } + case APL_REQ_FUNTYPE_STRING:{ + req_field_string_f func = (req_field_string_f)rft->fun; + char *rs; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01488) + "request_rec->dispatching %s -> string", name); + rs = (*func) (r); + lua_pushstring(L, rs); + return 1; + } + case APL_REQ_FUNTYPE_INT:{ + req_field_int_f func = (req_field_int_f)rft->fun; + int rs; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01489) + "request_rec->dispatching %s -> int", name); + rs = (*func) (r); + lua_pushinteger(L, rs); + return 1; + } + case APL_REQ_FUNTYPE_BOOLEAN:{ + req_field_int_f func = (req_field_int_f)rft->fun; + int rs; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01490) + "request_rec->dispatching %s -> boolean", name); + rs = (*func) (r); + lua_pushboolean(L, rs); + return 1; + } + } + } + + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01491) "nothing for %s", name); + return 0; +} + +/* helper function for the logging functions below */ +static int req_log_at(lua_State *L, int level) +{ + const char *msg; + request_rec *r = ap_lua_check_request_rec(L, 1); + lua_Debug dbg; + + lua_getstack(L, 1, &dbg); + lua_getinfo(L, "Sl", &dbg); + + msg = luaL_checkstring(L, 2); + /* Intentional no APLOGNO */ + ap_log_rerror(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0, + r, "%s", msg); + return 0; +} + +/* r:debug(String) and friends which use apache logging */ +static int req_emerg(lua_State *L) +{ + return req_log_at(L, APLOG_EMERG); +} +static int req_alert(lua_State *L) +{ + return req_log_at(L, APLOG_ALERT); +} +static int req_crit(lua_State *L) +{ + return req_log_at(L, APLOG_CRIT); +} +static int req_err(lua_State *L) +{ + return req_log_at(L, APLOG_ERR); +} +static int req_warn(lua_State *L) +{ + return req_log_at(L, APLOG_WARNING); +} +static int req_notice(lua_State *L) +{ + return req_log_at(L, APLOG_NOTICE); +} +static int req_info(lua_State *L) +{ + return req_log_at(L, APLOG_INFO); +} +static int req_debug(lua_State *L) +{ + return req_log_at(L, APLOG_DEBUG); +} + +static int lua_ivm_get(lua_State *L) +{ + const char *key, *raw_key; + apr_pool_t *pool; + lua_ivm_object *object = NULL; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + raw_key = apr_pstrcat(r->pool, "lua_ivm_", key, NULL); + apr_global_mutex_lock(lua_ivm_mutex); + pool = *((apr_pool_t**) apr_shm_baseaddr_get(lua_ivm_shm)); + apr_pool_userdata_get((void **)&object, raw_key, pool); + if (object) { + if (object->type == LUA_TBOOLEAN) lua_pushboolean(L, (int) object->number); + else if (object->type == LUA_TNUMBER) lua_pushnumber(L, object->number); + else if (object->type == LUA_TSTRING) lua_pushlstring(L, object->vb.buf, object->size); + apr_global_mutex_unlock(lua_ivm_mutex); + return 1; + } + else { + apr_global_mutex_unlock(lua_ivm_mutex); + return 0; + } +} + + +static int lua_ivm_set(lua_State *L) +{ + const char *key, *raw_key; + const char *value = NULL; + apr_pool_t *pool; + size_t str_len; + lua_ivm_object *object = NULL; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + luaL_checkany(L, 3); + raw_key = apr_pstrcat(r->pool, "lua_ivm_", key, NULL); + + apr_global_mutex_lock(lua_ivm_mutex); + pool = *((apr_pool_t**) apr_shm_baseaddr_get(lua_ivm_shm)); + apr_pool_userdata_get((void **)&object, raw_key, pool); + if (!object) { + object = apr_pcalloc(pool, sizeof(lua_ivm_object)); + ap_varbuf_init(pool, &object->vb, 2); + object->size = 1; + object->vb_size = 1; + } + object->type = lua_type(L, 3); + if (object->type == LUA_TNUMBER) object->number = lua_tonumber(L, 3); + else if (object->type == LUA_TBOOLEAN) object->number = lua_tonumber(L, 3); + else if (object->type == LUA_TSTRING) { + value = lua_tolstring(L, 3, &str_len); + str_len++; /* add trailing \0 */ + if ( str_len > object->vb_size) { + ap_varbuf_grow(&object->vb, str_len); + object->vb_size = str_len; + } + object->size = str_len-1; + memset(object->vb.buf, 0, str_len); + memcpy(object->vb.buf, value, str_len-1); + } + apr_pool_userdata_set(object, raw_key, NULL, pool); + apr_global_mutex_unlock(lua_ivm_mutex); + return 0; +} + +static int lua_get_cookie(lua_State *L) +{ + const char *key, *cookie; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + cookie = NULL; + ap_cookie_read(r, key, &cookie, 0); + if (cookie != NULL) { + lua_pushstring(L, cookie); + return 1; + } + return 0; +} + +static int lua_set_cookie(lua_State *L) +{ + const char *key, *value, *out, *path = "", *domain = ""; + const char *strexpires = "", *strdomain = "", *strpath = ""; + int secure = 0, expires = 0, httponly = 0; + char cdate[APR_RFC822_DATE_LEN+1]; + apr_status_t rv; + request_rec *r = ap_lua_check_request_rec(L, 1); + + /* New >= 2.4.8 method: */ + if (lua_istable(L, 2)) { + + /* key */ + lua_pushstring(L, "key"); + lua_gettable(L, -2); + key = luaL_checkstring(L, -1); + lua_pop(L, 1); + + /* value */ + lua_pushstring(L, "value"); + lua_gettable(L, -2); + value = luaL_checkstring(L, -1); + lua_pop(L, 1); + + /* expiry */ + lua_pushstring(L, "expires"); + lua_gettable(L, -2); + expires = (int)luaL_optinteger(L, -1, 0); + lua_pop(L, 1); + + /* secure */ + lua_pushstring(L, "secure"); + lua_gettable(L, -2); + if (lua_isboolean(L, -1)) { + secure = lua_toboolean(L, -1); + } + lua_pop(L, 1); + + /* httponly */ + lua_pushstring(L, "httponly"); + lua_gettable(L, -2); + if (lua_isboolean(L, -1)) { + httponly = lua_toboolean(L, -1); + } + lua_pop(L, 1); + + /* path */ + lua_pushstring(L, "path"); + lua_gettable(L, -2); + path = luaL_optstring(L, -1, "/"); + lua_pop(L, 1); + + /* domain */ + lua_pushstring(L, "domain"); + lua_gettable(L, -2); + domain = luaL_optstring(L, -1, ""); + lua_pop(L, 1); + } + /* Old <= 2.4.7 method: */ + else { + key = luaL_checkstring(L, 2); + value = luaL_checkstring(L, 3); + secure = 0; + if (lua_isboolean(L, 4)) { + secure = lua_toboolean(L, 4); + } + expires = luaL_optinteger(L, 5, 0); + } + + /* Calculate expiry if set */ + if (expires > 0) { + rv = apr_rfc822_date(cdate, apr_time_from_sec(expires)); + if (rv == APR_SUCCESS) { + strexpires = apr_psprintf(r->pool, "Expires=%s;", cdate); + } + } + + /* Create path segment */ + if (path != NULL && strlen(path) > 0) { + strpath = apr_psprintf(r->pool, "Path=%s;", path); + } + + /* Create domain segment */ + if (domain != NULL && strlen(domain) > 0) { + /* Domain does NOT like quotes in most browsers, so let's avoid that */ + strdomain = apr_psprintf(r->pool, "Domain=%s;", domain); + } + + /* URL-encode key/value */ + value = ap_escape_urlencoded(r->pool, value); + key = ap_escape_urlencoded(r->pool, key); + + /* Create the header */ + out = apr_psprintf(r->pool, "%s=%s; %s %s %s %s %s", key, value, + secure ? "Secure;" : "", + expires ? strexpires : "", + httponly ? "HttpOnly;" : "", + *strdomain ? strdomain : "", + *strpath ? strpath : ""); + + apr_table_add(r->err_headers_out, "Set-Cookie", out); + return 0; +} + +static apr_uint64_t ap_ntoh64(const apr_uint64_t *input) +{ + apr_uint64_t rval; + unsigned char *data = (unsigned char *)&rval; + if (APR_IS_BIGENDIAN) { + return *input; + } + + data[0] = *input >> 56; + data[1] = *input >> 48; + data[2] = *input >> 40; + data[3] = *input >> 32; + data[4] = *input >> 24; + data[5] = *input >> 16; + data[6] = *input >> 8; + data[7] = *input >> 0; + + return rval; +} + +static int lua_websocket_greet(lua_State *L) +{ + const char *key = NULL; + unsigned char digest[APR_SHA1_DIGESTSIZE]; + apr_sha1_ctx_t sha1; + char *encoded; + int encoded_len; + request_rec *r = ap_lua_check_request_rec(L, 1); + key = apr_table_get(r->headers_in, "Sec-WebSocket-Key"); + if (key != NULL) { + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03011) + "Websocket: Got websocket key: %s", key); + key = apr_pstrcat(r->pool, key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", + NULL); + apr_sha1_init(&sha1); + apr_sha1_update(&sha1, key, strlen(key)); + apr_sha1_final(digest, &sha1); + encoded_len = apr_base64_encode_len(APR_SHA1_DIGESTSIZE); + if (encoded_len) { + encoded = apr_palloc(r->pool, encoded_len); + encoded_len = apr_base64_encode(encoded, (char*) digest, APR_SHA1_DIGESTSIZE); + r->status = 101; + apr_table_setn(r->headers_out, "Upgrade", "websocket"); + apr_table_setn(r->headers_out, "Connection", "Upgrade"); + apr_table_setn(r->headers_out, "Sec-WebSocket-Accept", encoded); + + /* Trick httpd into NOT using the chunked filter, IMPORTANT!!!111*/ + apr_table_setn(r->headers_out, "Transfer-Encoding", "chunked"); + + r->clength = 0; + r->bytes_sent = 0; + r->read_chunked = 0; + ap_rflush(r); + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03012) + "Websocket: Upgraded from HTTP to Websocket"); + lua_pushboolean(L, 1); + return 1; + } + } + ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, APLOGNO(02666) + "Websocket: Upgrade from HTTP to Websocket failed"); + return 0; +} + +static apr_status_t lua_websocket_readbytes(conn_rec* c, + apr_bucket_brigade *brigade, + char* buffer, apr_off_t len) +{ + apr_size_t delivered; + apr_status_t rv; + + rv = ap_get_brigade(c->input_filters, brigade, AP_MODE_READBYTES, + APR_BLOCK_READ, len); + if (rv == APR_SUCCESS) { + delivered = len; + rv = apr_brigade_flatten(brigade, buffer, &delivered); + if ((rv == APR_SUCCESS) && (delivered < len)) { + rv = APR_INCOMPLETE; + } + } + apr_brigade_cleanup(brigade); + return rv; +} + +static int lua_websocket_peek(lua_State *L) +{ + apr_status_t rv; + apr_bucket_brigade *brigade; + + request_rec *r = ap_lua_check_request_rec(L, 1); + + brigade = apr_brigade_create(r->connection->pool, + r->connection->bucket_alloc); + rv = ap_get_brigade(r->connection->input_filters, brigade, + AP_MODE_READBYTES, APR_NONBLOCK_READ, 1); + if (rv == APR_SUCCESS) { + lua_pushboolean(L, 1); + } + else { + lua_pushboolean(L, 0); + } + apr_brigade_cleanup(brigade); + return 1; +} + +static int lua_websocket_read(lua_State *L) +{ + apr_status_t rv; + int do_read = 1; + int n = 0; + apr_size_t plen = 0; + unsigned short payload_short = 0; + apr_uint64_t payload_long = 0; + unsigned char *mask_bytes; + char byte; + apr_bucket_brigade *brigade; + conn_rec* c; + + request_rec *r = ap_lua_check_request_rec(L, 1); + c = r->connection; + + mask_bytes = apr_pcalloc(r->pool, 4); + + brigade = apr_brigade_create(r->pool, c->bucket_alloc); + + while (do_read) { + do_read = 0; + /* Get opcode and FIN bit */ + rv = lua_websocket_readbytes(c, brigade, &byte, 1); + if (rv == APR_SUCCESS) { + unsigned char ubyte, fin, opcode, mask, payload; + ubyte = (unsigned char)byte; + /* fin bit is the first bit */ + fin = ubyte >> (CHAR_BIT - 1); + /* opcode is the last four bits (there's 3 reserved bits we don't care about) */ + opcode = ubyte & 0xf; + + /* Get the payload length and mask bit */ + rv = lua_websocket_readbytes(c, brigade, &byte, 1); + if (rv == APR_SUCCESS) { + ubyte = (unsigned char)byte; + /* Mask is the first bit */ + mask = ubyte >> (CHAR_BIT - 1); + /* Payload is the last 7 bits */ + payload = ubyte & 0x7f; + plen = payload; + + /* Extended payload? */ + if (payload == 126) { + rv = lua_websocket_readbytes(c, brigade, + (char*) &payload_short, 2); + + if (rv != APR_SUCCESS) { + return 0; + } + + plen = ntohs(payload_short); + } + /* Super duper extended payload? */ + if (payload == 127) { + rv = lua_websocket_readbytes(c, brigade, + (char*) &payload_long, 8); + + if (rv != APR_SUCCESS) { + return 0; + } + + plen = ap_ntoh64(&payload_long); + } + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03210) + "Websocket: Reading %" APR_SIZE_T_FMT " (%s) bytes, masking is %s. %s", + plen, + (payload >= 126) ? "extra payload" : "no extra payload", + mask ? "on" : "off", + fin ? "This is a final frame" : "more to follow"); + if (mask) { + rv = lua_websocket_readbytes(c, brigade, + (char*) mask_bytes, 4); + + if (rv != APR_SUCCESS) { + return 0; + } + } + if (plen < (HUGE_STRING_LEN*1024) && plen > 0) { + apr_size_t remaining = plen; + char *buffer = apr_palloc(r->pool, plen+1); + buffer[plen] = 0; + + rv = lua_websocket_readbytes(c, brigade, buffer, remaining); + + if (rv != APR_SUCCESS) { + return 0; + } + + ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, + "Websocket: Frame contained %" APR_SIZE_T_FMT \ + " bytes, pushed to Lua stack", remaining); + if (mask) { + for (n = 0; n < plen; n++) { + buffer[n] ^= mask_bytes[n%4]; + } + } + + lua_pushlstring(L, buffer, (size_t) plen); /* push to stack */ + lua_pushboolean(L, fin); /* push FIN bit to stack as boolean */ + return 2; + } + + /* Decide if we need to react to the opcode or not */ + if (opcode == 0x09) { /* ping */ + char frame[2]; + apr_bucket *b; + + frame[0] = 0x8A; + frame[1] = 0; + + /* Pong! */ + b = apr_bucket_transient_create(frame, 2, c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(brigade, b); + + rv = ap_pass_brigade(c->output_filters, brigade); + apr_brigade_cleanup(brigade); + + if (rv != APR_SUCCESS) { + return 0; + } + + do_read = 1; + } + } + } + } + return 0; +} + + +static int lua_websocket_write(lua_State *L) +{ + const char *string; + apr_status_t rv; + size_t len; + int raw = 0; + char prelude; + request_rec *r = ap_lua_check_request_rec(L, 1); + + if (lua_isboolean(L, 3)) { + raw = lua_toboolean(L, 3); + } + string = lua_tolstring(L, 2, &len); + + if (raw != 1) { + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03013) + "Websocket: Writing framed message to client"); + + prelude = 0x81; /* text frame, FIN */ + ap_rputc(prelude, r); + if (len < 126) { + ap_rputc(len, r); + } + else if (len < 65535) { + apr_uint16_t slen = len; + ap_rputc(126, r); + slen = htons(slen); + ap_rwrite((char*) &slen, 2, r); + } + else { + apr_uint64_t llen = len; + ap_rputc(127, r); + llen = ap_ntoh64(&llen); /* ntoh doubles as hton */ + ap_rwrite((char*) &llen, 8, r); + } + } + else { + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03014) + "Websocket: Writing raw message to client"); + } + ap_rwrite(string, len, r); + rv = ap_rflush(r); + if (rv == APR_SUCCESS) { + lua_pushboolean(L, 1); + } + else { + lua_pushboolean(L, 0); + } + return 1; +} + + +static int lua_websocket_close(lua_State *L) +{ + apr_socket_t *sock; + char prelude[2]; + request_rec *r = ap_lua_check_request_rec(L, 1); + + sock = ap_get_conn_socket(r->connection); + + /* Send a header that says: socket is closing. */ + prelude[0] = 0x88; /* closing socket opcode */ + prelude[1] = 0; /* zero length frame */ + ap_rwrite(prelude, 2, r); + + /* Close up tell the MPM and filters to back off */ + apr_socket_close(sock); + r->output_filters = NULL; + r->connection->keepalive = AP_CONN_CLOSE; + return 0; +} + +static int lua_websocket_ping(lua_State *L) +{ + apr_socket_t *sock; + apr_size_t plen; + char prelude[2]; + apr_status_t rv; + request_rec *r = ap_lua_check_request_rec(L, 1); + sock = ap_get_conn_socket(r->connection); + + /* Send a header that says: PING. */ + prelude[0] = 0x89; /* ping opcode */ + prelude[1] = 0; + plen = 2; + apr_socket_send(sock, prelude, &plen); + + + /* Get opcode and FIN bit from pong */ + plen = 2; + rv = apr_socket_recv(sock, prelude, &plen); + if (rv == APR_SUCCESS) { + unsigned char opcode = prelude[0]; + unsigned char len = prelude[1]; + unsigned char mask = len >> 7; + if (mask) len -= 128; + plen = len; + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03015) + "Websocket: Got PONG opcode: %x", opcode); + if (opcode == 0x8A) { + lua_pushboolean(L, 1); + } + else { + lua_pushboolean(L, 0); + } + if (plen > 0) { + ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, + "Websocket: Reading %" APR_SIZE_T_FMT " bytes of PONG", plen); + return 1; + } + if (mask) { + plen = 2; + apr_socket_recv(sock, prelude, &plen); + plen = 2; + apr_socket_recv(sock, prelude, &plen); + } + } + else { + lua_pushboolean(L, 0); + } + return 1; +} + + +#define APLUA_REQ_TRACE(lev) static int req_trace##lev(lua_State *L) \ +{ \ + return req_log_at(L, APLOG_TRACE##lev); \ +} + +APLUA_REQ_TRACE(1) +APLUA_REQ_TRACE(2) +APLUA_REQ_TRACE(3) +APLUA_REQ_TRACE(4) +APLUA_REQ_TRACE(5) +APLUA_REQ_TRACE(6) +APLUA_REQ_TRACE(7) +APLUA_REQ_TRACE(8) + +/* handle r.status = 201 */ +static int req_newindex(lua_State *L) +{ + const char *key; + /* request_rec* r = lua_touserdata(L, lua_upvalueindex(1)); */ + /* const char* key = luaL_checkstring(L, -2); */ + request_rec *r = ap_lua_check_request_rec(L, 1); + key = luaL_checkstring(L, 2); + + if (0 == strcmp("args", key)) { + const char *value = luaL_checkstring(L, 3); + r->args = apr_pstrdup(r->pool, value); + return 0; + } + + if (0 == strcmp("content_type", key)) { + const char *value = luaL_checkstring(L, 3); + ap_set_content_type(r, apr_pstrdup(r->pool, value)); + return 0; + } + + if (0 == strcmp("filename", key)) { + const char *value = luaL_checkstring(L, 3); + r->filename = apr_pstrdup(r->pool, value); + return 0; + } + + if (0 == strcmp("handler", key)) { + const char *value = luaL_checkstring(L, 3); + r->handler = apr_pstrdup(r->pool, value); + return 0; + } + + if (0 == strcmp("proxyreq", key)) { + int value = luaL_checkinteger(L, 3); + r->proxyreq = value; + return 0; + } + + if (0 == strcmp("status", key)) { + int code = luaL_checkinteger(L, 3); + r->status = code; + return 0; + } + + if (0 == strcmp("uri", key)) { + const char *value = luaL_checkstring(L, 3); + r->uri = apr_pstrdup(r->pool, value); + return 0; + } + + if (0 == strcmp("user", key)) { + const char *value = luaL_checkstring(L, 3); + r->user = apr_pstrdup(r->pool, value); + 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; +} + + + +/* helper function for walking config trees */ +static void read_cfg_tree(lua_State *L, request_rec *r, ap_directive_t *rcfg) { + int x = 0; + const char* value; + ap_directive_t *cfg; + lua_newtable(L); + + for (cfg = rcfg; cfg; cfg = cfg->next) { + x++; + lua_pushnumber(L, x); + lua_newtable(L); + value = apr_psprintf(r->pool, "%s %s", cfg->directive, cfg->args); + lua_pushstring(L, "directive"); + lua_pushstring(L, value); + lua_settable(L, -3); + lua_pushstring(L, "file"); + lua_pushstring(L, cfg->filename); + lua_settable(L, -3); + lua_pushstring(L, "line"); + lua_pushnumber(L, cfg->line_num); + lua_settable(L, -3); + if (cfg->first_child) { + lua_pushstring(L, "children"); + read_cfg_tree(L, r, cfg->first_child); + lua_settable(L, -3); + } + lua_settable(L, -3); + } +} + +static int lua_ap_get_config(lua_State *L) { + request_rec *r = ap_lua_check_request_rec(L, 1); + read_cfg_tree(L, r, ap_conftree); + + return 1; +} + + +/* Hack, hack, hack...! TODO: Make this actually work properly */ +static int lua_ap_get_active_config(lua_State *L) { + ap_directive_t *subdir; + ap_directive_t *dir = ap_conftree; + request_rec *r = ap_lua_check_request_rec(L, 1); + + for (dir = ap_conftree; dir; dir = dir->next) { + if (ap_strcasestr(dir->directive, "<virtualhost") && dir->first_child) { + for (subdir = dir->first_child; subdir; subdir = subdir->next) { + if (ap_strcasecmp_match(subdir->directive, "servername") && + !ap_strcasecmp_match(r->hostname, subdir->args)) { + read_cfg_tree(L, r, dir->first_child); + return 1; + } + if (ap_strcasecmp_match(subdir->directive, "serveralias") && + !ap_strcasecmp_match(r->hostname, subdir->args)) { + read_cfg_tree(L, r, dir->first_child); + return 1; + } + } + } + } + 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 char* lua_ap_auth_name(request_rec* r) +{ + const char *name; + name = ap_auth_name(r); + return name ? name : ""; +} + +static const char* lua_ap_get_server_name(request_rec* r) +{ + const char *name; + name = ap_get_server_name(r); + return name ? name : "localhost"; +} + + + + +static const struct luaL_Reg server_methods[] = { + {NULL, NULL} +}; + + +static req_fun_t *makefun(const 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 ap_lua_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, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "write", APR_HASH_KEY_STRING, + makefun(&req_write, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "document_root", APR_HASH_KEY_STRING, + makefun(&req_document_root, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "context_prefix", APR_HASH_KEY_STRING, + makefun(&req_context_prefix, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "context_document_root", APR_HASH_KEY_STRING, + makefun(&req_context_document_root, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "parseargs", APR_HASH_KEY_STRING, + makefun(&req_parseargs, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "parsebody", APR_HASH_KEY_STRING, + makefun(&req_parsebody, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "debug", APR_HASH_KEY_STRING, + makefun(&req_debug, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "info", APR_HASH_KEY_STRING, + makefun(&req_info, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "notice", APR_HASH_KEY_STRING, + makefun(&req_notice, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "warn", APR_HASH_KEY_STRING, + makefun(&req_warn, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "err", APR_HASH_KEY_STRING, + makefun(&req_err, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "crit", APR_HASH_KEY_STRING, + makefun(&req_crit, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "alert", APR_HASH_KEY_STRING, + makefun(&req_alert, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "emerg", APR_HASH_KEY_STRING, + makefun(&req_emerg, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace1", APR_HASH_KEY_STRING, + makefun(&req_trace1, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace2", APR_HASH_KEY_STRING, + makefun(&req_trace2, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace3", APR_HASH_KEY_STRING, + makefun(&req_trace3, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace4", APR_HASH_KEY_STRING, + makefun(&req_trace4, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace5", APR_HASH_KEY_STRING, + makefun(&req_trace5, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace6", APR_HASH_KEY_STRING, + makefun(&req_trace6, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace7", APR_HASH_KEY_STRING, + makefun(&req_trace7, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "trace8", APR_HASH_KEY_STRING, + makefun(&req_trace8, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "add_output_filter", APR_HASH_KEY_STRING, + makefun(&req_add_output_filter, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "construct_url", APR_HASH_KEY_STRING, + makefun(&req_construct_url, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "escape_html", APR_HASH_KEY_STRING, + makefun(&req_escape_html, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "ssl_var_lookup", APR_HASH_KEY_STRING, + makefun(&req_ssl_var_lookup, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "is_https", APR_HASH_KEY_STRING, + makefun(&req_ssl_is_https_field, APL_REQ_FUNTYPE_BOOLEAN, p)); + apr_hash_set(dispatch, "assbackwards", APR_HASH_KEY_STRING, + makefun(&req_assbackwards_field, APL_REQ_FUNTYPE_BOOLEAN, p)); + apr_hash_set(dispatch, "status", APR_HASH_KEY_STRING, + makefun(&req_status_field, APL_REQ_FUNTYPE_INT, p)); + apr_hash_set(dispatch, "protocol", APR_HASH_KEY_STRING, + makefun(&req_protocol_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "range", APR_HASH_KEY_STRING, + makefun(&req_range_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "content_type", APR_HASH_KEY_STRING, + makefun(&req_content_type_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "content_encoding", APR_HASH_KEY_STRING, + makefun(&req_content_encoding_field, APL_REQ_FUNTYPE_STRING, + p)); + apr_hash_set(dispatch, "ap_auth_type", APR_HASH_KEY_STRING, + makefun(&req_ap_auth_type_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "unparsed_uri", APR_HASH_KEY_STRING, + makefun(&req_unparsed_uri_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "user", APR_HASH_KEY_STRING, + makefun(&req_user_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "filename", APR_HASH_KEY_STRING, + makefun(&req_filename_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "canonical_filename", APR_HASH_KEY_STRING, + makefun(&req_canonical_filename_field, + APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "path_info", APR_HASH_KEY_STRING, + makefun(&req_path_info_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "args", APR_HASH_KEY_STRING, + makefun(&req_args_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "handler", APR_HASH_KEY_STRING, + makefun(&req_handler_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "hostname", APR_HASH_KEY_STRING, + makefun(&req_hostname_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "uri", APR_HASH_KEY_STRING, + makefun(&req_uri_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "the_request", APR_HASH_KEY_STRING, + makefun(&req_the_request_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "log_id", APR_HASH_KEY_STRING, + makefun(&req_log_id_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "useragent_ip", APR_HASH_KEY_STRING, + makefun(&req_useragent_ip_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "method", APR_HASH_KEY_STRING, + makefun(&req_method_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "proxyreq", APR_HASH_KEY_STRING, + makefun(&req_proxyreq_field, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "headers_in", APR_HASH_KEY_STRING, + makefun(&req_headers_in, APL_REQ_FUNTYPE_TABLE, p)); + apr_hash_set(dispatch, "headers_in_table", APR_HASH_KEY_STRING, + makefun(&req_headers_in_table, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "headers_out", APR_HASH_KEY_STRING, + makefun(&req_headers_out, APL_REQ_FUNTYPE_TABLE, p)); + apr_hash_set(dispatch, "headers_out_table", APR_HASH_KEY_STRING, + makefun(&req_headers_out_table, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "err_headers_out", APR_HASH_KEY_STRING, + makefun(&req_err_headers_out, APL_REQ_FUNTYPE_TABLE, p)); + apr_hash_set(dispatch, "err_headers_out_table", APR_HASH_KEY_STRING, + makefun(&req_err_headers_out_table, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "notes", APR_HASH_KEY_STRING, + makefun(&req_notes, APL_REQ_FUNTYPE_TABLE, p)); + apr_hash_set(dispatch, "notes_table", APR_HASH_KEY_STRING, + makefun(&req_notes_table, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "subprocess_env", APR_HASH_KEY_STRING, + makefun(&req_subprocess_env, APL_REQ_FUNTYPE_TABLE, p)); + apr_hash_set(dispatch, "subprocess_env_table", APR_HASH_KEY_STRING, + makefun(&req_subprocess_env_table, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "flush", APR_HASH_KEY_STRING, + makefun(&lua_ap_rflush, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "port", APR_HASH_KEY_STRING, + makefun(&req_ap_get_server_port, APL_REQ_FUNTYPE_INT, p)); + apr_hash_set(dispatch, "banner", APR_HASH_KEY_STRING, + makefun(&ap_get_server_banner, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "options", APR_HASH_KEY_STRING, + makefun(&lua_ap_options, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "allowoverrides", APR_HASH_KEY_STRING, + makefun(&lua_ap_allowoverrides, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "started", APR_HASH_KEY_STRING, + makefun(&lua_ap_started, APL_REQ_FUNTYPE_INT, p)); + apr_hash_set(dispatch, "basic_auth_pw", APR_HASH_KEY_STRING, + makefun(&lua_ap_basic_auth_pw, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "limit_req_body", APR_HASH_KEY_STRING, + makefun(&lua_ap_limit_req_body, APL_REQ_FUNTYPE_INT, p)); + apr_hash_set(dispatch, "server_built", APR_HASH_KEY_STRING, + makefun(&ap_get_server_built, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "is_initial_req", APR_HASH_KEY_STRING, + makefun(&lua_ap_is_initial_req, APL_REQ_FUNTYPE_BOOLEAN, p)); + apr_hash_set(dispatch, "remaining", APR_HASH_KEY_STRING, + makefun(&req_remaining_field, APL_REQ_FUNTYPE_INT, p)); + apr_hash_set(dispatch, "some_auth_required", APR_HASH_KEY_STRING, + makefun(&lua_ap_some_auth_required, APL_REQ_FUNTYPE_BOOLEAN, p)); + apr_hash_set(dispatch, "server_name", APR_HASH_KEY_STRING, + makefun(&lua_ap_get_server_name, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "auth_name", APR_HASH_KEY_STRING, + makefun(&lua_ap_auth_name, APL_REQ_FUNTYPE_STRING, p)); + apr_hash_set(dispatch, "sendfile", APR_HASH_KEY_STRING, + makefun(&lua_ap_sendfile, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "dbacquire", APR_HASH_KEY_STRING, + makefun(&lua_db_acquire, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "stat", APR_HASH_KEY_STRING, + makefun(&lua_ap_stat, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "get_direntries", APR_HASH_KEY_STRING, + makefun(&lua_ap_getdir, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "regex", APR_HASH_KEY_STRING, + makefun(&lua_ap_regex, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "usleep", APR_HASH_KEY_STRING, + makefun(&lua_ap_usleep, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "base64_encode", APR_HASH_KEY_STRING, + makefun(&lua_apr_b64encode, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "base64_decode", APR_HASH_KEY_STRING, + makefun(&lua_apr_b64decode, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "md5", APR_HASH_KEY_STRING, + makefun(&lua_apr_md5, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "sha1", APR_HASH_KEY_STRING, + makefun(&lua_apr_sha1, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "htpassword", APR_HASH_KEY_STRING, + makefun(&lua_apr_htpassword, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "touch", APR_HASH_KEY_STRING, + makefun(&lua_apr_touch, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "mkdir", APR_HASH_KEY_STRING, + makefun(&lua_apr_mkdir, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "mkrdir", APR_HASH_KEY_STRING, + makefun(&lua_apr_mkrdir, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "rmdir", APR_HASH_KEY_STRING, + makefun(&lua_apr_rmdir, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "date_parse_rfc", APR_HASH_KEY_STRING, + makefun(&lua_apr_date_parse_rfc, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "escape", APR_HASH_KEY_STRING, + makefun(&lua_ap_escape, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "unescape", APR_HASH_KEY_STRING, + makefun(&lua_ap_unescape, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "mpm_query", APR_HASH_KEY_STRING, + makefun(&lua_ap_mpm_query, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "expr", APR_HASH_KEY_STRING, + makefun(&lua_ap_expr, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "scoreboard_process", APR_HASH_KEY_STRING, + makefun(&lua_ap_scoreboard_process, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "scoreboard_worker", APR_HASH_KEY_STRING, + makefun(&lua_ap_scoreboard_worker, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "clock", APR_HASH_KEY_STRING, + makefun(&lua_ap_clock, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "requestbody", APR_HASH_KEY_STRING, + makefun(&lua_ap_requestbody, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "add_input_filter", APR_HASH_KEY_STRING, + makefun(&lua_ap_add_input_filter, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "module_info", APR_HASH_KEY_STRING, + makefun(&lua_ap_module_info, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "loaded_modules", APR_HASH_KEY_STRING, + makefun(&lua_ap_loaded_modules, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "runtime_dir_relative", APR_HASH_KEY_STRING, + makefun(&lua_ap_runtime_dir_relative, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "server_info", APR_HASH_KEY_STRING, + makefun(&lua_ap_server_info, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "set_document_root", APR_HASH_KEY_STRING, + makefun(&lua_ap_set_document_root, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "set_context_info", APR_HASH_KEY_STRING, + makefun(&lua_ap_set_context_info, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "os_escape_path", APR_HASH_KEY_STRING, + makefun(&lua_ap_os_escape_path, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "escape_logitem", APR_HASH_KEY_STRING, + makefun(&lua_ap_escape_logitem, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "strcmp_match", APR_HASH_KEY_STRING, + makefun(&lua_ap_strcmp_match, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "set_keepalive", APR_HASH_KEY_STRING, + makefun(&lua_ap_set_keepalive, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "make_etag", APR_HASH_KEY_STRING, + makefun(&lua_ap_make_etag, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "send_interim_response", APR_HASH_KEY_STRING, + makefun(&lua_ap_send_interim_response, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "custom_response", APR_HASH_KEY_STRING, + makefun(&lua_ap_custom_response, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "exists_config_define", APR_HASH_KEY_STRING, + makefun(&lua_ap_exists_config_define, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "state_query", APR_HASH_KEY_STRING, + makefun(&lua_ap_state_query, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "get_server_name_for_url", APR_HASH_KEY_STRING, + makefun(&lua_ap_get_server_name_for_url, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "ivm_get", APR_HASH_KEY_STRING, + makefun(&lua_ivm_get, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "ivm_set", APR_HASH_KEY_STRING, + makefun(&lua_ivm_set, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "getcookie", APR_HASH_KEY_STRING, + makefun(&lua_get_cookie, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "setcookie", APR_HASH_KEY_STRING, + makefun(&lua_set_cookie, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wsupgrade", APR_HASH_KEY_STRING, + makefun(&lua_websocket_greet, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wsread", APR_HASH_KEY_STRING, + makefun(&lua_websocket_read, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wspeek", APR_HASH_KEY_STRING, + makefun(&lua_websocket_peek, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wswrite", APR_HASH_KEY_STRING, + makefun(&lua_websocket_write, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wsclose", APR_HASH_KEY_STRING, + makefun(&lua_websocket_close, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "wsping", APR_HASH_KEY_STRING, + makefun(&lua_websocket_ping, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "config", APR_HASH_KEY_STRING, + makefun(&lua_ap_get_config, APL_REQ_FUNTYPE_LUACFUN, p)); + apr_hash_set(dispatch, "activeconfig", APR_HASH_KEY_STRING, + makefun(&lua_ap_get_active_config, APL_REQ_FUNTYPE_LUACFUN, 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_setfuncs_compat(L, request_methods); /* [metatable] */ + + lua_pop(L, 2); + + luaL_newmetatable(L, "Apache2.Connection"); /* [metatable] */ + lua_pushvalue(L, -1); + + lua_setfield(L, -2, "__index"); + luaL_setfuncs_compat(L, connection_methods); /* [metatable] */ + + lua_pop(L, 2); + + luaL_newmetatable(L, "Apache2.Server"); /* [metatable] */ + lua_pushvalue(L, -1); + + lua_setfield(L, -2, "__index"); + luaL_setfuncs_compat(L, server_methods); /* [metatable] */ + + lua_pop(L, 2); + +} + +void ap_lua_push_connection(lua_State *L, conn_rec *c) +{ + req_table_t* t; + lua_boxpointer(L, c); + luaL_getmetatable(L, "Apache2.Connection"); + lua_setmetatable(L, -2); + luaL_getmetatable(L, "Apache2.Connection"); + + t = apr_pcalloc(c->pool, sizeof(req_table_t)); + t->t = c->notes; + t->r = NULL; + t->n = "notes"; + ap_lua_push_apr_table(L, t); + lua_setfield(L, -2, "notes"); + + lua_pushstring(L, c->client_ip); + lua_setfield(L, -2, "client_ip"); + + lua_pop(L, 1); +} + + +void ap_lua_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 ap_lua_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/lua/lua_request.h b/modules/lua/lua_request.h new file mode 100644 index 0000000..a39f4b9 --- /dev/null +++ b/modules/lua/lua_request.h @@ -0,0 +1,58 @@ +/** + * 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 _LUA_REQUEST_H_ +#define _LUA_REQUEST_H_ + +#include "mod_lua.h" +#include "util_varbuf.h" + +void ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p); +void ap_lua_push_connection(lua_State *L, conn_rec *r); +void ap_lua_push_server(lua_State *L, server_rec *r); +void ap_lua_push_request(lua_State *L, request_rec *r); + +#define APL_REQ_FUNTYPE_STRING 1 +#define APL_REQ_FUNTYPE_INT 2 +#define APL_REQ_FUNTYPE_TABLE 3 +#define APL_REQ_FUNTYPE_LUACFUN 4 +#define APL_REQ_FUNTYPE_BOOLEAN 5 + +typedef struct +{ + const void *fun; + int type; +} req_fun_t; + + +/* Struct to use as userdata for request_rec tables */ +typedef struct +{ + request_rec *r; /* Request_rec */ + apr_table_t *t; /* apr_table_t* */ + const char *n; /* name of table */ +} req_table_t; + +typedef struct { + int type; + size_t size; + size_t vb_size; + lua_Number number; + struct ap_varbuf vb; +} lua_ivm_object; + +#endif /* !_LUA_REQUEST_H_ */ diff --git a/modules/lua/lua_vmprep.c b/modules/lua/lua_vmprep.c new file mode 100644 index 0000000..001897a --- /dev/null +++ b/modules/lua/lua_vmprep.c @@ -0,0 +1,552 @@ +/** + * 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_lua.h" +#include "http_log.h" +#include "apr_uuid.h" +#include "lua_config.h" +#include "apr_file_info.h" +#include "mod_auth.h" + +APLOG_USE_MODULE(lua); + +#ifndef AP_LUA_MODULE_EXT +#if defined(NETWARE) +#define AP_LUA_MODULE_EXT ".nlm" +#elif defined(WIN32) +#define AP_LUA_MODULE_EXT ".dll" +#elif (defined(__hpux__) || defined(__hpux)) && !defined(__ia64) +#define AP_LUA_MODULE_EXT ".sl" +#else +#define AP_LUA_MODULE_EXT ".so" +#endif +#endif + +#if APR_HAS_THREADS + apr_thread_mutex_t *ap_lua_mutex; +#endif +extern apr_global_mutex_t *lua_ivm_mutex; + +void ap_lua_init_mutex(apr_pool_t *pool, server_rec *s) +{ + apr_status_t rv; + + /* global IVM mutex */ + rv = apr_global_mutex_child_init(&lua_ivm_mutex, + apr_global_mutex_lockfile(lua_ivm_mutex), + pool); + if (rv != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(03016) + "mod_lua: Failed to reopen mutex lua-ivm-shm in child"); + exit(1); /* bah :( */ + } + + /* Server pool mutex */ +#if APR_HAS_THREADS + apr_thread_mutex_create(&ap_lua_mutex, APR_THREAD_MUTEX_DEFAULT, pool); +#endif +} + +/* forward dec'l from this file */ + +#if 0 +static void pstack_dump(lua_State *L, apr_pool_t *r, int level, + const char *msg) +{ + int i; + int top = lua_gettop(L); + + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03211) + "Lua Stack Dump: [%s]", msg); + + 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, APLOGNO(03212) + "%d: '%s'", i, lua_tostring(L, i)); + break; + } + case LUA_TUSERDATA:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03213) + "%d: userdata", i); + break; + } + case LUA_TLIGHTUSERDATA:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03214) + "%d: lightuserdata", i); + break; + } + case LUA_TNIL:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03215) + "%d: NIL", i); + break; + } + case LUA_TNONE:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03216) + "%d: None", i); + break; + } + case LUA_TBOOLEAN:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03217) + "%d: %s", + i, lua_toboolean(L, i) ? "true" : "false"); + break; + } + case LUA_TNUMBER:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03218) + "%d: %g", i, lua_tonumber(L, i)); + break; + } + case LUA_TTABLE:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03219) + "%d: <table>", i); + break; + } + case LUA_TTHREAD:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03220) + "%d: <thread>", i); + break; + } + case LUA_TFUNCTION:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03221) + "%d: <function>", i); + break; + } + default:{ + ap_log_perror(APLOG_MARK, level, 0, r, APLOGNO(03222) + "%d: unknown: [%s]", i, lua_typename(L, i)); + break; + } + } + } +} +#endif + +/* BEGIN modules*/ + +/* BEGIN apache lmodule */ + +#define makeintegerfield(L, n) lua_pushinteger(L, n); lua_setfield(L, -2, #n) + +void ap_lua_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_pushstring(L, ap_get_server_banner()); + lua_setfield(L, -2, "version"); + + makeintegerfield(L, OK); + makeintegerfield(L, DECLINED); + makeintegerfield(L, DONE); + makeintegerfield(L, HTTP_MOVED_TEMPORARILY); + makeintegerfield(L, PROXYREQ_NONE); + makeintegerfield(L, PROXYREQ_PROXY); + makeintegerfield(L, PROXYREQ_REVERSE); + makeintegerfield(L, PROXYREQ_RESPONSE); + makeintegerfield(L, PROXYREQ_RESPONSE); + makeintegerfield(L, AUTHZ_DENIED); + makeintegerfield(L, AUTHZ_GRANTED); + makeintegerfield(L, AUTHZ_NEUTRAL); + makeintegerfield(L, AUTHZ_GENERAL_ERROR); + makeintegerfield(L, AUTHZ_DENIED_NO_USER); + + /* + makeintegerfield(L, HTTP_CONTINUE); + makeintegerfield(L, HTTP_SWITCHING_PROTOCOLS); + makeintegerfield(L, HTTP_PROCESSING); + makeintegerfield(L, HTTP_OK); + makeintegerfield(L, HTTP_CREATED); + makeintegerfield(L, HTTP_ACCEPTED); + makeintegerfield(L, HTTP_NON_AUTHORITATIVE); + makeintegerfield(L, HTTP_NO_CONTENT); + makeintegerfield(L, HTTP_RESET_CONTENT); + makeintegerfield(L, HTTP_PARTIAL_CONTENT); + makeintegerfield(L, HTTP_MULTI_STATUS); + makeintegerfield(L, HTTP_ALREADY_REPORTED); + makeintegerfield(L, HTTP_IM_USED); + makeintegerfield(L, HTTP_MULTIPLE_CHOICES); + makeintegerfield(L, HTTP_MOVED_PERMANENTLY); + makeintegerfield(L, HTTP_MOVED_TEMPORARILY); + makeintegerfield(L, HTTP_SEE_OTHER); + makeintegerfield(L, HTTP_NOT_MODIFIED); + makeintegerfield(L, HTTP_USE_PROXY); + makeintegerfield(L, HTTP_TEMPORARY_REDIRECT); + makeintegerfield(L, HTTP_PERMANENT_REDIRECT); + makeintegerfield(L, HTTP_BAD_REQUEST); + makeintegerfield(L, HTTP_UNAUTHORIZED); + makeintegerfield(L, HTTP_PAYMENT_REQUIRED); + makeintegerfield(L, HTTP_FORBIDDEN); + makeintegerfield(L, HTTP_NOT_FOUND); + makeintegerfield(L, HTTP_METHOD_NOT_ALLOWED); + makeintegerfield(L, HTTP_NOT_ACCEPTABLE); + makeintegerfield(L, HTTP_PROXY_AUTHENTICATION_REQUIRED); + makeintegerfield(L, HTTP_REQUEST_TIME_OUT); + makeintegerfield(L, HTTP_CONFLICT); + makeintegerfield(L, HTTP_GONE); + makeintegerfield(L, HTTP_LENGTH_REQUIRED); + makeintegerfield(L, HTTP_PRECONDITION_FAILED); + makeintegerfield(L, HTTP_REQUEST_ENTITY_TOO_LARGE); + makeintegerfield(L, HTTP_REQUEST_URI_TOO_LARGE); + makeintegerfield(L, HTTP_UNSUPPORTED_MEDIA_TYPE); + makeintegerfield(L, HTTP_RANGE_NOT_SATISFIABLE); + makeintegerfield(L, HTTP_EXPECTATION_FAILED); + makeintegerfield(L, HTTP_UNPROCESSABLE_ENTITY); + makeintegerfield(L, HTTP_LOCKED); + makeintegerfield(L, HTTP_FAILED_DEPENDENCY); + makeintegerfield(L, HTTP_UPGRADE_REQUIRED); + makeintegerfield(L, HTTP_PRECONDITION_REQUIRED); + makeintegerfield(L, HTTP_TOO_MANY_REQUESTS); + makeintegerfield(L, HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE); + makeintegerfield(L, HTTP_INTERNAL_SERVER_ERROR); + makeintegerfield(L, HTTP_NOT_IMPLEMENTED); + makeintegerfield(L, HTTP_BAD_GATEWAY); + makeintegerfield(L, HTTP_SERVICE_UNAVAILABLE); + makeintegerfield(L, HTTP_GATEWAY_TIME_OUT); + makeintegerfield(L, HTTP_VERSION_NOT_SUPPORTED); + makeintegerfield(L, HTTP_VARIANT_ALSO_VARIES); + makeintegerfield(L, HTTP_INSUFFICIENT_STORAGE); + makeintegerfield(L, HTTP_LOOP_DETECTED); + makeintegerfield(L, HTTP_NOT_EXTENDED); + makeintegerfield(L, HTTP_NETWORK_AUTHENTICATION_REQUIRED); + */ +} + +/* 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) +{ + AP_DEBUG_ASSERT(l != NULL); + lua_close((lua_State *) l); + return APR_SUCCESS; +} + +static apr_status_t server_cleanup_lua(void *resource, void *params, apr_pool_t *pool) +{ + ap_lua_server_spec* spec = (ap_lua_server_spec*) resource; + AP_DEBUG_ASSERT(spec != NULL); + if (spec->L != NULL) { + lua_close((lua_State *) spec->L); + } + return APR_SUCCESS; +} + +/* + munge_path(L, + "path", + "?.lua", + "./?.lua", + lifecycle_pool, + spec->package_paths, + spec->file); +*/ +/** + * field -> "path" or "cpath" + * sub_pat -> "?.lua" + * rep_pat -> "./?.lua" + * pool -> lifecycle pool for allocations + * paths -> things to add + * file -> ??? + */ +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) +{ + const char *current; + const char *parent_dir; + const char *pattern; + const char *modified; + char *part; + + lua_getglobal(L, "package"); + lua_getfield(L, -1, field); + + current = lua_tostring(L, -1); + + parent_dir = ap_make_dirstr_parent(pool, file); + + 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); + modified = lua_tostring(L, -1); + + + lua_pop(L, 2); + + part = apr_pstrcat(pool, modified, ";", apr_array_pstrcat(pool, paths, ';'), + NULL); + + lua_pushstring(L, part); + lua_setfield(L, -2, field); + lua_pop(L, 1); /* pop "package" off the stack */ +} + +#ifdef AP_ENABLE_LUAJIT +static int loadjitmodule(lua_State *L, apr_pool_t *lifecycle_pool) +{ + lua_getglobal(L, "require"); + lua_pushliteral(L, "jit."); + lua_pushvalue(L, -3); + lua_concat(L, 2); + if (lua_pcall(L, 1, 1, 0)) { + const char *msg = lua_tostring(L, -1); + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01480) + "Failed to init LuaJIT: %s", msg); + return 1; + } + lua_getfield(L, -1, "start"); + lua_remove(L, -2); /* drop module table */ + return 0; +} + +#endif + +static apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifecycle_pool) +{ + lua_State* L; + + ap_lua_vm_spec *spec = params; + + L = luaL_newstate(); +#ifdef AP_ENABLE_LUAJIT + luaopen_jit(L); +#endif + luaL_openlibs(L); + if (spec->package_paths) { + munge_path(L, + "path", "?.lua", "./?.lua", + lifecycle_pool, + spec->package_paths, + spec->file); + } + if (spec->package_cpaths) { + munge_path(L, + "cpath", "?" AP_LUA_MODULE_EXT, "./?" AP_LUA_MODULE_EXT, + lifecycle_pool, + spec->package_cpaths, + spec->file); + } + + if (spec->cb) { + spec->cb(L, lifecycle_pool, spec->cb_arg); + } + + + if (spec->bytecode && spec->bytecode_len > 0) { + luaL_loadbuffer(L, spec->bytecode, spec->bytecode_len, spec->file); + lua_pcall(L, 0, LUA_MULTRET, 0); + } + else { + int rc; + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01481) + "loading lua file %s", spec->file); + rc = luaL_loadfile(L, spec->file); + if (rc != 0) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(01482) + "Error loading %s: %s", spec->file, + rc == LUA_ERRMEM ? "memory allocation error" + : lua_tostring(L, 0)); + return APR_EBADF; + } + if ( lua_pcall(L, 0, LUA_MULTRET, 0) == LUA_ERRRUN ) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(02613) + "Error loading %s: %s", spec->file, + lua_tostring(L, -1)); + return APR_EBADF; + } + } + +#ifdef AP_ENABLE_LUAJIT + loadjitmodule(L, lifecycle_pool); +#endif + lua_pushlightuserdata(L, lifecycle_pool); + lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool"); + *vm = L; + + return APR_SUCCESS; +} + +static ap_lua_vm_spec* copy_vm_spec(apr_pool_t* pool, ap_lua_vm_spec* spec) +{ + ap_lua_vm_spec* copied_spec = apr_pcalloc(pool, sizeof(ap_lua_vm_spec)); + copied_spec->bytecode_len = spec->bytecode_len; + copied_spec->bytecode = apr_pstrdup(pool, spec->bytecode); + copied_spec->cb = spec->cb; + copied_spec->cb_arg = NULL; + copied_spec->file = apr_pstrdup(pool, spec->file); + copied_spec->package_cpaths = apr_array_copy(pool, spec->package_cpaths); + copied_spec->package_paths = apr_array_copy(pool, spec->package_paths); + copied_spec->pool = pool; + copied_spec->scope = AP_LUA_SCOPE_SERVER; + copied_spec->codecache = spec->codecache; + return copied_spec; +} + +static apr_status_t server_vm_construct(lua_State **resource, void *params, apr_pool_t *pool) +{ + lua_State* L; + ap_lua_server_spec* spec = apr_pcalloc(pool, sizeof(ap_lua_server_spec)); + *resource = NULL; + if (vm_construct(&L, params, pool) == APR_SUCCESS) { + spec->finfo = apr_pcalloc(pool, sizeof(ap_lua_finfo)); + if (L != NULL) { + spec->L = L; + *resource = (void*) spec; + lua_pushlightuserdata(L, spec); + lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec"); + return APR_SUCCESS; + } + } + return APR_EGENERAL; +} + +/** + * Function used to create a lua_State instance bound into the web + * server in the appropriate scope. + */ +lua_State *ap_lua_get_lua_state(apr_pool_t *lifecycle_pool, + ap_lua_vm_spec *spec, request_rec* r) +{ + lua_State *L = NULL; + ap_lua_finfo *cache_info = NULL; + int tryCache = 0; + + if (spec->scope == AP_LUA_SCOPE_SERVER) { + char *hash; + apr_reslist_t* reslist = NULL; + ap_lua_server_spec* sspec = NULL; + hash = apr_psprintf(r->pool, "reslist:%s", spec->file); +#if APR_HAS_THREADS + apr_thread_mutex_lock(ap_lua_mutex); +#endif + if (apr_pool_userdata_get((void **)&reslist, hash, + r->server->process->pool) == APR_SUCCESS) { + if (reslist != NULL) { + if (apr_reslist_acquire(reslist, (void**) &sspec) == APR_SUCCESS) { + L = sspec->L; + cache_info = sspec->finfo; + } + } + } + if (L == NULL) { + ap_lua_vm_spec* server_spec = copy_vm_spec(r->server->process->pool, spec); + if ( + apr_reslist_create(&reslist, spec->vm_min, spec->vm_max, spec->vm_max, 0, + (apr_reslist_constructor) server_vm_construct, + (apr_reslist_destructor) server_cleanup_lua, + server_spec, r->server->process->pool) + == APR_SUCCESS && reslist != NULL) { + apr_pool_userdata_set(reslist, hash, NULL, + r->server->process->pool); + if (apr_reslist_acquire(reslist, (void**) &sspec) == APR_SUCCESS) { + L = sspec->L; + cache_info = sspec->finfo; + } + else { +#if APR_HAS_THREADS + apr_thread_mutex_unlock(ap_lua_mutex); +#endif + return NULL; + } + } + } +#if APR_HAS_THREADS + apr_thread_mutex_unlock(ap_lua_mutex); +#endif + } + else { + if (apr_pool_userdata_get((void **)&L, spec->file, + lifecycle_pool) != APR_SUCCESS) { + L = NULL; + } + } + if (L == NULL) { + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01483) + "creating lua_State with file %s", spec->file); + /* not available, so create */ + + if (!vm_construct(&L, spec, lifecycle_pool)) { + AP_DEBUG_ASSERT(L != NULL); + apr_pool_userdata_set(L, spec->file, cleanup_lua, lifecycle_pool); + } + } + + if (spec->codecache == AP_LUA_CACHE_FOREVER || (spec->bytecode && spec->bytecode_len > 0)) { + tryCache = 1; + } + else { + char* mkey; + if (spec->scope != AP_LUA_SCOPE_SERVER) { + mkey = apr_psprintf(r->pool, "ap_lua_modified:%s", spec->file); + apr_pool_userdata_get((void **)&cache_info, mkey, lifecycle_pool); + if (cache_info == NULL) { + cache_info = apr_pcalloc(lifecycle_pool, sizeof(ap_lua_finfo)); + apr_pool_userdata_set((void*) cache_info, mkey, NULL, lifecycle_pool); + } + } + if (spec->codecache == AP_LUA_CACHE_STAT) { + apr_finfo_t lua_finfo; + apr_stat(&lua_finfo, spec->file, APR_FINFO_MTIME|APR_FINFO_SIZE, lifecycle_pool); + + /* On first visit, modified will be zero, but that's fine - The file is + loaded in the vm_construct function. + */ + if ((cache_info->modified == lua_finfo.mtime && cache_info->size == lua_finfo.size) + || cache_info->modified == 0) { + tryCache = 1; + } + cache_info->modified = lua_finfo.mtime; + cache_info->size = lua_finfo.size; + } + else if (spec->codecache == AP_LUA_CACHE_NEVER) { + if (cache_info->runs == 0) + tryCache = 1; + } + cache_info->runs++; + } + if (tryCache == 0 && spec->scope != AP_LUA_SCOPE_ONCE) { + int rc; + ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(02332) + "(re)loading lua file %s", spec->file); + rc = luaL_loadfile(L, spec->file); + if (rc != 0) { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(02333) + "Error loading %s: %s", spec->file, + rc == LUA_ERRMEM ? "memory allocation error" + : lua_tostring(L, 0)); + return 0; + } + lua_pcall(L, 0, LUA_MULTRET, 0); + } + + return L; +} diff --git a/modules/lua/lua_vmprep.h b/modules/lua/lua_vmprep.h new file mode 100644 index 0000000..e46ac9b --- /dev/null +++ b/modules/lua/lua_vmprep.h @@ -0,0 +1,147 @@ +/** + * 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" +#include "apr_reslist.h" + + +#ifndef VMPREP_H +#define VMPREP_H + +#define AP_LUA_SCOPE_UNSET 0 +#define AP_LUA_SCOPE_ONCE 1 +#define AP_LUA_SCOPE_REQUEST 2 +#define AP_LUA_SCOPE_CONN 3 +#define AP_LUA_SCOPE_THREAD 4 +#define AP_LUA_SCOPE_SERVER 5 + +#define AP_LUA_CACHE_UNSET 0 +#define AP_LUA_CACHE_NEVER 1 +#define AP_LUA_CACHE_STAT 2 +#define AP_LUA_CACHE_FOREVER 3 + +#define AP_LUA_FILTER_INPUT 1 +#define AP_LUA_FILTER_OUTPUT 2 + +typedef void (*ap_lua_state_open_callback) (lua_State *L, apr_pool_t *p, + void *ctx); +/** + * 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 */ + const char *file; + + /* APL_SCOPE_ONCE | APL_SCOPE_REQUEST | APL_SCOPE_CONN | APL_SCOPE_THREAD | APL_SCOPE_SERVER */ + int scope; + unsigned int vm_min; + unsigned int vm_max; + + ap_lua_state_open_callback cb; + void* cb_arg; + + /* pool to use for lifecycle if APL_SCOPE_ONCE is set, otherwise unused */ + apr_pool_t *pool; + + /* Pre-compiled Lua Byte code to load directly. If bytecode_len is >0, + * the file part of this structure is ignored for loading purposes, but + * it is used for error messages. + */ + const char *bytecode; + apr_size_t bytecode_len; + + int codecache; +} ap_lua_vm_spec; + +typedef struct +{ + const char *function_name; + const char *file_name; + int scope; + ap_regex_t *uri_pattern; + const char *bytecode; + apr_size_t bytecode_len; + int codecache; +} ap_lua_mapped_handler_spec; + +typedef struct +{ + const char *function_name; + const char *file_name; + const char* filter_name; + int direction; /* AP_LUA_FILTER_INPUT | AP_LUA_FILTER_OUTPUT */ +} ap_lua_filter_handler_spec; + +typedef struct { + apr_size_t runs; + apr_time_t modified; + apr_off_t size; +} ap_lua_finfo; + +typedef struct { + lua_State* L; + ap_lua_finfo* finfo; +} ap_lua_server_spec; + +/** + * Fake out addition of the "apache2" module + */ +void ap_lua_load_apache2_lmodule(lua_State *L); + +/* + * 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 *ap_lua_get_lua_state(apr_pool_t *lifecycle_pool, + ap_lua_vm_spec *spec, request_rec* r); + +#if APR_HAS_THREADS || defined(DOXYGEN) +/* + * Initialize mod_lua mutex. + * @pool pool for mutex + * @s server_rec for logging + */ +void ap_lua_init_mutex(apr_pool_t *pool, server_rec *s); +#endif + +#endif diff --git a/modules/lua/mod_lua.c b/modules/lua/mod_lua.c new file mode 100644 index 0000000..303890e --- /dev/null +++ b/modules/lua/mod_lua.c @@ -0,0 +1,2197 @@ +/** + * 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_lua.h" +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include <apr_thread_mutex.h> +#include <apr_pools.h> +#include "lua_apr.h" +#include "lua_config.h" +#include "apr_optional.h" +#include "mod_auth.h" +#include "util_mutex.h" + + +#ifdef APR_HAS_THREADS +#include "apr_thread_proc.h" +#endif + +/* getpid for *NIX */ +#if APR_HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#if APR_HAVE_UNISTD_H +#include <unistd.h> +#endif + +/* getpid for Windows */ +#if APR_HAVE_PROCESS_H +#include <process.h> +#endif + +APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open, + (lua_State *L, apr_pool_t *p), + (L, p), OK, DECLINED) + +APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_request, + (lua_State *L, request_rec *r), + (L, r), OK, DECLINED) + +module AP_MODULE_DECLARE_DATA lua_module; + +#define AP_LUA_HOOK_FIRST (APR_HOOK_FIRST - 1) +#define AP_LUA_HOOK_LAST (APR_HOOK_LAST + 1) + +typedef struct { + const char *name; + const char *file_name; + const char *function_name; + ap_lua_vm_spec *spec; +} lua_authz_provider_spec; + +typedef struct { + lua_authz_provider_spec *spec; + apr_array_header_t *args; +} lua_authz_provider_func; + +apr_hash_t *lua_authz_providers; + +typedef struct +{ + apr_bucket_brigade *tmpBucket; + lua_State *L; + ap_lua_vm_spec *spec; + int broken; +} lua_filter_ctx; + +#define DEFAULT_LUA_SHMFILE "lua_ivm_shm" + +apr_global_mutex_t *lua_ivm_mutex; +apr_shm_t *lua_ivm_shm; +char *lua_ivm_shmfile; + +static apr_status_t shm_cleanup_wrapper(void *unused) +{ + if (lua_ivm_shm) { + return apr_shm_destroy(lua_ivm_shm); + } + return OK; +} + +/** + * 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) +{ + const char *lua_response; + r->status = HTTP_INTERNAL_SERVER_ERROR; + r->content_type = "text/html"; + ap_rputs("<h3>Error!</h3>\n", r); + ap_rputs("<pre>", r); + lua_response = lua_tostring(L, -1); + ap_rputs(ap_escape_html(r->pool, lua_response), r); + ap_rputs("</pre>\n", r); + + ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, APLOGNO(01471) "Lua error: %s", + lua_response); +} + +static void lua_open_callback(lua_State *L, apr_pool_t *p, void *ctx) +{ + ap_lua_init(L, p); + ap_lua_load_apache2_lmodule(L); + ap_lua_load_request_lmodule(L, p); + ap_lua_load_config_lmodule(L); +} + +static int lua_open_hook(lua_State *L, apr_pool_t *p) +{ + lua_open_callback(L, p, NULL); + return OK; +} + +static const char *scope_to_string(unsigned int scope) +{ + switch (scope) { + case AP_LUA_SCOPE_ONCE: + case AP_LUA_SCOPE_UNSET: + return "once"; + case AP_LUA_SCOPE_REQUEST: + return "request"; + case AP_LUA_SCOPE_CONN: + return "conn"; +#if APR_HAS_THREADS + case AP_LUA_SCOPE_THREAD: + return "thread"; + case AP_LUA_SCOPE_SERVER: + return "server"; +#endif + default: + ap_assert(0); + return 0; + } +} + +static void ap_lua_release_state(lua_State* L, ap_lua_vm_spec* spec, request_rec* r) +{ + char *hash; + apr_reslist_t* reslist = NULL; + + if (spec->scope == AP_LUA_SCOPE_SERVER) { + ap_lua_server_spec* sspec = NULL; + lua_settop(L, 0); + lua_getfield(L, LUA_REGISTRYINDEX, "Apache2.Lua.server_spec"); + sspec = (ap_lua_server_spec*) lua_touserdata(L, 1); + hash = apr_psprintf(r->pool, "reslist:%s", spec->file); + if (apr_pool_userdata_get((void **)&reslist, hash, + r->server->process->pool) == APR_SUCCESS) { + AP_DEBUG_ASSERT(sspec != NULL); + if (reslist != NULL) { + apr_reslist_release(reslist, sspec); + } + } + } +} + +static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool, + request_rec *r, + const ap_lua_dir_cfg *cfg, + const ap_lua_server_cfg *server_cfg, + const char *filename, + const char *bytecode, + apr_size_t bytecode_len, + const char *function, + const char *what) +{ + apr_pool_t *pool; + ap_lua_vm_spec *spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec)); + + spec->scope = cfg->vm_scope; + spec->pool = r->pool; + spec->package_paths = cfg->package_paths; + spec->package_cpaths = cfg->package_cpaths; + spec->cb = &lua_open_callback; + spec->cb_arg = NULL; + spec->bytecode = bytecode; + spec->bytecode_len = bytecode_len; + spec->codecache = (cfg->codecache == AP_LUA_CACHE_UNSET) ? AP_LUA_CACHE_STAT : cfg->codecache; + spec->vm_min = cfg->vm_min ? cfg->vm_min : 1; + spec->vm_max = cfg->vm_max ? cfg->vm_max : 1; + + if (filename) { + char *file; + apr_filepath_merge(&file, server_cfg->root_path, + filename, APR_FILEPATH_NOTRELATIVE, r->pool); + spec->file = file; + } + else { + spec->file = r->filename; + } + ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO(02313) + "%s details: scope: %s, file: %s, func: %s", + what, scope_to_string(spec->scope), spec->file, + function ? function : "-"); + + switch (spec->scope) { + case AP_LUA_SCOPE_ONCE: + case AP_LUA_SCOPE_UNSET: + apr_pool_create(&pool, r->pool); + apr_pool_tag(pool, "mod_lua-vm"); + break; + case AP_LUA_SCOPE_REQUEST: + pool = r->pool; + break; + case AP_LUA_SCOPE_CONN: + pool = r->connection->pool; + break; +#if APR_HAS_THREADS + case AP_LUA_SCOPE_THREAD: + pool = apr_thread_pool_get(r->connection->current_thread); + break; + case AP_LUA_SCOPE_SERVER: + pool = r->server->process->pool; + break; +#endif + default: + ap_assert(0); + } + + *lifecycle_pool = pool; + return spec; +} + +static const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* string, const char** values) +{ + char *stringBetween; + const char* ret; + int srclen,x,y; + srclen = strlen(string); + ret = ""; + y = 0; + for (x=0; x < srclen; x++) { + if (string[x] == '$' && x != srclen-1 && string[x+1] >= '0' && string[x+1] <= '9') { + int v = *(string+x+1) - '0'; + if (x-y > 0) { + stringBetween = apr_pstrndup(pool, string+y, x-y); + } + else { + stringBetween = ""; + } + ret = apr_pstrcat(pool, ret, stringBetween, values[v], NULL); + y = ++x+1; + } + } + + if (x-y > 0 && y > 0) { + stringBetween = apr_pstrndup(pool, string+y, x-y); + ret = apr_pstrcat(pool, ret, stringBetween, NULL); + } + /* If no replacement was made, just return the original string */ + else if (y == 0) { + return string; + } + return ret; +} + + + +/** + * "main" + */ +static int lua_handler(request_rec *r) +{ + int rc = OK; + if (strcmp(r->handler, "lua-script")) { + return DECLINED; + } + /* Decline the request if the script does not exist (or is a directory), + * rather than just returning internal server error */ + if ( + (r->finfo.filetype == APR_NOFILE) + || (r->finfo.filetype & APR_DIR) + ) { + return DECLINED; + } + ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(01472) + "handling [%s] in mod_lua", r->filename); + + /* XXX: This seems wrong because it may generate wrong headers for HEAD requests */ + if (!r->header_only) { + lua_State *L; + apr_pool_t *pool; + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + ap_lua_vm_spec *spec = create_vm_spec(&pool, r, cfg, NULL, NULL, NULL, + 0, "handle", "request handler"); + + L = ap_lua_get_lua_state(pool, spec, r); + if (!L) { + /* TODO annotate spec with failure reason */ + r->status = HTTP_INTERNAL_SERVER_ERROR; + ap_rputs("Unable to compile VM, see logs", r); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, APLOGNO(01474) "got a vm!"); + lua_getglobal(L, "handle"); + if (!lua_isfunction(L, -1)) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475) + "lua: Unable to find entry function '%s' in %s (not a valid function)", + "handle", + spec->file); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + ap_lua_run_lua_request(L, r); + if (lua_pcall(L, 1, 1, 0)) { + report_lua_error(L, r); + } + if (lua_isnumber(L, -1)) { + rc = lua_tointeger(L, -1); + } + ap_lua_release_state(L, spec, r); + } + return rc; +} + + +/* ------------------- Input/output content filters ------------------- */ + + +static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_filter_ctx** c) +{ + apr_pool_t *pool; + ap_lua_vm_spec *spec; + int n, rc, nres; + lua_State *L; + lua_filter_ctx *ctx; + ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, + &lua_module); + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + + ctx = apr_pcalloc(r->pool, sizeof(lua_filter_ctx)); + ctx->broken = 0; + *c = ctx; + /* Find the filter that was called. + * XXX: If we were wired with mod_filter, the filter (mod_filters name) + * and the provider (our underlying filters name) need to have matched. + */ + for (n = 0; n < cfg->mapped_filters->nelts; n++) { + ap_lua_filter_handler_spec *hook_spec = + ((ap_lua_filter_handler_spec **) cfg->mapped_filters->elts)[n]; + + if (hook_spec == NULL) { + continue; + } + if (!strcasecmp(hook_spec->filter_name, f->frec->name)) { + spec = create_vm_spec(&pool, r, cfg, server_cfg, + hook_spec->file_name, + NULL, + 0, + hook_spec->function_name, + "filter"); + L = ap_lua_get_lua_state(pool, spec, r); + if (L) { + L = lua_newthread(L); + } + + if (!L) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02328) + "lua: Failed to obtain lua interpreter for %s %s", + hook_spec->function_name, hook_spec->file_name); + ap_lua_release_state(L, spec, r); + return APR_EGENERAL; + } + 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, APLOGNO(02329) + "lua: Unable to find entry function '%s' in %s (not a valid function)", + hook_spec->function_name, + hook_spec->file_name); + ap_lua_release_state(L, spec, r); + return APR_EGENERAL; + } + + ap_lua_run_lua_request(L, r); + } + else { + int t; + ap_lua_run_lua_request(L, r); + + t = lua_gettop(L); + lua_setglobal(L, "r"); + lua_settop(L, t); + } + ctx->L = L; + ctx->spec = spec; + + /* If a Lua filter is interested in filtering a request, it must first do a yield, + * otherwise we'll assume that it's not interested and pretend we didn't find it. + */ + rc = lua_resume(L, 1, &nres); + if (rc == LUA_YIELD) { + if (f->frec->providers == NULL) { + /* Not wired by mod_filter */ + apr_table_unset(r->headers_out, "Content-Length"); + apr_table_unset(r->headers_out, "Content-MD5"); + apr_table_unset(r->headers_out, "ETAG"); + } + return OK; + } + else { + ap_lua_release_state(L, spec, r); + return APR_ENOENT; + } + } + } + return APR_ENOENT; +} + +static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade *pbbIn) +{ + request_rec *r = f->r; + int rc, nres; + lua_State *L; + lua_filter_ctx* ctx; + conn_rec *c = r->connection; + apr_bucket *pbktIn; + apr_status_t rv; + + /* Set up the initial filter context and acquire the function. + * The corresponding Lua function should yield here. + */ + if (!f->ctx) { + rc = lua_setup_filter_ctx(f,r,&ctx); + if (rc == APR_EGENERAL) { + return HTTP_INTERNAL_SERVER_ERROR; + } + if (rc == APR_ENOENT) { + /* No filter entry found (or the script declined to filter), just pass on the buckets */ + ap_remove_output_filter(f); + return ap_pass_brigade(f->next,pbbIn); + } + else { + /* We've got a willing lua filter, setup and check for a prefix */ + size_t olen; + apr_bucket *pbktOut; + const char* output = lua_tolstring(ctx->L, 1, &olen); + + f->ctx = ctx; + ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc); + + if (olen > 0) { + pbktOut = apr_bucket_heap_create(output, olen, NULL, c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut); + rv = ap_pass_brigade(f->next, ctx->tmpBucket); + apr_brigade_cleanup(ctx->tmpBucket); + if (rv != APR_SUCCESS) { + return rv; + } + } + } + } + ctx = (lua_filter_ctx*) f->ctx; + L = ctx->L; + /* While the Lua function is still yielding, pass in buckets to the coroutine */ + if (!ctx->broken) { + for (pbktIn = APR_BRIGADE_FIRST(pbbIn); + pbktIn != APR_BRIGADE_SENTINEL(pbbIn); + pbktIn = APR_BUCKET_NEXT(pbktIn)) + { + const char *data; + apr_size_t len; + apr_bucket *pbktOut; + + /* read the bucket */ + apr_bucket_read(pbktIn,&data,&len,APR_BLOCK_READ); + + /* Push the bucket onto the Lua stack as a global var */ + lua_pushlstring(L, data, len); + lua_setglobal(L, "bucket"); + + /* If Lua yielded, it means we have something to pass on */ + if (lua_resume(L, 0, &nres) == LUA_YIELD && nres == 1) { + size_t olen; + const char* output = lua_tolstring(L, 1, &olen); + if (olen > 0) { + pbktOut = apr_bucket_heap_create(output, olen, NULL, + c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut); + rv = ap_pass_brigade(f->next, ctx->tmpBucket); + apr_brigade_cleanup(ctx->tmpBucket); + if (rv != APR_SUCCESS) { + return rv; + } + } + } + else { + ctx->broken = 1; + ap_lua_release_state(L, ctx->spec, r); + ap_remove_output_filter(f); + apr_brigade_cleanup(pbbIn); + apr_brigade_cleanup(ctx->tmpBucket); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02663) + "lua: Error while executing filter: %s", + lua_tostring(L, -1)); + return HTTP_INTERNAL_SERVER_ERROR; + } + } + /* If we've safely reached the end, do a final call to Lua to allow for any + finishing moves by the script, such as appending a tail. */ + if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(pbbIn))) { + apr_bucket *pbktEOS; + lua_pushnil(L); + lua_setglobal(L, "bucket"); + if (lua_resume(L, 0, &nres) == LUA_YIELD && nres == 1) { + apr_bucket *pbktOut; + size_t olen; + const char* output = lua_tolstring(L, 1, &olen); + if (olen > 0) { + pbktOut = apr_bucket_heap_create(output, olen, NULL, + c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut); + } + } + pbktEOS = apr_bucket_eos_create(c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktEOS); + ap_lua_release_state(L, ctx->spec, r); + rv = ap_pass_brigade(f->next, ctx->tmpBucket); + apr_brigade_cleanup(ctx->tmpBucket); + if (rv != APR_SUCCESS) { + return rv; + } + } + } + /* Clean up */ + apr_brigade_cleanup(pbbIn); + return APR_SUCCESS; +} + + + +static apr_status_t lua_input_filter_handle(ap_filter_t *f, + apr_bucket_brigade *pbbOut, + ap_input_mode_t eMode, + apr_read_type_e eBlock, + apr_off_t nBytes) +{ + request_rec *r = f->r; + int rc, lastCall = 0, nres; + lua_State *L; + lua_filter_ctx* ctx; + conn_rec *c = r->connection; + apr_status_t ret; + + /* Set up the initial filter context and acquire the function. + * The corresponding Lua function should yield here. + */ + if (!f->ctx) { + rc = lua_setup_filter_ctx(f,r,&ctx); + f->ctx = ctx; + if (rc == APR_EGENERAL) { + ctx->broken = 1; + ap_remove_input_filter(f); + return HTTP_INTERNAL_SERVER_ERROR; + } + if (rc == APR_ENOENT ) { + ap_remove_input_filter(f); + ctx->broken = 1; + } + if (rc == APR_SUCCESS) { + ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc); + } + } + ctx = (lua_filter_ctx*) f->ctx; + L = ctx->L; + /* If the Lua script broke or denied serving the request, just pass the buckets through */ + if (ctx->broken) { + return ap_get_brigade(f->next, pbbOut, eMode, eBlock, nBytes); + } + + if (APR_BRIGADE_EMPTY(ctx->tmpBucket)) { + ret = ap_get_brigade(f->next, ctx->tmpBucket, eMode, eBlock, nBytes); + if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS) + return ret; + } + + /* While the Lua function is still yielding, pass buckets to the coroutine */ + if (!ctx->broken) { + lastCall = 0; + while (!APR_BRIGADE_EMPTY(ctx->tmpBucket)) { + apr_bucket *pbktIn = APR_BRIGADE_FIRST(ctx->tmpBucket); + apr_bucket *pbktOut; + const char *data; + apr_size_t len; + + if (APR_BUCKET_IS_EOS(pbktIn)) { + APR_BUCKET_REMOVE(pbktIn); + break; + } + + /* read the bucket */ + ret = apr_bucket_read(pbktIn, &data, &len, eBlock); + if (ret != APR_SUCCESS) + return ret; + + /* Push the bucket onto the Lua stack as a global var */ + lastCall++; + lua_pushlstring(L, data, len); + lua_setglobal(L, "bucket"); + + /* If Lua yielded, it means we have something to pass on */ + if (lua_resume(L, 0, &nres) == LUA_YIELD && nres == 1) { + size_t olen; + const char* output = lua_tolstring(L, 1, &olen); + pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); + apr_bucket_delete(pbktIn); + return APR_SUCCESS; + } + else { + ctx->broken = 1; + ap_lua_release_state(L, ctx->spec, r); + ap_remove_input_filter(f); + apr_bucket_delete(pbktIn); + return HTTP_INTERNAL_SERVER_ERROR; + } + } + /* If we've safely reached the end, do a final call to Lua to allow for any + finishing moves by the script, such as appending a tail. */ + if (lastCall == 0) { + apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc); + lua_pushnil(L); + lua_setglobal(L, "bucket"); + if (lua_resume(L, 0, &nres) == LUA_YIELD && nres == 1) { + apr_bucket *pbktOut; + size_t olen; + const char* output = lua_tolstring(L, 1, &olen); + pbktOut = apr_bucket_heap_create(output, olen, 0, c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); + } + APR_BRIGADE_INSERT_TAIL(pbbOut,pbktEOS); + ap_lua_release_state(L, ctx->spec, r); + } + } + return APR_SUCCESS; +} + + +/* ---------------- Configury stuff --------------- */ + +/** harnesses for magic hooks **/ + +static int lua_request_rec_hook_harness(request_rec *r, const char *name, int apr_hook_when) +{ + int rc; + apr_pool_t *pool; + lua_State *L; + ap_lua_vm_spec *spec; + ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, + &lua_module); + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + const char *key = apr_psprintf(r->pool, "%s_%d", name, apr_hook_when); + apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key, + APR_HASH_KEY_STRING); + if (hook_specs) { + int i; + for (i = 0; i < hook_specs->nelts; i++) { + ap_lua_mapped_handler_spec *hook_spec = + ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i]; + + if (hook_spec == NULL) { + continue; + } + spec = create_vm_spec(&pool, r, cfg, server_cfg, + hook_spec->file_name, + hook_spec->bytecode, + hook_spec->bytecode_len, + hook_spec->function_name, + "request hook"); + + L = ap_lua_get_lua_state(pool, spec, r); + + if (!L) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477) + "lua: Failed to obtain lua interpreter for entry function '%s' in %s", + hook_spec->function_name, hook_spec->file_name); + return HTTP_INTERNAL_SERVER_ERROR; + } + + 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, APLOGNO(01478) + "lua: Unable to find entry function '%s' in %s (not a valid function)", + hook_spec->function_name, + hook_spec->file_name); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + + ap_lua_run_lua_request(L, r); + } + else { + int t; + ap_lua_run_lua_request(L, r); + + t = lua_gettop(L); + lua_setglobal(L, "r"); + lua_settop(L, t); + } + + if (lua_pcall(L, 1, 1, 0)) { + report_lua_error(L, r); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + rc = DECLINED; + if (lua_isnumber(L, -1)) { + rc = lua_tointeger(L, -1); + ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "Lua hook %s:%s for phase %s returned %d", + hook_spec->file_name, hook_spec->function_name, name, rc); + } + else { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(03017) + "Lua hook %s:%s for phase %s did not return a numeric value", + hook_spec->file_name, hook_spec->function_name, name); + return HTTP_INTERNAL_SERVER_ERROR; + } + if (rc != DECLINED) { + ap_lua_release_state(L, spec, r); + return rc; + } + ap_lua_release_state(L, spec, r); + } + } + return DECLINED; +} + + +/* Fix for making sure that LuaMapHandler works when FallbackResource is set */ +static int lua_map_handler_fixups(request_rec *r) +{ + /* If there is no handler set yet, this might be a LuaMapHandler request */ + if (r->handler == NULL) { + int n = 0; + ap_regmatch_t match[10]; + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + for (n = 0; n < cfg->mapped_handlers->nelts; n++) { + ap_lua_mapped_handler_spec *hook_spec = + ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n]; + + if (hook_spec == NULL) { + continue; + } + if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) { + r->handler = apr_pstrdup(r->pool, "lua-map-handler"); + return OK; + } + } + } + return DECLINED; +} + + +static int lua_map_handler(request_rec *r) +{ + int rc, n = 0; + apr_pool_t *pool; + lua_State *L; + const char *filename, *function_name; + const char *values[10]; + ap_lua_vm_spec *spec; + ap_regmatch_t match[10]; + ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, + &lua_module); + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + for (n = 0; n < cfg->mapped_handlers->nelts; n++) { + ap_lua_mapped_handler_spec *hook_spec = + ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n]; + + if (hook_spec == NULL) { + continue; + } + if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) { + int i; + for (i=0 ; i < 10; i++) { + if (match[i].rm_eo >= 0) { + values[i] = apr_pstrndup(r->pool, r->uri+match[i].rm_so, match[i].rm_eo - match[i].rm_so); + } + else values[i] = ""; + } + filename = ap_lua_interpolate_string(r->pool, hook_spec->file_name, values); + function_name = ap_lua_interpolate_string(r->pool, hook_spec->function_name, values); + spec = create_vm_spec(&pool, r, cfg, server_cfg, + filename, + hook_spec->bytecode, + hook_spec->bytecode_len, + function_name, + "mapped handler"); + L = ap_lua_get_lua_state(pool, spec, r); + + if (!L) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330) + "lua: Failed to obtain Lua interpreter for entry function '%s' in %s", + function_name, filename); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + + if (function_name != NULL) { + lua_getglobal(L, function_name); + if (!lua_isfunction(L, -1)) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331) + "lua: Unable to find entry function '%s' in %s (not a valid function)", + function_name, + filename); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + + ap_lua_run_lua_request(L, r); + } + else { + int t; + ap_lua_run_lua_request(L, r); + + t = lua_gettop(L); + lua_setglobal(L, "r"); + lua_settop(L, t); + } + + if (lua_pcall(L, 1, 1, 0)) { + report_lua_error(L, r); + ap_lua_release_state(L, spec, r); + return HTTP_INTERNAL_SERVER_ERROR; + } + rc = DECLINED; + if (lua_isnumber(L, -1)) { + rc = lua_tointeger(L, -1); + } + else { + ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02483) + "lua: Lua handler %s in %s did not return a value, assuming apache2.OK", + function_name, + filename); + rc = OK; + } + ap_lua_release_state(L, spec, r); + if (rc != DECLINED) { + return rc; + } + } + } + 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) { + apr_status_t rc = (cfg->getstr) (buf, bufsiz, cfg->param); + if (rc == APR_SUCCESS) { + i = strlen(buf); + if (i && buf[i - 1] == '\n') + ++cfg->line_number; + } + else { + buf[0] = '\0'; + i = 0; + } + } + else { + while (i < bufsiz) { + char ch; + apr_status_t rc = (cfg->getch) (&ch, cfg->param); + if (rc != APR_SUCCESS) + 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 explanation -- 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] == '/') { + apr_size_t 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; + ap_lua_mapped_handler_spec *spec; + int apr_hook_when; +} 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) +{ + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + ap_directive_t *directive = cmd->directive; + hack_section_baton *baton = directive->data; + const char *key = apr_psprintf(cmd->pool, "%s_%d", baton->name, baton->apr_hook_when); + + apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key, + APR_HASH_KEY_STRING); + if (!hook_specs) { + hook_specs = apr_array_make(cmd->pool, 2, + sizeof(ap_lua_mapped_handler_spec *)); + apr_hash_set(cfg->hooks, key, + APR_HASH_KEY_STRING, hook_specs); + } + + baton->spec->scope = cfg->vm_scope; + + *(ap_lua_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 = NULL; + ap_lua_mapped_handler_spec *spec; + int when = APR_HOOK_MIDDLE; + const char *endp = ap_strrchr_c(line, '>'); + + if (endp == NULL) { + return apr_pstrcat(cmd->pool, cmd->cmd->name, + "> directive missing closing '>'", NULL); + } + + line = apr_pstrndup(cmd->temp_pool, line, endp - line); + + if (line[0]) { + const char *word; + word = ap_getword_conf(cmd->temp_pool, &line); + if (*word) { + function = apr_pstrdup(cmd->pool, word); + } + word = ap_getword_conf(cmd->temp_pool, &line); + if (*word) { + if (!strcasecmp("early", word)) { + when = AP_LUA_HOOK_FIRST; + } + else if (!strcasecmp("late", word)) { + when = AP_LUA_HOOK_LAST; + } + else { + return apr_pstrcat(cmd->pool, cmd->cmd->name, + "> 2nd argument must be 'early' or 'late'", NULL); + } + } + } + + spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec)); + + { + cr_ctx ctx; + lua_State *lvm; + char *tmp; + int rv; + ap_directive_t **current; + hack_section_baton *baton; + + spec->file_name = apr_psprintf(cmd->pool, "%s:%u", + cmd->config_file->name, + cmd->config_file->line_number); + if (function) { + spec->function_name = (char *) function; + } + else { + function = NULL; + } + + 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_rawlen(lvm, -1); + spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1), + spec->bytecode_len); + lua_close(lvm); + } + + current = mconfig; + + /* Here, we have to replace our current config node for the next pass */ + if (!*current) { + *current = apr_pcalloc(cmd->pool, sizeof(**current)); + } + + baton = apr_pcalloc(cmd->pool, sizeof(hack_section_baton)); + baton->name = name; + baton->spec = spec; + baton->apr_hook_when = when; + + (*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, + int apr_hook_when) +{ + ap_lua_mapped_handler_spec *spec; + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + const char *key = apr_psprintf(cmd->pool, "%s_%d", name, apr_hook_when); + apr_array_header_t *hook_specs = apr_hash_get(cfg->hooks, key, + APR_HASH_KEY_STRING); + + if (!hook_specs) { + hook_specs = apr_array_make(cmd->pool, 2, + sizeof(ap_lua_mapped_handler_spec *)); + apr_hash_set(cfg->hooks, key, APR_HASH_KEY_STRING, hook_specs); + } + + spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_mapped_handler_spec)); + spec->file_name = apr_pstrdup(cmd->pool, file); + spec->function_name = apr_pstrdup(cmd->pool, function); + spec->scope = cfg->vm_scope; + + *(ap_lua_mapped_handler_spec **) apr_array_push(hook_specs) = spec; + return NULL; +} +static const char *register_mapped_file_function_hook(const char *pattern, + cmd_parms *cmd, + void *_cfg, + const char *file, + const char *function) +{ + ap_lua_mapped_handler_spec *spec; + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + ap_regex_t *regex = apr_pcalloc(cmd->pool, sizeof(ap_regex_t)); + if (ap_regcomp(regex, pattern,0)) { + return "Invalid regex pattern!"; + } + + spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_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->uri_pattern = regex; + + *(ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) = spec; + return NULL; +} +static const char *register_filter_function_hook(const char *filter, + cmd_parms *cmd, + void *_cfg, + const char *file, + const char *function, + int direction) +{ + ap_lua_filter_handler_spec *spec; + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + + spec = apr_pcalloc(cmd->pool, sizeof(ap_lua_filter_handler_spec)); + spec->file_name = apr_pstrdup(cmd->pool, file); + spec->function_name = apr_pstrdup(cmd->pool, function); + spec->filter_name = filter; + + *(ap_lua_filter_handler_spec **) apr_array_push(cfg->mapped_filters) = spec; + /* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */ + if (direction == AP_LUA_FILTER_OUTPUT) { + spec->direction = AP_LUA_FILTER_OUTPUT; + ap_register_output_filter_protocol(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE, + AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH); + } + else { + spec->direction = AP_LUA_FILTER_INPUT; + ap_register_input_filter(filter, lua_input_filter_handle, NULL, AP_FTYPE_RESOURCE); + } + return NULL; +} +/* disabled (see reference below) +static int lua_check_user_id_harness_first(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST); +} +*/ +static int lua_check_user_id_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE); +} +/* disabled (see reference below) +static int lua_check_user_id_harness_last(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST); +} +*/ + +static int lua_pre_trans_name_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "pre_translate_name", APR_HOOK_MIDDLE); +} + +static int lua_translate_name_harness_first(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_FIRST); +} +static int lua_translate_name_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "translate_name", APR_HOOK_MIDDLE); +} +static int lua_translate_name_harness_last(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "translate_name", AP_LUA_HOOK_LAST); +} + +static int lua_fixup_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "fixups", APR_HOOK_MIDDLE); +} + +static int lua_map_to_storage_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "map_to_storage", APR_HOOK_MIDDLE); +} + +static int lua_type_checker_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "type_checker", APR_HOOK_MIDDLE); +} + +static int lua_access_checker_harness_first(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_FIRST); +} +static int lua_access_checker_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "access_checker", APR_HOOK_MIDDLE); +} +static int lua_access_checker_harness_last(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "access_checker", AP_LUA_HOOK_LAST); +} + +static int lua_auth_checker_harness_first(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_FIRST); +} +static int lua_auth_checker_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "auth_checker", APR_HOOK_MIDDLE); +} +static int lua_auth_checker_harness_last(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "auth_checker", AP_LUA_HOOK_LAST); +} +static void lua_insert_filter_harness(request_rec *r) +{ + /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(03223) + * "LuaHookInsertFilter not yet implemented"); */ +} + +static int lua_log_transaction_harness(request_rec *r) +{ + return lua_request_rec_hook_harness(r, "log_transaction", APR_HOOK_FIRST); +} + +static int lua_quick_harness(request_rec *r, int lookup) +{ + if (lookup) { + return DECLINED; + } + return lua_request_rec_hook_harness(r, "quick", APR_HOOK_MIDDLE); +} + +static const char *register_pre_trans_name_hook(cmd_parms *cmd, void *_cfg, + const char *file, + const char *function) +{ + return register_named_file_function_hook("pre_translate_name", cmd, _cfg, file, + function, APR_HOOK_MIDDLE); +} + +static const char *register_pre_trans_name_block(cmd_parms *cmd, void *_cfg, + const char *line) +{ + return register_named_block_function_hook("pre_translate_name", cmd, _cfg, + line); +} + +static const char *register_translate_name_hook(cmd_parms *cmd, void *_cfg, + const char *file, + const char *function, + const char *when) +{ + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES| + NOT_IN_HTACCESS); + int apr_hook_when = APR_HOOK_MIDDLE; + if (err) { + return err; + } + + if (when) { + if (!strcasecmp(when, "early")) { + apr_hook_when = AP_LUA_HOOK_FIRST; + } + else if (!strcasecmp(when, "late")) { + apr_hook_when = AP_LUA_HOOK_LAST; + } + else { + return "Third argument must be 'early' or 'late'"; + } + } + + return register_named_file_function_hook("translate_name", cmd, _cfg, + file, function, apr_hook_when); +} + +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, APR_HOOK_MIDDLE); +} +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, APR_HOOK_MIDDLE); +} + +static const char *register_log_transaction_hook(cmd_parms *cmd, void *_cfg, + const char *file, + const char *function) +{ + return register_named_file_function_hook("log_transaction", cmd, _cfg, + file, function, APR_HOOK_FIRST); +} + +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, + const char *when) +{ + int apr_hook_when = APR_HOOK_MIDDLE; +/* XXX: This does not currently work!! + if (when) { + if (!strcasecmp(when, "early")) { + apr_hook_when = AP_LUA_HOOK_FIRST; + } + else if (!strcasecmp(when, "late")) { + apr_hook_when = AP_LUA_HOOK_LAST; + } + else { + return "Third argument must be 'early' or 'late'"; + } + } +*/ + return register_named_file_function_hook("check_user_id", cmd, _cfg, file, + function, apr_hook_when); +} +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, APR_HOOK_MIDDLE); +} +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, + const char *when) +{ + int apr_hook_when = APR_HOOK_MIDDLE; + + if (when) { + if (!strcasecmp(when, "early")) { + apr_hook_when = AP_LUA_HOOK_FIRST; + } + else if (!strcasecmp(when, "late")) { + apr_hook_when = AP_LUA_HOOK_LAST; + } + else { + return "Third argument must be 'early' or 'late'"; + } + } + + return register_named_file_function_hook("access_checker", cmd, _cfg, + file, function, apr_hook_when); +} +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, + const char *when) +{ + int apr_hook_when = APR_HOOK_MIDDLE; + + if (when) { + if (!strcasecmp(when, "early")) { + apr_hook_when = AP_LUA_HOOK_FIRST; + } + else if (!strcasecmp(when, "late")) { + apr_hook_when = AP_LUA_HOOK_LAST; + } + else { + return "Third argument must be 'early' or 'late'"; + } + } + + return register_named_file_function_hook("auth_checker", cmd, _cfg, file, + function, apr_hook_when); +} +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) +{ + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES| + NOT_IN_HTACCESS); + if (err) { + return err; + } + return register_named_file_function_hook("quick", cmd, _cfg, file, + function, APR_HOOK_MIDDLE); +} +static const char *register_map_handler(cmd_parms *cmd, void *_cfg, + const char* match, const char *file, const char *function) +{ + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES| + NOT_IN_HTACCESS); + if (err) { + return err; + } + if (!function) function = "handle"; + return register_mapped_file_function_hook(match, cmd, _cfg, file, + function); +} +static const char *register_output_filter(cmd_parms *cmd, void *_cfg, + const char* filter, const char *file, const char *function) +{ + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES| + NOT_IN_HTACCESS); + if (err) { + return err; + } + if (!function) function = "handle"; + return register_filter_function_hook(filter, cmd, _cfg, file, + function, AP_LUA_FILTER_OUTPUT); +} +static const char *register_input_filter(cmd_parms *cmd, void *_cfg, + const char* filter, const char *file, const char *function) +{ + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES| + NOT_IN_HTACCESS); + if (err) { + return err; + } + if (!function) function = "handle"; + return register_filter_function_hook(filter, cmd, _cfg, file, + function, AP_LUA_FILTER_INPUT); +} +static const char *register_quick_block(cmd_parms *cmd, void *_cfg, + const char *line) +{ + return register_named_block_function_hook("quick", cmd, _cfg, + line); +} + + + +static const char *register_package_helper(cmd_parms *cmd, + const char *arg, + apr_array_header_t *dir_array) +{ + apr_status_t rv; + + ap_lua_server_cfg *server_cfg = + ap_get_module_config(cmd->server->module_config, &lua_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 + */ +static const char *register_package_dir(cmd_parms *cmd, void *_cfg, + const char *arg) +{ + ap_lua_dir_cfg *cfg = (ap_lua_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 + */ +static const char *register_package_cdir(cmd_parms *cmd, + void *_cfg, + const char *arg) +{ + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + + return register_package_helper(cmd, arg, cfg->package_cpaths); +} + +static const char *register_lua_inherit(cmd_parms *cmd, + void *_cfg, + const char *arg) +{ + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + + if (strcasecmp("none", arg) == 0) { + cfg->inherit = AP_LUA_INHERIT_NONE; + } + else if (strcasecmp("parent-first", arg) == 0) { + cfg->inherit = AP_LUA_INHERIT_PARENT_FIRST; + } + else if (strcasecmp("parent-last", arg) == 0) { + cfg->inherit = AP_LUA_INHERIT_PARENT_LAST; + } + else { + return apr_psprintf(cmd->pool, + "LuaInherit type of '%s' not recognized, valid " + "options are 'none', 'parent-first', and 'parent-last'", + arg); + } + return NULL; +} +static const char *register_lua_codecache(cmd_parms *cmd, + void *_cfg, + const char *arg) +{ + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + + if (strcasecmp("never", arg) == 0) { + cfg->codecache = AP_LUA_CACHE_NEVER; + } + else if (strcasecmp("stat", arg) == 0) { + cfg->codecache = AP_LUA_CACHE_STAT; + } + else if (strcasecmp("forever", arg) == 0) { + cfg->codecache = AP_LUA_CACHE_FOREVER; + } + else { + return apr_psprintf(cmd->pool, + "LuaCodeCache type of '%s' not recognized, valid " + "options are 'never', 'stat', and 'forever'", + arg); + } + return NULL; +} +static const char *register_lua_scope(cmd_parms *cmd, + void *_cfg, + const char *scope, + const char *min, + const char *max) +{ + ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg; + if (strcmp("once", scope) == 0) { + cfg->vm_scope = AP_LUA_SCOPE_ONCE; + } + else if (strcmp("request", scope) == 0) { + cfg->vm_scope = AP_LUA_SCOPE_REQUEST; + } + else if (strcmp("conn", scope) == 0) { + cfg->vm_scope = AP_LUA_SCOPE_CONN; + } + else if (strcmp("thread", scope) == 0) { +#if !APR_HAS_THREADS + return apr_psprintf(cmd->pool, + "Scope type of '%s' cannot be used because this " + "server does not have threading support " + "(APR_HAS_THREADS)", + scope); +#endif + cfg->vm_scope = AP_LUA_SCOPE_THREAD; + } + else if (strcmp("server", scope) == 0) { + unsigned int vmin, vmax; +#if !APR_HAS_THREADS + return apr_psprintf(cmd->pool, + "Scope type of '%s' cannot be used because this " + "server does not have threading support " + "(APR_HAS_THREADS)", + scope); +#endif + cfg->vm_scope = AP_LUA_SCOPE_SERVER; + vmin = min ? atoi(min) : 1; + vmax = max ? atoi(max) : 1; + if (vmin == 0) { + vmin = 1; + } + if (vmax < vmin) { + vmax = vmin; + } + cfg->vm_min = vmin; + cfg->vm_max = vmax; + } + else { + return apr_psprintf(cmd->pool, + "Invalid value for LuaScope, '%s', acceptable " + "values are: 'once', 'request', 'conn'" +#if APR_HAS_THREADS + ", 'thread', 'server'" +#endif + ,scope); + } + + return NULL; +} + + + +static const char *register_lua_root(cmd_parms *cmd, void *_cfg, + const char *root) +{ + /* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */ + ap_lua_server_cfg *cfg = ap_get_module_config(cmd->server->module_config, + &lua_module); + + cfg->root_path = root; + return NULL; +} + +const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, + request_rec *r, const char *var) +{ + return ap_ssl_var_lookup(p, s, c, r, var); +} + +int ap_lua_ssl_is_https(conn_rec *c) +{ + return ap_ssl_conn_is_ssl(c); +} + +/*******************************/ + +static const char *lua_authz_parse(cmd_parms *cmd, const char *require_line, + const void **parsed_require_line) +{ + const char *provider_name; + lua_authz_provider_spec *spec; + lua_authz_provider_func *func = apr_pcalloc(cmd->pool, sizeof(lua_authz_provider_func)); + + apr_pool_userdata_get((void**)&provider_name, AUTHZ_PROVIDER_NAME_NOTE, + cmd->temp_pool); + ap_assert(provider_name != NULL); + + spec = apr_hash_get(lua_authz_providers, provider_name, APR_HASH_KEY_STRING); + ap_assert(spec != NULL); + func->spec = spec; + + if (require_line && *require_line) { + const char *arg; + func->args = apr_array_make(cmd->pool, 2, sizeof(const char *)); + while ((arg = ap_getword_conf(cmd->pool, &require_line)) && *arg) { + APR_ARRAY_PUSH(func->args, const char *) = arg; + } + } + + *parsed_require_line = func; + return NULL; +} + +static authz_status lua_authz_check(request_rec *r, const char *require_line, + const void *parsed_require_line) +{ + apr_pool_t *pool; + ap_lua_vm_spec *spec; + lua_State *L; + ap_lua_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, + &lua_module); + const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, + &lua_module); + const lua_authz_provider_func *prov_func = parsed_require_line; + const lua_authz_provider_spec *prov_spec = prov_func->spec; + int result; + int nargs = 0; + + spec = create_vm_spec(&pool, r, cfg, server_cfg, prov_spec->file_name, + NULL, 0, prov_spec->function_name, "authz provider"); + + L = ap_lua_get_lua_state(pool, spec, r); + if (L == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02314) + "Unable to compile VM for authz provider %s", prov_spec->name); + return AUTHZ_GENERAL_ERROR; + } + lua_getglobal(L, prov_spec->function_name); + if (!lua_isfunction(L, -1)) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319) + "Unable to find entry function '%s' in %s (not a valid function)", + prov_spec->function_name, prov_spec->file_name); + ap_lua_release_state(L, spec, r); + return AUTHZ_GENERAL_ERROR; + } + ap_lua_run_lua_request(L, r); + if (prov_func->args) { + int i; + if (!lua_checkstack(L, prov_func->args->nelts)) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02315) + "Error: authz provider %s: too many arguments", prov_spec->name); + ap_lua_release_state(L, spec, r); + return AUTHZ_GENERAL_ERROR; + } + for (i = 0; i < prov_func->args->nelts; i++) { + const char *arg = APR_ARRAY_IDX(prov_func->args, i, const char *); + lua_pushstring(L, arg); + } + nargs = prov_func->args->nelts; + } + if (lua_pcall(L, 1 + nargs, 1, 0)) { + const char *err = lua_tostring(L, -1); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02316) + "Error executing authz provider %s: %s", prov_spec->name, err); + ap_lua_release_state(L, spec, r); + return AUTHZ_GENERAL_ERROR; + } + if (!lua_isnumber(L, -1)) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02317) + "Error: authz provider %s did not return integer", prov_spec->name); + ap_lua_release_state(L, spec, r); + return AUTHZ_GENERAL_ERROR; + } + result = lua_tointeger(L, -1); + ap_lua_release_state(L, spec, r); + switch (result) { + case AUTHZ_DENIED: + case AUTHZ_GRANTED: + case AUTHZ_NEUTRAL: + case AUTHZ_GENERAL_ERROR: + case AUTHZ_DENIED_NO_USER: + return result; + default: + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02318) + "Error: authz provider %s: invalid return value %d", + prov_spec->name, result); + } + return AUTHZ_GENERAL_ERROR; +} + +static const authz_provider lua_authz_provider = +{ + &lua_authz_check, + &lua_authz_parse, +}; + +static const char *register_authz_provider(cmd_parms *cmd, void *_cfg, + const char *name, const char *file, + const char *function) +{ + lua_authz_provider_spec *spec; + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); + if (err) + return err; + + spec = apr_pcalloc(cmd->pool, sizeof(*spec)); + spec->name = name; + spec->file_name = file; + spec->function_name = function; + + apr_hash_set(lua_authz_providers, name, APR_HASH_KEY_STRING, spec); + ap_register_auth_provider(cmd->pool, AUTHZ_PROVIDER_GROUP, name, + AUTHZ_PROVIDER_VERSION, + &lua_authz_provider, + AP_AUTH_INTERNAL_PER_CONF); + return NULL; +} + + +static const command_rec lua_commands[] = { + + AP_INIT_TAKE1("LuaRoot", register_lua_root, NULL, OR_ALL, + "Specify the base path for resolving relative paths for mod_lua 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_TAKE3("LuaAuthzProvider", register_authz_provider, NULL, RSRC_CONF|EXEC_ON_READ, + "Provide an authorization provider"), + + AP_INIT_TAKE2("LuaHookPreTranslateName", register_pre_trans_name_hook, NULL, + OR_ALL, + "Provide a hook for the pre_translate name phase of request processing"), + + AP_INIT_RAW_ARGS("<LuaHookPreTranslateName", register_pre_trans_name_block, NULL, + EXEC_ON_READ | OR_ALL, + "Provide a hook for the pre_translate name phase of request processing"), + + AP_INIT_TAKE23("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_TAKE23("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_TAKE23("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_TAKE23("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_TAKE2("LuaHookLog", register_log_transaction_hook, NULL, + OR_ALL, + "Provide a hook for the logging phase of request processing"), + + AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL, + "One of once, request, conn, server -- default is once"), + + AP_INIT_TAKE1("LuaInherit", register_lua_inherit, NULL, OR_ALL, + "Controls how Lua scripts in parent contexts are merged with the current " + " context: none|parent-last|parent-first (default: parent-first) "), + + AP_INIT_TAKE1("LuaCodeCache", register_lua_codecache, NULL, OR_ALL, + "Controls the behavior of the in-memory code cache " + " context: stat|forever|never (default: stat) "), + + 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"), + AP_INIT_TAKE23("LuaMapHandler", register_map_handler, NULL, OR_ALL, + "Maps a path to a lua handler"), + AP_INIT_TAKE3("LuaOutputFilter", register_output_filter, NULL, OR_ALL, + "Registers a Lua function as an output filter"), + AP_INIT_TAKE3("LuaInputFilter", register_input_filter, NULL, OR_ALL, + "Registers a Lua function as an input filter"), + {NULL} +}; + + +static void *create_dir_config(apr_pool_t *p, char *dir) +{ + ap_lua_dir_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_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(ap_lua_mapped_handler_spec *)); + cfg->mapped_filters = + apr_array_make(p, 1, sizeof(ap_lua_filter_handler_spec *)); + cfg->pool = p; + cfg->hooks = apr_hash_make(p); + cfg->dir = apr_pstrdup(p, dir); + cfg->vm_scope = AP_LUA_SCOPE_UNSET; + cfg->codecache = AP_LUA_CACHE_UNSET; + cfg->vm_min = 0; + cfg->vm_max = 0; + cfg->inherit = AP_LUA_INHERIT_UNSET; + + return cfg; +} + +static int create_request_config(request_rec *r) +{ + ap_lua_request_cfg *cfg = apr_palloc(r->pool, sizeof(ap_lua_request_cfg)); + cfg->mapped_request_details = NULL; + cfg->request_scoped_vms = apr_hash_make(r->pool); + ap_set_module_config(r->request_config, &lua_module, cfg); + return OK; +} + +static void *create_server_config(apr_pool_t *p, server_rec *s) +{ + + ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg)); + cfg->root_path = NULL; + + return cfg; +} + +static int lua_request_hook(lua_State *L, request_rec *r) +{ + ap_lua_push_request(L, r); + return OK; +} + +static int lua_pre_config(apr_pool_t *pconf, apr_pool_t *plog, + apr_pool_t *ptemp) +{ + ap_mutex_register(pconf, "lua-ivm-shm", NULL, APR_LOCK_DEFAULT, 0); + return OK; +} + +static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog, + apr_pool_t *ptemp, server_rec *s) +{ + apr_pool_t **pool; + apr_status_t rs; + + if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) + return OK; + + /* Create ivm mutex */ + rs = ap_global_mutex_create(&lua_ivm_mutex, NULL, "lua-ivm-shm", NULL, + s, pconf, 0); + if (APR_SUCCESS != rs) { + return HTTP_INTERNAL_SERVER_ERROR; + } + + /* Create shared memory space, anonymous first if possible. */ + rs = apr_shm_create(&lua_ivm_shm, sizeof pool, NULL, pconf); + if (APR_STATUS_IS_ENOTIMPL(rs)) { + /* Fall back to filename-based; nuke any left-over first. */ + lua_ivm_shmfile = ap_runtime_dir_relative(pconf, DEFAULT_LUA_SHMFILE); + + apr_shm_remove(lua_ivm_shmfile, pconf); + + rs = apr_shm_create(&lua_ivm_shm, sizeof pool, lua_ivm_shmfile, pconf); + } + if (rs != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_ERR, rs, s, APLOGNO(02665) + "mod_lua: Failed to create shared memory segment on file %s", + lua_ivm_shmfile ? lua_ivm_shmfile : "(anonymous)"); + return HTTP_INTERNAL_SERVER_ERROR; + } + pool = (apr_pool_t **)apr_shm_baseaddr_get(lua_ivm_shm); + apr_pool_create(pool, pconf); + apr_pool_tag(*pool, "mod_lua-shared"); + apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper, + apr_pool_cleanup_null); + return OK; +} +static void *overlay_hook_specs(apr_pool_t *p, + const void *key, + apr_ssize_t klen, + const void *overlay_val, + const void *base_val, + const void *data) +{ + const apr_array_header_t *overlay_info = (const apr_array_header_t*)overlay_val; + const apr_array_header_t *base_info = (const apr_array_header_t*)base_val; + return apr_array_append(p, base_info, overlay_info); +} + +static void *merge_dir_config(apr_pool_t *p, void *basev, void *overridesv) +{ + ap_lua_dir_cfg *a, *base, *overrides; + + a = (ap_lua_dir_cfg *)apr_pcalloc(p, sizeof(ap_lua_dir_cfg)); + base = (ap_lua_dir_cfg*)basev; + overrides = (ap_lua_dir_cfg*)overridesv; + + a->pool = overrides->pool; + a->dir = apr_pstrdup(p, overrides->dir); + + a->vm_scope = (overrides->vm_scope == AP_LUA_SCOPE_UNSET) ? base->vm_scope: overrides->vm_scope; + a->inherit = (overrides->inherit == AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit; + a->codecache = (overrides->codecache == AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache; + + a->vm_min = (overrides->vm_min == 0) ? base->vm_min : overrides->vm_min; + a->vm_max = (overrides->vm_max == 0) ? base->vm_max : overrides->vm_max; + + if (a->inherit == AP_LUA_INHERIT_UNSET || a->inherit == AP_LUA_INHERIT_PARENT_FIRST) { + a->package_paths = apr_array_append(p, base->package_paths, overrides->package_paths); + a->package_cpaths = apr_array_append(p, base->package_cpaths, overrides->package_cpaths); + a->mapped_handlers = apr_array_append(p, base->mapped_handlers, overrides->mapped_handlers); + a->mapped_filters = apr_array_append(p, base->mapped_filters, overrides->mapped_filters); + a->hooks = apr_hash_merge(p, overrides->hooks, base->hooks, overlay_hook_specs, NULL); + } + else if (a->inherit == AP_LUA_INHERIT_PARENT_LAST) { + a->package_paths = apr_array_append(p, overrides->package_paths, base->package_paths); + a->package_cpaths = apr_array_append(p, overrides->package_cpaths, base->package_cpaths); + a->mapped_handlers = apr_array_append(p, overrides->mapped_handlers, base->mapped_handlers); + a->mapped_filters = apr_array_append(p, overrides->mapped_filters, base->mapped_filters); + a->hooks = apr_hash_merge(p, base->hooks, overrides->hooks, overlay_hook_specs, NULL); + } + else { + a->package_paths = overrides->package_paths; + a->package_cpaths = overrides->package_cpaths; + a->mapped_handlers= overrides->mapped_handlers; + a->mapped_filters= overrides->mapped_filters; + a->hooks= overrides->hooks; + } + + return a; +} + +static void lua_register_hooks(apr_pool_t *p) +{ + /* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */ + ap_hook_handler(lua_handler, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_create_request(create_request_config, NULL, NULL, + APR_HOOK_MIDDLE); + + /* http_request.h hooks */ + ap_hook_pre_translate_name(lua_pre_trans_name_harness, NULL, NULL, + APR_HOOK_MIDDLE); + + ap_hook_translate_name(lua_translate_name_harness_first, NULL, NULL, + AP_LUA_HOOK_FIRST); + ap_hook_translate_name(lua_translate_name_harness, NULL, NULL, + APR_HOOK_MIDDLE); + ap_hook_translate_name(lua_translate_name_harness_last, NULL, NULL, + AP_LUA_HOOK_LAST); + + ap_hook_fixups(lua_fixup_harness, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL, + APR_HOOK_MIDDLE); + +/* XXX: Does not work :( + * ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL, + AP_LUA_HOOK_FIRST); + */ + ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL, + APR_HOOK_MIDDLE); +/* XXX: Does not work :( + * ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL, + AP_LUA_HOOK_LAST); +*/ + ap_hook_type_checker(lua_type_checker_harness, NULL, NULL, + APR_HOOK_MIDDLE); + + ap_hook_access_checker(lua_access_checker_harness_first, NULL, NULL, + AP_LUA_HOOK_FIRST); + ap_hook_access_checker(lua_access_checker_harness, NULL, NULL, + APR_HOOK_MIDDLE); + ap_hook_access_checker(lua_access_checker_harness_last, NULL, NULL, + AP_LUA_HOOK_LAST); + ap_hook_auth_checker(lua_auth_checker_harness_first, NULL, NULL, + AP_LUA_HOOK_FIRST); + ap_hook_auth_checker(lua_auth_checker_harness, NULL, NULL, + APR_HOOK_MIDDLE); + ap_hook_auth_checker(lua_auth_checker_harness_last, NULL, NULL, + AP_LUA_HOOK_LAST); + + ap_hook_insert_filter(lua_insert_filter_harness, NULL, NULL, + APR_HOOK_MIDDLE); + ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST); + + ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_pre_config(lua_pre_config, NULL, NULL, APR_HOOK_MIDDLE); + + APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL, + APR_HOOK_REALLY_FIRST); + + APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL, + APR_HOOK_REALLY_FIRST); + ap_hook_handler(lua_map_handler, NULL, NULL, AP_LUA_HOOK_FIRST); + + /* Hook this right before FallbackResource kicks in */ + ap_hook_fixups(lua_map_handler_fixups, NULL, NULL, AP_LUA_HOOK_LAST-2); + ap_hook_child_init(ap_lua_init_mutex, NULL, NULL, APR_HOOK_MIDDLE); + + /* providers */ + lua_authz_providers = apr_hash_make(p); + + /* Logging catcher */ + ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL, + APR_HOOK_FIRST); +} + +AP_DECLARE_MODULE(lua) = { + STANDARD20_MODULE_STUFF, + create_dir_config, /* create per-dir config structures */ + merge_dir_config, /* merge per-dir config structures */ + create_server_config, /* create per-server config structures */ + NULL, /* merge per-server config structures */ + lua_commands, /* table of config file commands */ + lua_register_hooks /* register hooks */ +}; diff --git a/modules/lua/mod_lua.dep b/modules/lua/mod_lua.dep new file mode 100644 index 0000000..126b7ce --- /dev/null +++ b/modules/lua/mod_lua.dep @@ -0,0 +1,418 @@ +# Microsoft Developer Studio Generated Dependency File, included by mod_lua.mak + +..\..\build\win32\httpd.rc : \ + "..\..\include\ap_release.h"\ + + +.\lua_apr.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_mpm.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_main.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\os.h"\ + "..\..\include\scoreboard.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_base64.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_md5.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_sha1.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apr_xlate.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + ".\lua_apr.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + + +.\lua_config.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\os.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + ".\lua_config.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + + +.\lua_dbd.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\os.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_dbd.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + "..\database\mod_dbd.h"\ + ".\lua_dbd.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + + +.\lua_passwd.c : \ + "..\..\srclib\apr-util\include\apr_md5.h"\ + "..\..\srclib\apr-util\include\apr_sha1.h"\ + "..\..\srclib\apr-util\include\apr_xlate.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr-util\include\apu_version.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_lib.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_version.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + ".\lua_passwd.h"\ + + +.\lua_request.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_mpm.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_main.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\os.h"\ + "..\..\include\scoreboard.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_cookies.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_md5.h"\ + "..\..\include\util_script.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_base64.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_date.h"\ + "..\..\srclib\apr-util\include\apr_dbd.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_md5.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_sha1.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apr_xlate.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr-util\include\apu_version.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_lib.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_version.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + "..\database\mod_dbd.h"\ + ".\lua_apr.h"\ + ".\lua_dbd.h"\ + ".\lua_passwd.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + + +.\lua_vmprep.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\mod_auth.h"\ + "..\..\include\os.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apr_uuid.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + ".\lua_config.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + + +.\mod_lua.c : \ + "..\..\include\ap_config.h"\ + "..\..\include\ap_config_layout.h"\ + "..\..\include\ap_expr.h"\ + "..\..\include\ap_hooks.h"\ + "..\..\include\ap_mmn.h"\ + "..\..\include\ap_mpm.h"\ + "..\..\include\ap_regex.h"\ + "..\..\include\ap_release.h"\ + "..\..\include\apache_noprobes.h"\ + "..\..\include\http_config.h"\ + "..\..\include\http_core.h"\ + "..\..\include\http_log.h"\ + "..\..\include\http_main.h"\ + "..\..\include\http_protocol.h"\ + "..\..\include\http_request.h"\ + "..\..\include\httpd.h"\ + "..\..\include\mod_auth.h"\ + "..\..\include\os.h"\ + "..\..\include\scoreboard.h"\ + "..\..\include\util_cfgtree.h"\ + "..\..\include\util_filter.h"\ + "..\..\include\util_mutex.h"\ + "..\..\include\util_varbuf.h"\ + "..\..\srclib\apr-util\include\apr_base64.h"\ + "..\..\srclib\apr-util\include\apr_buckets.h"\ + "..\..\srclib\apr-util\include\apr_hooks.h"\ + "..\..\srclib\apr-util\include\apr_md5.h"\ + "..\..\srclib\apr-util\include\apr_optional.h"\ + "..\..\srclib\apr-util\include\apr_optional_hooks.h"\ + "..\..\srclib\apr-util\include\apr_reslist.h"\ + "..\..\srclib\apr-util\include\apr_sha1.h"\ + "..\..\srclib\apr-util\include\apr_uri.h"\ + "..\..\srclib\apr-util\include\apr_xlate.h"\ + "..\..\srclib\apr-util\include\apu.h"\ + "..\..\srclib\apr\include\apr.h"\ + "..\..\srclib\apr\include\apr_allocator.h"\ + "..\..\srclib\apr\include\apr_dso.h"\ + "..\..\srclib\apr\include\apr_errno.h"\ + "..\..\srclib\apr\include\apr_file_info.h"\ + "..\..\srclib\apr\include\apr_file_io.h"\ + "..\..\srclib\apr\include\apr_general.h"\ + "..\..\srclib\apr\include\apr_global_mutex.h"\ + "..\..\srclib\apr\include\apr_hash.h"\ + "..\..\srclib\apr\include\apr_inherit.h"\ + "..\..\srclib\apr\include\apr_mmap.h"\ + "..\..\srclib\apr\include\apr_network_io.h"\ + "..\..\srclib\apr\include\apr_poll.h"\ + "..\..\srclib\apr\include\apr_pools.h"\ + "..\..\srclib\apr\include\apr_portable.h"\ + "..\..\srclib\apr\include\apr_proc_mutex.h"\ + "..\..\srclib\apr\include\apr_ring.h"\ + "..\..\srclib\apr\include\apr_shm.h"\ + "..\..\srclib\apr\include\apr_strings.h"\ + "..\..\srclib\apr\include\apr_tables.h"\ + "..\..\srclib\apr\include\apr_thread_mutex.h"\ + "..\..\srclib\apr\include\apr_thread_proc.h"\ + "..\..\srclib\apr\include\apr_thread_rwlock.h"\ + "..\..\srclib\apr\include\apr_time.h"\ + "..\..\srclib\apr\include\apr_user.h"\ + "..\..\srclib\apr\include\apr_want.h"\ + "..\ssl\mod_ssl.h"\ + ".\lua_apr.h"\ + ".\lua_config.h"\ + ".\lua_request.h"\ + ".\lua_vmprep.h"\ + ".\mod_lua.h"\ + diff --git a/modules/lua/mod_lua.dsp b/modules/lua/mod_lua.dsp new file mode 100644 index 0000000..ef3294e --- /dev/null +++ b/modules/lua/mod_lua.dsp @@ -0,0 +1,163 @@ +# Microsoft Developer Studio Project File - Name="mod_lua" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 + +CFG=mod_lua - Win32 Release +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "mod_lua.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "mod_lua.mak" CFG="mod_lua - Win32 Release" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "mod_lua - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "mod_lua - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "mod_lua - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MD /W3 /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /FD /c +# ADD CPP /nologo /MD /W3 /O2 /Oy- /Zi /I "../../include" /I "../../srclib/apr/include" /I "../../srclib/apr-util/include" /I "../../srclib/lua/src" /I "../ssl" /I "../database" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "AP_LUA_DECLARE_EXPORT" /Fd"Release\mod_lua_src" /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /fo"Release/mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /d "NDEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib ws2_32.lib /nologo /subsystem:windows /dll /out:".\Release\mod_lua.so" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so +# ADD LINK32 kernel32.lib ws2_32.lib lua51.lib /nologo /subsystem:windows /dll /incremental:no /debug /out:".\Release\mod_lua.so" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so /opt:ref /libpath:"../../srclib/lua/src" +# Begin Special Build Tool +TargetPath=.\Release\mod_lua.so +SOURCE="$(InputPath)" +PostBuild_Desc=Embed .manifest +PostBuild_Cmds=if exist $(TargetPath).manifest mt.exe -manifest $(TargetPath).manifest -outputresource:$(TargetPath);2 +# End Special Build Tool + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MDd /W3 /EHsc /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /FD /c +# ADD CPP /nologo /MDd /W3 /EHsc /Zi /Od /I "../../include" /I "../../srclib/apr/include" /I "../../srclib/apr-util/include" /I "../../srclib/lua/src" /I "../ssl" /I "../database" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "AP_LUA_DECLARE_EXPORT" /Fd"Debug\mod_lua_src" /FD /c +# ADD BASE MTL /nologo /D "_DEBUG" /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /fo"Debug/mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /d "_DEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib ws2_32.lib /nologo /subsystem:windows /dll /incremental:no /debug /out:".\Debug\mod_lua.so" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so +# ADD LINK32 kernel32.lib ws2_32.lib lua51.lib /nologo /subsystem:windows /dll /incremental:no /debug /out:".\Debug\mod_lua.so" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so /libpath:"../../srclib/lua/src" +# Begin Special Build Tool +TargetPath=.\Debug\mod_lua.so +SOURCE="$(InputPath)" +PostBuild_Desc=Embed .manifest +PostBuild_Cmds=if exist $(TargetPath).manifest mt.exe -manifest $(TargetPath).manifest -outputresource:$(TargetPath);2 +# End Special Build Tool + +!ENDIF + +# Begin Target + +# Name "mod_lua - Win32 Release" +# Name "mod_lua - Win32 Debug" +# Begin Source File + +SOURCE=.\lua_apr.c +# End Source File +# Begin Source File + +SOURCE=.\lua_apr.h +# End Source File +# Begin Source File + +SOURCE=.\lua_config.c +# End Source File +# Begin Source File + +SOURCE=.\lua_config.h +# End Source File +# Begin Source File + +SOURCE=.\lua_passwd.c +# End Source File +# Begin Source File + +SOURCE=.\lua_passwd.h +# End Source File +# Begin Source File + +SOURCE=.\lua_request.c +# End Source File +# Begin Source File + +SOURCE=.\lua_request.h +# End Source File +# Begin Source File + +SOURCE=.\lua_vmprep.c +# End Source File +# Begin Source File + +SOURCE=.\lua_vmprep.h +# End Source File +# Begin Source File + +SOURCE=.\mod_lua.c +# End Source File +# Begin Source File + +SOURCE=.\mod_lua.h +# End Source File +# Begin Source File + +SOURCE=.\lua_dbd.c +# End Source File +# Begin Source File + +SOURCE=.\lua_dbd.h +# End Source File +# Begin Source File + +SOURCE=..\..\build\win32\httpd.rc +# End Source File +# End Target +# End Project diff --git a/modules/lua/mod_lua.h b/modules/lua/mod_lua.h new file mode 100644 index 0000000..33807fb --- /dev/null +++ b/modules/lua/mod_lua.h @@ -0,0 +1,187 @@ +/** + * 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 _MOD_LUA_H_ +#define _MOD_LUA_H_ + +#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 "http_ssl.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 "apr_reslist.h" + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#if LUA_VERSION_NUM > 501 +/* Load mode for lua_load() */ +#define lua_load(a,b,c,d) lua_load(a,b,c,d,NULL) + +#if LUA_VERSION_NUM > 503 +#define lua_resume(a,b,c) lua_resume(a, NULL, b, c) +#else +/* ### For version < 5.4, assume that exactly one stack item is on the + * stack, which is what the code did before but seems dubious. */ +#define lua_resume(a,b,c) (*(c) = 1, lua_resume(a, NULL, b)) +#endif + +#define luaL_setfuncs_compat(a,b) luaL_setfuncs(a,b,0) +#else +#define lua_rawlen(L,i) lua_objlen(L, (i)) +#define luaL_setfuncs_compat(a,b) luaL_register(a,NULL,b) +#define lua_resume(a,b,c) (*(c) = 1, lua_resume(a, b)) +#endif +#if LUA_VERSION_NUM > 502 +#define lua_dump(a,b,c) lua_dump(a,b,c,0) +#endif + +/* Create a set of AP_LUA_DECLARE(type), AP_LUA_DECLARE_NONSTD(type) and + * AP_LUA_DECLARE_DATA with appropriate export and import tags for the platform + */ +#if !defined(WIN32) +#define AP_LUA_DECLARE(type) type +#define AP_LUA_DECLARE_NONSTD(type) type +#define AP_LUA_DECLARE_DATA +#elif defined(AP_LUA_DECLARE_STATIC) +#define AP_LUA_DECLARE(type) type __stdcall +#define AP_LUA_DECLARE_NONSTD(type) type +#define AP_LUA_DECLARE_DATA +#elif defined(AP_LUA_DECLARE_EXPORT) +#define AP_LUA_DECLARE(type) __declspec(dllexport) type __stdcall +#define AP_LUA_DECLARE_NONSTD(type) __declspec(dllexport) type +#define AP_LUA_DECLARE_DATA __declspec(dllexport) +#else +#define AP_LUA_DECLARE(type) __declspec(dllimport) type __stdcall +#define AP_LUA_DECLARE_NONSTD(type) __declspec(dllimport) type +#define AP_LUA_DECLARE_DATA __declspec(dllimport) +#endif + + +#include "lua_request.h" +#include "lua_vmprep.h" + +typedef enum { + AP_LUA_INHERIT_UNSET = -1, + AP_LUA_INHERIT_NONE = 0, + AP_LUA_INHERIT_PARENT_FIRST = 1, + AP_LUA_INHERIT_PARENT_LAST = 2 +} ap_lua_inherit_t; + +/** + * 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 ap_lua_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/filters + */ + apr_array_header_t *mapped_handlers; + apr_array_header_t *mapped_filters; + + apr_pool_t *pool; + + /** + * AP_LUA_SCOPE_ONCE | AP_LUA_SCOPE_REQUEST | AP_LUA_SCOPE_CONN | AP_LUA_SCOPE_SERVER + */ + unsigned int vm_scope; + unsigned int vm_min; + unsigned int vm_max; + + /* info for the hook harnesses */ + apr_hash_t *hooks; /* <wombat_hook_info> */ + + /* the actual directory being configured */ + const char *dir; + + /* Whether Lua scripts in a sub-dir are run before parents */ + ap_lua_inherit_t inherit; + + /** + * AP_LUA_CACHE_NEVER | AP_LUA_CACHE_STAT | AP_LUA_CACHE_FOREVER + */ + unsigned int codecache; + +} ap_lua_dir_cfg; + +typedef struct +{ + /* value of the LuaRoot directive */ + const char *root_path; +} ap_lua_server_cfg; + +typedef struct +{ + const char *function_name; + ap_lua_vm_spec *spec; +} mapped_request_details; + +typedef struct +{ + mapped_request_details *mapped_request_details; + apr_hash_t *request_scoped_vms; +} ap_lua_request_cfg; + +typedef struct +{ + lua_State *L; + const char *function; +} ap_lua_filter_ctx; + +extern module AP_MODULE_DECLARE_DATA lua_module; + +APR_DECLARE_EXTERNAL_HOOK(ap_lua, AP_LUA, int, lua_open, + (lua_State *L, apr_pool_t *p)) + +APR_DECLARE_EXTERNAL_HOOK(ap_lua, AP_LUA, int, lua_request, + (lua_State *L, request_rec *r)) + +const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, + request_rec *r, const char *var); + +int ap_lua_ssl_is_https(conn_rec *c); + +#endif /* !_MOD_LUA_H_ */ diff --git a/modules/lua/mod_lua.mak b/modules/lua/mod_lua.mak new file mode 100644 index 0000000..114d6bd --- /dev/null +++ b/modules/lua/mod_lua.mak @@ -0,0 +1,407 @@ +# Microsoft Developer Studio Generated NMAKE File, Based on mod_lua.dsp +!IF "$(CFG)" == "" +CFG=mod_lua - Win32 Release +!MESSAGE No configuration specified. Defaulting to mod_lua - Win32 Release. +!ENDIF + +!IF "$(CFG)" != "mod_lua - Win32 Release" && "$(CFG)" != "mod_lua - Win32 Debug" +!MESSAGE Invalid configuration "$(CFG)" specified. +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "mod_lua.mak" CFG="mod_lua - Win32 Release" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "mod_lua - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "mod_lua - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE +!ERROR An invalid configuration is specified. +!ENDIF + +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +!IF "$(CFG)" == "mod_lua - Win32 Release" + +OUTDIR=.\Release +INTDIR=.\Release +DS_POSTBUILD_DEP=$(INTDIR)\postbld.dep +# Begin Custom Macros +OutDir=.\Release +# End Custom Macros + +!IF "$(RECURSE)" == "0" + +ALL : "$(OUTDIR)\mod_lua.so" "$(DS_POSTBUILD_DEP)" + +!ELSE + +ALL : "libhttpd - Win32 Release" "libaprutil - Win32 Release" "libapr - Win32 Release" "$(OUTDIR)\mod_lua.so" "$(DS_POSTBUILD_DEP)" + +!ENDIF + +!IF "$(RECURSE)" == "1" +CLEAN :"libapr - Win32 ReleaseCLEAN" "libaprutil - Win32 ReleaseCLEAN" "libhttpd - Win32 ReleaseCLEAN" +!ELSE +CLEAN : +!ENDIF + -@erase "$(INTDIR)\lua_apr.obj" + -@erase "$(INTDIR)\lua_config.obj" + -@erase "$(INTDIR)\lua_dbd.obj" + -@erase "$(INTDIR)\lua_passwd.obj" + -@erase "$(INTDIR)\lua_request.obj" + -@erase "$(INTDIR)\lua_vmprep.obj" + -@erase "$(INTDIR)\mod_lua.obj" + -@erase "$(INTDIR)\mod_lua.res" + -@erase "$(INTDIR)\mod_lua_src.idb" + -@erase "$(INTDIR)\mod_lua_src.pdb" + -@erase "$(OUTDIR)\mod_lua.exp" + -@erase "$(OUTDIR)\mod_lua.lib" + -@erase "$(OUTDIR)\mod_lua.pdb" + -@erase "$(OUTDIR)\mod_lua.so" + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP=cl.exe +CPP_PROJ=/nologo /MD /W3 /Zi /O2 /Oy- /I "../../include" /I "../../srclib/apr/include" /I "../../srclib/apr-util/include" /I "../../srclib/lua/src" /I "../ssl" /I "../database" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "AP_LUA_DECLARE_EXPORT" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\mod_lua_src" /FD /c + +.c{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.c{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +MTL=midl.exe +MTL_PROJ=/nologo /D "NDEBUG" /mktyplib203 /win32 +RSC=rc.exe +RSC_PROJ=/l 0x409 /fo"$(INTDIR)\mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /d "NDEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" +BSC32=bscmake.exe +BSC32_FLAGS=/nologo /o"$(OUTDIR)\mod_lua.bsc" +BSC32_SBRS= \ + +LINK32=link.exe +LINK32_FLAGS=kernel32.lib ws2_32.lib lua51.lib /nologo /subsystem:windows /dll /incremental:no /pdb:"$(OUTDIR)\mod_lua.pdb" /debug /out:"$(OUTDIR)\mod_lua.so" /implib:"$(OUTDIR)\mod_lua.lib" /libpath:"../../srclib/lua/src" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so /opt:ref +LINK32_OBJS= \ + "$(INTDIR)\lua_apr.obj" \ + "$(INTDIR)\lua_config.obj" \ + "$(INTDIR)\lua_passwd.obj" \ + "$(INTDIR)\lua_request.obj" \ + "$(INTDIR)\lua_vmprep.obj" \ + "$(INTDIR)\mod_lua.obj" \ + "$(INTDIR)\lua_dbd.obj" \ + "$(INTDIR)\mod_lua.res" \ + "..\..\srclib\apr\Release\libapr-1.lib" \ + "..\..\srclib\apr-util\Release\libaprutil-1.lib" \ + "..\..\Release\libhttpd.lib" + +"$(OUTDIR)\mod_lua.so" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS) + $(LINK32) @<< + $(LINK32_FLAGS) $(LINK32_OBJS) +<< + +TargetPath=.\Release\mod_lua.so +SOURCE="$(InputPath)" +PostBuild_Desc=Embed .manifest +DS_POSTBUILD_DEP=$(INTDIR)\postbld.dep + +# Begin Custom Macros +OutDir=.\Release +# End Custom Macros + +"$(DS_POSTBUILD_DEP)" : "$(OUTDIR)\mod_lua.so" + if exist .\Release\mod_lua.so.manifest mt.exe -manifest .\Release\mod_lua.so.manifest -outputresource:.\Release\mod_lua.so;2 + echo Helper for Post-build step > "$(DS_POSTBUILD_DEP)" + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + +OUTDIR=.\Debug +INTDIR=.\Debug +DS_POSTBUILD_DEP=$(INTDIR)\postbld.dep +# Begin Custom Macros +OutDir=.\Debug +# End Custom Macros + +!IF "$(RECURSE)" == "0" + +ALL : "$(OUTDIR)\mod_lua.so" "$(DS_POSTBUILD_DEP)" + +!ELSE + +ALL : "libhttpd - Win32 Debug" "libaprutil - Win32 Debug" "libapr - Win32 Debug" "$(OUTDIR)\mod_lua.so" "$(DS_POSTBUILD_DEP)" + +!ENDIF + +!IF "$(RECURSE)" == "1" +CLEAN :"libapr - Win32 DebugCLEAN" "libaprutil - Win32 DebugCLEAN" "libhttpd - Win32 DebugCLEAN" +!ELSE +CLEAN : +!ENDIF + -@erase "$(INTDIR)\lua_apr.obj" + -@erase "$(INTDIR)\lua_config.obj" + -@erase "$(INTDIR)\lua_dbd.obj" + -@erase "$(INTDIR)\lua_passwd.obj" + -@erase "$(INTDIR)\lua_request.obj" + -@erase "$(INTDIR)\lua_vmprep.obj" + -@erase "$(INTDIR)\mod_lua.obj" + -@erase "$(INTDIR)\mod_lua.res" + -@erase "$(INTDIR)\mod_lua_src.idb" + -@erase "$(INTDIR)\mod_lua_src.pdb" + -@erase "$(OUTDIR)\mod_lua.exp" + -@erase "$(OUTDIR)\mod_lua.lib" + -@erase "$(OUTDIR)\mod_lua.pdb" + -@erase "$(OUTDIR)\mod_lua.so" + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP=cl.exe +CPP_PROJ=/nologo /MDd /W3 /Zi /Od /I "../../include" /I "../../srclib/apr/include" /I "../../srclib/apr-util/include" /I "../../srclib/lua/src" /I "../ssl" /I "../database" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "AP_LUA_DECLARE_EXPORT" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\mod_lua_src" /FD /EHsc /c + +.c{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.c{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(INTDIR)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +MTL=midl.exe +MTL_PROJ=/nologo /D "_DEBUG" /mktyplib203 /win32 +RSC=rc.exe +RSC_PROJ=/l 0x409 /fo"$(INTDIR)\mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /d "_DEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" +BSC32=bscmake.exe +BSC32_FLAGS=/nologo /o"$(OUTDIR)\mod_lua.bsc" +BSC32_SBRS= \ + +LINK32=link.exe +LINK32_FLAGS=kernel32.lib ws2_32.lib lua51.lib /nologo /subsystem:windows /dll /incremental:no /pdb:"$(OUTDIR)\mod_lua.pdb" /debug /out:"$(OUTDIR)\mod_lua.so" /implib:"$(OUTDIR)\mod_lua.lib" /libpath:"../../srclib/lua/src" /base:@..\..\os\win32\BaseAddr.ref,mod_lua.so +LINK32_OBJS= \ + "$(INTDIR)\lua_apr.obj" \ + "$(INTDIR)\lua_config.obj" \ + "$(INTDIR)\lua_passwd.obj" \ + "$(INTDIR)\lua_request.obj" \ + "$(INTDIR)\lua_vmprep.obj" \ + "$(INTDIR)\mod_lua.obj" \ + "$(INTDIR)\lua_dbd.obj" \ + "$(INTDIR)\mod_lua.res" \ + "..\..\srclib\apr\Debug\libapr-1.lib" \ + "..\..\srclib\apr-util\Debug\libaprutil-1.lib" \ + "..\..\Debug\libhttpd.lib" + +"$(OUTDIR)\mod_lua.so" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS) + $(LINK32) @<< + $(LINK32_FLAGS) $(LINK32_OBJS) +<< + +TargetPath=.\Debug\mod_lua.so +SOURCE="$(InputPath)" +PostBuild_Desc=Embed .manifest +DS_POSTBUILD_DEP=$(INTDIR)\postbld.dep + +# Begin Custom Macros +OutDir=.\Debug +# End Custom Macros + +"$(DS_POSTBUILD_DEP)" : "$(OUTDIR)\mod_lua.so" + if exist .\Debug\mod_lua.so.manifest mt.exe -manifest .\Debug\mod_lua.so.manifest -outputresource:.\Debug\mod_lua.so;2 + echo Helper for Post-build step > "$(DS_POSTBUILD_DEP)" + +!ENDIF + + +!IF "$(NO_EXTERNAL_DEPS)" != "1" +!IF EXISTS("mod_lua.dep") +!INCLUDE "mod_lua.dep" +!ELSE +!MESSAGE Warning: cannot find "mod_lua.dep" +!ENDIF +!ENDIF + + +!IF "$(CFG)" == "mod_lua - Win32 Release" || "$(CFG)" == "mod_lua - Win32 Debug" + +!IF "$(CFG)" == "mod_lua - Win32 Release" + +"libapr - Win32 Release" : + cd ".\..\..\srclib\apr" + $(MAKE) /$(MAKEFLAGS) /F ".\libapr.mak" CFG="libapr - Win32 Release" + cd "..\..\modules\lua" + +"libapr - Win32 ReleaseCLEAN" : + cd ".\..\..\srclib\apr" + $(MAKE) /$(MAKEFLAGS) /F ".\libapr.mak" CFG="libapr - Win32 Release" RECURSE=1 CLEAN + cd "..\..\modules\lua" + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + +"libapr - Win32 Debug" : + cd ".\..\..\srclib\apr" + $(MAKE) /$(MAKEFLAGS) /F ".\libapr.mak" CFG="libapr - Win32 Debug" + cd "..\..\modules\lua" + +"libapr - Win32 DebugCLEAN" : + cd ".\..\..\srclib\apr" + $(MAKE) /$(MAKEFLAGS) /F ".\libapr.mak" CFG="libapr - Win32 Debug" RECURSE=1 CLEAN + cd "..\..\modules\lua" + +!ENDIF + +!IF "$(CFG)" == "mod_lua - Win32 Release" + +"libaprutil - Win32 Release" : + cd ".\..\..\srclib\apr-util" + $(MAKE) /$(MAKEFLAGS) /F ".\libaprutil.mak" CFG="libaprutil - Win32 Release" + cd "..\..\modules\lua" + +"libaprutil - Win32 ReleaseCLEAN" : + cd ".\..\..\srclib\apr-util" + $(MAKE) /$(MAKEFLAGS) /F ".\libaprutil.mak" CFG="libaprutil - Win32 Release" RECURSE=1 CLEAN + cd "..\..\modules\lua" + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + +"libaprutil - Win32 Debug" : + cd ".\..\..\srclib\apr-util" + $(MAKE) /$(MAKEFLAGS) /F ".\libaprutil.mak" CFG="libaprutil - Win32 Debug" + cd "..\..\modules\lua" + +"libaprutil - Win32 DebugCLEAN" : + cd ".\..\..\srclib\apr-util" + $(MAKE) /$(MAKEFLAGS) /F ".\libaprutil.mak" CFG="libaprutil - Win32 Debug" RECURSE=1 CLEAN + cd "..\..\modules\lua" + +!ENDIF + +!IF "$(CFG)" == "mod_lua - Win32 Release" + +"libhttpd - Win32 Release" : + cd ".\..\.." + $(MAKE) /$(MAKEFLAGS) /F ".\libhttpd.mak" CFG="libhttpd - Win32 Release" + cd ".\modules\lua" + +"libhttpd - Win32 ReleaseCLEAN" : + cd ".\..\.." + $(MAKE) /$(MAKEFLAGS) /F ".\libhttpd.mak" CFG="libhttpd - Win32 Release" RECURSE=1 CLEAN + cd ".\modules\lua" + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + +"libhttpd - Win32 Debug" : + cd ".\..\.." + $(MAKE) /$(MAKEFLAGS) /F ".\libhttpd.mak" CFG="libhttpd - Win32 Debug" + cd ".\modules\lua" + +"libhttpd - Win32 DebugCLEAN" : + cd ".\..\.." + $(MAKE) /$(MAKEFLAGS) /F ".\libhttpd.mak" CFG="libhttpd - Win32 Debug" RECURSE=1 CLEAN + cd ".\modules\lua" + +!ENDIF + +SOURCE=..\..\build\win32\httpd.rc + +!IF "$(CFG)" == "mod_lua - Win32 Release" + + +"$(INTDIR)\mod_lua.res" : $(SOURCE) "$(INTDIR)" + $(RSC) /l 0x409 /fo"$(INTDIR)\mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /i "../../build\win32" /d "NDEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" $(SOURCE) + + +!ELSEIF "$(CFG)" == "mod_lua - Win32 Debug" + + +"$(INTDIR)\mod_lua.res" : $(SOURCE) "$(INTDIR)" + $(RSC) /l 0x409 /fo"$(INTDIR)\mod_lua.res" /i "../../include" /i "../../srclib/apr/include" /i "../../build\win32" /d "_DEBUG" /d BIN_NAME="mod_lua.so" /d LONG_NAME="lua_module for Apache" $(SOURCE) + + +!ENDIF + +SOURCE=.\lua_apr.c + +"$(INTDIR)\lua_apr.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\lua_config.c + +"$(INTDIR)\lua_config.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\lua_dbd.c + +"$(INTDIR)\lua_dbd.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\lua_passwd.c + +"$(INTDIR)\lua_passwd.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\lua_request.c + +"$(INTDIR)\lua_request.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\lua_vmprep.c + +"$(INTDIR)\lua_vmprep.obj" : $(SOURCE) "$(INTDIR)" + + +SOURCE=.\mod_lua.c + +"$(INTDIR)\mod_lua.obj" : $(SOURCE) "$(INTDIR)" + + + +!ENDIF + diff --git a/modules/lua/test/helpers.lua b/modules/lua/test/helpers.lua new file mode 100644 index 0000000..79bd269 --- /dev/null +++ b/modules/lua/test/helpers.lua @@ -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/lua/test/htdocs/config_tests.lua b/modules/lua/test/htdocs/config_tests.lua new file mode 100644 index 0000000..698bedf --- /dev/null +++ b/modules/lua/test/htdocs/config_tests.lua @@ -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/lua/test/htdocs/filters.lua b/modules/lua/test/htdocs/filters.lua new file mode 100644 index 0000000..cad0ca8 --- /dev/null +++ b/modules/lua/test/htdocs/filters.lua @@ -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/lua/test/htdocs/find_me.txt b/modules/lua/test/htdocs/find_me.txt new file mode 100644 index 0000000..cb96e9e --- /dev/null +++ b/modules/lua/test/htdocs/find_me.txt @@ -0,0 +1 @@ +please find me
\ No newline at end of file diff --git a/modules/lua/test/htdocs/headers.lua b/modules/lua/test/htdocs/headers.lua new file mode 100644 index 0000000..35938ea --- /dev/null +++ b/modules/lua/test/htdocs/headers.lua @@ -0,0 +1,6 @@ +function handle(r) + local host = r.headers_in['host'] + r:debug(host) + r:puts(host) + r.headers_out['wombat'] = 'lua' +end diff --git a/modules/lua/test/htdocs/hooks.lua b/modules/lua/test/htdocs/hooks.lua new file mode 100644 index 0000000..b8a8248 --- /dev/null +++ b/modules/lua/test/htdocs/hooks.lua @@ -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/lua/test/htdocs/other.lua b/modules/lua/test/htdocs/other.lua new file mode 100644 index 0000000..90c6ed2 --- /dev/null +++ b/modules/lua/test/htdocs/other.lua @@ -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/lua/test/htdocs/simple.lua b/modules/lua/test/htdocs/simple.lua new file mode 100644 index 0000000..a3f8861 --- /dev/null +++ b/modules/lua/test/htdocs/simple.lua @@ -0,0 +1,4 @@ +function handle(r) + r.content_type = "text/plain" + r:puts("Hi there!") +end diff --git a/modules/lua/test/htdocs/test.lua b/modules/lua/test/htdocs/test.lua new file mode 100644 index 0000000..c0b96ab --- /dev/null +++ b/modules/lua/test/htdocs/test.lua @@ -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/lua/test/lib/kangaroo.lua b/modules/lua/test/lib/kangaroo.lua new file mode 100644 index 0000000..00ba3e5 --- /dev/null +++ b/modules/lua/test/lib/kangaroo.lua @@ -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/lua/test/moonunit.lua b/modules/lua/test/moonunit.lua new file mode 100644 index 0000000..8537a1e --- /dev/null +++ b/modules/lua/test/moonunit.lua @@ -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/lua/test/test.lua b/modules/lua/test/test.lua new file mode 100755 index 0000000..25da4d9 --- /dev/null +++ b/modules/lua/test/test.lua @@ -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:8008" + +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/lua/test/test_httpd.conf b/modules/lua/test/test_httpd.conf new file mode 100644 index 0000000..27e3a50 --- /dev/null +++ b/modules/lua/test/test_httpd.conf @@ -0,0 +1,31 @@ +# 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 lua_module modules/mod_lua.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 |