From: Daniel Gruno Date: Tue, 31 Jul 2012 11:52:42 +0000 (+0000) Subject: Add some examples for LuaHookMapToStorage and LuaAuthzProvider X-Git-Tag: 2.5.0-alpha~6555 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b58c0e978061555de9549477a6a67daa740673d8;p=apache Add some examples for LuaHookMapToStorage and LuaAuthzProvider git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1367506 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/mod/mod_lua.xml b/docs/manual/mod/mod_lua.xml index 7664cacc88..7fca709b44 100644 --- a/docs/manual/mod/mod_lua.xml +++ b/docs/manual/mod/mod_lua.xml @@ -610,6 +610,7 @@ LuaPackagePath /scripts/lib/?/init.lua server configvirtual host directory.htaccess +2.5 and later All

@@ -692,7 +693,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 @@ -714,7 +715,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 + +

+
@@ -870,15 +910,21 @@ hook function usually returns OK, DECLINED, or HTTP_FORBIDDEN.

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 + +