summaryrefslogtreecommitdiff
path: root/modules/lua
diff options
context:
space:
mode:
Diffstat (limited to 'modules/lua')
-rw-r--r--modules/lua/NWGNUmakefile11
-rw-r--r--modules/lua/README10
-rw-r--r--modules/lua/config.m42
-rw-r--r--modules/lua/lua_apr.c35
-rw-r--r--modules/lua/lua_apr.h17
-rw-r--r--modules/lua/lua_config.c4
-rw-r--r--modules/lua/lua_config.h4
-rw-r--r--modules/lua/lua_dbd.c7
-rw-r--r--modules/lua/lua_dbd.h2
-rw-r--r--modules/lua/lua_passwd.c178
-rw-r--r--modules/lua/lua_passwd.h91
-rw-r--r--modules/lua/lua_request.c1533
-rw-r--r--modules/lua/lua_request.h20
-rw-r--r--modules/lua/lua_vmprep.c198
-rw-r--r--modules/lua/lua_vmprep.h53
-rw-r--r--modules/lua/mod_lua.c660
-rw-r--r--modules/lua/mod_lua.dsp8
-rw-r--r--modules/lua/mod_lua.h19
18 files changed, 2725 insertions, 127 deletions
diff --git a/modules/lua/NWGNUmakefile b/modules/lua/NWGNUmakefile
index be418cfe..15e6c91e 100644
--- a/modules/lua/NWGNUmakefile
+++ b/modules/lua/NWGNUmakefile
@@ -26,10 +26,10 @@ include $(AP_WORK)/build/NWGNUhead.inc
XINCDIRS += \
$(APR)/include \
$(APRUTIL)/include \
- $(AP_WORK)/include \
- $(AP_WORK)/modules/database \
- $(AP_WORK)/modules/http \
- $(AP_WORK)/modules/ssl \
+ $(SRC)/include \
+ $(STDMOD)/database \
+ $(STDMOD)/http \
+ $(STDMOD)/ssl \
$(NWOS) \
$(LUASRC)/src \
$(EOLIST)
@@ -129,7 +129,7 @@ NLM_VERSION =
#
# If this is specified, it will override the default of 64K
#
-NLM_STACK_SIZE = 8192
+NLM_STACK_SIZE = 131072
#
@@ -181,6 +181,7 @@ 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 \
diff --git a/modules/lua/README b/modules/lua/README
index c614b3e2..0be0adeb 100644
--- a/modules/lua/README
+++ b/modules/lua/README
@@ -38,13 +38,10 @@
* Task List
** TODO Use r->file to determine file, doing rewriting in translate_name
-** TODO Change to controlling lifecycle by passing in a pool?
- Need to determine how to handle server scoped then!
** TODO Provide means to get useful output from lua errors in response body
Probably have to put it on the vm spec for pre-handler errors, as
it is pre-handler, will prolly be on the request_config somewhere,
but sometimes cannot put there, so... fun
-** TODO Filters
** TODO Mapping in the server_rec
** TODO Connection scoped vms
** TODO Figure out how reentrancy works regarding filter chain stuff.
@@ -52,14 +49,10 @@
** TODO Flesh out apw_*getvm for each flavor we allow
** TODO Rework apw_sgetvm to use the create_vm stuff like apw_rgetvm
** TODO apw_rgetvm needs to handle connection scoped vms
-** TODO options in server scoped vms (ie, min and max vm counts)
** TODO provide means to implement authn and authz providers
** TODO: Flatten LuaHook* to LuaHook phase file fn ?
-** TODO: Lua and ap_expr integration in one or both directions
** TODO: document or remove block sections
** TODO: test per-dir behavior of block sections
-** TODO: Catch-up documentation on r: methods
-** TODO: 500 errors instead of 404 with AddHandler lua-script
** TODO: Suppress internal details (fs path to scripts, etc) in error responses
* License
@@ -82,3 +75,6 @@
** Brian Akins
** Justin Erenkrantz
** Philip M. Gollucci
+** Stefan Fritsch
+** Eric Covener
+** Daniel Gruno
diff --git a/modules/lua/config.m4 b/modules/lua/config.m4
index 2d1ac052..8a7a11bf 100644
--- a/modules/lua/config.m4
+++ b/modules/lua/config.m4
@@ -136,7 +136,7 @@ else
fi
])
-lua_objects="lua_apr.lo lua_config.lo mod_lua.lo lua_request.lo lua_vmprep.lo lua_dbd.lo"
+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()
diff --git a/modules/lua/lua_apr.c b/modules/lua/lua_apr.c
index c93ea9b1..8a1dcf68 100644
--- a/modules/lua/lua_apr.c
+++ b/modules/lua/lua_apr.c
@@ -14,32 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include "apr.h"
-#include "apr_tables.h"
#include "mod_lua.h"
#include "lua_apr.h"
-/**
- * make a userdata out of a C pointer, and vice versa
- * instead of using lightuserdata
- */
-#ifndef lua_boxpointer
-#define lua_boxpointer(L,u) (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
-#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
-#endif
-
-
-AP_LUA_DECLARE(apr_table_t*) ap_lua_check_apr_table(lua_State *L, int index)
+apr_table_t *ap_lua_check_apr_table(lua_State *L, int index)
{
apr_table_t *t;
luaL_checkudata(L, index, "Apr.Table");
- t = (apr_table_t *) lua_unboxpointer(L, index);
+ t = lua_unboxpointer(L, index);
return t;
}
-AP_LUA_DECLARE(void) ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
+void ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
{
lua_boxpointer(L, t);
luaL_getmetatable(L, "Apr.Table");
@@ -48,9 +36,9 @@ AP_LUA_DECLARE(void) ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
static int lua_table_set(lua_State *L)
{
- apr_table_t *t = ap_lua_check_apr_table(L, 1);
- const char *key = luaL_checkstring(L, 2);
- const char *val = luaL_checkstring(L, 3);
+ apr_table_t *t = ap_lua_check_apr_table(L, 1);
+ const char *key = luaL_checkstring(L, 2);
+ const char *val = luaL_checkstring(L, 3);
apr_table_set(t, key, val);
return 0;
@@ -58,9 +46,9 @@ static int lua_table_set(lua_State *L)
static int lua_table_get(lua_State *L)
{
- apr_table_t *t = ap_lua_check_apr_table(L, 1);
- const char *key = luaL_checkstring(L, 2);
- const char *val = apr_table_get(t, key);
+ apr_table_t *t = ap_lua_check_apr_table(L, 1);
+ const char *key = luaL_checkstring(L, 2);
+ const char *val = apr_table_get(t, key);
lua_pushstring(L, val);
return 1;
}
@@ -72,7 +60,7 @@ static const luaL_Reg lua_table_methods[] = {
};
-AP_LUA_DECLARE(int) ap_lua_init(lua_State *L, apr_pool_t *p)
+int ap_lua_init(lua_State *L, apr_pool_t *p)
{
luaL_newmetatable(L, "Apr.Table");
luaL_register(L, "apr_table", lua_table_methods);
@@ -88,3 +76,6 @@ AP_LUA_DECLARE(int) ap_lua_init(lua_State *L, apr_pool_t *p)
return 0;
}
+
+
+
diff --git a/modules/lua/lua_apr.h b/modules/lua/lua_apr.h
index c66cdde9..8a1428ff 100644
--- a/modules/lua/lua_apr.h
+++ b/modules/lua/lua_apr.h
@@ -18,8 +18,19 @@
#ifndef _LUA_APR_H_
#define _LUA_APR_H_
-AP_LUA_DECLARE(int) ap_lua_init(lua_State *L, apr_pool_t * p);
-AP_LUA_DECLARE(apr_table_t*) ap_lua_check_apr_table(lua_State *L, int index);
-AP_LUA_DECLARE(void) ap_lua_push_apr_table(lua_State *L, apr_table_t *t);
+#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);
+apr_table_t *ap_lua_check_apr_table(lua_State *L, int index);
+void ap_lua_push_apr_table(lua_State *L, apr_table_t *t);
#endif /* !_LUA_APR_H_ */
diff --git a/modules/lua/lua_config.c b/modules/lua/lua_config.c
index 07dd932b..bb082380 100644
--- a/modules/lua/lua_config.c
+++ b/modules/lua/lua_config.c
@@ -51,7 +51,7 @@ static int apl_toscope(const char *name)
return AP_LUA_SCOPE_ONCE;
}
-AP_LUA_DECLARE(apr_status_t) ap_lua_map_handler(ap_lua_dir_cfg *cfg,
+apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg,
const char *file,
const char *function,
const char *pattern,
@@ -257,7 +257,7 @@ static const struct luaL_Reg cmd_methods[] = {
{NULL, NULL}
};
-AP_LUA_DECLARE(void) ap_lua_load_config_lmodule(lua_State *L)
+void ap_lua_load_config_lmodule(lua_State *L)
{
luaL_newmetatable(L, "Apache2.DirConfig"); /* [metatable] */
lua_pushvalue(L, -1);
diff --git a/modules/lua/lua_config.h b/modules/lua/lua_config.h
index d2689da1..8a778ad8 100644
--- a/modules/lua/lua_config.h
+++ b/modules/lua/lua_config.h
@@ -20,9 +20,9 @@
#ifndef _APL_CONFIG_H_
#define _APL_CONFIG_H_
-AP_LUA_DECLARE(void) ap_lua_load_config_lmodule(lua_State *L);
+void ap_lua_load_config_lmodule(lua_State *L);
-AP_LUA_DECLARE(apr_status_t) ap_lua_map_handler(ap_lua_dir_cfg *cfg,
+apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg,
const char *file,
const char *function,
const char *pattern,
diff --git a/modules/lua/lua_dbd.c b/modules/lua/lua_dbd.c
index 350ec247..501156f8 100644
--- a/modules/lua/lua_dbd.c
+++ b/modules/lua/lua_dbd.c
@@ -16,7 +16,6 @@
*/
#include "mod_lua.h"
-#include "lua_apr.h"
#include "lua_dbd.h"
APLOG_USE_MODULE(lua);
@@ -377,7 +376,7 @@ int lua_db_prepared_select(lua_State *L)
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 prepped statements made through lua. */
+ * 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);
@@ -468,7 +467,7 @@ int lua_db_prepared_query(lua_State *L)
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 prepped statements made through lua. */
+ * 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);
@@ -704,7 +703,7 @@ static lua_db_handle* lua_push_db_handle(lua_State *L, request_rec* r, int type,
supported.
=============================================================================
*/
-AP_LUA_DECLARE(int) lua_db_acquire(lua_State *L)
+int lua_db_acquire(lua_State *L)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const char *type;
diff --git a/modules/lua/lua_dbd.h b/modules/lua/lua_dbd.h
index 6f74efd0..566204b1 100644
--- a/modules/lua/lua_dbd.h
+++ b/modules/lua/lua_dbd.h
@@ -50,7 +50,7 @@ typedef struct {
lua_db_handle *db;
} lua_db_prepared_statement;
-AP_LUA_DECLARE(int) lua_db_acquire(lua_State* L);
+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);
diff --git a/modules/lua/lua_passwd.c b/modules/lua/lua_passwd.c
new file mode 100644
index 00000000..ad865362
--- /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 00000000..797556bf
--- /dev/null
+++ b/modules/lua/lua_passwd.h
@@ -0,0 +1,91 @@
+/* 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;
+// const char *passwd;
+ 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
index 63b192dc..c2cfb535 100644
--- a/modules/lua/lua_request.c
+++ b/modules/lua/lua_request.c
@@ -16,23 +16,38 @@
*/
#include "mod_lua.h"
-#include "util_script.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 <lua.h>
+
+extern apr_thread_mutex_t* lua_ivm_mutex;
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 apr_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) {
@@ -153,6 +168,87 @@ static int req_aprtable2luatable_cb(void *l, const char *key,
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)
+{
+ int rc = OK;
+
+ 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;
+ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+ *rbuf = (const char *) apr_pcalloc(r->pool, (apr_size_t) (length + 1));
+ *size = length;
+ while ((len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0) {
+ if ((rpos + len_read) > length) {
+ rsize = length - rpos;
+ }
+ else {
+ rsize = len_read;
+ }
+
+ memcpy((char *) *rbuf + rpos, argsbuffer, (size_t) rsize);
+ rpos += rsize;
+ }
+ }
+
+ 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;
+
+ 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;
+ apr_size_t written;
+
+ *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,
+ &written);
+ if (written != rsize || rc != OK)
+ return APR_ENOSPC;
+ rpos += rsize;
+ }
+ }
+
+ return rc;
+}
+
/* r:parseargs() returning a lua table */
static int req_parseargs(lua_State *L)
{
@@ -165,7 +261,7 @@ static int req_parseargs(lua_State *L)
return 2; /* [table<string, string>, table<string, array<string>>] */
}
-/* r:parsebody() returning a lua table */
+/* 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;
@@ -173,26 +269,127 @@ static int req_parsebody(lua_State *L)
int res;
apr_size_t size;
apr_size_t max_post_size;
- char *buffer;
+ char *multipart;
+ const char *contentType;
request_rec *r = ap_lua_check_request_rec(L, 1);
max_post_size = (apr_size_t) luaL_optint(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] */
- 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);
+ 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) != OK) {
+ return 2;
+ }
+ len = strlen(multipart);
+ i = 0;
+ for
+ (
+ start = strstr((char *) data, multipart);
+ start != start + size;
+ start = end
+ ) {
+ i++;
+ if (i == POST_MAX_VARS) break;
+ end = strstr((char *) (start + 1), multipart);
+ if (!end) end = start + size;
+ crlf = strstr((char *) start, "\r\n\r\n");
+ if (!crlf) break;
+ key = (char *) apr_pcalloc(r->pool, 256);
+ filename = (char *) apr_pcalloc(r->pool, 256);
+ 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 (strlen(key)) {
+ req_aprtable2luatable_cb(L, key, buffer);
+ }
+ }
+ }
+ 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 = luaL_optint(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) != 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)
{
@@ -212,10 +409,12 @@ 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);
- ap_rwrite((void *) buf, n, r);
- return 0;
+ rv = ap_rwrite((void *) buf, n, r);
+ lua_pushinteger(L, rv);
+ return 1;
}
/* r:addoutputfilter(name|function) */
@@ -246,6 +445,7 @@ static int req_escape_html(lua_State *L)
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)
{
@@ -256,6 +456,7 @@ static int req_ssl_var_lookup(lua_State *L)
lua_pushstring(L, res);
return 1;
}
+
/* BEGIN dispatch mathods for request_rec fields */
/* not really a field, but we treat it like one */
@@ -372,6 +573,11 @@ 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;
@@ -412,7 +618,1095 @@ static int req_ssl_is_https_field(request_rec *r)
return ap_lua_ssl_is_https(r->connection);
}
-/* END dispatch mathods for request_rec fields */
+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);
+ return apr_psprintf(r->pool, "%s %s %s %s %s %s", (opts&OR_NONE) ? "None" : "", (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 = luaL_optnumber(L, 3, 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(&regex, pattern, flags);
+ if (rv) {
+ lua_pushboolean(L, 0);
+ err = apr_palloc(r->pool, 256);
+ ap_regerror(rv, &regex, 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(&regex, 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;
+
+ luaL_checktype(L, 1, LUA_TUSERDATA);
+ luaL_checktype(L, 2, LUA_TNUMBER);
+ luaL_checktype(L, 3, LUA_TNUMBER);
+ i = lua_tointeger(L, 2);
+ j = lua_tointeger(L, 3);
+ ws_record = ap_get_scoreboard_worker_from_indexes(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, "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 patterm 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 = luaL_optint(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)
{
@@ -463,7 +1757,7 @@ static int req_dispatch(lua_State *L)
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01489)
"request_rec->dispatching %s -> int", name);
rs = (*func) (r);
- lua_pushnumber(L, rs);
+ lua_pushinteger(L, rs);
return 1;
}
case APL_REQ_FUNTYPE_BOOLEAN:{
@@ -532,6 +1826,67 @@ 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;
+ 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_thread_mutex_lock(lua_ivm_mutex);
+ apr_pool_userdata_get((void **)&object, raw_key, r->server->process->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_thread_mutex_unlock(lua_ivm_mutex);
+ return 1;
+ }
+ else {
+ apr_thread_mutex_unlock(lua_ivm_mutex);
+ return 0;
+ }
+}
+
+
+static int lua_ivm_set(lua_State *L)
+{
+ const char *key, *raw_key;
+ const char *value = NULL;
+ 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_thread_mutex_lock(lua_ivm_mutex);
+ apr_pool_userdata_get((void **)&object, raw_key, r->server->process->pool);
+ if (!object) {
+ object = apr_pcalloc(r->server->process->pool, sizeof(lua_ivm_object));
+ ap_varbuf_init(r->server->process->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, r->server->process->pool);
+ apr_thread_mutex_unlock(lua_ivm_mutex);
+ return 0;
+}
+
#define APLUA_REQ_TRACE(lev) static int req_trace##lev(lua_State *L) \
{ \
return req_log_at(L, APLOG_TRACE##lev); \
@@ -623,6 +1978,21 @@ 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}
@@ -637,7 +2007,7 @@ static req_fun_t *makefun(const void *fun, int type, apr_pool_t *pool)
return rft;
}
-AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
+void ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
{
apr_hash_t *dispatch = apr_hash_make(p);
@@ -752,10 +2122,121 @@ AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
makefun(&req_notes, APL_REQ_FUNTYPE_TABLE, p));
apr_hash_set(dispatch, "subprocess_env", APR_HASH_KEY_STRING,
makefun(&req_subprocess_env, APL_REQ_FUNTYPE_TABLE, 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));
+
lua_pushlightuserdata(L, dispatch);
lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Request.dispatch");
@@ -785,7 +2266,7 @@ AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
}
-AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
+void ap_lua_push_connection(lua_State *L, conn_rec *c)
{
lua_boxpointer(L, c);
luaL_getmetatable(L, "Apache2.Connection");
@@ -802,7 +2283,7 @@ AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *c)
}
-AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
+void ap_lua_push_server(lua_State *L, server_rec *s)
{
lua_boxpointer(L, s);
luaL_getmetatable(L, "Apache2.Server");
@@ -815,7 +2296,7 @@ AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *s)
lua_pop(L, 1);
}
-AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r)
+void ap_lua_push_request(lua_State *L, request_rec *r)
{
lua_boxpointer(L, r);
luaL_getmetatable(L, "Apache2.Request");
diff --git a/modules/lua/lua_request.h b/modules/lua/lua_request.h
index ad272dc9..b5ed3e5f 100644
--- a/modules/lua/lua_request.h
+++ b/modules/lua/lua_request.h
@@ -15,15 +15,16 @@
* limitations under the License.
*/
-#include "mod_lua.h"
-
#ifndef _LUA_REQUEST_H_
#define _LUA_REQUEST_H_
-AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p);
-AP_LUA_DECLARE(void) ap_lua_push_connection(lua_State *L, conn_rec *r);
-AP_LUA_DECLARE(void) ap_lua_push_server(lua_State *L, server_rec *r);
-AP_LUA_DECLARE(void) ap_lua_push_request(lua_State *L, request_rec *r);
+#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
@@ -37,5 +38,12 @@ typedef struct
int type;
} req_fun_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
index e821fee3..b0eb01c4 100644
--- a/modules/lua/lua_vmprep.c
+++ b/modules/lua/lua_vmprep.c
@@ -23,6 +23,27 @@
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;
+
+void ap_lua_init_mutex(apr_pool_t *pool, server_rec *s)
+{
+ apr_thread_mutex_create(&ap_lua_mutex, APR_THREAD_MUTEX_DEFAULT, pool);
+}
+#endif
+
/* forward dec'l from this file */
#if 0
@@ -99,7 +120,7 @@ static void pstack_dump(lua_State *L, apr_pool_t *r, int level,
#define makeintegerfield(L, n) lua_pushinteger(L, n); lua_setfield(L, -2, #n)
-AP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L)
+void ap_lua_load_apache2_lmodule(lua_State *L)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
@@ -127,7 +148,7 @@ AP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L)
makeintegerfield(L, AUTHZ_NEUTRAL);
makeintegerfield(L, AUTHZ_GENERAL_ERROR);
makeintegerfield(L, AUTHZ_DENIED_NO_USER);
-
+
/*
makeintegerfield(L, HTTP_CONTINUE);
makeintegerfield(L, HTTP_SWITCHING_PROTOCOLS);
@@ -201,6 +222,16 @@ static apr_status_t cleanup_lua(void *l)
return APR_SUCCESS;
}
+static apr_status_t server_cleanup_lua(void *resource)
+{
+ 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",
@@ -295,8 +326,11 @@ static apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifec
spec->file);
}
if (spec->package_cpaths) {
- munge_path(L, "cpath", "?.so", "./?.so", lifecycle_pool,
- spec->package_cpaths, spec->file);
+ munge_path(L,
+ "cpath", "?" AP_LUA_MODULE_EXT, "./?" AP_LUA_MODULE_EXT,
+ lifecycle_pool,
+ spec->package_cpaths,
+ spec->file);
}
if (spec->cb) {
@@ -333,33 +367,155 @@ static apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifec
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.
*/
-AP_LUA_DECLARE(lua_State*)ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
- ap_lua_vm_spec *spec)
+lua_State *ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
+ ap_lua_vm_spec *spec, request_rec* r)
{
lua_State *L = NULL;
-
- if (apr_pool_userdata_get((void **)&L, spec->file,
- lifecycle_pool) == APR_SUCCESS) {
-
- if(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 {
+ 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);
+ "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 (!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
index 1d3758ca..e46ac9b8 100644
--- a/modules/lua/lua_vmprep.h
+++ b/modules/lua/lua_vmprep.h
@@ -29,6 +29,7 @@
#include "apr_file_info.h"
#include "apr_time.h"
#include "apr_pools.h"
+#include "apr_reslist.h"
#ifndef VMPREP_H
@@ -39,11 +40,18 @@
#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
*/
@@ -56,8 +64,10 @@ typedef struct
/* 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_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;
@@ -71,6 +81,8 @@ typedef struct
*/
const char *bytecode;
apr_size_t bytecode_len;
+
+ int codecache;
} ap_lua_vm_spec;
typedef struct
@@ -81,18 +93,32 @@ typedef struct
ap_regex_t *uri_pattern;
const char *bytecode;
apr_size_t bytecode_len;
+ int codecache;
} ap_lua_mapped_handler_spec;
-/* remove and make static once out of mod_wombat.c */
-AP_LUA_DECLARE(void) ap_lua_openlibs(lua_State *L);
+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;
-/* remove and make static once out of mod_wombat.c */
-AP_LUA_DECLARE(void) ap_lua_registerlib(lua_State *L, char *name, lua_CFunction f);
+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
*/
-AP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L);
+void ap_lua_load_apache2_lmodule(lua_State *L);
/*
* alternate means of getting lua_State (preferred eventually)
@@ -106,9 +132,16 @@ AP_LUA_DECLARE(void) ap_lua_load_apache2_lmodule(lua_State *L);
* @cb callback for vm initialization called *before* the file is opened
* @ctx a baton passed to cb
*/
-AP_LUA_DECLARE(lua_State*) ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
- ap_lua_vm_spec *spec);
-
+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
index b5b626d1..7c35011e 100644
--- a/modules/lua/mod_lua.c
+++ b/modules/lua/mod_lua.c
@@ -19,6 +19,7 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
+#include <apr_thread_mutex.h>
#include "lua_apr.h"
#include "lua_config.h"
@@ -55,6 +56,15 @@ typedef struct {
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;
+
+apr_thread_mutex_t* lua_ivm_mutex = NULL;
/**
* error reporting if lua has an error.
@@ -65,7 +75,6 @@ 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("<b>Error!</b>\n", r);
ap_rputs("<p>", r);
lua_response = lua_tostring(L, -1);
@@ -103,9 +112,31 @@ static const char *scope_to_string(unsigned int scope)
#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);
+ }
+ }
}
}
@@ -130,7 +161,10 @@ static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
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,
@@ -160,6 +194,9 @@ static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
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);
@@ -169,15 +206,58 @@ static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_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);
@@ -190,11 +270,12 @@ static int lua_handler(request_rec *r)
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);
+ 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!");
@@ -204,17 +285,309 @@ static int lua_handler(request_rec *r)
"lua: Unable to find function %s in %s",
"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, 0, 0)) {
+ 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 OK;
+ 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;
+ 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 */
+ 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 function %s in %s",
+ 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);
+ if (rc == LUA_YIELD) {
+ 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;
+ 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);
+ }
+ f->ctx = ctx;
+ ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
+ }
+ 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) == LUA_YIELD) {
+ size_t olen;
+ const char* output = lua_tolstring(L, 1, &olen);
+ 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);
+ 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) == LUA_YIELD) {
+ apr_bucket *pbktOut;
+ size_t olen;
+ const char* output = lua_tolstring(L, 1, &olen);
+ 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;
+ 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) == LUA_YIELD) {
+ 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) == LUA_YIELD) {
+ 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 --------------- */
@@ -249,7 +622,7 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
hook_spec->function_name,
"request hook");
- L = ap_lua_get_lua_state(pool, spec);
+ L = ap_lua_get_lua_state(pool, spec, r);
if (!L) {
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
@@ -265,6 +638,7 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
"lua: Unable to find function %s in %s",
hook_spec->function_name,
hook_spec->file_name);
+ ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
@@ -281,6 +655,7 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
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;
@@ -288,6 +663,102 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
rc = lua_tointeger(L, -1);
}
if (rc != DECLINED) {
+ ap_lua_release_state(L, spec, r);
+ return rc;
+ }
+ ap_lua_release_state(L, spec, r);
+ }
+ }
+ 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 %s %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 function %s in %s",
+ 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;
}
}
@@ -564,7 +1035,55 @@ static const char *register_named_file_function_hook(const char *name,
*(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(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE);
+ }
+ else {
+ spec->direction = AP_LUA_FILTER_INPUT;
+ ap_register_input_filter(filter, lua_input_filter_handle, NULL, AP_FTYPE_RESOURCE);
+ }
+ return NULL;
+}
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);
@@ -827,6 +1346,42 @@ static const char *register_quick_hook(cmd_parms *cmd, void *_cfg,
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)
{
@@ -910,6 +1465,29 @@ static const char *register_lua_inherit(cmd_parms *cmd,
}
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,
@@ -936,12 +1514,33 @@ static const char *register_lua_scope(cmd_parms *cmd,
#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', 'server'"
+ "values are: 'once', 'request', 'conn'"
#if APR_HAS_THREADS
- ", 'thread'"
+ ", 'thread', 'server'"
#endif
,scope);
}
@@ -961,7 +1560,9 @@ static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
cfg->root_path = root;
return NULL;
}
-AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var)
+
+const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c,
+ request_rec *r, const char *var)
{
if (lua_ssl_val) {
return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
@@ -969,7 +1570,7 @@ AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_r
return NULL;
}
-AP_LUA_DECLARE(int) ap_lua_ssl_is_https(conn_rec *c)
+int ap_lua_ssl_is_https(conn_rec *c)
{
return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
}
@@ -1018,7 +1619,7 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
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);
+ 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);
@@ -1029,6 +1630,7 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
"Unable to find function %s in %s",
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);
@@ -1037,6 +1639,7 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
if (!lua_checkstack(L, prov_spec->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_spec->args->nelts; i++) {
@@ -1049,14 +1652,17 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
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:
@@ -1184,6 +1790,10 @@ command_rec lua_commands[] = {
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"),
@@ -1193,6 +1803,12 @@ command_rec lua_commands[] = {
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}
};
@@ -1204,10 +1820,15 @@ static void *create_dir_config(apr_pool_t *p, char *dir)
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;
return cfg;
}
@@ -1269,24 +1890,31 @@ static void *merge_dir_config(apr_pool_t *p, void *basev, void *overridesv)
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->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;
}
@@ -1346,9 +1974,15 @@ static void lua_register_hooks(apr_pool_t *p)
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);
+#if APR_HAS_THREADS
+ ap_hook_child_init(ap_lua_init_mutex, NULL, NULL, APR_HOOK_MIDDLE);
+#endif
/* providers */
lua_authz_providers = apr_hash_make(p);
+
+ /* ivm mutex */
+ apr_thread_mutex_create(&lua_ivm_mutex, APR_THREAD_MUTEX_DEFAULT, p);
}
AP_DECLARE_MODULE(lua) = {
diff --git a/modules/lua/mod_lua.dsp b/modules/lua/mod_lua.dsp
index 71d33b8b..770c13a1 100644
--- a/modules/lua/mod_lua.dsp
+++ b/modules/lua/mod_lua.dsp
@@ -117,6 +117,14 @@ 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
diff --git a/modules/lua/mod_lua.h b/modules/lua/mod_lua.h
index 52ff96ad..7fd11533 100644
--- a/modules/lua/mod_lua.h
+++ b/modules/lua/mod_lua.h
@@ -39,6 +39,7 @@
#include "apr_file_info.h"
#include "apr_time.h"
#include "apr_hooks.h"
+#include "apr_reslist.h"
/* Allow for Lua 5.2 backwards compatibility */
#define LUA_COMPAT_ALL
@@ -50,6 +51,7 @@
#if LUA_VERSION_NUM > 501
/* Load mode for lua_load() */
#define lua_load(a,b,c,d) lua_load(a,b,c,d,NULL)
+#define lua_resume(a,b) lua_resume(a, NULL, b)
#endif
/* Create a set of AP_LUA_DECLARE(type), AP_LUA_DECLARE_NONSTD(type) and
@@ -81,7 +83,7 @@ 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_PARENT_LAST = 2
} ap_lua_inherit_t;
/**
@@ -101,9 +103,10 @@ typedef struct
apr_array_header_t *package_cpaths;
/**
- * mapped handlers
+ * mapped handlers/filters
*/
apr_array_header_t *mapped_handlers;
+ apr_array_header_t *mapped_filters;
apr_pool_t *pool;
@@ -111,6 +114,8 @@ typedef struct
* 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> */
@@ -120,6 +125,11 @@ typedef struct
/* 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;
@@ -158,8 +168,9 @@ APR_DECLARE_EXTERNAL_HOOK(ap_lua, AP_LUA, int, lua_open,
APR_DECLARE_EXTERNAL_HOOK(ap_lua, AP_LUA, int, lua_request,
(lua_State *L, request_rec *r))
-AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
+const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c,
+ request_rec *r, const char *var);
-AP_LUA_DECLARE(int) ap_lua_ssl_is_https(conn_rec *c);
+int ap_lua_ssl_is_https(conn_rec *c);
#endif /* !_MOD_LUA_H_ */