switch to a 60 bit hash
[httpd-crcsyncproxy.git] / modules / lua / docs / writing-handlers.txt
blob10cfb5d890ab41694e942340ce11cf453cca1ca3
1 mod_wombat always looks to invoke a function for the handler, rather than
2 just evaluating a script body CGI style. A handler function looks
3 something like this:
5     -- example.lua --
6     require "string"
8     function handle_something(r)
9         r.content_type = "text/plain"
10         r:puts("Hello Lua World!\n")
11     
12         if r.method == 'GET' then
13             for k, v in pairs( r:parseargs() ) do
14                 r:puts( string.format("%s: %s", k, v) )
15             end
16         elseif r.method == 'POST' then
17             for k, v in pairs( r:parsebody() ) do
18                 r:puts( string.format("%s: %s", k, v) )
19             end
20         else
21             r:puts("unknown HTTP method " .. r.method)
22         end 
23     end
25 This handler function just prints out the uri or form encoded
26 arguments to a plaintext page.
28 This means (and in fact encourages) that you can have multiple
29 handlers (or hooks, or filters) in the same script.
31 Data Structures:
32     request_rec:
33         the request_rec is mapped in as a userdata. It has a metatable
34         which lets you do useful things with it. For the most part it
35         has the same fields as the request_rec struct (see httpd.h 
36         until we get better docs here) many of which are writeable as
37         well as readable, and has (at least) the following methods:
38         
39         r:puts("hello", " world", "!") -- print to response body
40         
41         -- logging functions
42         r:debug("This is a debug log message")
43         r:info("This is an info log message")
44         r:notice("This is an notice log message")
45         r:warn("This is an warn log message")
46         r:err("This is an err log message")
47         r:alert("This is an alert log message")
48         r:crit("This is an crit log message")
49         r:emerg("This is an emerg log message")