]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_lua.html.en
xforms
[apache] / docs / manual / mod / mod_lua.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>mod_lua - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
12 <script src="../style/scripts/prettify.js" type="text/javascript">
13 </script>
14
15 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
16 <body>
17 <div id="page-header">
18 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
19 <p class="apache">Apache HTTP Server Version 2.5</p>
20 <img alt="" src="../images/feather.gif" /></div>
21 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
22 <div id="path">
23 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Modules</a></div>
24 <div id="page-content">
25 <div id="preamble"><h1>Apache Module mod_lua</h1>
26 <div class="toplang">
27 <p><span>Available Languages: </span><a href="../en/mod/mod_lua.html" title="English">&nbsp;en&nbsp;</a> |
28 <a href="../fr/mod/mod_lua.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
29 </div>
30 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Provides Lua hooks into various portions of the httpd
31 request processing</td></tr>
32 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
33 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>lua_module</td></tr>
34 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>mod_lua.c</td></tr>
35 <tr><th><a href="module-dict.html#Compatibility">Compatibility:</a></th><td>2.3 and later</td></tr></table>
36 <h3>Summary</h3>
37
38 <p>This module allows the server to be extended with scripts written in the
39 Lua programming language.  The extension points (hooks) available with
40 <code class="module"><a href="../mod/mod_lua.html">mod_lua</a></code> include many of the hooks available to
41 natively compiled Apache HTTP Server modules, such as mapping requests to
42 files, generating dynamic responses, access control, authentication, and
43 authorization</p>
44
45 <p>More information on the Lua programming language can be found at the
46 <a href="http://www.lua.org/">the Lua website</a>.</p>
47
48 <div class="note"><code>mod_lua</code> is still in experimental state.
49 Until it is declared stable, usage and behavior may change
50 at any time, even between stable releases of the 2.4.x series.
51 Be sure to check the CHANGES file before upgrading.</div>
52
53 <div class="warning"><h3>Warning</h3>
54 <p>This module holds a great deal of power over httpd, which is both a 
55 strength and a potential security risk. It is <strong>not</strong> recommended 
56 that you use this module on a server that is shared with users you do not 
57 trust, as it can be abused to change the internal workings of httpd.</p>
58 </div>
59
60 </div>
61 <div id="quickview"><h3 class="directives">Directives</h3>
62 <ul id="toc">
63 <li><img alt="" src="../images/down.gif" /> <a href="#luaauthzprovider">LuaAuthzProvider</a></li>
64 <li><img alt="" src="../images/down.gif" /> <a href="#luacodecache">LuaCodeCache</a></li>
65 <li><img alt="" src="../images/down.gif" /> <a href="#luahookaccesschecker">LuaHookAccessChecker</a></li>
66 <li><img alt="" src="../images/down.gif" /> <a href="#luahookauthchecker">LuaHookAuthChecker</a></li>
67 <li><img alt="" src="../images/down.gif" /> <a href="#luahookcheckuserid">LuaHookCheckUserID</a></li>
68 <li><img alt="" src="../images/down.gif" /> <a href="#luahookfixups">LuaHookFixups</a></li>
69 <li><img alt="" src="../images/down.gif" /> <a href="#luahookinsertfilter">LuaHookInsertFilter</a></li>
70 <li><img alt="" src="../images/down.gif" /> <a href="#luahookmaptostorage">LuaHookMapToStorage</a></li>
71 <li><img alt="" src="../images/down.gif" /> <a href="#luahooktranslatename">LuaHookTranslateName</a></li>
72 <li><img alt="" src="../images/down.gif" /> <a href="#luahooktypechecker">LuaHookTypeChecker</a></li>
73 <li><img alt="" src="../images/down.gif" /> <a href="#luainherit">LuaInherit</a></li>
74 <li><img alt="" src="../images/down.gif" /> <a href="#luamaphandler">LuaMapHandler</a></li>
75 <li><img alt="" src="../images/down.gif" /> <a href="#luapackagecpath">LuaPackageCPath</a></li>
76 <li><img alt="" src="../images/down.gif" /> <a href="#luapackagepath">LuaPackagePath</a></li>
77 <li><img alt="" src="../images/down.gif" /> <a href="#luaquickhandler">LuaQuickHandler</a></li>
78 <li><img alt="" src="../images/down.gif" /> <a href="#luaroot">LuaRoot</a></li>
79 <li><img alt="" src="../images/down.gif" /> <a href="#luascope">LuaScope</a></li>
80 </ul>
81 <h3>Topics</h3>
82 <ul id="topics">
83 <li><img alt="" src="../images/down.gif" /> <a href="#basicconf">Basic Configuration</a></li>
84 <li><img alt="" src="../images/down.gif" /> <a href="#writinghandlers">Writing Handlers</a></li>
85 <li><img alt="" src="../images/down.gif" /> <a href="#writingauthzproviders">Writing Authorization Providers</a></li>
86 <li><img alt="" src="../images/down.gif" /> <a href="#writinghooks">Writing Hooks</a></li>
87 <li><img alt="" src="../images/down.gif" /> <a href="#datastructures">Data Structures</a></li>
88 <li><img alt="" src="../images/down.gif" /> <a href="#logging">Logging Functions</a></li>
89 <li><img alt="" src="../images/down.gif" /> <a href="#apache2">apache2 Package</a></li>
90 </ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
91 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
92 <div class="section">
93 <h2><a name="basicconf" id="basicconf">Basic Configuration</a></h2>
94
95 <p>The basic module loading directive is</p>
96
97 <pre class="prettyprint lang-config">
98     LoadModule lua_module modules/mod_lua.so
99 </pre>
100
101
102 <p>
103 <code>mod_lua</code> provides a handler named <code>lua-script</code>,
104 which can be used with an <code>AddHandler</code> directive:</p>
105
106 <pre class="prettyprint lang-config">
107 AddHandler lua-script .lua
108 </pre>
109
110
111 <p>
112 This will cause <code>mod_lua</code> to handle requests for files
113 ending in <code>.lua</code> by invoking that file's
114 <code>handle</code> function.
115 </p>
116
117 <p>For more flexibility, see <code class="directive">LuaMapHandler</code>.
118 </p>
119
120 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
121 <div class="section">
122 <h2><a name="writinghandlers" id="writinghandlers">Writing Handlers</a></h2>
123 <p> In the Apache HTTP Server API, the handler is a specific kind of hook
124 responsible for generating the response.  Examples of modules that include a
125 handler are <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>, <code class="module"><a href="../mod/mod_cgi.html">mod_cgi</a></code>,
126 and <code class="module"><a href="../mod/mod_status.html">mod_status</a></code>.</p>
127
128 <p><code>mod_lua</code> always looks to invoke a Lua function for the handler, rather than
129 just evaluating a script body CGI style. A handler function looks
130 something like this:</p>
131
132
133 <pre class="prettyprint lang-lua">
134 <strong>example.lua</strong><br />
135 -- example handler
136
137 require "string"
138
139 --[[
140      This is the default method name for Lua handlers, see the optional
141      function-name in the LuaMapHandler directive to choose a different
142      entry point.
143 --]]
144 function handle(r)
145     r.content_type = "text/plain"
146     r:puts("Hello Lua World!\n")
147
148     if r.method == 'GET' then
149         for k, v in pairs( r:parseargs() ) do
150             r:puts( string.format("%s: %s\n", k, v) )
151         end
152     elseif r.method == 'POST' then
153         for k, v in pairs( r:parsebody() ) do
154             r:puts( string.format("%s: %s\n", k, v) )
155         end
156     else
157         r:puts("Unsupported HTTP method " .. r.method)
158     end
159 end
160 </pre>
161
162
163 <p>
164 This handler function just prints out the uri or form encoded
165 arguments to a plaintext page.
166 </p>
167
168 <p>
169 This means (and in fact encourages) that you can have multiple
170 handlers (or hooks, or filters) in the same script.
171 </p>
172
173 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
174 <div class="section">
175 <h2><a name="writingauthzproviders" id="writingauthzproviders">Writing Authorization Providers</a></h2>
176
177
178 <p><code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code> provides a high-level interface to
179 authorization that is much easier to use than using into the relevant
180 hooks directly. The first argument to the
181 <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> directive gives
182 the name of the responsible authorization provider. For any
183 <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> line,
184 <code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code> will call the authorization provider
185 of the given name, passing the rest of the line as parameters. The
186 provider will then check authorization and pass the result as return
187 value.</p>
188
189 <p>The authz provider is normally called before authentication. If it needs to
190 know the authenticated user name (or if the user will be authenticated at
191 all), the provider must return <code>apache2.AUTHZ_DENIED_NO_USER</code>.
192 This will cause authentication to proceed and the authz provider to be
193 called a second time.</p>
194
195 <p>The following authz provider function takes two arguments, one ip
196 address and one user name. It will allow access from the given ip address
197 without authentication, or if the authenticated user matches the second
198 argument:</p>
199
200 <pre class="prettyprint lang-lua">
201 <strong>authz_provider.lua</strong><br />
202
203 require 'apache2'
204
205 function authz_check_foo(r, ip, user)
206     if r.useragent_ip == ip then
207         return apache2.AUTHZ_GRANTED
208     elseif r.user == nil then
209         return apache2.AUTHZ_DENIED_NO_USER
210     elseif r.user == user then
211         return apache2.AUTHZ_GRANTED
212     else
213         return apache2.AUTHZ_DENIED
214     end
215 end
216 </pre>
217
218
219 <p>The following configuration registers this function as provider
220 <code>foo</code> and configures it for URL <code>/</code>:</p>
221 <pre class="prettyprint lang-config">
222 LuaAuthzProvider foo authz_provider.lua authz_check_foo
223 &lt;Location /&gt;
224   Require foo 10.1.2.3 john_doe
225 &lt;/Location&gt;
226 </pre>
227
228
229 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
230 <div class="section">
231 <h2><a name="writinghooks" id="writinghooks">Writing Hooks</a></h2>
232
233 <p>Hook functions are how modules (and Lua scripts) participate in the
234 processing of requests. Each type of hook exposed by the server exists for
235 a specific purposes such as mapping requests to the filesystem,
236 performing access control, or setting mimetypes:</p>
237
238 <table class="bordered"><tr class="header">
239         <th>Hook phase</th>
240         <th>mod_lua directive</th>
241         <th>Description</th>
242     </tr>
243 <tr>
244         <td>Quick handler</td>
245         <td><code class="directive"><a href="#luaquickhandler">LuaQuickHandler</a></code></td>
246         <td>This is the first hook that will be called after a request has 
247             been mapped to a host or virtual host</td>
248     </tr>
249 <tr class="odd">
250         <td>Translate name</td>
251         <td><code class="directive"><a href="#luahooktranslatename">LuaHookTranslateName</a></code></td>
252         <td>This phase translates the requested URI into a filename on the 
253             system. Modules such as <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> and
254             <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> operate in this phase.</td>
255     </tr>
256 <tr>
257         <td>Map to storage</td>
258         <td><code class="directive"><a href="#luahookmaptostorage">LuaHookMapToStorage</a></code></td>
259         <td>This phase maps files to their physical, cached or external/proxied storage. 
260             It can be used by proxy or caching modules</td>
261     </tr>
262 <tr class="odd">
263         <td>Check Access</td>
264         <td><code class="directive"><a href="#luahookaccesschecker">LuaHookAccessChecker</a></code></td>
265         <td>This phase checks whether a client has access to a resource. This 
266             phase is run before the user is authenticated, so beware.
267         </td>
268     </tr>
269 <tr>
270         <td>Check User ID</td>
271         <td><code class="directive"><a href="#luahookcheckuserid">LuaHookCheckUserID</a></code></td>
272         <td>This phase it used to check the negotiated user ID</td>
273     </tr>
274 <tr class="odd">
275         <td>Check Authorization</td>
276         <td><code class="directive"><a href="#luahookauthchecker">LuaHookAuthChecker</a></code> or 
277             <code class="directive"><a href="#luaauthzprovider">LuaAuthzProvider</a></code></td>
278         <td>This phase authorizes a user based on the negotiated credentials, such as 
279             user ID, client certificate etc.
280         </td>
281     </tr>
282 <tr>
283         <td>Check Type</td>
284         <td><code class="directive"><a href="#luahooktypechecker">LuaHookTypeChecker</a></code></td>
285         <td>This phase checks the requested file and assigns a content type and 
286             a handler to it</td>
287     </tr>
288 <tr class="odd">
289         <td>Fixups</td>
290         <td><code class="directive"><a href="#luahookfixups">LuaHookFixups</a></code></td>
291         <td>This is the final "fix anything" phase before the content handlers 
292             are run. Any last-minute changes to the request should be made here.</td>
293     </tr>
294 <tr>
295         <td>Content handler</td>
296         <td>fx. <code>.lua</code> files or through <code class="directive"><a href="#luamaphandler">LuaMapHandler</a></code></td>
297         <td>This is where the content is handled. Files are read, parsed, some are run, 
298             and the result is sent to the client</td>
299     </tr>
300 <tr class="odd">
301         <td>Logging</td>
302         <td>(none)</td>
303         <td>Once a request has been handled, it enters several logging phases, 
304             which logs the request in either the error or access log</td>
305     </tr>
306 </table>
307
308 <p>Hook functions are passed the request object as their only argument 
309 (except for LuaAuthzProvider, which also gets passed the arguments from 
310 the Require directive).
311 They can return any value, depending on the hook, but most commonly
312 they'll return OK, DONE, or DECLINED, which you can write in lua as
313 <code>apache2.OK</code>, <code>apache2.DONE</code>, or
314 <code>apache2.DECLINED</code>, or else an HTTP status code.</p>
315
316
317 <pre class="prettyprint lang-lua">
318 <strong>translate_name.lua</strong><br />
319 -- example hook that rewrites the URI to a filesystem path.
320
321 require 'apache2'
322
323 function translate_name(r)
324     if r.uri == "/translate-name" then
325         r.filename = r.document_root .. "/find_me.txt"
326         return apache2.OK
327     end
328     -- we don't care about this URL, give another module a chance
329     return apache2.DECLINED
330 end
331 </pre>
332
333
334
335 <pre class="prettyprint lang-lua">
336 <strong>translate_name2.lua</strong><br />
337 --[[ example hook that rewrites one URI to another URI. It returns a
338      apache2.DECLINED to give other URL mappers a chance to work on the
339      substitution, including the core translate_name hook which maps based
340      on the DocumentRoot.
341
342      Note: Use the early/late flags in the directive to make it run before
343            or after mod_alias.
344 --]]
345
346 require 'apache2'
347
348 function translate_name(r)
349     if r.uri == "/translate-name" then
350         r.uri = "/find_me.txt"
351         return apache2.DECLINED
352     end
353     return apache2.DECLINED
354 end
355 </pre>
356
357 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
358 <div class="section">
359 <h2><a name="datastructures" id="datastructures">Data Structures</a></h2>
360
361 <dl>
362 <dt>request_rec</dt>
363         <dd>
364         <p>The request_rec is mapped in as a userdata. It has a metatable
365         which lets you do useful things with it. For the most part it
366         has the same fields as the request_rec struct, many of which are writeable as
367         well as readable.  (The table fields' content can be changed, but the
368         fields themselves cannot be set to different tables.)</p>
369
370         <table class="bordered"><tr class="header">
371           <th><strong>Name</strong></th>
372           <th><strong>Lua type</strong></th>
373           <th><strong>Writable</strong></th>
374           <th><strong>Description</strong></th>
375         </tr>
376 <tr>
377           <td><code>allowoverrides</code></td>
378           <td>string</td>
379           <td>no</td>
380           <td>The AllowOverride options applied to the current request.</td>
381         </tr>
382 <tr class="odd">
383           <td><code>ap_auth_type</code></td>
384           <td>string</td>
385           <td>no</td>
386           <td>If an authentication check was made, this is set to the type 
387           of authentication (f.x. <code>basic</code>)</td>
388         </tr>
389 <tr>
390           <td><code>args</code></td>
391           <td>string</td>
392           <td>yes</td>
393           <td>The query string arguments extracted from the request 
394             (f.x. <code>foo=bar&amp;name=johnsmith</code>)</td>
395         </tr>
396 <tr class="odd">
397           <td><code>assbackwards</code></td>
398           <td>boolean</td>
399           <td>no</td>
400           <td>Set to true if this is an HTTP/0.9 style request 
401             (e.g. <code>GET /foo</code> (with no headers) )</td>
402         </tr>
403 <tr>
404           <td><code>auth_name</code></td>
405           <td>string</td>
406           <td>no</td>
407           <td>The realm name used for authorization (if applicable).</td>
408         </tr>
409 <tr class="odd">
410           <td><code>banner</code></td>
411           <td>string</td>
412           <td>no</td>
413           <td>The server banner, f.x. <code>Apache HTTP Server/2.4.3 openssl/0.9.8c</code></td>
414         </tr>
415 <tr>
416           <td><code>basic_auth_pw</code></td>
417           <td>string</td>
418           <td>no</td>
419           <td>The basic auth password sent with this request, if any</td>
420         </tr>
421 <tr class="odd">
422           <td><code>canonical_filename</code></td>
423           <td>string</td>
424           <td>no</td>
425           <td>The canonical filename of the request</td>
426         </tr>
427 <tr>
428           <td><code>content_encoding</code></td>
429           <td>string</td>
430           <td>no</td>
431           <td>The content encoding of the current request</td>
432         </tr>
433 <tr class="odd">
434           <td><code>content_type</code></td>
435           <td>string</td>
436           <td>yes</td>
437           <td>The content type of the current request, as determined in the 
438             type_check phase (f.x. <code>image/gif</code> or <code>text/html</code>)</td>
439         </tr>
440 <tr>
441           <td><code>context_prefix</code></td>
442           <td>string</td>
443           <td>no</td>
444           <td />
445         </tr>
446 <tr class="odd">
447           <td><code>context_document_root</code></td>
448           <td>string</td>
449           <td>no</td>
450           <td />
451         </tr>
452 <tr>
453           <td><code>document_root</code></td>
454           <td>string</td>
455           <td>no</td>
456           <td>The document root of the host</td>
457         </tr>
458 <tr class="odd">
459           <td><code>err_headers_out</code></td>
460           <td>table</td>
461           <td>no</td>
462           <td>MIME header environment for the response, printed even on errors and
463             persist across internal redirects</td>
464         </tr>
465 <tr>
466           <td><code>filename</code></td>
467           <td>string</td>
468           <td>yes</td>
469           <td>The file name that the request maps to, f.x. /www/example.com/foo.txt. This can be 
470             changed in the translate-name or map-to-storage phases of a request to allow the 
471             default handler (or script handlers) to serve a different file than what was requested.</td>
472         </tr>
473 <tr class="odd">
474           <td><code>handler</code></td>
475           <td>string</td>
476           <td>yes</td>
477           <td>The name of the <a href="../handler.html">handler</a> that should serve this request, f.x. 
478             <code>lua-script</code> if it is to be served by mod_lua. This is typically set by the 
479             <code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code> or <code class="directive"><a href="../mod/core.html#sethandler">SetHandler</a></code> 
480             directives, but could also be set via mod_lua to allow another handler to serve up a specific request 
481             that would otherwise not be served by it.
482             </td>
483         </tr>
484 <tr>
485           <td><code>headers_in</code></td>
486           <td>table</td>
487           <td>yes</td>
488           <td>MIME header environment from the request. This contains headers such as <code>Host, 
489             User-Agent, Referer</code> and so on.</td>
490         </tr>
491 <tr class="odd">
492           <td><code>headers_out</code></td>
493           <td>table</td>
494           <td>yes</td>
495           <td>MIME header environment for the response.</td>
496         </tr>
497 <tr>
498           <td><code>hostname</code></td>
499           <td>string</td>
500           <td>no</td>
501           <td>The host name, as set by the <code>Host:</code> header or by a full URI.</td>
502         </tr>
503 <tr class="odd">
504           <td><code>is_https</code></td>
505           <td>boolean</td>
506           <td>no</td>
507           <td>Whether or not this request is done via HTTPS</td>
508         </tr>
509 <tr>
510           <td><code>is_initial_req</code></td>
511           <td>boolean</td>
512           <td>no</td>
513           <td>Whether this request is the initial request or a sub-request</td>
514         </tr>
515 <tr class="odd">
516           <td><code>limit_req_body</code></td>
517           <td>number</td>
518           <td>no</td>
519           <td>The size limit of the request body for this request, or 0 if no limit.</td>
520         </tr>
521 <tr>
522           <td><code>log_id</code></td>
523           <td>string</td>
524           <td>no</td>
525           <td>The ID to identify request in access and error log.</td>
526         </tr>
527 <tr class="odd">
528           <td><code>method</code></td>
529           <td>string</td>
530           <td>no</td>
531           <td>The request method, f.x. <code>GET</code> or <code>POST</code>.</td>
532         </tr>
533 <tr>
534           <td><code>notes</code></td>
535           <td>table</td>
536           <td>yes</td>
537           <td>A list of notes that can be passed on from one module to another.</td>
538         </tr>
539 <tr class="odd">
540           <td><code>options</code></td>
541           <td>string</td>
542           <td>no</td>
543           <td>The Options directive applied to the current request.</td>
544         </tr>
545 <tr>
546           <td><code>path_info</code></td>
547           <td>string</td>
548           <td>no</td>
549           <td>The PATH_INFO extracted from this request.</td>
550         </tr>
551 <tr class="odd">
552           <td><code>port</code></td>
553           <td>number</td>
554           <td>no</td>
555           <td>The server port used by the request.</td>
556         </tr>
557 <tr>
558           <td><code>protocol</code></td>
559           <td>string</td>
560           <td>no</td>
561           <td>The protocol used, f.x. <code>HTTP/1.1</code></td>
562         </tr>
563 <tr class="odd">
564           <td><code>proxyreq</code></td>
565           <td>string</td>
566           <td>yes</td>
567           <td>Denotes whether this is a proxy request or not. This value is generally set in 
568             the post_read_request/translate_name phase of a request.</td>
569         </tr>
570 <tr>
571           <td><code>range</code></td>
572           <td>string</td>
573           <td>no</td>
574           <td>The contents of the <code>Range:</code> header.</td>
575         </tr>
576 <tr class="odd">
577           <td><code>remaining</code></td>
578           <td>number</td>
579           <td>no</td>
580           <td>The number of bytes remaining to be read from the request body.</td>
581         </tr>
582 <tr>
583           <td><code>server_built</code></td>
584           <td>string</td>
585           <td>no</td>
586           <td>The time the server executable was built.</td>
587         </tr>
588 <tr class="odd">
589           <td><code>server_name</code></td>
590           <td>string</td>
591           <td>no</td>
592           <td>The server name for this request.</td>
593         </tr>
594 <tr>
595           <td><code>some_auth_required</code></td>
596           <td>boolean</td>
597           <td>no</td>
598           <td>Whether some authorization is/was required for this request.</td>
599         </tr>
600 <tr class="odd">
601           <td><code>subprocess_env</code></td>
602           <td>table</td>
603           <td>yes</td>
604           <td>The environment variables set for this request.</td>
605         </tr>
606 <tr>
607           <td><code>started</code></td>
608           <td>number</td>
609           <td>no</td>
610           <td>The time the server was (re)started, in seconds since the epoch (Jan 1st, 1970)</td>
611         </tr>
612 <tr class="odd">
613           <td><code>status</code></td>
614           <td>number</td>
615           <td>yes</td>
616           <td>The (current) HTTP return code for this request, f.x. <code>200</code> or <code>404</code>.</td>
617         </tr>
618 <tr>
619           <td><code>the_request</code></td>
620           <td>string</td>
621           <td>no</td>
622           <td>The request string as sent by the client, f.x. <code>GET /foo/bar HTTP/1.1</code>.</td>
623         </tr>
624 <tr class="odd">
625           <td><code>unparsed_uri</code></td>
626           <td>string</td>
627           <td>no</td>
628           <td>The unparsed URI of the request</td>
629         </tr>
630 <tr>
631           <td><code>uri</code></td>
632           <td>string</td>
633           <td>yes</td>
634           <td>The URI after it has been parsed by httpd</td>
635         </tr>
636 <tr class="odd">
637           <td><code>user</code></td>
638           <td>string</td>
639           <td>yes</td>
640           <td>If an authentication check has been made, this is set to the name of the authenticated user.</td>
641         </tr>
642 <tr>
643           <td><code>useragent_ip</code></td>
644           <td>string</td>
645           <td>no</td>
646           <td>The IP of the user agent making the request</td>
647         </tr>
648 </table>
649
650         <p>The request_rec has (at least) the following methods:</p>
651
652         <pre class="prettyprint lang-lua">
653         r:flush() -- flushes the output buffer
654         </pre>
655
656
657         <pre class="prettyprint lang-lua">
658         r:addoutputfilter(name|function) -- add an output filter
659         </pre>
660
661
662         <pre class="prettyprint lang-lua">
663         r:sendfile(filename) -- sends an entire file to the client, using sendfile if supported by the current platform
664         </pre>
665
666
667         <pre class="prettyprint lang-lua">
668         r:parseargs() -- returns a Lua table containing the request's query string arguments
669         </pre>
670
671
672         <pre class="prettyprint lang-lua">
673         r:parsebody() -- parse any POST data in the request and return it as a Lua table
674         </pre>
675
676
677         <pre class="prettyprint lang-lua">
678         r:puts("hello", " world", "!") -- print to response body
679         </pre>
680
681
682         <pre class="prettyprint lang-lua">
683         r:write("a single string") -- print to response body
684         </pre>
685
686
687         <pre class="prettyprint lang-lua">
688         r:escape_html("&lt;html&gt;test&lt;/html&gt;") -- Escapes HTML code and returns the escaped result
689         </pre>
690
691         </dd>
692     </dl>
693
694 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
695 <div class="section">
696 <h2><a name="logging" id="logging">Logging Functions</a></h2>
697
698 <pre class="prettyprint lang-lua">
699         -- examples of logging messages<br />
700         r:trace1("This is a trace log message") -- trace1 through trace8 can be used <br />
701         r:debug("This is a debug log message")<br />
702         r:info("This is an info log message")<br />
703         r:notice("This is an notice log message")<br />
704         r:warn("This is an warn log message")<br />
705         r:err("This is an err log message")<br />
706         r:alert("This is an alert log message")<br />
707         r:crit("This is an crit log message")<br />
708         r:emerg("This is an emerg log message")<br />
709 </pre>
710
711
712 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
713 <div class="section">
714 <h2><a name="apache2" id="apache2">apache2 Package</a></h2>
715 <p>A package named <code>apache2</code> is available with (at least) the following contents.</p>
716 <dl>
717   <dt>apache2.OK</dt>
718   <dd>internal constant OK.  Handlers should return this if they've
719   handled the request.</dd>
720   <dt>apache2.DECLINED</dt>
721   <dd>internal constant DECLINED.  Handlers should return this if
722   they are not going to handle the request.</dd>
723   <dt>apache2.DONE</dt>
724   <dd>internal constant DONE.</dd>
725   <dt>apache2.version</dt>
726   <dd>Apache HTTP server version string</dd>
727   <dt>apache2.HTTP_MOVED_TEMPORARILY</dt>
728   <dd>HTTP status code</dd>
729   <dt>apache2.PROXYREQ_NONE, apache2.PROXYREQ_PROXY, apache2.PROXYREQ_REVERSE, apache2.PROXYREQ_RESPONSE</dt>
730   <dd>internal constants used by <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code></dd>
731   <dt>apache2.AUTHZ_DENIED, apache2.AUTHZ_GRANTED, apache2.AUTHZ_NEUTRAL, apache2.AUTHZ_GENERAL_ERROR, apache2.AUTHZ_DENIED_NO_USER</dt>
732   <dd>internal constants used by <code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code></dd>
733
734 </dl>
735 <p>(Other HTTP status codes are not yet implemented.)</p>
736 </div>
737 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
738 <div class="directive-section"><h2><a name="LuaAuthzProvider" id="LuaAuthzProvider">LuaAuthzProvider</a> <a name="luaauthzprovider" id="luaauthzprovider">Directive</a></h2>
739 <table class="directive">
740 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Plug an authorization provider function into <code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code>
741 </td></tr>
742 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaAuthzProvider provider_name /path/to/lua/script.lua function_name</code></td></tr>
743 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config</td></tr>
744 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
745 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
746 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.5.0 and later</td></tr>
747 </table>
748 <p>After a lua function has been registered as authorization provider, it can be used
749 with the <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> directive:</p>
750
751 <pre class="prettyprint lang-config">
752 LuaRoot /usr/local/apache2/lua
753 LuaAuthzProvider foo authz.lua authz_check_foo
754 &lt;Location /&gt;
755   Require foo johndoe
756 &lt;/Location&gt;
757 </pre>
758
759 <pre class="prettyprint lang-lua">
760 require "apache2"
761 function authz_check_foo(r, who)
762     if r.user ~= who then return apache2.AUTHZ_DENIED
763     return apache2.AUTHZ_GRANTED
764 end
765 </pre>
766
767
768
769
770 </div>
771 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
772 <div class="directive-section"><h2><a name="LuaCodeCache" id="LuaCodeCache">LuaCodeCache</a> <a name="luacodecache" id="luacodecache">Directive</a></h2>
773 <table class="directive">
774 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Configure the compiled code cache.</td></tr>
775 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaCodeCache stat|forever|never</code></td></tr>
776 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaCodeCache stat</code></td></tr>
777 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
778 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
779 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
780 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
781 </table><p>
782     Specify the behavior of the in-memory code cache. The default
783     is stat, which stats the top level script (not any included
784     ones) each time that file is needed, and reloads it if the
785     modified time indicates it is newer than the one it has
786     already loaded. The other values cause it to keep the file
787     cached forever (don't stat and replace) or to never cache the
788     file.</p>
789
790     <p>In general stat or forever is good for production, and stat or never
791     for development.</p>
792
793     <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
794 LuaCodeCache stat
795 LuaCodeCache forever
796 LuaCodeCache never
797     </pre>
798 </div>
799
800
801 </div>
802 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
803 <div class="directive-section"><h2><a name="LuaHookAccessChecker" id="LuaHookAccessChecker">LuaHookAccessChecker</a> <a name="luahookaccesschecker" id="luahookaccesschecker">Directive</a></h2>
804 <table class="directive">
805 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the access_checker phase of request processing</td></tr>
806 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookAccessChecker  /path/to/lua/script.lua  hook_function_name [early|late]</code></td></tr>
807 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
808 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
809 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
810 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
811 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>The optional third argument is supported in 2.3.15 and later</td></tr>
812 </table>
813 <p>Add your hook to the access_checker phase.  An access checker
814 hook function usually returns OK, DECLINED, or HTTP_FORBIDDEN.</p>
815    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
816    control when this script runs relative to other modules.</p></div>
817
818 </div>
819 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
820 <div class="directive-section"><h2><a name="LuaHookAuthChecker" id="LuaHookAuthChecker">LuaHookAuthChecker</a> <a name="luahookauthchecker" id="luahookauthchecker">Directive</a></h2>
821 <table class="directive">
822 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the auth_checker phase of request processing</td></tr>
823 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookAuthChecker  /path/to/lua/script.lua hook_function_name [early|late]</code></td></tr>
824 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
825 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
826 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
827 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
828 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>The optional third argument is supported in 2.3.15 and later</td></tr>
829 </table>
830 <p>Invoke a lua function in the auth_checker phase of processing
831 a request.  This can be used to implement arbitrary authentication
832 and authorization checking.  A very simple example:
833 </p>
834 <pre class="prettyprint lang-lua">
835 require 'apache2'
836
837 -- fake authcheck hook
838 -- If request has no auth info, set the response header and
839 -- return a 401 to ask the browser for basic auth info.
840 -- If request has auth info, don't actually look at it, just
841 -- pretend we got userid 'foo' and validated it.
842 -- Then check if the userid is 'foo' and accept the request.
843 function authcheck_hook(r)
844
845    -- look for auth info
846    auth = r.headers_in['Authorization']
847    if auth ~= nil then
848      -- fake the user
849      r.user = 'foo'
850    end
851
852    if r.user == nil then
853       r:debug("authcheck: user is nil, returning 401")
854       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
855       return 401
856    elseif r.user == "foo" then
857       r:debug('user foo: OK')
858    else
859       r:debug("authcheck: user='" .. r.user .. "'")
860       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
861       return 401
862    end
863    return apache2.OK
864 end
865 </pre>
866
867    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
868    control when this script runs relative to other modules.</p></div>
869
870 </div>
871 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
872 <div class="directive-section"><h2><a name="LuaHookCheckUserID" id="LuaHookCheckUserID">LuaHookCheckUserID</a> <a name="luahookcheckuserid" id="luahookcheckuserid">Directive</a></h2>
873 <table class="directive">
874 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the check_user_id phase of request processing</td></tr>
875 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookCheckUserID  /path/to/lua/script.lua hook_function_name [early|late]</code></td></tr>
876 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
877 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
878 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
879 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
880 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>The optional third argument is supported in 2.3.15 and later</td></tr>
881 </table><p>...</p>
882    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
883    control when this script runs relative to other modules.</p></div>
884
885 </div>
886 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
887 <div class="directive-section"><h2><a name="LuaHookFixups" id="LuaHookFixups">LuaHookFixups</a> <a name="luahookfixups" id="luahookfixups">Directive</a></h2>
888 <table class="directive">
889 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the fixups phase of a request
890 processing</td></tr>
891 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookFixups  /path/to/lua/script.lua hook_function_name</code></td></tr>
892 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
893 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
894 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
895 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
896 </table>
897 <p>
898     Just like LuaHookTranslateName, but executed at the fixups phase
899 </p>
900
901 </div>
902 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
903 <div class="directive-section"><h2><a name="LuaHookInsertFilter" id="LuaHookInsertFilter">LuaHookInsertFilter</a> <a name="luahookinsertfilter" id="luahookinsertfilter">Directive</a></h2>
904 <table class="directive">
905 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the insert_filter phase of request processing</td></tr>
906 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookInsertFilter  /path/to/lua/script.lua hook_function_name</code></td></tr>
907 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
908 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
909 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
910 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
911 </table><p>Not Yet Implemented</p>
912 </div>
913 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
914 <div class="directive-section"><h2><a name="LuaHookMapToStorage" id="LuaHookMapToStorage">LuaHookMapToStorage</a> <a name="luahookmaptostorage" id="luahookmaptostorage">Directive</a></h2>
915 <table class="directive">
916 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the map_to_storage phase of request processing</td></tr>
917 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookMapToStorage  /path/to/lua/script.lua hook_function_name</code></td></tr>
918 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
919 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
920 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
921 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
922 </table>
923     <p>Like <code class="directive">LuaHookTranslateName</code> but executed at the 
924     map-to-storage phase of a request. Modules like mod_cache run at this phase,
925     which makes for an interesting example on what to do here:</p>
926     <pre class="prettyprint lang-config">
927     LuaHookMapToStorage /path/to/lua/script.lua check_cache
928     </pre>
929
930     <pre class="prettyprint lang-lua">
931 require"apache2"
932 cached_files = {}
933
934 function read_file(filename) 
935     local input = io.open(filename, "r")
936     if input then
937         local data = input:read("*a")
938         cached_files[filename] = data
939         file = cached_files[filename]
940         input:close()
941     end
942     return cached_files[filename]
943 end
944
945 function check_cache(r)
946     if r.filename:match("%.png$") then -- Only match PNG files
947         local file = cached_files[r.filename] -- Check cache entries
948         if not file then
949             file = read_file(r.filename)  -- Read file into cache
950         end
951         if file then -- If file exists, write it out
952             r.status = 200
953             r:write(file)
954             r:info(("Sent %s to client from cache"):format(r.filename))
955             return apache2.DONE -- skip default handler for PNG files
956         end
957     end
958     return apache2.DECLINED -- If we had nothing to do, let others serve this.
959 end
960     </pre>
961
962
963     
964 </div>
965 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
966 <div class="directive-section"><h2><a name="LuaHookTranslateName" id="LuaHookTranslateName">LuaHookTranslateName</a> <a name="luahooktranslatename" id="luahooktranslatename">Directive</a></h2>
967 <table class="directive">
968 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the translate name phase of request processing</td></tr>
969 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookTranslateName  /path/to/lua/script.lua  hook_function_name [early|late]</code></td></tr>
970 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</td></tr>
971 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
972 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
973 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
974 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>The optional third argument is supported in 2.3.15 and later</td></tr>
975 </table><p>
976     Add a hook (at APR_HOOK_MIDDLE) to the translate name phase of
977     request processing. The hook function receives a single
978     argument, the request_rec, and should return a status code,
979     which is either an HTTP error code, or the constants defined
980     in the apache2 module: apache2.OK, apache2.DECLINED, or
981     apache2.DONE. </p>
982
983     <p>For those new to hooks, basically each hook will be invoked
984     until one of them returns apache2.OK. If your hook doesn't
985     want to do the translation it should just return
986     apache2.DECLINED. If the request should stop processing, then
987     return apache2.DONE.</p>
988
989     <p>Example:</p>
990
991 <pre class="prettyprint lang-config">
992 # httpd.conf
993 LuaHookTranslateName /scripts/conf/hooks.lua silly_mapper
994 </pre>
995
996
997 <pre class="prettyprint lang-lua">
998 -- /scripts/conf/hooks.lua --
999 require "apache2"
1000 function silly_mapper(r)
1001     if r.uri == "/" then
1002         r.filename = "/var/www/home.lua"
1003         return apache2.OK
1004     else
1005         return apache2.DECLINED
1006     end
1007 end
1008 </pre>
1009
1010
1011    <div class="note"><h3>Context</h3><p>This directive is not valid in <code class="directive"><a href="../mod/core.html#directory">&lt;Directory&gt;</a></code>, <code class="directive"><a href="../mod/core.html#files">&lt;Files&gt;</a></code>, or htaccess
1012    context.</p></div>
1013
1014    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
1015    control when this script runs relative to other modules.</p></div>
1016
1017
1018 </div>
1019 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1020 <div class="directive-section"><h2><a name="LuaHookTypeChecker" id="LuaHookTypeChecker">LuaHookTypeChecker</a> <a name="luahooktypechecker" id="luahooktypechecker">Directive</a></h2>
1021 <table class="directive">
1022 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the type_checker phase of request processing</td></tr>
1023 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaHookTypeChecker  /path/to/lua/script.lua hook_function_name</code></td></tr>
1024 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1025 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1026 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1027 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1028 </table><p>
1029     This directive provides a hook for the type_checker phase of the request processing. 
1030     This phase is where requests are assigned a content type and a handler, and thus can 
1031     be used to modify the type and handler based on input:
1032     </p>
1033     <pre class="prettyprint lang-config">
1034     LuaHookTypeChecker /path/to/lua/script.lua type_checker
1035     </pre>
1036
1037     <pre class="prettyprint lang-lua">
1038     function type_checker(r)
1039         if r.uri:match("%.to_gif$") then -- match foo.png.to_gif
1040             r.content_type = "image/gif" -- assign it the image/gif type
1041             r.handler = "gifWizard"      -- tell the gifWizard module to handle this
1042             r.filename = r.uri:gsub("%.to_gif$", "") -- fix the filename requested
1043             return apache2.OK
1044         end
1045
1046         return apache2.DECLINED
1047     end
1048     </pre>
1049
1050
1051 </div>
1052 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1053 <div class="directive-section"><h2><a name="LuaInherit" id="LuaInherit">LuaInherit</a> <a name="luainherit" id="luainherit">Directive</a></h2>
1054 <table class="directive">
1055 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Controls how parent configuration sections are merged into children</td></tr>
1056 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaInherit none|parent-first|parent-last</code></td></tr>
1057 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaInherit parent-first</code></td></tr>
1058 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1059 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1060 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1061 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1062 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.4.0 and later</td></tr>
1063 </table><p>By default, if LuaHook* directives are used in overlapping
1064     Directory or Location configuration sections, the scripts defined in the
1065     more specific section are run <em>after</em> those defined in the more
1066     generic section (LuaInherit parent-first).  You can reverse this order, or
1067     make the parent context not apply at all.</p>
1068     
1069     <p> In previous 2.3.x releases, the default was effectively to ignore LuaHook*
1070     directives from parent configuration sections.</p>
1071 </div>
1072 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1073 <div class="directive-section"><h2><a name="LuaMapHandler" id="LuaMapHandler">LuaMapHandler</a> <a name="luamaphandler" id="luamaphandler">Directive</a></h2>
1074 <table class="directive">
1075 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Map a path to a lua handler</td></tr>
1076 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaMapHandler uri-pattern /path/to/lua/script.lua [function-name]</code></td></tr>
1077 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1078 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1079 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1080 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1081 </table>
1082     <p>This directive matches a uri pattern to invoke a specific
1083     handler function in a specific file. It uses PCRE regular
1084     expressions to match the uri, and supports interpolating
1085     match groups into both the file path and the function name. 
1086     Be careful writing your regular expressions to avoid security
1087     issues.</p>
1088    <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
1089     LuaMapHandler /(\w+)/(\w+) /scripts/$1.lua handle_$2
1090     </pre>
1091 </div>
1092         <p>This would match uri's such as /photos/show?id=9
1093         to the file /scripts/photos.lua and invoke the
1094         handler function handle_show on the lua vm after
1095         loading that file.</p>
1096
1097 <pre class="prettyprint lang-config">
1098     LuaMapHandler /bingo /scripts/wombat.lua
1099 </pre>
1100
1101         <p>This would invoke the "handle" function, which
1102         is the default if no specific function name is
1103         provided.</p>
1104
1105 </div>
1106 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1107 <div class="directive-section"><h2><a name="LuaPackageCPath" id="LuaPackageCPath">LuaPackageCPath</a> <a name="luapackagecpath" id="luapackagecpath">Directive</a></h2>
1108 <table class="directive">
1109 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Add a directory to lua's package.cpath</td></tr>
1110 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaPackageCPath /path/to/include/?.soa</code></td></tr>
1111 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1112 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1113 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1114 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1115 </table>
1116     <p>Add a path to lua's shared library search path. Follows the same
1117     conventions as lua. This just munges the package.cpath in the
1118     lua vms.</p>
1119
1120
1121 </div>
1122 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1123 <div class="directive-section"><h2><a name="LuaPackagePath" id="LuaPackagePath">LuaPackagePath</a> <a name="luapackagepath" id="luapackagepath">Directive</a></h2>
1124 <table class="directive">
1125 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Add a directory to lua's package.path</td></tr>
1126 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaPackagePath /path/to/include/?.lua</code></td></tr>
1127 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1128 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1129 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1130 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1131 </table><p>Add a path to lua's module search path. Follows the same
1132     conventions as lua. This just munges the package.path in the
1133     lua vms.</p>
1134
1135     <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
1136 LuaPackagePath /scripts/lib/?.lua
1137 LuaPackagePath /scripts/lib/?/init.lua
1138     </pre>
1139 </div>
1140
1141 </div>
1142 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1143 <div class="directive-section"><h2><a name="LuaQuickHandler" id="LuaQuickHandler">LuaQuickHandler</a> <a name="luaquickhandler" id="luaquickhandler">Directive</a></h2>
1144 <table class="directive">
1145 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the quick handler of request processing</td></tr>
1146 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaQuickHandler /path/to/script.lua hook_function_name</code></td></tr>
1147 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</td></tr>
1148 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1149 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1150 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1151 </table><p>...</p>
1152    <div class="note"><h3>Context</h3><p>This directive is not valid in <code class="directive"><a href="../mod/core.html#directory">&lt;Directory&gt;</a></code>, <code class="directive"><a href="../mod/core.html#files">&lt;Files&gt;</a></code>, or htaccess
1153    context.</p></div>
1154
1155 </div>
1156 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1157 <div class="directive-section"><h2><a name="LuaRoot" id="LuaRoot">LuaRoot</a> <a name="luaroot" id="luaroot">Directive</a></h2>
1158 <table class="directive">
1159 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Specify the base path for resolving relative paths for mod_lua directives</td></tr>
1160 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaRoot /path/to/a/directory</code></td></tr>
1161 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1162 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1163 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1164 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1165 </table>
1166     <p>Specify the base path which will be used to evaluate all
1167     relative paths within mod_lua. If not specified they
1168     will be resolved relative to the current working directory,
1169     which may not always work well for a server.</p>
1170
1171 </div>
1172 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1173 <div class="directive-section"><h2><a name="LuaScope" id="LuaScope">LuaScope</a> <a name="luascope" id="luascope">Directive</a></h2>
1174 <table class="directive">
1175 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>One of once, request, conn, thread -- default is once</td></tr>
1176 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaScope once|request|conn|thread|server [min] [max]</code></td></tr>
1177 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaScope once</code></td></tr>
1178 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1179 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1180 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1181 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1182 </table>
1183     <p>Specify the lifecycle scope of the Lua interpreter which will
1184     be used by handlers in this "Directory." The default is "once"</p>
1185
1186    <dl>
1187     <dt>once:</dt> <dd>use the interpreter once and throw it away.</dd>
1188
1189     <dt>request:</dt> <dd>use the interpreter to handle anything based on
1190              the same file within this request, which is also
1191              request scoped.</dd>
1192
1193     <dt>conn:</dt> <dd>Same as request but attached to the connection_rec</dd>
1194
1195     <dt>thread:</dt> <dd>Use the interpreter for the lifetime of the thread 
1196             handling the request (only available with threaded MPMs).</dd>
1197
1198     <dt>server:</dt>  <dd>This one is different than others because the
1199             server scope is quite long lived, and multiple threads
1200             will have the same server_rec. To accommodate this,
1201             server scoped Lua states are stored in an apr
1202             resource list. The <code>min</code> and <code>max</code> arguments 
1203             specify the minimum and maximum number of Lua states to keep in the 
1204             pool.</dd>
1205    </dl>
1206     <p>
1207     Generally speaking, the <code>thread</code> and <code>server</code> scopes 
1208     execute roughly 2-3 times faster than the rest, because they don't have to 
1209     spawn new Lua states on every request (especially with the event MPM, as 
1210     even keepalive requests will use a new thread for each request). If you are 
1211     satisfied that your scripts will not have problems reusing a state, then 
1212     the <code>thread</code> or <code>server</code> scopes should be used for 
1213     maximum performance. While the <code>thread</code> scope will provide the 
1214     fastest responses, the <code>server</code> scope will use less memory, as 
1215     states are pooled, allowing f.x. 1000 threads to share only 100 Lua states, 
1216     thus using only 10% of the memory required by the <code>thread</code> scope.
1217     </p>
1218
1219 </div>
1220 </div>
1221 <div class="bottomlang">
1222 <p><span>Available Languages: </span><a href="../en/mod/mod_lua.html" title="English">&nbsp;en&nbsp;</a> |
1223 <a href="../fr/mod/mod_lua.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
1224 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
1225 <script type="text/javascript"><!--//--><![CDATA[//><!--
1226 var comments_shortname = 'httpd';
1227 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/mod_lua.html';
1228 (function(w, d) {
1229     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
1230         d.write('<div id="comments_thread"><\/div>');
1231         var s = d.createElement('script');
1232         s.type = 'text/javascript';
1233         s.async = true;
1234         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
1235         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
1236     }
1237     else {
1238         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
1239     }
1240 })(window, document);
1241 //--><!]]></script></div><div id="footer">
1242 <p class="apache">Copyright 2012 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
1243 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
1244 if (typeof(prettyPrint) !== 'undefined') {
1245     prettyPrint();
1246 }
1247 //--><!]]></script>
1248 </body></html>