Return-Path: X-Original-To: apmail-httpd-cvs-archive@www.apache.org Delivered-To: apmail-httpd-cvs-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EBA8E10B15 for ; Mon, 10 Jun 2013 14:37:22 +0000 (UTC) Received: (qmail 80411 invoked by uid 500); 10 Jun 2013 14:37:22 -0000 Delivered-To: apmail-httpd-cvs-archive@httpd.apache.org Received: (qmail 80353 invoked by uid 500); 10 Jun 2013 14:37:22 -0000 Mailing-List: contact cvs-help@httpd.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@httpd.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@httpd.apache.org Received: (qmail 80346 invoked by uid 99); 10 Jun 2013 14:37:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Jun 2013 14:37:22 +0000 X-ASF-Spam-Status: No, hits=-1996.9 required=5.0 tests=ALL_TRUSTED,URI_OBFU_WWW X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Jun 2013 14:37:17 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2086D23888E3; Mon, 10 Jun 2013 14:36:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1491468 [1/2] - in /httpd/httpd/branches/2.4.x: ./ docs/manual/mod/ modules/lua/ Date: Mon, 10 Jun 2013 14:36:57 -0000 To: cvs@httpd.apache.org From: minfrin@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130610143658.2086D23888E3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: minfrin Date: Mon Jun 10 14:36:56 2013 New Revision: 1491468 URL: http://svn.apache.org/r1491468 Log: mod_lua: Fully sync 2.4.x with trunk (catch up to r1490700): 2.4.x patch: http://www.humbedooh.com/mod_lua_2013_06_07.patch Submitted by: humbedooh Reviewed by: fuankg, minfrin Modified: httpd/httpd/branches/2.4.x/STATUS httpd/httpd/branches/2.4.x/docs/manual/mod/mod_lua.xml httpd/httpd/branches/2.4.x/modules/lua/README httpd/httpd/branches/2.4.x/modules/lua/config.m4 httpd/httpd/branches/2.4.x/modules/lua/lua_apr.c httpd/httpd/branches/2.4.x/modules/lua/lua_apr.h httpd/httpd/branches/2.4.x/modules/lua/lua_config.c httpd/httpd/branches/2.4.x/modules/lua/lua_config.h httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.c httpd/httpd/branches/2.4.x/modules/lua/lua_dbd.h httpd/httpd/branches/2.4.x/modules/lua/lua_request.c httpd/httpd/branches/2.4.x/modules/lua/lua_request.h httpd/httpd/branches/2.4.x/modules/lua/lua_vmprep.c httpd/httpd/branches/2.4.x/modules/lua/lua_vmprep.h httpd/httpd/branches/2.4.x/modules/lua/mod_lua.c httpd/httpd/branches/2.4.x/modules/lua/mod_lua.dsp httpd/httpd/branches/2.4.x/modules/lua/mod_lua.h Modified: httpd/httpd/branches/2.4.x/STATUS URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/STATUS?rev=1491468&r1=1491467&r2=1491468&view=diff ============================================================================== --- httpd/httpd/branches/2.4.x/STATUS (original) +++ httpd/httpd/branches/2.4.x/STATUS Mon Jun 10 14:36:56 2013 @@ -100,10 +100,6 @@ PATCHES ACCEPTED TO BACKPORT FROM TRUNK: 2.4.x patch: trunk patch works modulo CHANGES and mmn bump +1: minfrin, jim, sf - * mod_lua: Fully sync 2.4.x with trunk (catch up to r1490700): - 2.4.x patch: http://www.humbedooh.com/mod_lua_2013_06_07.patch - +1: humbedooh, fuankg, minfrin - PATCHES PROPOSED TO BACKPORT FROM TRUNK: [ New proposals should be added at the end of the list ] Modified: httpd/httpd/branches/2.4.x/docs/manual/mod/mod_lua.xml URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/docs/manual/mod/mod_lua.xml?rev=1491468&r1=1491467&r2=1491468&view=diff ============================================================================== --- httpd/httpd/branches/2.4.x/docs/manual/mod/mod_lua.xml (original) +++ httpd/httpd/branches/2.4.x/docs/manual/mod/mod_lua.xml Mon Jun 10 14:36:56 2013 @@ -47,6 +47,13 @@ Until it is declared stable, usage and b at any time, even between stable releases of the 2.4.x series. Be sure to check the CHANGES file before upgrading. +Warning +

This module holds a great deal of power over httpd, which is both a +strength and a potential security risk. It is not recommended +that you use this module on a server that is shared with users you do not +trust, as it can be abused to change the internal workings of httpd.

+
+
Basic Configuration @@ -70,10 +77,10 @@ This will cause mod_lua to ending in .lua by invoking that file's handle function.

- +
Writing Handlers @@ -88,7 +95,7 @@ something like this:

-example.lua +example.lua
-- example handler require "string" @@ -100,19 +107,27 @@ require "string" --]] function handle(r) r.content_type = "text/plain" - r:puts("Hello Lua World!\n") if r.method == 'GET' then + r:puts("Hello Lua World!\n") for k, v in pairs( r:parseargs() ) do r:puts( string.format("%s: %s\n", k, v) ) end elseif r.method == 'POST' then + r:puts("Hello Lua World!\n") for k, v in pairs( r:parsebody() ) do r:puts( string.format("%s: %s\n", k, v) ) end - else + elseif r.method == 'PUT' then +-- use our own Error contents r:puts("Unsupported HTTP method " .. r.method) + r.status = 405 + return apache2.ok + else +-- use the ErrorDocument + return 501 end + return apache2.OK end
@@ -154,7 +169,7 @@ without authentication, or if the authen argument:

-authz_provider.lua +authz_provider.lua
require 'apache2' @@ -186,18 +201,92 @@ LuaAuthzProvider foo authz_provider.lua

Hook functions are how modules (and Lua scripts) participate in the processing of requests. Each type of hook exposed by the server exists for -a specific purpose, such as mapping requests to the filesystem, -performing access control, or setting mimetypes. General purpose hooks -that simply run at handy times in the request lifecycle exist as well.

+a specific purpose, such as mapping requests to the file system, +performing access control, or setting mime types:

-

Hook functions are passed the request object as their only argument. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Hook phasemod_lua directiveDescription
Quick handlerLuaQuickHandlerThis is the first hook that will be called after a request has + been mapped to a host or virtual host
Translate nameLuaHookTranslateNameThis phase translates the requested URI into a filename on the + system. Modules such as mod_alias and + mod_rewrite operate in this phase.
Map to storageLuaHookMapToStorageThis phase maps files to their physical, cached or external/proxied storage. + It can be used by proxy or caching modules
Check AccessLuaHookAccessCheckerThis phase checks whether a client has access to a resource. This + phase is run before the user is authenticated, so beware. +
Check User IDLuaHookCheckUserIDThis phase it used to check the negotiated user ID
Check AuthorizationLuaHookAuthChecker or + LuaAuthzProviderThis phase authorizes a user based on the negotiated credentials, such as + user ID, client certificate etc. +
Check TypeLuaHookTypeCheckerThis phase checks the requested file and assigns a content type and + a handler to it
FixupsLuaHookFixupsThis is the final "fix anything" phase before the content handlers + are run. Any last-minute changes to the request should be made here.
Content handlerfx. .lua files or through LuaMapHandlerThis is where the content is handled. Files are read, parsed, some are run, + and the result is sent to the client
Logging(none)Once a request has been handled, it enters several logging phases, + which logs the request in either the error or access log
+ +

Hook functions are passed the request object as their only argument +(except for LuaAuthzProvider, which also gets passed the arguments from +the Require directive). They can return any value, depending on the hook, but most commonly -they'll return OK, DONE, or DECLINED, which you can write in lua as +they'll return OK, DONE, or DECLINED, which you can write in Lua as apache2.OK, apache2.DONE, or apache2.DECLINED, or else an HTTP status code.

+ -translate_name.lua +translate_name.lua
-- example hook that rewrites the URI to a filesystem path. require 'apache2' @@ -212,15 +301,16 @@ function translate_name(r) end
+ -translate_name2.lua +translate_name2.lua
--[[ example hook that rewrites one URI to another URI. It returns a apache2.DECLINED to give other URL mappers a chance to work on the substitution, including the core translate_name hook which maps based on the DocumentRoot. - Note: It is currently undefined as to whether this runs before or after - mod_alias. + Note: Use the early/late flags in the directive to make it run before + or after mod_alias. --]] require 'apache2' @@ -242,175 +332,326 @@ end

The request_rec is mapped in as a userdata. It has a metatable which lets you do useful things with it. For the most part it - has the same fields as the request_rec struct (see httpd.h - until we get better docs here) many of which are writeable as + has the same fields as the request_rec struct, many of which are writable as well as readable. (The table fields' content can be changed, but the fields themselves cannot be set to different tables.)

- +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name Lua type WritableDescription
allowoverridesstringnoThe AllowOverride options applied to the current request.
ap_auth_type string noIf an authentication check was made, this is set to the type + of authentication (f.x. basic)
args string yesThe query string arguments extracted from the request + (f.x. foo=bar&name=johnsmith)
assbackwards boolean noSet to true if this is an HTTP/0.9 style request + (e.g. GET /foo (with no headers) )
auth_namestringnoThe realm name used for authorization (if applicable).
bannerstringnoThe server banner, f.x. Apache HTTP Server/2.4.3 openssl/0.9.8c
basic_auth_pwstringnoThe basic auth password sent with this request, if any
canonical_filename string noThe canonical filename of the request
content_encoding string noThe content encoding of the current request
content_type string yesThe 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 noThe document root of the host
err_headers_out table noMIME header environment for the response, printed even on errors and + persist across internal redirects
filename string yesThe 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 yesThe 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 yesMIME header environment from the request. This contains headers such as Host, + User-Agent, Referer and so on.
headers_out table yesMIME header environment for the response.
hostname string noThe host name, as set by the Host: header or by a full URI.
is_httpsbooleannoWhether or not this request is done via HTTPS
is_initial_reqbooleannoWhether this request is the initial request or a sub-request
limit_req_bodynumbernoThe size limit of the request body for this request, or 0 if no limit.
log_id string noThe ID to identify request in access and error log.
method string noThe request method, f.x. GET or POST.
notes table yesA list of notes that can be passed on from one module to another.
optionsstringnoThe Options directive applied to the current request.
path_info string noThe PATH_INFO extracted from this request.
portnumbernoThe server port used by the request.
protocol string noThe protocol used, f.x. HTTP/1.1
proxyreq string yesDenotes 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 noThe contents of the Range: header.
remainingnumbernoThe number of bytes remaining to be read from the request body.
server_builtstringnoThe time the server executable was built.
server_namestringnoThe server name for this request.
some_auth_requiredbooleannoWhether some authorization is/was required for this request.
subprocess_env table yesThe environment variables set for this request.
startednumbernoThe time the server was (re)started, in seconds since the epoch (Jan 1st, 1970)
status number yesThe (current) HTTP return code for this request, f.x. 200 or 404.
the_request string noThe request string as sent by the client, f.x. GET /foo/bar HTTP/1.1.
unparsed_uri string noThe unparsed URI of the request
uri string yesThe URI after it has been parsed by httpd
user string yesIf an authentication check has been made, this is set to the name of the authenticated user.
useragent_ip string noThe IP of the user agent making the request
+
+ +
+
Built in functions + +

The request_rec object has (at least) the following methods:

+ + +r:flush() -- flushes the output buffer. + -- Returns true if the flush was successful, false otherwise. + +while we_have_stuff_to_send do + r:puts("Bla bla bla\n") -- print something to client + r:flush() -- flush the buffer (send to client) + r:sleep(0.5) -- fake processing time and repeat +end + + + +r:addoutputfilter(name|function) -- add an output filter: + +r:addoutputfilter("fooFilter") -- add the fooFilter to the output stream + -

The request_rec has (at least) the following methods:

+ +r:sendfile(filename) -- sends an entire file to the client, using sendfile if supported by the current platform: - - r:addoutputfilter(name|function) -- add an output filter - +if use_sendfile_thing then + r:sendfile("/var/www/large_file.img") +end + - + r:parseargs() -- returns two tables; one standard key/value table for regular GET data, -- and one for multi-value data (fx. foo=1&foo=2&foo=3): @@ -427,20 +668,256 @@ r:parsebody([sizeLimit]) -- parse the re local POST, POSTMULTI = r:parsebody(1024*1024) r:puts("Your name is: " .. POST['name'] or "Unknown") - - r:puts("hello", " world", "!") -- print to response body - - - - r:write("a single string") -- print to response body - - - + + +r:puts("hello", " world", "!") -- print to response body, self explanatory + + + +r:write("a single string") -- print to response body, self explanatory + + + +r:escape_html("<html>test</html>") -- Escapes HTML code and returns the escaped result + + + +r:base64_encode(string) -- Encodes a string using the Base64 encoding standard: + +local encoded = r:base64_encode("This is a test") -- returns VGhpcyBpcyBhIHRlc3Q= + + + +r:base64_decode(string) -- Decodes a Base64-encoded string: + +local decoded = r:base64_decode("VGhpcyBpcyBhIHRlc3Q=") -- returns 'This is a test' + + + +r:md5(string) -- Calculates and returns the MD5 digest of a string (binary safe): + +local hash = r:md5("This is a test") -- returns ce114e4501d2f4e2dcea3e17b546f339 + + + +r:sha1(string) -- Calculates and returns the SHA1 digest of a string (binary safe): + +local hash = r:sha1("This is a test") -- returns a54d88e06612d820bc3be72877c74f257b561b19 + + + +r:escape(string) -- URL-Escapes a string: + +local url = "http://foo.bar/1 2 3 & 4 + 5" +local escaped = r:escape(url) -- returns 'http%3a%2f%2ffoo.bar%2f1+2+3+%26+4+%2b+5' + + + +r:unescape(string) -- Unescapes an URL-escaped string: + +local url = "http%3a%2f%2ffoo.bar%2f1+2+3+%26+4+%2b+5" +local unescaped = r:escape(url) -- returns 'http://foo.bar/1 2 3 & 4 + 5' + + + +r:mpm_query(number) -- Queries the server for MPM information using ap_mpm_query: + +local mpm = r.mpm_query(14) +if mpm == 1 then + r:puts("This server uses the Event MPM") +end + + + +r:expr(string) -- Evaluates an expr string. + +if r:expr("%{HTTP_HOST} =~ /^www/") then + r:puts("This host name starts with www") +end + + + +r:scoreboard_process(a) -- Queries the server for information about the process at position a: + +local process = r:scoreboard_process(1) +r:puts("Server 1 has PID " .. process.pid) + + + +r:scoreboard_worker(a, b) -- Queries for information about the worker thread, b, in process a: + +local thread = r:scoreboard_worker(1, 1) +r:puts("Server 1's thread 1 has thread ID " .. thread.tid .. " and is in " .. thread.status .. " status") + + + + +r:clock() -- Returns the current time with microsecond precision + + + +r:requestbody(filename) -- Reads and returns the request body of a request. + -- If 'filename' is specified, it instead saves the + -- contents to that file: + +local input = r:requestbody() +r:puts("You sent the following request body to me:\n") +r:puts(input) + + + +r:add_input_filter(filter_name) -- Adds 'filter_name' as an input filter + + + +r.module_info(module_name) -- Queries the server for information about a module + +local mod = r.module_info("mod_lua.c") +if mod then + for k, v in pairs(mod.commands) do + r:puts( ("%s: %s\n"):format(k,v)) -- print out all directives accepted by this module + end +end + + + +r:loaded_modules() -- Returns a list of modules loaded by httpd: + +for k, module in pairs(r:loaded_modules()) do + r:puts("I have loaded module " .. module .. "\n") +end + + + +r:runtime_dir_relative(filename) -- Compute the name of a run-time file (e.g., shared memory "file") + -- relative to the appropriate run-time directory. + + + +r:server_info() -- Returns a table containing server information, such as + -- the name of the httpd executable file, mpm used etc. + + + +r:set_document_root(file_path) -- Sets the document root for the request to file_path + + + + + +r:set_context_info(prefix, docroot) -- Sets the context prefix and context document root for a request + + + +r:os_escape_path(file_path) -- Converts an OS path to a URL in an OS dependent way + + + +r:escape_logitem(string) -- Escapes a string for logging + + + +r.strcmp_match(string, pattern) -- Checks if 'string' matches 'pattern' using strcmp_match (globs). + -- fx. whether 'www.example.com' matches '*.example.com': + +local match = r.strcmp_match("foobar.com", "foo*.com") +if match then + r:puts("foobar.com matches foo*.com") +end + + + +r:set_keepalive() -- Sets the keepalive status for a request. Returns true if possible, false otherwise. + + + +r:make_etag() -- Constructs and returns the etag for the current request. + + + +r:send_interim_response(clear) -- Sends an interim (1xx) response to the client. + -- if 'clear' is true, available headers will be sent and cleared. + + + +r:custom_response(status_code, string) -- Construct and set a custom response for a given status code. + -- This works much like the ErrorDocument directive: + +r:custom_response(404, "Baleted!") + + + +r.exists_config_define(string) -- Checks whether a configuration definition exists or not: + +if r.exists_config_define("FOO") then + r:puts("httpd was probably run with -DFOO, or it was defined in the configuration") +end + + + +r:state_query(string) -- Queries the server for state information + + + +r:stat(filename) -- Runs stat() on a file, and returns a table with file information: + +local info = r:stat("/var/www/foo.txt") +if info then + r:puts("This file exists and was last modified at: " .. info.modified) +end + + + +r:regex(string, pattern, [flags]) -- Runs a regular expression match on a string, returning captures if matched: + +local matches = r:regex("foo bar baz", [[foo (\w+) (\S*)]]) +if matches then + r:puts("The regex matched, and the last word captured ($2) was: " .. matches[2]) +end + +-- Example ignoring case sensitivity: +local matches = r:regex("FOO bar BAz", [[(foo) bar]], 1) + +-- Flags can be a bitwise combination of: +-- 0x01: Ignore case +-- 0x02: Multiline search + + + +r:sleep(number_of_seconds) -- Puts the script to sleep for a given number of seconds. + -- This can be a floating point number like 1.25 for extra accuracy. + + + r:dbacquire(dbType[, dbParams]) -- Acquires a connection to a database and returns a database class. - -- See 'Database connectivity' for details. - - - + -- See 'Database connectivity' for details. + + + +r:ivm_set("key", value) -- Set an Inter-VM variable to hold a specific value. + -- These values persist even though the VM is gone or not being used, + -- and so should only be used if MaxConnectionsPerChild is > 0 + -- Values can be numbers, strings and booleans, and are stored on a + -- per process basis (so they won't do much good with a prefork mpm) + +r:ivm_get("key") -- Fetches a variable set by ivm_set. Returns the contents of the variable + -- if it exists or nil if no such variable exists. + +-- An example getter/setter that saves a global variable outside the VM: +function handle(r) + -- First VM to call this will get no value, and will have to create it + local foo = r:ivm_get("cached_data") + if not foo then + foo = do_some_calcs() -- fake some return value + r:ivm_set("cached_data", foo) -- set it globally + end + r:puts("Cached data is: ", foo) +end +
@@ -478,10 +955,53 @@ r:dbacquire(dbType[, dbParams]) -- Acqui
HTTP status code
apache2.PROXYREQ_NONE, apache2.PROXYREQ_PROXY, apache2.PROXYREQ_REVERSE, apache2.PROXYREQ_RESPONSE
internal constants used by mod_proxy
+
apache2.AUTHZ_DENIED, apache2.AUTHZ_GRANTED, apache2.AUTHZ_NEUTRAL, apache2.AUTHZ_GENERAL_ERROR, apache2.AUTHZ_DENIED_NO_USER
+
internal constants used by mod_authz_core
+

(Other HTTP status codes are not yet implemented.)

+
+ Modifying contents with Lua filters +

+ Filter functions implemented via LuaInputFilter + or LuaOutputFilter are designed as + three-stage non-blocking functions using coroutines to suspend and resume a + function as buckets are sent down the filter chain. The core structure of + such a function is: +

+ +function filter(r) + -- Our first yield is to signal that we are ready to receive buckets. + -- Before this yield, we can set up our environment, check for conditions, + -- and, if we deem it necessary, decline filtering a request alltogether: + if something_bad then + return -- This would skip this filter. + end + -- Regardless of whether we have data to prepend, a yield MUST be called here. + -- Note that only output filters can prepend data. Input filters must use the + -- final stage to append data to the content. + coroutine.yield([optional header to be prepended to the content]) + + -- After we have yielded, buckets will be sent to us, one by one, and we can + -- do whatever we want with them and then pass on the result. + -- Buckets are stored in the global variable 'bucket', so we create a loop + -- that checks if 'bucket' is not nil: + while bucket ~= nil do + local output = mangle(bucket) -- Do some stuff to the content + coroutine.yield(output) -- Return our new content to the filter chain + end + + -- Once the buckets are gone, 'bucket' is set to nil, which will exit the + -- loop and land us here. Anything extra we want to append to the content + -- can be done by doing a final yield here. Both input and output filters + -- can append data to the content in this phase. + coroutine.yield([optional footer to be appended to the content]) +end + +
+
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. +

Context

This 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_ */