Name |
Lua type |
Writable |
+ Description |
+
+
+ allowoverrides |
+ string |
+ no |
+ The AllowOverride options applied to the current request. |
ap_auth_type |
string |
no |
+ If an authentication check was made, this is set to the type
+ of authentication (f.x. basic ) |
args |
string |
yes |
+ The query string arguments extracted from the request
+ (f.x. foo=bar&name=johnsmith ) |
assbackwards |
boolean |
no |
+ Set to true if this is an HTTP/0.9 style request
+ (e.g. GET /foo (with no headers) ) |
+
+
+ auth_name |
+ string |
+ no |
+ The realm name used for authorization (if applicable). |
+
+
+ banner |
+ string |
+ no |
+ The server banner, f.x. Apache HTTP Server/2.4.3 openssl/0.9.8c |
+
+
+ basic_auth_pw |
+ string |
+ no |
+ The basic auth password sent with this request, if any |
-
canonical_filename |
string |
no |
+ The canonical filename of the request |
content_encoding |
string |
no |
+ The content encoding of the current request |
content_type |
string |
yes |
+ The content type of the current request, as determined in the
+ type_check phase (f.x. image/gif or text/html ) |
context_prefix |
string |
no |
+ |
context_document_root |
string |
no |
+ |
document_root |
string |
no |
+ The document root of the host |
err_headers_out |
table |
no |
+ MIME header environment for the response, printed even on errors and
+ persist across internal redirects |
filename |
string |
yes |
+ The file name that the request maps to, f.x. /www/example.com/foo.txt. This can be
+ changed in the translate-name or map-to-storage phases of a request to allow the
+ default handler (or script handlers) to serve a different file than what was requested. |
handler |
string |
yes |
+ The name of the handler that should serve this request, f.x.
+ lua-script if it is to be served by mod_lua. This is typically set by the
+ AddHandler or SetHandler
+ directives, but could also be set via mod_lua to allow another handler to serve up a specific request
+ that would otherwise not be served by it.
+ |
headers_in |
table |
yes |
+ MIME header environment from the request. This contains headers such as Host,
+ User-Agent, Referer and so on. |
headers_out |
table |
yes |
+ MIME header environment for the response. |
hostname |
string |
no |
+ The host name, as set by the Host: header or by a full URI. |
+
+
+ is_https |
+ boolean |
+ no |
+ Whether or not this request is done via HTTPS |
+
+
+ is_initial_req |
+ boolean |
+ no |
+ Whether this request is the initial request or a sub-request |
+
+
+ limit_req_body |
+ number |
+ no |
+ The size limit of the request body for this request, or 0 if no limit. |
log_id |
string |
no |
+ The ID to identify request in access and error log. |
method |
string |
no |
+ The request method, f.x. GET or POST . |
notes |
table |
yes |
+ A list of notes that can be passed on from one module to another. |
+
+
+ options |
+ string |
+ no |
+ The Options directive applied to the current request. |
path_info |
string |
no |
+ The PATH_INFO extracted from this request. |
+
+
+ port |
+ number |
+ no |
+ The server port used by the request. |
protocol |
string |
no |
+ The protocol used, f.x. HTTP/1.1 |
proxyreq |
string |
yes |
+ Denotes whether this is a proxy request or not. This value is generally set in
+ the post_read_request/translate_name phase of a request. |
range |
string |
no |
+ The contents of the Range: header. |
+
+
+ remaining |
+ number |
+ no |
+ The number of bytes remaining to be read from the request body. |
+
+
+ server_built |
+ string |
+ no |
+ The time the server executable was built. |
+
+
+ server_name |
+ string |
+ no |
+ The server name for this request. |
+
+
+ some_auth_required |
+ boolean |
+ no |
+ Whether some authorization is/was required for this request. |
subprocess_env |
table |
yes |
+ The environment variables set for this request. |
+
+
+ started |
+ number |
+ no |
+ The time the server was (re)started, in seconds since the epoch (Jan 1st, 1970) |
status |
number |
yes |
+ The (current) HTTP return code for this request, f.x. 200 or 404 . |
the_request |
string |
no |
+ The request string as sent by the client, f.x. GET /foo/bar HTTP/1.1 . |
unparsed_uri |
string |
no |
+ The unparsed URI of the request |
uri |
string |
yes |
+ The URI after it has been parsed by httpd |
user |
string |
yes |
+ If an authentication check has been made, this is set to the name of the authenticated user. |
useragent_ip |
string |
no |
+ The IP of the user agent making the request |
+
+
+
+
Database connectivity
@@ -489,11 +1009,13 @@ r:dbacquire(dbType[, dbParams]) -- Acqui
on the most popular database engines (mySQL, PostgreSQL, FreeTDS, ODBC, SQLite, Oracle)
as well as mod_dbd.
- Connecting and firing off queries is as easy as:
+ The example below shows how to acquire a database handle and return information from a table:
-function handler(r)
- local database, err = r:dbacquire("mysql", "server=localhost&user=root&database=mydb")
+function handle(r)
+ -- Acquire a database handle
+ local database, err = r:dbacquire("mysql", "server=localhost,user=root,dbname=mydb")
if not err then
+ -- Select some information from it
local results, err = database:select(r, "SELECT `name`, `age` FROM `people` WHERE 1")
if not err then
local rows = results(0) -- fetch all rows synchronously
@@ -510,7 +1032,7 @@ function handler(r)
end
- To utilize mod_dbd, simply specify mod_dbd
+ To utilize mod_dbd, specify mod_dbd
as the database type, or leave the field blank:
@@ -532,7 +1054,7 @@ local result, errmsg = database:select(r
-- Create and run a prepared statement:
local statement, errmsg = database:prepare(r, "DELETE FROM `tbl` WHERE `age` > %u")
if not errmsg then
- local result, errmsg = statement:query(20) -- run the statement with age >20
+ local result, errmsg = statement:query(20) -- run the statement with age > 20
end
-- Fetch a prepared statement from a DBDPrepareSQL directive:
@@ -662,7 +1184,7 @@ collectgarbage() -- close the handle via
LuaScope
One of once, request, conn, thread -- default is once
-LuaScope once|request|conn|thread
+LuaScope once|request|conn|thread|server [min] [max]
LuaScope once
server configvirtual host
directory.htaccess
@@ -670,7 +1192,7 @@ collectgarbage() -- close the handle via
All
- Specify the lifecycle scope of the Lua interpreter which will
+
Specify the life cycle scope of the Lua interpreter which will
be used by handlers in this "Directory." The default is "once"
@@ -681,22 +1203,33 @@ collectgarbage() -- close the handle via
request scoped.
- conn:
- Same as request but attached to the connection_rec
+
- thread:
- Use the interpreter for the lifetime of the thread
handling the request (only available with threaded MPMs).
-
+ will have the same server_rec. To accommodate this,
+ server scoped Lua states are stored in an apr
+ resource list. The min
and max
arguments
+ specify the minimum and maximum number of Lua states to keep in the
+ pool.
+
+ Generally speaking, the thread
and server
scopes
+ execute roughly 2-3 times faster than the rest, because they don't have to
+ spawn new Lua states on every request (especially with the event MPM, as
+ even keepalive requests will use a new thread for each request). If you are
+ satisfied that your scripts will not have problems reusing a state, then
+ the thread
or server
scopes should be used for
+ maximum performance. While the thread
scope will provide the
+ fastest responses, the server
scope will use less memory, as
+ states are pooled, allowing f.x. 1000 threads to share only 100 Lua states,
+ thus using only 10% of the memory required by the thread
scope.
+
-
LuaPackagePath
@@ -770,13 +1302,13 @@ LuaPackagePath /scripts/lib/?/init.lua
-
LuaHookTranslateName
@@ -861,7 +1392,7 @@ end
LuaHookFixups
-Provide a hook for the fixups phase of request
+Provide a hook for the fixups phase of a request
processing
LuaHookFixups /path/to/lua/script.lua hook_function_name
server configvirtual host
@@ -883,7 +1414,46 @@ processing
directory.htaccess
All
- ...
+
+ Like LuaHookTranslateName but executed at the
+ map-to-storage phase of a request. Modules like mod_cache run at this phase,
+ which makes for an interesting example on what to do here:
+
+ LuaHookMapToStorage /path/to/lua/script.lua check_cache
+
+
+require"apache2"
+cached_files = {}
+
+function read_file(filename)
+ local input = io.open(filename, "r")
+ if input then
+ local data = input:read("*a")
+ cached_files[filename] = data
+ file = cached_files[filename]
+ input:close()
+ end
+ return cached_files[filename]
+end
+
+function check_cache(r)
+ if r.filename:match("%.png$") then -- Only match PNG files
+ local file = cached_files[r.filename] -- Check cache entries
+ if not file then
+ file = read_file(r.filename) -- Read file into cache
+ end
+ if file then -- If file exists, write it out
+ r.status = 200
+ r:write(file)
+ r:info(("Sent %s to client from cache"):format(r.filename))
+ return apache2.DONE -- skip default handler for PNG files
+ end
+ end
+ return apache2.DECLINED -- If we had nothing to do, let others serve this.
+end
+
+
+
@@ -909,7 +1479,27 @@ processing
directory.htaccess
All
- ...
+
+ This directive provides a hook for the type_checker phase of the request processing.
+ This phase is where requests are assigned a content type and a handler, and thus can
+ be used to modify the type and handler based on input:
+
+
+ LuaHookTypeChecker /path/to/lua/script.lua type_checker
+
+
+ function type_checker(r)
+ if r.uri:match("%.to_gif$") then -- match foo.png.to_gif
+ r.content_type = "image/gif" -- assign it the image/gif type
+ r.handler = "gifWizard" -- tell the gifWizard module to handle this
+ r.filename = r.uri:gsub("%.to_gif$", "") -- fix the filename requested
+ return apache2.OK
+ end
+
+ return apache2.DECLINED
+ end
+
+
@@ -1018,7 +1608,16 @@ hook function usually returns OK, DECLIN
server configvirtual host
All
-...
+
+
+ This phase is run immediately after the request has been mapped to a virtal host,
+ and can be used to either do some request processing before the other phases kick
+ in, or to serve a request without the need to translate, map to storage et cetera.
+ As this phase is run before anything else, directives such as Location or Directory are void in this phase, just as
+ URIs have not been properly parsed yet.
+
ContextThis directive is not valid in Directory, Files, or htaccess
@@ -1038,18 +1637,140 @@ hook function usually returns OK, DECLIN
After a lua function has been registered as authorization provider, it can be used
with the Require directive:
-
LuaRoot /usr/local/apache2/lua
LuaAuthzProvider foo authz.lua authz_check_foo
<Location />
- Require foo bar
+ Require foo johndoe
</Location>
-
+
+require "apache2"
+function authz_check_foo(r, who)
+ if r.user ~= who then return apache2.AUTHZ_DENIED
+ return apache2.AUTHZ_GRANTED
+end
+
+
+
+
+
+
+
+
+LuaInputFilter
+Provide a Lua function for content input filtering
+LuaInputFilter filter_name /path/to/lua/script.lua function_name
+server config
+2.5.0 and later
+
+
+Provides a means of adding a Lua function as an input filter.
+As with output filters, input filters work as coroutines,
+first yielding before buffers are sent, then yielding whenever
+a bucket needs to be passed down the chain, and finally (optionally)
+yielding anything that needs to be appended to the input data. The
+global variable bucket
holds the buckets as they are passed
+onto the Lua script:
+
+
+
+LuaInputFilter myInputFilter /www/filter.lua input_filter
+<FilesMatch "\.lua>
+ SetInputFilter myInputFilter
+</FilesMatch>
+
+
+--[[
+ Example input filter that converts all POST data to uppercase.
+]]--
+function input_filter(r)
+ print("luaInputFilter called") -- debug print
+ coroutine.yield() -- Yield and wait for buckets
+ while bucket do -- For each bucket, do...
+ local output = string.upper(bucket) -- Convert all POST data to uppercase
+ coroutine.yield(output) -- Send converted data down the chain
+ end
+ -- No more buckets available.
+ coroutine.yield("&filterSignature=1234") -- Append signature at the end
+end
+
+
+The input filter supports denying/skipping a filter if it is deemed unwanted:
+
+
+function input_filter(r)
+ if not good then
+ return -- Simply deny filtering, passing on the original content instead
+ end
+ coroutine.yield() -- wait for buckets
+ ... -- insert filter stuff here
+end
+
+
+See "Modifying contents with Lua
+filters" for more information.
+
+
+
+
+LuaOutputFilter
+Provide a Lua function for content output filtering
+LuaOutputFilter filter_name /path/to/lua/script.lua function_name
+server config
+2.5.0 and later
+
+
+Provides a means of adding a Lua function as an output filter.
+As with input filters, output filters work as coroutines,
+first yielding before buffers are sent, then yielding whenever
+a bucket needs to be passed down the chain, and finally (optionally)
+yielding anything that needs to be appended to the input data. The
+global variable bucket
holds the buckets as they are passed
+onto the Lua script:
+
+
+
+LuaOutputFilter myOutputFilter /www/filter.lua output_filter
+<FilesMatch "\.lua>
+ SetOutputFilter myOutputFilter
+</FilesMatch>
+
+
+--[[
+ Example output filter that escapes all HTML entities in the output
+]]--
+function output_filter(r)
+ coroutine.yield("(Handled by myOutputFilter)<br/>\n") -- Prepend some data to the output,
+ -- yield and wait for buckets.
+ while bucket do -- For each bucket, do...
+ local output = r:escape_html(bucket) -- Escape all output
+ coroutine.yield(output) -- Send converted data down the chain
+ end
+ -- No more buckets available.
+end
+
+
+As with the input filter, the output filter supports denying/skipping a filter
+if it is deemed unwanted:
+
+
+function output_filter(r)
+ if not r.content_type:match("text/html") then
+ return -- Simply deny filtering, passing on the original content instead
+ end
+ coroutine.yield() -- wait for buckets
+ ... -- insert filter stuff here
+end
+
+
+See "Modifying contents with Lua filters" for more
+information.
+
+
Modified: httpd/httpd/branches/2.4.x/modules/lua/README
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/README?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/README (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/README Mon Jun 10 14:36:56 2013
@@ -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
Modified: httpd/httpd/branches/2.4.x/modules/lua/config.m4
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/config.m4?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/config.m4 (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/config.m4 Mon Jun 10 14:36:56 2013
@@ -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()
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_apr.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_apr.c?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_apr.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_apr.c Mon Jun 10 14:36:56 2013
@@ -18,17 +18,7 @@
#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");
@@ -37,7 +27,7 @@ AP_LUA_DECLARE(apr_table_t *) ap_lua_che
}
-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");
@@ -70,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);
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_apr.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_apr.h?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_apr.h (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_apr.h Mon Jun 10 14:36:56 2013
@@ -29,9 +29,8 @@
#include "apr_base64.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);
-AP_LUA_DECLARE(int) ap_lua_load_httpd_functions(lua_State *L);
+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_ */
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_config.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_config.c?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_config.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_config.c Mon Jun 10 14:36:56 2013
@@ -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);
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_config.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_config.h?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_config.h (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_config.h Mon Jun 10 14:36:56 2013
@@ -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,
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.c?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.c Mon Jun 10 14:36:56 2013
@@ -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
supported.
=============================================================================
*/
-AP_LUA_DECLARE(int) lua_db_acquire(lua_State *L)
+int lua_db_acquire(lua_State *L)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const char *type;
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.h?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.h (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.h Mon Jun 10 14:36:56 2013
@@ -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);
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_request.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_request.c?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_request.c (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_request.c Mon Jun 10 14:36:56 2013
@@ -16,19 +16,33 @@
*/
#include "mod_lua.h"
-#include "util_script.h"
#include "lua_apr.h"
-#include "scoreboard.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
+
+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;
@@ -431,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)
{
@@ -441,6 +456,7 @@ static int req_ssl_var_lookup(lua_State
lua_pushstring(L, res);
return 1;
}
+
/* BEGIN dispatch mathods for request_rec fields */
/* not really a field, but we treat it like one */
@@ -602,6 +618,11 @@ static int req_ssl_is_https_field(reques
return ap_lua_ssl_is_https(r->connection);
}
+static int req_ap_get_server_port(request_rec *r)
+{
+ return (int) ap_get_server_port(r);
+}
+
static int lua_ap_rflush (lua_State *L) {
int returnValue;
@@ -613,10 +634,6 @@ static int lua_ap_rflush (lua_State *L)
return 1;
}
-static int lua_ap_port(request_rec* r)
-{
- return (int) ap_get_server_port(r);
-}
static const char* lua_ap_options(request_rec* r)
{
@@ -634,7 +651,7 @@ static const char* lua_ap_allowoverrides
static int lua_ap_started(request_rec* r)
{
- return ap_scoreboard_image->global->restart_time;
+ return (int)(ap_scoreboard_image->global->restart_time / 1000000);
}
static const char* lua_ap_basic_auth_pw(request_rec* r)
@@ -670,7 +687,7 @@ static int lua_ap_sendfile(lua_State *L)
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_NORM, r->pool);
+ 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);
}
@@ -682,7 +699,7 @@ static int lua_ap_sendfile(lua_State *L)
rc = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT,
r->pool);
if (rc == APR_SUCCESS) {
- ap_send_fd(file, r, 0, file_info.size, &sent);
+ ap_send_fd(file, r, 0, (apr_size_t)file_info.size, &sent);
apr_file_close(file);
lua_pushinteger(L, sent);
}
@@ -709,10 +726,12 @@ static int lua_apr_b64encode(lua_State *
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) + 1;
+ encoded_len = apr_base64_encode_len(plain_len);
if (encoded_len) {
encoded = apr_palloc(r->pool, encoded_len);
- apr_base64_encode(encoded, plain, plain_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;
}
@@ -728,13 +747,16 @@ static int lua_apr_b64decode(lua_State *
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) + 1;
+ decoded_len = apr_base64_decode_len(encoded);
if (decoded_len) {
plain = apr_palloc(r->pool, decoded_len);
- apr_base64_decode(plain, encoded);
+ 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;
}
@@ -822,14 +844,103 @@ static int lua_apr_sha1(lua_State *L)
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_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_ap_banner; r:banner() - Returns the current server banner
+ * lua_apr_rmdir; r:rmdir(string) - Removes a directory
*/
-static int lua_ap_banner(lua_State *L)
+static int lua_apr_rmdir(lua_State *L)
{
- lua_pushstring(L, ap_get_server_banner());
+ 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;
}
@@ -841,9 +952,9 @@ static int lua_ap_mpm_query(lua_State *L
int x,
y;
- x = lua_tonumber(L, 1);
+ x = lua_tointeger(L, 1);
ap_mpm_query(x, &y);
- lua_pushnumber(L, y);
+ lua_pushinteger(L, y);
return 1;
}
@@ -889,28 +1000,30 @@ static int lua_ap_expr(lua_State *L)
/*
- * lua_ap_regex; r:regex(string, pattern) - Evaluates a regex and returns
- * captures if matched
+ * 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;
+ rv,
+ flags;
const char *pattern,
*source;
char *err;
ap_regex_t regex;
- ap_regmatch_t matches[AP_MAX_REG_MATCH+1];
+ 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);
- pattern = lua_tostring(L, 2);
- source = lua_tostring(L, 3);
+ source = lua_tostring(L, 2);
+ pattern = lua_tostring(L, 3);
+ flags = luaL_optinteger(L, 4, 0);
- rv = ap_regcomp(®ex, pattern, 0);
+ rv = ap_regcomp(®ex, pattern, flags);
if (rv) {
lua_pushboolean(L, 0);
err = apr_palloc(r->pool, 256);
@@ -919,7 +1032,17 @@ static int lua_ap_regex(lua_State *L)
return 2;
}
- rv = ap_regexec(®ex, source, AP_MAX_REG_MATCH, matches, 0);
+ if (regex.re_nsub > MODLUA_MAX_REG_MATCH) {
+ lua_pushboolean(L, 0);
+ err = apr_palloc(r->pool, 64);
+ apr_snprintf(err, 64,
+ "regcomp found %d matches; only %d allowed.",
+ regex.re_nsub, MODLUA_MAX_REG_MATCH);
+ lua_pushstring(L, err);
+ return 2;
+ }
+
+ rv = ap_regexec(®ex, source, MODLUA_MAX_REG_MATCH, matches, 0);
if (rv == AP_REG_NOMATCH) {
lua_pushboolean(L, 0);
return 1;
@@ -953,7 +1076,7 @@ static int lua_ap_scoreboard_process(lua
luaL_checktype(L, 1, LUA_TUSERDATA);
luaL_checktype(L, 2, LUA_TNUMBER);
- i = lua_tonumber(L, 2);
+ i = lua_tointeger(L, 2);
ps_record = ap_get_scoreboard_process(i);
if (ps_record) {
lua_newtable(L);
@@ -1008,8 +1131,8 @@ static int lua_ap_scoreboard_worker(lua_
luaL_checktype(L, 1, LUA_TUSERDATA);
luaL_checktype(L, 2, LUA_TNUMBER);
luaL_checktype(L, 3, LUA_TNUMBER);
- i = lua_tonumber(L, 2);
- j = lua_tonumber(L, 3);
+ 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);
@@ -1019,7 +1142,7 @@ static int lua_ap_scoreboard_worker(lua_
lua_settable(L, -3);
lua_pushstring(L, "bytes_served");
- lua_pushnumber(L, ws_record->bytes_served);
+ lua_pushnumber(L, (lua_Number) ws_record->bytes_served);
lua_settable(L, -3);
lua_pushstring(L, "client");
@@ -1027,7 +1150,7 @@ static int lua_ap_scoreboard_worker(lua_
lua_settable(L, -3);
lua_pushstring(L, "conn_bytes");
- lua_pushnumber(L, ws_record->conn_bytes);
+ lua_pushnumber(L, (lua_Number) ws_record->conn_bytes);
lua_settable(L, -3);
lua_pushstring(L, "conn_count");
@@ -1039,7 +1162,7 @@ static int lua_ap_scoreboard_worker(lua_
lua_settable(L, -3);
lua_pushstring(L, "last_used");
- lua_pushnumber(L, ws_record->last_used);
+ lua_pushnumber(L, (lua_Number) ws_record->last_used);
lua_settable(L, -3);
lua_pushstring(L, "pid");
@@ -1051,7 +1174,7 @@ static int lua_ap_scoreboard_worker(lua_
lua_settable(L, -3);
lua_pushstring(L, "start_time");
- lua_pushnumber(L, ws_record->start_time);
+ lua_pushnumber(L, (lua_Number) ws_record->start_time);
lua_settable(L, -3);
lua_pushstring(L, "status");
@@ -1059,7 +1182,7 @@ static int lua_ap_scoreboard_worker(lua_
lua_settable(L, -3);
lua_pushstring(L, "stop_time");
- lua_pushnumber(L, ws_record->stop_time);
+ lua_pushnumber(L, (lua_Number) ws_record->stop_time);
lua_settable(L, -3);
lua_pushstring(L, "tid");
@@ -1085,23 +1208,13 @@ static int lua_ap_scoreboard_worker(lua_
}
/*
- * lua_ap_restarted; r:started() - Returns the timestamp of last server
- * (re)start
- */
-static int lua_ap_restarted(lua_State *L)
-{
- lua_pushnumber(L, ap_scoreboard_image->global->restart_time);
- return 1;
-}
-
-/*
* 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, now);
+ lua_pushnumber(L, (lua_Number) now);
return 1;
}
@@ -1141,7 +1254,7 @@ static int lua_ap_module_info(lua_State
luaL_checktype(L, 1, LUA_TSTRING);
moduleName = lua_tostring(L, 1);
mod = ap_find_linked_module(moduleName);
- if (mod) {
+ if (mod && mod->cmds) {
const command_rec *cmd;
lua_newtable(L);
lua_pushstring(L, "commands");
@@ -1191,42 +1304,93 @@ static int lua_ap_set_document_root(lua_
}
/*
- * lua_ap_stat; r:stat(filename) - Runs stat on a file and returns the file
- * info as a table
+ * 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);
- if (apr_stat(&file_info, filename, APR_FINFO_NORM, r->pool) == OK) {
+ wanted = luaL_optinteger(L, 3, APR_FINFO_MIN);
+ if (apr_stat(&file_info, filename, wanted, r->pool) == OK) {
lua_newtable(L);
-
- lua_pushstring(L, "mtime");
- lua_pushinteger(L, file_info.mtime);
- lua_settable(L, -3);
-
- lua_pushstring(L, "atime");
- lua_pushinteger(L, file_info.atime);
- lua_settable(L, -3);
-
- lua_pushstring(L, "ctime");
- lua_pushinteger(L, file_info.ctime);
- lua_settable(L, -3);
-
- lua_pushstring(L, "size");
- lua_pushinteger(L, file_info.size);
- lua_settable(L, -3);
-
- lua_pushstring(L, "filetype");
- lua_pushinteger(L, file_info.filetype);
- lua_settable(L, -3);
-
+ 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 {
@@ -1293,7 +1457,6 @@ static int lua_ap_server_info(lua_State
*/
static int lua_ap_set_context_info(lua_State *L)
{
-
request_rec *r;
const char *prefix;
const char *document_root;
@@ -1319,7 +1482,6 @@ static int lua_ap_set_context_info(lua_S
*/
static int lua_ap_os_escape_path(lua_State *L)
{
-
char *returnValue;
request_rec *r;
const char *path;
@@ -1345,7 +1507,6 @@ static int lua_ap_os_escape_path(lua_Sta
*/
static int lua_ap_escape_logitem(lua_State *L)
{
-
char *returnValue;
request_rec *r;
const char *str;
@@ -1368,7 +1529,6 @@ static int lua_ap_escape_logitem(lua_Sta
*/
static int lua_ap_strcmp_match(lua_State *L)
{
-
int returnValue;
const char *str;
const char *expected;
@@ -1396,7 +1556,6 @@ static int lua_ap_strcmp_match(lua_State
*/
static int lua_ap_set_keepalive(lua_State *L)
{
-
int returnValue;
request_rec *r;
luaL_checktype(L, 1, LUA_TUSERDATA);
@@ -1417,7 +1576,6 @@ static int lua_ap_set_keepalive(lua_Stat
*/
static int lua_ap_make_etag(lua_State *L)
{
-
char *returnValue;
request_rec *r;
int force_weak;
@@ -1440,7 +1598,6 @@ static int lua_ap_make_etag(lua_State *L
*/
static int lua_ap_send_interim_response(lua_State *L)
{
-
request_rec *r;
int send_headers = 0;
luaL_checktype(L, 1, LUA_TUSERDATA);
@@ -1462,7 +1619,6 @@ static int lua_ap_send_interim_response(
*/
static int lua_ap_custom_response(lua_State *L)
{
-
request_rec *r;
int status;
const char *string;
@@ -1485,19 +1641,17 @@ static int lua_ap_custom_response(lua_St
*/
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_pushinteger(L, returnValue);
+ 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);
@@ -1507,10 +1661,7 @@ static int lua_ap_get_server_name_for_ur
return 1;
}
-
-
-/**
- * ap_state_query (int query_code) item starts a new field */
+/* ap_state_query (int query_code) item starts a new field */
static int lua_ap_state_query(lua_State *L)
{
@@ -1523,12 +1674,15 @@ static int lua_ap_state_query(lua_State
return 1;
}
-static int lua_ap_sleep(lua_State *L)
+/*
+ * lua_ap_usleep; r:usleep(microseconds)
+ * - Sleep for the specified number of microseconds.
+ */
+static int lua_ap_usleep(lua_State *L)
{
-
- int msec;
+ apr_interval_time_t msec;
luaL_checktype(L, 1, LUA_TNUMBER);
- msec = (lua_tonumber(L, 1) * 1000000);
+ msec = (apr_interval_time_t)lua_tonumber(L, 1);
apr_sleep(msec);
return 0;
}
@@ -1584,7 +1738,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:{
@@ -1653,6 +1807,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); \
@@ -1744,6 +1959,21 @@ static const struct luaL_Reg connection_
{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}
@@ -1758,7 +1988,7 @@ static req_fun_t *makefun(const void *fu
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);
@@ -1876,7 +2106,7 @@ AP_LUA_DECLARE(void) ap_lua_load_request
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(&lua_ap_port, APL_REQ_FUNTYPE_INT, p));
+ 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,
@@ -1898,19 +2128,21 @@ AP_LUA_DECLARE(void) ap_lua_load_request
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(&ap_get_server_name, APL_REQ_FUNTYPE_STRING, p));
+ makefun(&lua_ap_get_server_name, APL_REQ_FUNTYPE_STRING, p));
apr_hash_set(dispatch, "auth_name", APR_HASH_KEY_STRING,
- makefun(&ap_auth_name, APL_REQ_FUNTYPE_STRING, p));
+ 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, "sleep", APR_HASH_KEY_STRING,
- makefun(&lua_ap_sleep, 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,
@@ -1919,14 +2151,20 @@ AP_LUA_DECLARE(void) ap_lua_load_request
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, "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, "banner", APR_HASH_KEY_STRING,
- makefun(&lua_ap_banner, APL_REQ_FUNTYPE_LUACFUN, p));
- apr_hash_set(dispatch, "port", APR_HASH_KEY_STRING,
- makefun(&lua_ap_port, 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,
@@ -1935,8 +2173,6 @@ AP_LUA_DECLARE(void) ap_lua_load_request
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, "started", APR_HASH_KEY_STRING,
- makefun(&lua_ap_restarted, 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,
@@ -1975,6 +2211,10 @@ AP_LUA_DECLARE(void) ap_lua_load_request
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");
@@ -2005,7 +2245,7 @@ AP_LUA_DECLARE(void) ap_lua_load_request
}
-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");
@@ -2022,7 +2262,7 @@ AP_LUA_DECLARE(void) ap_lua_push_connect
}
-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");
@@ -2035,7 +2275,7 @@ AP_LUA_DECLARE(void) ap_lua_push_server(
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");
Modified: httpd/httpd/branches/2.4.x/modules/lua/lua_request.h
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/modules/lua/lua_request.h?rev=1491468&r1=1491467&r2=1491468&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/modules/lua/lua_request.h (original)
+++ httpd/httpd/branches/2.4.x/modules/lua/lua_request.h Mon Jun 10 14:36:56 2013
@@ -15,21 +15,23 @@
* 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
#define APL_REQ_FUNTYPE_TABLE 3
#define APL_REQ_FUNTYPE_LUACFUN 4
#define APL_REQ_FUNTYPE_BOOLEAN 5
+#define APL_REQ_FUNTYPE_INT64 6
typedef struct
{
@@ -37,5 +39,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_ */