]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_lua.html.en
ye gods what have I done
[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/quickreference.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="#luainputfilter">LuaInputFilter</a></li>
75 <li><img alt="" src="../images/down.gif" /> <a href="#luamaphandler">LuaMapHandler</a></li>
76 <li><img alt="" src="../images/down.gif" /> <a href="#luaoutputfilter">LuaOutputFilter</a></li>
77 <li><img alt="" src="../images/down.gif" /> <a href="#luapackagecpath">LuaPackageCPath</a></li>
78 <li><img alt="" src="../images/down.gif" /> <a href="#luapackagepath">LuaPackagePath</a></li>
79 <li><img alt="" src="../images/down.gif" /> <a href="#luaquickhandler">LuaQuickHandler</a></li>
80 <li><img alt="" src="../images/down.gif" /> <a href="#luaroot">LuaRoot</a></li>
81 <li><img alt="" src="../images/down.gif" /> <a href="#luascope">LuaScope</a></li>
82 </ul>
83 <h3>Topics</h3>
84 <ul id="topics">
85 <li><img alt="" src="../images/down.gif" /> <a href="#basicconf">Basic Configuration</a></li>
86 <li><img alt="" src="../images/down.gif" /> <a href="#writinghandlers">Writing Handlers</a></li>
87 <li><img alt="" src="../images/down.gif" /> <a href="#writingauthzproviders">Writing Authorization Providers</a></li>
88 <li><img alt="" src="../images/down.gif" /> <a href="#writinghooks">Writing Hooks</a></li>
89 <li><img alt="" src="../images/down.gif" /> <a href="#datastructures">Data Structures</a></li>
90 <li><img alt="" src="../images/down.gif" /> <a href="#logging">Logging Functions</a></li>
91 <li><img alt="" src="../images/down.gif" /> <a href="#apache2">apache2 Package</a></li>
92 <li><img alt="" src="../images/down.gif" /> <a href="#modifying_buckets">Modifying contents with Lua filters</a></li>
93 <li><img alt="" src="../images/down.gif" /> <a href="#databases">Database connectivity</a></li>
94 </ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
95 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
96 <div class="section">
97 <h2><a name="basicconf" id="basicconf">Basic Configuration</a></h2>
98
99 <p>The basic module loading directive is</p>
100
101 <pre class="prettyprint lang-config">
102     LoadModule lua_module modules/mod_lua.so
103 </pre>
104
105
106 <p>
107 <code>mod_lua</code> provides a handler named <code>lua-script</code>,
108 which can be used with an <code>AddHandler</code> directive:</p>
109
110 <pre class="prettyprint lang-config">
111 AddHandler lua-script .lua
112 </pre>
113
114
115 <p>
116 This will cause <code>mod_lua</code> to handle requests for files
117 ending in <code>.lua</code> by invoking that file's
118 <code>handle</code> function.
119 </p>
120
121 <p>For more flexibility, see <code class="directive">LuaMapHandler</code>.
122 </p>
123
124 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
125 <div class="section">
126 <h2><a name="writinghandlers" id="writinghandlers">Writing Handlers</a></h2>
127 <p> In the Apache HTTP Server API, the handler is a specific kind of hook
128 responsible for generating the response.  Examples of modules that include a
129 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>,
130 and <code class="module"><a href="../mod/mod_status.html">mod_status</a></code>.</p>
131
132 <p><code>mod_lua</code> always looks to invoke a Lua function for the handler, rather than
133 just evaluating a script body CGI style. A handler function looks
134 something like this:</p>
135
136
137 <pre class="prettyprint lang-lua">
138 <strong>example.lua</strong><br />
139 -- example handler
140
141 require "string"
142
143 --[[
144      This is the default method name for Lua handlers, see the optional
145      function-name in the LuaMapHandler directive to choose a different
146      entry point.
147 --]]
148 function handle(r)
149     r.content_type = "text/plain"
150     r:puts("Hello Lua World!\n")
151
152     if r.method == 'GET' then
153         for k, v in pairs( r:parseargs() ) do
154             r:puts( string.format("%s: %s\n", k, v) )
155         end
156     elseif r.method == 'POST' then
157         for k, v in pairs( r:parsebody() ) do
158             r:puts( string.format("%s: %s\n", k, v) )
159         end
160     else
161         r:puts("Unsupported HTTP method " .. r.method)
162     end
163 end
164 </pre>
165
166
167 <p>
168 This handler function just prints out the uri or form encoded
169 arguments to a plaintext page.
170 </p>
171
172 <p>
173 This means (and in fact encourages) that you can have multiple
174 handlers (or hooks, or filters) in the same script.
175 </p>
176
177 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
178 <div class="section">
179 <h2><a name="writingauthzproviders" id="writingauthzproviders">Writing Authorization Providers</a></h2>
180
181
182 <p><code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code> provides a high-level interface to
183 authorization that is much easier to use than using into the relevant
184 hooks directly. The first argument to the
185 <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> directive gives
186 the name of the responsible authorization provider. For any
187 <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> line,
188 <code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code> will call the authorization provider
189 of the given name, passing the rest of the line as parameters. The
190 provider will then check authorization and pass the result as return
191 value.</p>
192
193 <p>The authz provider is normally called before authentication. If it needs to
194 know the authenticated user name (or if the user will be authenticated at
195 all), the provider must return <code>apache2.AUTHZ_DENIED_NO_USER</code>.
196 This will cause authentication to proceed and the authz provider to be
197 called a second time.</p>
198
199 <p>The following authz provider function takes two arguments, one ip
200 address and one user name. It will allow access from the given ip address
201 without authentication, or if the authenticated user matches the second
202 argument:</p>
203
204 <pre class="prettyprint lang-lua">
205 <strong>authz_provider.lua</strong><br />
206
207 require 'apache2'
208
209 function authz_check_foo(r, ip, user)
210     if r.useragent_ip == ip then
211         return apache2.AUTHZ_GRANTED
212     elseif r.user == nil then
213         return apache2.AUTHZ_DENIED_NO_USER
214     elseif r.user == user then
215         return apache2.AUTHZ_GRANTED
216     else
217         return apache2.AUTHZ_DENIED
218     end
219 end
220 </pre>
221
222
223 <p>The following configuration registers this function as provider
224 <code>foo</code> and configures it for URL <code>/</code>:</p>
225 <pre class="prettyprint lang-config">
226 LuaAuthzProvider foo authz_provider.lua authz_check_foo
227 &lt;Location /&gt;
228   Require foo 10.1.2.3 john_doe
229 &lt;/Location&gt;
230 </pre>
231
232
233 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
234 <div class="section">
235 <h2><a name="writinghooks" id="writinghooks">Writing Hooks</a></h2>
236
237 <p>Hook functions are how modules (and Lua scripts) participate in the
238 processing of requests. Each type of hook exposed by the server exists for
239 a specific purpose, such as mapping requests to the filesystem,
240 performing access control, or setting mimetypes:</p>
241
242 <table class="bordered"><tr class="header">
243         <th>Hook phase</th>
244         <th>mod_lua directive</th>
245         <th>Description</th>
246     </tr>
247 <tr>
248         <td>Quick handler</td>
249         <td><code class="directive"><a href="#luaquickhandler">LuaQuickHandler</a></code></td>
250         <td>This is the first hook that will be called after a request has 
251             been mapped to a host or virtual host</td>
252     </tr>
253 <tr class="odd">
254         <td>Translate name</td>
255         <td><code class="directive"><a href="#luahooktranslatename">LuaHookTranslateName</a></code></td>
256         <td>This phase translates the requested URI into a filename on the 
257             system. Modules such as <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> and
258             <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> operate in this phase.</td>
259     </tr>
260 <tr>
261         <td>Map to storage</td>
262         <td><code class="directive"><a href="#luahookmaptostorage">LuaHookMapToStorage</a></code></td>
263         <td>This phase maps files to their physical, cached or external/proxied storage. 
264             It can be used by proxy or caching modules</td>
265     </tr>
266 <tr class="odd">
267         <td>Check Access</td>
268         <td><code class="directive"><a href="#luahookaccesschecker">LuaHookAccessChecker</a></code></td>
269         <td>This phase checks whether a client has access to a resource. This 
270             phase is run before the user is authenticated, so beware.
271         </td>
272     </tr>
273 <tr>
274         <td>Check User ID</td>
275         <td><code class="directive"><a href="#luahookcheckuserid">LuaHookCheckUserID</a></code></td>
276         <td>This phase it used to check the negotiated user ID</td>
277     </tr>
278 <tr class="odd">
279         <td>Check Authorization</td>
280         <td><code class="directive"><a href="#luahookauthchecker">LuaHookAuthChecker</a></code> or 
281             <code class="directive"><a href="#luaauthzprovider">LuaAuthzProvider</a></code></td>
282         <td>This phase authorizes a user based on the negotiated credentials, such as 
283             user ID, client certificate etc.
284         </td>
285     </tr>
286 <tr>
287         <td>Check Type</td>
288         <td><code class="directive"><a href="#luahooktypechecker">LuaHookTypeChecker</a></code></td>
289         <td>This phase checks the requested file and assigns a content type and 
290             a handler to it</td>
291     </tr>
292 <tr class="odd">
293         <td>Fixups</td>
294         <td><code class="directive"><a href="#luahookfixups">LuaHookFixups</a></code></td>
295         <td>This is the final "fix anything" phase before the content handlers 
296             are run. Any last-minute changes to the request should be made here.</td>
297     </tr>
298 <tr>
299         <td>Content handler</td>
300         <td>fx. <code>.lua</code> files or through <code class="directive"><a href="#luamaphandler">LuaMapHandler</a></code></td>
301         <td>This is where the content is handled. Files are read, parsed, some are run, 
302             and the result is sent to the client</td>
303     </tr>
304 <tr class="odd">
305         <td>Logging</td>
306         <td>(none)</td>
307         <td>Once a request has been handled, it enters several logging phases, 
308             which logs the request in either the error or access log</td>
309     </tr>
310 </table>
311
312 <p>Hook functions are passed the request object as their only argument 
313 (except for LuaAuthzProvider, which also gets passed the arguments from 
314 the Require directive).
315 They can return any value, depending on the hook, but most commonly
316 they'll return OK, DONE, or DECLINED, which you can write in lua as
317 <code>apache2.OK</code>, <code>apache2.DONE</code>, or
318 <code>apache2.DECLINED</code>, or else an HTTP status code.</p>
319
320
321 <pre class="prettyprint lang-lua">
322 <strong>translate_name.lua</strong><br />
323 -- example hook that rewrites the URI to a filesystem path.
324
325 require 'apache2'
326
327 function translate_name(r)
328     if r.uri == "/translate-name" then
329         r.filename = r.document_root .. "/find_me.txt"
330         return apache2.OK
331     end
332     -- we don't care about this URL, give another module a chance
333     return apache2.DECLINED
334 end
335 </pre>
336
337
338
339 <pre class="prettyprint lang-lua">
340 <strong>translate_name2.lua</strong><br />
341 --[[ example hook that rewrites one URI to another URI. It returns a
342      apache2.DECLINED to give other URL mappers a chance to work on the
343      substitution, including the core translate_name hook which maps based
344      on the DocumentRoot.
345
346      Note: Use the early/late flags in the directive to make it run before
347            or after mod_alias.
348 --]]
349
350 require 'apache2'
351
352 function translate_name(r)
353     if r.uri == "/translate-name" then
354         r.uri = "/find_me.txt"
355         return apache2.DECLINED
356     end
357     return apache2.DECLINED
358 end
359 </pre>
360
361 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
362 <div class="section">
363 <h2><a name="datastructures" id="datastructures">Data Structures</a></h2>
364
365 <dl>
366 <dt>request_rec</dt>
367         <dd>
368         <p>The request_rec is mapped in as a userdata. It has a metatable
369         which lets you do useful things with it. For the most part it
370         has the same fields as the request_rec struct, many of which are writeable as
371         well as readable.  (The table fields' content can be changed, but the
372         fields themselves cannot be set to different tables.)</p>
373
374         <table class="bordered"><tr class="header">
375           <th><strong>Name</strong></th>
376           <th><strong>Lua type</strong></th>
377           <th><strong>Writable</strong></th>
378           <th><strong>Description</strong></th>
379         </tr>
380 <tr>
381           <td><code>allowoverrides</code></td>
382           <td>string</td>
383           <td>no</td>
384           <td>The AllowOverride options applied to the current request.</td>
385         </tr>
386 <tr class="odd">
387           <td><code>ap_auth_type</code></td>
388           <td>string</td>
389           <td>no</td>
390           <td>If an authentication check was made, this is set to the type 
391           of authentication (f.x. <code>basic</code>)</td>
392         </tr>
393 <tr>
394           <td><code>args</code></td>
395           <td>string</td>
396           <td>yes</td>
397           <td>The query string arguments extracted from the request 
398             (f.x. <code>foo=bar&amp;name=johnsmith</code>)</td>
399         </tr>
400 <tr class="odd">
401           <td><code>assbackwards</code></td>
402           <td>boolean</td>
403           <td>no</td>
404           <td>Set to true if this is an HTTP/0.9 style request 
405             (e.g. <code>GET /foo</code> (with no headers) )</td>
406         </tr>
407 <tr>
408           <td><code>auth_name</code></td>
409           <td>string</td>
410           <td>no</td>
411           <td>The realm name used for authorization (if applicable).</td>
412         </tr>
413 <tr class="odd">
414           <td><code>banner</code></td>
415           <td>string</td>
416           <td>no</td>
417           <td>The server banner, f.x. <code>Apache HTTP Server/2.4.3 openssl/0.9.8c</code></td>
418         </tr>
419 <tr>
420           <td><code>basic_auth_pw</code></td>
421           <td>string</td>
422           <td>no</td>
423           <td>The basic auth password sent with this request, if any</td>
424         </tr>
425 <tr class="odd">
426           <td><code>canonical_filename</code></td>
427           <td>string</td>
428           <td>no</td>
429           <td>The canonical filename of the request</td>
430         </tr>
431 <tr>
432           <td><code>content_encoding</code></td>
433           <td>string</td>
434           <td>no</td>
435           <td>The content encoding of the current request</td>
436         </tr>
437 <tr class="odd">
438           <td><code>content_type</code></td>
439           <td>string</td>
440           <td>yes</td>
441           <td>The content type of the current request, as determined in the 
442             type_check phase (f.x. <code>image/gif</code> or <code>text/html</code>)</td>
443         </tr>
444 <tr>
445           <td><code>context_prefix</code></td>
446           <td>string</td>
447           <td>no</td>
448           <td />
449         </tr>
450 <tr class="odd">
451           <td><code>context_document_root</code></td>
452           <td>string</td>
453           <td>no</td>
454           <td />
455         </tr>
456 <tr>
457           <td><code>document_root</code></td>
458           <td>string</td>
459           <td>no</td>
460           <td>The document root of the host</td>
461         </tr>
462 <tr class="odd">
463           <td><code>err_headers_out</code></td>
464           <td>table</td>
465           <td>no</td>
466           <td>MIME header environment for the response, printed even on errors and
467             persist across internal redirects</td>
468         </tr>
469 <tr>
470           <td><code>filename</code></td>
471           <td>string</td>
472           <td>yes</td>
473           <td>The file name that the request maps to, f.x. /www/example.com/foo.txt. This can be 
474             changed in the translate-name or map-to-storage phases of a request to allow the 
475             default handler (or script handlers) to serve a different file than what was requested.</td>
476         </tr>
477 <tr class="odd">
478           <td><code>handler</code></td>
479           <td>string</td>
480           <td>yes</td>
481           <td>The name of the <a href="../handler.html">handler</a> that should serve this request, f.x. 
482             <code>lua-script</code> if it is to be served by mod_lua. This is typically set by the 
483             <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> 
484             directives, but could also be set via mod_lua to allow another handler to serve up a specific request 
485             that would otherwise not be served by it.
486             </td>
487         </tr>
488 <tr>
489           <td><code>headers_in</code></td>
490           <td>table</td>
491           <td>yes</td>
492           <td>MIME header environment from the request. This contains headers such as <code>Host, 
493             User-Agent, Referer</code> and so on.</td>
494         </tr>
495 <tr class="odd">
496           <td><code>headers_out</code></td>
497           <td>table</td>
498           <td>yes</td>
499           <td>MIME header environment for the response.</td>
500         </tr>
501 <tr>
502           <td><code>hostname</code></td>
503           <td>string</td>
504           <td>no</td>
505           <td>The host name, as set by the <code>Host:</code> header or by a full URI.</td>
506         </tr>
507 <tr class="odd">
508           <td><code>is_https</code></td>
509           <td>boolean</td>
510           <td>no</td>
511           <td>Whether or not this request is done via HTTPS</td>
512         </tr>
513 <tr>
514           <td><code>is_initial_req</code></td>
515           <td>boolean</td>
516           <td>no</td>
517           <td>Whether this request is the initial request or a sub-request</td>
518         </tr>
519 <tr class="odd">
520           <td><code>limit_req_body</code></td>
521           <td>number</td>
522           <td>no</td>
523           <td>The size limit of the request body for this request, or 0 if no limit.</td>
524         </tr>
525 <tr>
526           <td><code>log_id</code></td>
527           <td>string</td>
528           <td>no</td>
529           <td>The ID to identify request in access and error log.</td>
530         </tr>
531 <tr class="odd">
532           <td><code>method</code></td>
533           <td>string</td>
534           <td>no</td>
535           <td>The request method, f.x. <code>GET</code> or <code>POST</code>.</td>
536         </tr>
537 <tr>
538           <td><code>notes</code></td>
539           <td>table</td>
540           <td>yes</td>
541           <td>A list of notes that can be passed on from one module to another.</td>
542         </tr>
543 <tr class="odd">
544           <td><code>options</code></td>
545           <td>string</td>
546           <td>no</td>
547           <td>The Options directive applied to the current request.</td>
548         </tr>
549 <tr>
550           <td><code>path_info</code></td>
551           <td>string</td>
552           <td>no</td>
553           <td>The PATH_INFO extracted from this request.</td>
554         </tr>
555 <tr class="odd">
556           <td><code>port</code></td>
557           <td>number</td>
558           <td>no</td>
559           <td>The server port used by the request.</td>
560         </tr>
561 <tr>
562           <td><code>protocol</code></td>
563           <td>string</td>
564           <td>no</td>
565           <td>The protocol used, f.x. <code>HTTP/1.1</code></td>
566         </tr>
567 <tr class="odd">
568           <td><code>proxyreq</code></td>
569           <td>string</td>
570           <td>yes</td>
571           <td>Denotes whether this is a proxy request or not. This value is generally set in 
572             the post_read_request/translate_name phase of a request.</td>
573         </tr>
574 <tr>
575           <td><code>range</code></td>
576           <td>string</td>
577           <td>no</td>
578           <td>The contents of the <code>Range:</code> header.</td>
579         </tr>
580 <tr class="odd">
581           <td><code>remaining</code></td>
582           <td>number</td>
583           <td>no</td>
584           <td>The number of bytes remaining to be read from the request body.</td>
585         </tr>
586 <tr>
587           <td><code>server_built</code></td>
588           <td>string</td>
589           <td>no</td>
590           <td>The time the server executable was built.</td>
591         </tr>
592 <tr class="odd">
593           <td><code>server_name</code></td>
594           <td>string</td>
595           <td>no</td>
596           <td>The server name for this request.</td>
597         </tr>
598 <tr>
599           <td><code>some_auth_required</code></td>
600           <td>boolean</td>
601           <td>no</td>
602           <td>Whether some authorization is/was required for this request.</td>
603         </tr>
604 <tr class="odd">
605           <td><code>subprocess_env</code></td>
606           <td>table</td>
607           <td>yes</td>
608           <td>The environment variables set for this request.</td>
609         </tr>
610 <tr>
611           <td><code>started</code></td>
612           <td>number</td>
613           <td>no</td>
614           <td>The time the server was (re)started, in seconds since the epoch (Jan 1st, 1970)</td>
615         </tr>
616 <tr class="odd">
617           <td><code>status</code></td>
618           <td>number</td>
619           <td>yes</td>
620           <td>The (current) HTTP return code for this request, f.x. <code>200</code> or <code>404</code>.</td>
621         </tr>
622 <tr>
623           <td><code>the_request</code></td>
624           <td>string</td>
625           <td>no</td>
626           <td>The request string as sent by the client, f.x. <code>GET /foo/bar HTTP/1.1</code>.</td>
627         </tr>
628 <tr class="odd">
629           <td><code>unparsed_uri</code></td>
630           <td>string</td>
631           <td>no</td>
632           <td>The unparsed URI of the request</td>
633         </tr>
634 <tr>
635           <td><code>uri</code></td>
636           <td>string</td>
637           <td>yes</td>
638           <td>The URI after it has been parsed by httpd</td>
639         </tr>
640 <tr class="odd">
641           <td><code>user</code></td>
642           <td>string</td>
643           <td>yes</td>
644           <td>If an authentication check has been made, this is set to the name of the authenticated user.</td>
645         </tr>
646 <tr>
647           <td><code>useragent_ip</code></td>
648           <td>string</td>
649           <td>no</td>
650           <td>The IP of the user agent making the request</td>
651         </tr>
652 </table>
653
654         <p>The request_rec has (at least) the following methods:</p>
655
656         <pre class="prettyprint lang-lua">
657         r:flush() -- flushes the output buffer
658         </pre>
659
660
661         <pre class="prettyprint lang-lua">
662         r:addoutputfilter(name|function) -- add an output filter
663         </pre>
664
665
666         <pre class="prettyprint lang-lua">
667         r:sendfile(filename) -- sends an entire file to the client, using sendfile if supported by the current platform
668         </pre>
669
670
671         <pre class="prettyprint lang-lua">
672         r:parseargs() -- returns a Lua table containing the request's query string arguments
673         </pre>
674
675
676         <pre class="prettyprint lang-lua">
677         r:parsebody([sizeLimit]) -- parse the request body as a POST and return a lua table.
678                          -- An optional number may be passed to specify the maximum number 
679                          -- of bytes to parse. Default is 8192 bytes.
680         </pre>
681
682
683         <pre class="prettyprint lang-lua">
684         r:puts("hello", " world", "!") -- print to response body
685         </pre>
686
687
688         <pre class="prettyprint lang-lua">
689         r:write("a single string") -- print to response body
690         </pre>
691
692
693         <pre class="prettyprint lang-lua">
694         r:escape_html("&lt;html&gt;test&lt;/html&gt;") -- Escapes HTML code and returns the escaped result
695         </pre>
696
697
698         <pre class="prettyprint lang-lua">
699         r:base64_encode(string) -- Encodes a string using the Base64 encoding standard
700         </pre>
701
702
703         <pre class="prettyprint lang-lua">
704         r:base64_decode(string) -- Decodes a Base64-encoded string
705         </pre>
706
707
708         <pre class="prettyprint lang-lua">
709         r:md5(string) -- Calculates and returns the MD5 digest of a string (binary safe)
710         </pre>
711
712
713         <pre class="prettyprint lang-lua">
714         r:sha1(string) -- Calculates and returns the SHA1 digest of a string (binary safe)
715         </pre>
716
717
718         <pre class="prettyprint lang-lua">
719         r:escape(string) -- URL-Escapes a string
720         </pre>
721
722
723         <pre class="prettyprint lang-lua">
724         r:unescape(string) -- Unescapes an URL-escaped string
725         </pre>
726
727
728         <pre class="prettyprint lang-lua">
729         r:banner() -- Returns the current server banner
730         </pre>
731
732
733         <pre class="prettyprint lang-lua">
734         r:port() -- Returns the current server port used for the request
735         </pre>
736
737
738         <pre class="prettyprint lang-lua">
739         r:mpm_query(number) -- Queries the server for MPM information using ap_mpm_query
740         </pre>
741
742
743         <pre class="prettyprint lang-lua">
744         r:expr(string) -- Evaluates an <a href="../expr.html">expr</a> string.
745         </pre>
746
747
748         <pre class="prettyprint lang-lua">
749         r:scoreboard_process(a) -- Queries the server for information about the process at position <code>a</code>
750         </pre>
751
752
753         <pre class="prettyprint lang-lua">
754         r:scoreboard_worker(a, b) -- Queries for information about the worker thread, <code>b</code>, in process <code>a</code>
755         </pre>
756
757
758         <pre class="prettyprint lang-lua">
759         r:started() -- Returns the time of the last server (re)start
760         </pre>
761
762
763         <pre class="prettyprint lang-lua">
764         r:clock() -- Returns the current time with microsecond precision
765         </pre>
766
767
768         <pre class="prettyprint lang-lua">
769 r:requestbody(filename) -- Reads and returns the request body of a request.
770                         -- If 'filename' is specified, it instead saves the
771                         -- contents to that file.
772         </pre>
773
774
775         <pre class="prettyprint lang-lua">
776         r:add_input_filter(filter_name) -- Adds 'filter_name' as an input filter
777         </pre>
778
779
780         <pre class="prettyprint lang-lua">
781         r:module_info(module_name) -- Queries the server for information about a module
782         </pre>
783
784
785         <pre class="prettyprint lang-lua">
786         r:loaded_modules() -- Returns a list of modules loaded by httpd
787         </pre>
788
789
790         <pre class="prettyprint lang-lua">
791 r:runtime_dir_relative(filename) -- Compute the name of a run-time file (e.g., shared memory "file") 
792                                  -- relative to the appropriate run-time directory. 
793         </pre>
794
795
796         <pre class="prettyprint lang-lua">
797         r:server_info() -- Returns a table containing server information, such as 
798                         -- the name of the httpd executable file, mpm used etc.
799         </pre>
800
801
802         <pre class="prettyprint lang-lua">
803         r:set_document_root(file_path) -- Sets the document root for the request to file_path
804         </pre>
805
806
807         <pre class="prettyprint lang-lua">
808         r:add_version_component(component_string) -- Adds a component to the server banner.
809         </pre>
810
811
812         <pre class="prettyprint lang-lua">
813         r:set_context_info(prefix, docroot) -- Sets the context prefix and context document root for a request
814         </pre>
815
816
817         <pre class="prettyprint lang-lua">
818         r:os_escape_path(file_path) -- Converts an OS path to a URL in an OS dependant way
819         </pre>
820
821
822         <pre class="prettyprint lang-lua">
823         r:escape_logitem(string) -- Escapes a string for logging
824         </pre>
825
826
827         <pre class="prettyprint lang-lua">
828 r:strcmp_match(string, pattern) -- Checks if 'string' matches 'pattern' using strcmp_match (GLOBs).
829                                 -- fx. whether 'www.example.com' matches '*.example.com'
830         </pre>
831
832
833         <pre class="prettyprint lang-lua">
834         r:set_keepalive() -- Sets the keepalive status for a request. Returns true if possible, false otherwise.
835         </pre>
836
837
838         <pre class="prettyprint lang-lua">
839         r:make_etag() -- Constructs and returns the etag for the current request.
840         </pre>
841
842
843         <pre class="prettyprint lang-lua">
844 r:send_interim_response(clear) -- Sends an interim (1xx) response to the client.
845                                -- if 'clear' is true, available headers will be sent and cleared.
846         </pre>
847
848
849         <pre class="prettyprint lang-lua">
850 r:custom_response(status_code, string) -- Construct and set a custom response for a given status code.
851                                        -- This works much like the ErrorDocument directive.
852         </pre>
853
854
855         <pre class="prettyprint lang-lua">
856         r:exists_config_define(string) -- Checks whether a configuration definition exists or not.
857         </pre>
858
859
860         <pre class="prettyprint lang-lua">
861         r:state_query(string) -- Queries the server for state information
862         </pre>
863
864
865         <pre class="prettyprint lang-lua">
866         r:stat(filename) -- Runs stat() on a file, and returns a table with file information
867         </pre>
868
869
870         <pre class="prettyprint lang-lua">
871         r:regex(string, pattern) -- Runs a regular expression match on a string, returning captures if matched.
872         </pre>
873
874
875         <pre class="prettyprint lang-lua">
876         r:sleep(number_of_seconds) -- Puts the script to sleep for a given number of seconds.
877         </pre>
878
879
880         <pre class="prettyprint lang-lua">
881 r:dbacquire(dbType[, dbParams]) -- Acquires a connection to a database and returns a database class.
882                                 -- See '<a href="#databases">Database connectivity</a>' for details.
883         </pre>
884
885         </dd>
886     </dl>
887
888 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
889 <div class="section">
890 <h2><a name="logging" id="logging">Logging Functions</a></h2>
891
892 <pre class="prettyprint lang-lua">
893         -- examples of logging messages<br />
894         r:trace1("This is a trace log message") -- trace1 through trace8 can be used <br />
895         r:debug("This is a debug log message")<br />
896         r:info("This is an info log message")<br />
897         r:notice("This is a notice log message")<br />
898         r:warn("This is a warn log message")<br />
899         r:err("This is an err log message")<br />
900         r:alert("This is an alert log message")<br />
901         r:crit("This is a crit log message")<br />
902         r:emerg("This is an emerg log message")<br />
903 </pre>
904
905
906 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
907 <div class="section">
908 <h2><a name="apache2" id="apache2">apache2 Package</a></h2>
909 <p>A package named <code>apache2</code> is available with (at least) the following contents.</p>
910 <dl>
911   <dt>apache2.OK</dt>
912   <dd>internal constant OK.  Handlers should return this if they've
913   handled the request.</dd>
914   <dt>apache2.DECLINED</dt>
915   <dd>internal constant DECLINED.  Handlers should return this if
916   they are not going to handle the request.</dd>
917   <dt>apache2.DONE</dt>
918   <dd>internal constant DONE.</dd>
919   <dt>apache2.version</dt>
920   <dd>Apache HTTP server version string</dd>
921   <dt>apache2.HTTP_MOVED_TEMPORARILY</dt>
922   <dd>HTTP status code</dd>
923   <dt>apache2.PROXYREQ_NONE, apache2.PROXYREQ_PROXY, apache2.PROXYREQ_REVERSE, apache2.PROXYREQ_RESPONSE</dt>
924   <dd>internal constants used by <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code></dd>
925   <dt>apache2.AUTHZ_DENIED, apache2.AUTHZ_GRANTED, apache2.AUTHZ_NEUTRAL, apache2.AUTHZ_GENERAL_ERROR, apache2.AUTHZ_DENIED_NO_USER</dt>
926   <dd>internal constants used by <code class="module"><a href="../mod/mod_authz_core.html">mod_authz_core</a></code></dd>
927
928 </dl>
929 <p>(Other HTTP status codes are not yet implemented.)</p>
930 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
931 <div class="section">
932 <h2><a name="modifying_buckets" id="modifying_buckets">Modifying contents with Lua filters</a></h2>
933     
934     <p>
935     Filter functions implemented via <code class="directive"><a href="#luainputfilter">LuaInputFilter</a></code> 
936     or <code class="directive"><a href="#luaoutputfilter">LuaOutputFilter</a></code> are designed as 
937     three-stage non-blocking functions using coroutines to suspend and resume a 
938     function as buckets are sent down the filter chain. The core structure of 
939     such a function is:
940     </p>
941     <pre class="prettyprint lang-lua">
942 function filter(r)
943     -- Our first yield is to signal that we are ready to receive buckets.
944     -- Before this yield, we can set up our environment, check for conditions,
945     -- and, if we deem it necessary, decline filtering a request alltogether:
946     if something_bad then
947         return -- This would skip this filter.
948     end
949     -- Regardless of whether we have data to prepend, a yield MUST be called here.
950     -- Note that only output filters can prepend data. Input filters must use the 
951     -- final stage to append data to the content.
952     coroutine.yield([optional header to be prepended to the content])
953     
954     -- After we have yielded, buckets will be sent to us, one by one, and we can 
955     -- do whatever we want with them and then pass on the result.
956     -- Buckets are stored in the global variable 'bucket', so we create a loop
957     -- that checks if 'bucket' is not nil:
958     while bucket ~= nil do
959         local output = mangle(bucket) -- Do some stuff to the content
960         coroutine.yield(output) -- Return our new content to the filter chain
961     end
962
963     -- Once the buckets are gone, 'bucket' is set to nil, which will exit the 
964     -- loop and land us here. Anything extra we want to append to the content
965     -- can be done by doing a final yield here. Both input and output filters 
966     -- can append data to the content in this phase.
967     coroutine.yield([optional footer to be appended to the content])
968 end
969     </pre>
970
971 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
972 <div class="section">
973 <h2><a name="databases" id="databases">Database connectivity</a></h2>
974     
975     <p>
976     Mod_lua implements a simple database feature for querying and running commands
977     on the most popular database engines (mySQL, PostgreSQL, FreeTDS, ODBC, SQLite, Oracle)
978     as well as mod_dbd.
979     </p>
980     <p>The example below shows how to acquire a database handle and return information from a table:</p>
981     <pre class="prettyprint lang-lua">
982 function handler(r)
983     -- Acquire a database handle
984     local database, err = r:dbacquire("mysql", "server=localhost,user=root,dbname=mydb")
985     if not err then
986         -- Select some information from it
987         local results, err = database:select(r, "SELECT `name`, `age` FROM `people` WHERE 1")
988         if not err then
989             local rows = results(0) -- fetch all rows synchronously
990             for k, row in pairs(rows) do
991                 r:puts( string.format("Name: %s, Age: %s&lt;br/&gt;", row[1], row[2]) )
992             end
993         else
994             r:puts("Database query error: " .. err)
995         end
996         database:close()
997     else
998         r:puts("Could not connect to the database: " .. err)
999     end
1000 end
1001     </pre>
1002
1003     <p>
1004     To utilize <code class="module"><a href="../mod/mod_dbd.html">mod_dbd</a></code>, specify <code>mod_dbd</code>
1005     as the database type, or leave the field blank:
1006     </p>
1007     <pre class="prettyprint lang-lua">
1008     local database = r:dbacquire("mod_dbd")
1009     </pre>
1010
1011     <h3><a name="database_object" id="database_object">Database object and contained functions</a></h3>
1012         
1013         <p>The database object returned by <code>dbacquire</code> has the following methods:</p>
1014         <p><strong>Normal select and query from a database:</strong></p>
1015     <pre class="prettyprint lang-lua">
1016 -- Run a statement and return the number of rows affected:
1017 local affected, errmsg = database:query(r, "DELETE FROM `tbl` WHERE 1")
1018
1019 -- Run a statement and return a result set that can be used synchronously or async:
1020 local result, errmsg = database:select(r, "SELECT * FROM `people` WHERE 1")
1021     </pre>
1022
1023         <p><strong>Using prepared statements (recommended):</strong></p>
1024     <pre class="prettyprint lang-lua">
1025 -- Create and run a prepared statement:
1026 local statement, errmsg = database:prepare(r, "DELETE FROM `tbl` WHERE `age` &gt; %u")
1027 if not errmsg then
1028     local result, errmsg = statement:query(20) -- run the statement with age &gt;20
1029 end
1030
1031 -- Fetch a prepared statement from a DBDPrepareSQL directive:
1032 local statement, errmsg = database:prepared(r, "someTag")
1033 if not errmsg then
1034     local result, errmsg = statement:select("John Doe", 123) -- inject the values "John Doe" and 123 into the statement
1035 end
1036
1037 </pre>
1038
1039         <p><strong>Escaping values, closing databases etc:</strong></p>
1040     <pre class="prettyprint lang-lua">
1041 -- Escape a value for use in a statement:
1042 local escaped = database:escape(r, [["'|blabla]])
1043
1044 -- Close a database connection and free up handles:
1045 database:close()
1046
1047 -- Check whether a database connection is up and running:
1048 local connected = database:active()
1049     </pre>
1050
1051     
1052     <h3><a name="result_sets" id="result_sets">Working with result sets</a></h3>
1053     
1054     <p>The result set returned by <code>db:select</code> or by the prepared statement functions 
1055     created through <code>db:prepare</code> can be used to
1056     fetch rows synchronously or asynchronously, depending on the row number specified:<br />
1057     <code>result(0)</code> fetches all rows in a synchronous manner, returning a table of rows.<br />
1058     <code>result(-1)</code> fetches the next available row in the set, asynchronously.<br />
1059     <code>result(N)</code> fetches row number <code>N</code>, asynchronously:
1060     </p>
1061     <pre class="prettyprint lang-lua">
1062 -- fetch a result set using a regular query:
1063 local result, err = db:select(r, "SELECT * FROM `tbl` WHERE 1")
1064
1065 local rows = result(0) -- Fetch ALL rows synchronously
1066 local row = result(-1) -- Fetch the next available row, asynchronously
1067 local row = result(1234) -- Fetch row number 1234, asynchronously
1068     </pre>
1069
1070     <p>One can construct a function that returns an iterative function to iterate over all rows 
1071     in a synchronous or asynchronous way, depending on the async argument:
1072     </p>
1073     <pre class="prettyprint lang-lua">
1074 function rows(resultset, async)
1075     local a = 0
1076     local function getnext()
1077         a = a + 1
1078         local row = resultset(-1)
1079         return row and a or nil, row
1080     end
1081     if not async then
1082         return pairs(resultset(0))
1083     else
1084         return getnext, self
1085     end
1086 end
1087
1088 local statement, err = db:prepare(r, "SELECT * FROM `tbl` WHERE `age` &gt; %u")
1089 if not err then
1090      -- fetch rows asynchronously:
1091     local result, err = statement:select(20)
1092     if not err then
1093         for index, row in rows(result, true) do
1094             ....
1095         end
1096     end
1097
1098      -- fetch rows synchronously:
1099     local result, err = statement:select(20)
1100     if not err then
1101         for index, row in rows(result, false) do
1102             ....
1103         end
1104     end
1105 end
1106     </pre>
1107
1108     
1109     <h3><a name="closing_databases" id="closing_databases">Closing a database connection</a></h3>
1110         
1111
1112     <p>Database handles should be closed using <code>database:close()</code> when they are no longer
1113     needed. If you do not close them manually, they will eventually be garbage collected and 
1114     closed by mod_lua, but you may end up having too many unused connections to the database 
1115     if you leave the closing up to mod_lua. Essentially, the following two measures are
1116     the same:
1117     </p>
1118     <pre class="prettyprint lang-lua">
1119 -- Method 1: Manually close a handle
1120 local database = r:dbacquire("mod_dbd")
1121 database:close() -- All done
1122
1123 -- Method 2: Letting the garbage collector close it
1124 local database = r:dbacquire("mod_dbd")
1125 database = nil -- throw away the reference
1126 collectgarbage() -- close the handle via GC
1127 </pre>
1128
1129     
1130     <h3><a name="database_caveat" id="database_caveat">Precautions when working with databases</a></h3>
1131     
1132     <p>Although the standard <code>query</code> and <code>run</code> functions are freely 
1133     available, it is recommended that you use prepared statements whenever possible, to 
1134     both optimize performance (if your db handle lives on for a long time) and to minimize 
1135     the risk of SQL injection attacks. <code>run</code> and <code>query</code> should only
1136     be used when there are no variables inserted into a statement (a static statement). 
1137     When using dynamic statements, use <code>db:prepare</code> or <code>db:prepared</code>.
1138     </p>
1139     
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="LuaAuthzProvider" id="LuaAuthzProvider">LuaAuthzProvider</a> <a name="luaauthzprovider" id="luaauthzprovider">Directive</a></h2>
1144 <table class="directive">
1145 <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>
1146 </td></tr>
1147 <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>
1148 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config</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 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.4.3 and later</td></tr>
1152 </table>
1153 <p>After a lua function has been registered as authorization provider, it can be used
1154 with the <code class="directive"><a href="../mod/mod_authz_core.html#require">Require</a></code> directive:</p>
1155
1156 <pre class="prettyprint lang-config">
1157 LuaRoot /usr/local/apache2/lua
1158 LuaAuthzProvider foo authz.lua authz_check_foo
1159 &lt;Location /&gt;
1160   Require foo johndoe
1161 &lt;/Location&gt;
1162 </pre>
1163
1164 <pre class="prettyprint lang-lua">
1165 require "apache2"
1166 function authz_check_foo(r, who)
1167     if r.user ~= who then return apache2.AUTHZ_DENIED
1168     return apache2.AUTHZ_GRANTED
1169 end
1170 </pre>
1171
1172
1173
1174
1175 </div>
1176 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1177 <div class="directive-section"><h2><a name="LuaCodeCache" id="LuaCodeCache">LuaCodeCache</a> <a name="luacodecache" id="luacodecache">Directive</a></h2>
1178 <table class="directive">
1179 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Configure the compiled code cache.</td></tr>
1180 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaCodeCache stat|forever|never</code></td></tr>
1181 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaCodeCache stat</code></td></tr>
1182 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1183 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1184 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1185 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1186 </table><p>
1187     Specify the behavior of the in-memory code cache. The default
1188     is stat, which stats the top level script (not any included
1189     ones) each time that file is needed, and reloads it if the
1190     modified time indicates it is newer than the one it has
1191     already loaded. The other values cause it to keep the file
1192     cached forever (don't stat and replace) or to never cache the
1193     file.</p>
1194
1195     <p>In general stat or forever is good for production, and stat or never
1196     for development.</p>
1197
1198     <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
1199 LuaCodeCache stat
1200 LuaCodeCache forever
1201 LuaCodeCache never
1202     </pre>
1203 </div>
1204
1205
1206 </div>
1207 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1208 <div class="directive-section"><h2><a name="LuaHookAccessChecker" id="LuaHookAccessChecker">LuaHookAccessChecker</a> <a name="luahookaccesschecker" id="luahookaccesschecker">Directive</a></h2>
1209 <table class="directive">
1210 <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>
1211 <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>
1212 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1213 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1214 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1215 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1216 <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>
1217 </table>
1218 <p>Add your hook to the access_checker phase.  An access checker
1219 hook function usually returns OK, DECLINED, or HTTP_FORBIDDEN.</p>
1220    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
1221    control when this script runs relative to other modules.</p></div>
1222
1223 </div>
1224 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1225 <div class="directive-section"><h2><a name="LuaHookAuthChecker" id="LuaHookAuthChecker">LuaHookAuthChecker</a> <a name="luahookauthchecker" id="luahookauthchecker">Directive</a></h2>
1226 <table class="directive">
1227 <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>
1228 <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>
1229 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1230 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1231 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1232 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1233 <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>
1234 </table>
1235 <p>Invoke a lua function in the auth_checker phase of processing
1236 a request.  This can be used to implement arbitrary authentication
1237 and authorization checking.  A very simple example:
1238 </p>
1239 <pre class="prettyprint lang-lua">
1240 require 'apache2'
1241
1242 -- fake authcheck hook
1243 -- If request has no auth info, set the response header and
1244 -- return a 401 to ask the browser for basic auth info.
1245 -- If request has auth info, don't actually look at it, just
1246 -- pretend we got userid 'foo' and validated it.
1247 -- Then check if the userid is 'foo' and accept the request.
1248 function authcheck_hook(r)
1249
1250    -- look for auth info
1251    auth = r.headers_in['Authorization']
1252    if auth ~= nil then
1253      -- fake the user
1254      r.user = 'foo'
1255    end
1256
1257    if r.user == nil then
1258       r:debug("authcheck: user is nil, returning 401")
1259       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
1260       return 401
1261    elseif r.user == "foo" then
1262       r:debug('user foo: OK')
1263    else
1264       r:debug("authcheck: user='" .. r.user .. "'")
1265       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
1266       return 401
1267    end
1268    return apache2.OK
1269 end
1270 </pre>
1271
1272    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
1273    control when this script runs relative to other modules.</p></div>
1274
1275 </div>
1276 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1277 <div class="directive-section"><h2><a name="LuaHookCheckUserID" id="LuaHookCheckUserID">LuaHookCheckUserID</a> <a name="luahookcheckuserid" id="luahookcheckuserid">Directive</a></h2>
1278 <table class="directive">
1279 <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>
1280 <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>
1281 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1282 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1283 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1284 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1285 <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>
1286 </table><p>...</p>
1287    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
1288    control when this script runs relative to other modules.</p></div>
1289
1290 </div>
1291 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1292 <div class="directive-section"><h2><a name="LuaHookFixups" id="LuaHookFixups">LuaHookFixups</a> <a name="luahookfixups" id="luahookfixups">Directive</a></h2>
1293 <table class="directive">
1294 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the fixups phase of a request
1295 processing</td></tr>
1296 <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>
1297 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1298 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1299 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1300 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1301 </table>
1302 <p>
1303     Just like LuaHookTranslateName, but executed at the fixups phase
1304 </p>
1305
1306 </div>
1307 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1308 <div class="directive-section"><h2><a name="LuaHookInsertFilter" id="LuaHookInsertFilter">LuaHookInsertFilter</a> <a name="luahookinsertfilter" id="luahookinsertfilter">Directive</a></h2>
1309 <table class="directive">
1310 <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>
1311 <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>
1312 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1313 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1314 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1315 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1316 </table><p>Not Yet Implemented</p>
1317 </div>
1318 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1319 <div class="directive-section"><h2><a name="LuaHookMapToStorage" id="LuaHookMapToStorage">LuaHookMapToStorage</a> <a name="luahookmaptostorage" id="luahookmaptostorage">Directive</a></h2>
1320 <table class="directive">
1321 <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>
1322 <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>
1323 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1324 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1325 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1326 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1327 </table>
1328     <p>Like <code class="directive">LuaHookTranslateName</code> but executed at the 
1329     map-to-storage phase of a request. Modules like mod_cache run at this phase,
1330     which makes for an interesting example on what to do here:</p>
1331     <pre class="prettyprint lang-config">
1332     LuaHookMapToStorage /path/to/lua/script.lua check_cache
1333     </pre>
1334
1335     <pre class="prettyprint lang-lua">
1336 require"apache2"
1337 cached_files = {}
1338
1339 function read_file(filename) 
1340     local input = io.open(filename, "r")
1341     if input then
1342         local data = input:read("*a")
1343         cached_files[filename] = data
1344         file = cached_files[filename]
1345         input:close()
1346     end
1347     return cached_files[filename]
1348 end
1349
1350 function check_cache(r)
1351     if r.filename:match("%.png$") then -- Only match PNG files
1352         local file = cached_files[r.filename] -- Check cache entries
1353         if not file then
1354             file = read_file(r.filename)  -- Read file into cache
1355         end
1356         if file then -- If file exists, write it out
1357             r.status = 200
1358             r:write(file)
1359             r:info(("Sent %s to client from cache"):format(r.filename))
1360             return apache2.DONE -- skip default handler for PNG files
1361         end
1362     end
1363     return apache2.DECLINED -- If we had nothing to do, let others serve this.
1364 end
1365     </pre>
1366
1367
1368     
1369 </div>
1370 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1371 <div class="directive-section"><h2><a name="LuaHookTranslateName" id="LuaHookTranslateName">LuaHookTranslateName</a> <a name="luahooktranslatename" id="luahooktranslatename">Directive</a></h2>
1372 <table class="directive">
1373 <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>
1374 <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>
1375 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</td></tr>
1376 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1377 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1378 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1379 <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>
1380 </table><p>
1381     Add a hook (at APR_HOOK_MIDDLE) to the translate name phase of
1382     request processing. The hook function receives a single
1383     argument, the request_rec, and should return a status code,
1384     which is either an HTTP error code, or the constants defined
1385     in the apache2 module: apache2.OK, apache2.DECLINED, or
1386     apache2.DONE. </p>
1387
1388     <p>For those new to hooks, basically each hook will be invoked
1389     until one of them returns apache2.OK. If your hook doesn't
1390     want to do the translation it should just return
1391     apache2.DECLINED. If the request should stop processing, then
1392     return apache2.DONE.</p>
1393
1394     <p>Example:</p>
1395
1396 <pre class="prettyprint lang-config">
1397 # httpd.conf
1398 LuaHookTranslateName /scripts/conf/hooks.lua silly_mapper
1399 </pre>
1400
1401
1402 <pre class="prettyprint lang-lua">
1403 -- /scripts/conf/hooks.lua --
1404 require "apache2"
1405 function silly_mapper(r)
1406     if r.uri == "/" then
1407         r.filename = "/var/www/home.lua"
1408         return apache2.OK
1409     else
1410         return apache2.DECLINED
1411     end
1412 end
1413 </pre>
1414
1415
1416    <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
1417    context.</p></div>
1418
1419    <div class="note"><h3>Ordering</h3><p>The optional arguments "early" or "late" 
1420    control when this script runs relative to other modules.</p></div>
1421
1422
1423 </div>
1424 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1425 <div class="directive-section"><h2><a name="LuaHookTypeChecker" id="LuaHookTypeChecker">LuaHookTypeChecker</a> <a name="luahooktypechecker" id="luahooktypechecker">Directive</a></h2>
1426 <table class="directive">
1427 <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>
1428 <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>
1429 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1430 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1431 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1432 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1433 </table><p>
1434     This directive provides a hook for the type_checker phase of the request processing. 
1435     This phase is where requests are assigned a content type and a handler, and thus can 
1436     be used to modify the type and handler based on input:
1437     </p>
1438     <pre class="prettyprint lang-config">
1439     LuaHookTypeChecker /path/to/lua/script.lua type_checker
1440     </pre>
1441
1442     <pre class="prettyprint lang-lua">
1443     function type_checker(r)
1444         if r.uri:match("%.to_gif$") then -- match foo.png.to_gif
1445             r.content_type = "image/gif" -- assign it the image/gif type
1446             r.handler = "gifWizard"      -- tell the gifWizard module to handle this
1447             r.filename = r.uri:gsub("%.to_gif$", "") -- fix the filename requested
1448             return apache2.OK
1449         end
1450
1451         return apache2.DECLINED
1452     end
1453     </pre>
1454
1455
1456 </div>
1457 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1458 <div class="directive-section"><h2><a name="LuaInherit" id="LuaInherit">LuaInherit</a> <a name="luainherit" id="luainherit">Directive</a></h2>
1459 <table class="directive">
1460 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Controls how parent configuration sections are merged into children</td></tr>
1461 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaInherit none|parent-first|parent-last</code></td></tr>
1462 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaInherit parent-first</code></td></tr>
1463 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1464 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1465 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1466 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1467 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.4.0 and later</td></tr>
1468 </table><p>By default, if LuaHook* directives are used in overlapping
1469     Directory or Location configuration sections, the scripts defined in the
1470     more specific section are run <em>after</em> those defined in the more
1471     generic section (LuaInherit parent-first).  You can reverse this order, or
1472     make the parent context not apply at all.</p>
1473     
1474     <p> In previous 2.3.x releases, the default was effectively to ignore LuaHook*
1475     directives from parent configuration sections.</p>
1476 </div>
1477 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1478 <div class="directive-section"><h2><a name="LuaInputFilter" id="LuaInputFilter">LuaInputFilter</a> <a name="luainputfilter" id="luainputfilter">Directive</a></h2>
1479 <table class="directive">
1480 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a Lua function for content input filtering</td></tr>
1481 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaInputFilter filter_name /path/to/lua/script.lua function_name</code></td></tr>
1482 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config</td></tr>
1483 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1484 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1485 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.5.0 and later</td></tr>
1486 </table>
1487 <p>Provides a means of adding a Lua function as an input filter. 
1488 As with output filters, input filters work as coroutines, 
1489 first yielding before buffers are sent, then yielding whenever 
1490 a bucket needs to be passed down the chain, and finally (optionally) 
1491 yielding anything that needs to be appended to the input data. The 
1492 global variable <code>bucket</code> holds the buckets as they are passed 
1493 onto the Lua script:
1494 </p>
1495
1496 <pre class="prettyprint lang-config">
1497 LuaInputFilter myInputFilter /www/filter.lua input_filter
1498 &lt;FilesMatch "\.lua&gt;
1499   SetInputFilter myInputFilter
1500 &lt;/FilesMatch&gt;
1501 </pre>
1502
1503 <pre class="prettyprint lang-lua">
1504 --[[
1505     Example input filter that converts all POST data to uppercase.
1506 ]]--
1507 function input_filter(r)
1508     print("luaInputFilter called") -- debug print
1509     coroutine.yield() -- Yield and wait for buckets
1510     while bucket do -- For each bucket, do...
1511         local output = string.upper(bucket) -- Convert all POST data to uppercase
1512         coroutine.yield(output) -- Send converted data down the chain
1513     end
1514     -- No more buckets available.
1515     coroutine.yield("&amp;filterSignature=1234") -- Append signature at the end
1516 end
1517 </pre>
1518
1519 <p>
1520 The input filter supports denying/skipping a filter if it is deemed unwanted:
1521 </p>
1522 <pre class="prettyprint lang-lua">
1523 function input_filter(r)
1524     if not good then
1525         return -- Simply deny filtering, passing on the original content instead
1526     end
1527     coroutine.yield() -- wait for buckets
1528     ... -- insert filter stuff here
1529 end
1530 </pre>
1531
1532 <p>
1533 See "<a href="#modifying_buckets">Modifying contents with Lua 
1534 filters</a>" for more information.
1535 </p>
1536
1537 </div>
1538 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1539 <div class="directive-section"><h2><a name="LuaMapHandler" id="LuaMapHandler">LuaMapHandler</a> <a name="luamaphandler" id="luamaphandler">Directive</a></h2>
1540 <table class="directive">
1541 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Map a path to a lua handler</td></tr>
1542 <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>
1543 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1544 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1545 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1546 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1547 </table>
1548     <p>This directive matches a uri pattern to invoke a specific
1549     handler function in a specific file. It uses PCRE regular
1550     expressions to match the uri, and supports interpolating
1551     match groups into both the file path and the function name. 
1552     Be careful writing your regular expressions to avoid security
1553     issues.</p>
1554    <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
1555     LuaMapHandler /(\w+)/(\w+) /scripts/$1.lua handle_$2
1556     </pre>
1557 </div>
1558         <p>This would match uri's such as /photos/show?id=9
1559         to the file /scripts/photos.lua and invoke the
1560         handler function handle_show on the lua vm after
1561         loading that file.</p>
1562
1563 <pre class="prettyprint lang-config">
1564     LuaMapHandler /bingo /scripts/wombat.lua
1565 </pre>
1566
1567         <p>This would invoke the "handle" function, which
1568         is the default if no specific function name is
1569         provided.</p>
1570
1571 </div>
1572 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1573 <div class="directive-section"><h2><a name="LuaOutputFilter" id="LuaOutputFilter">LuaOutputFilter</a> <a name="luaoutputfilter" id="luaoutputfilter">Directive</a></h2>
1574 <table class="directive">
1575 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a Lua function for content output filtering</td></tr>
1576 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaOutputFilter filter_name /path/to/lua/script.lua function_name</code></td></tr>
1577 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config</td></tr>
1578 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1579 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1580 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>2.5.0 and later</td></tr>
1581 </table>
1582 <p>Provides a means of adding a Lua function as an output filter. 
1583 As with input filters, output filters work as coroutines, 
1584 first yielding before buffers are sent, then yielding whenever 
1585 a bucket needs to be passed down the chain, and finally (optionally) 
1586 yielding anything that needs to be appended to the input data. The 
1587 global variable <code>bucket</code> holds the buckets as they are passed 
1588 onto the Lua script:
1589 </p>
1590
1591 <pre class="prettyprint lang-config">
1592 LuaOutputFilter myOutputFilter /www/filter.lua output_filter
1593 &lt;FilesMatch "\.lua&gt;
1594   SetOutputFilter myOutputFilter
1595 &lt;/FilesMatch&gt;
1596 </pre>
1597
1598 <pre class="prettyprint lang-lua">
1599 --[[
1600     Example output filter that escapes all HTML entities in the output
1601 ]]--
1602 function output_filter(r)
1603     coroutine.yield("(Handled by myOutputFilter)&lt;br/&gt;\n") -- Prepend some data to the output,
1604                                                           -- yield and wait for buckets.
1605     while bucket do -- For each bucket, do...
1606         local output = r:escape_html(bucket) -- Escape all output
1607         coroutine.yield(output) -- Send converted data down the chain
1608     end
1609     -- No more buckets available.
1610 end
1611 </pre>
1612
1613 <p>
1614 As with the input filter, the output filter supports denying/skipping a filter 
1615 if it is deemed unwanted:
1616 </p>
1617 <pre class="prettyprint lang-lua">
1618 function output_filter(r)
1619     if not r.content_type:match("text/html") then
1620         return -- Simply deny filtering, passing on the original content instead
1621     end
1622     coroutine.yield() -- wait for buckets
1623     ... -- insert filter stuff here
1624 end
1625 </pre>
1626
1627 <p>
1628 See "<a href="#modifying_buckets">Modifying contents with Lua filters</a>" for more 
1629 information.
1630 </p>
1631
1632 </div>
1633 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1634 <div class="directive-section"><h2><a name="LuaPackageCPath" id="LuaPackageCPath">LuaPackageCPath</a> <a name="luapackagecpath" id="luapackagecpath">Directive</a></h2>
1635 <table class="directive">
1636 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Add a directory to lua's package.cpath</td></tr>
1637 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaPackageCPath /path/to/include/?.soa</code></td></tr>
1638 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1639 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1640 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1641 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1642 </table>
1643     <p>Add a path to lua's shared library search path. Follows the same
1644     conventions as lua. This just munges the package.cpath in the
1645     lua vms.</p>
1646
1647
1648 </div>
1649 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1650 <div class="directive-section"><h2><a name="LuaPackagePath" id="LuaPackagePath">LuaPackagePath</a> <a name="luapackagepath" id="luapackagepath">Directive</a></h2>
1651 <table class="directive">
1652 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Add a directory to lua's package.path</td></tr>
1653 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaPackagePath /path/to/include/?.lua</code></td></tr>
1654 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1655 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1656 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1657 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1658 </table><p>Add a path to lua's module search path. Follows the same
1659     conventions as lua. This just munges the package.path in the
1660     lua vms.</p>
1661
1662     <div class="example"><h3>Examples:</h3><pre class="prettyprint lang-config">
1663 LuaPackagePath /scripts/lib/?.lua
1664 LuaPackagePath /scripts/lib/?/init.lua
1665     </pre>
1666 </div>
1667
1668 </div>
1669 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1670 <div class="directive-section"><h2><a name="LuaQuickHandler" id="LuaQuickHandler">LuaQuickHandler</a> <a name="luaquickhandler" id="luaquickhandler">Directive</a></h2>
1671 <table class="directive">
1672 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Provide a hook for the quick handler of request processing</td></tr>
1673 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaQuickHandler /path/to/script.lua hook_function_name</code></td></tr>
1674 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</td></tr>
1675 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1676 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1677 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1678 </table>
1679     <p>
1680     This phase is run immediately after the request has been mapped to a virtal host, 
1681     and can be used to either do some request processing before the other phases kick 
1682     in, or to serve a request without the need to translate, map to storage et cetera. 
1683     As this phase is run before anything else, directives such as <code class="directive"><a href="../mod/core.html#location">&lt;Location&gt;</a></code> or <code class="directive"><a href="../mod/core.html#directory">&lt;Directory&gt;</a></code> are void in this phase, just as 
1684     URIs have not been properly parsed yet.
1685     </p>
1686    <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
1687    context.</p></div>
1688
1689 </div>
1690 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1691 <div class="directive-section"><h2><a name="LuaRoot" id="LuaRoot">LuaRoot</a> <a name="luaroot" id="luaroot">Directive</a></h2>
1692 <table class="directive">
1693 <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>
1694 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaRoot /path/to/a/directory</code></td></tr>
1695 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1696 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1697 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1698 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1699 </table>
1700     <p>Specify the base path which will be used to evaluate all
1701     relative paths within mod_lua. If not specified they
1702     will be resolved relative to the current working directory,
1703     which may not always work well for a server.</p>
1704
1705 </div>
1706 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
1707 <div class="directive-section"><h2><a name="LuaScope" id="LuaScope">LuaScope</a> <a name="luascope" id="luascope">Directive</a></h2>
1708 <table class="directive">
1709 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>One of once, request, conn, thread -- default is once</td></tr>
1710 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>LuaScope once|request|conn|thread|server [min] [max]</code></td></tr>
1711 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>LuaScope once</code></td></tr>
1712 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
1713 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>All</td></tr>
1714 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Experimental</td></tr>
1715 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_lua</td></tr>
1716 </table>
1717     <p>Specify the lifecycle scope of the Lua interpreter which will
1718     be used by handlers in this "Directory." The default is "once"</p>
1719
1720    <dl>
1721     <dt>once:</dt> <dd>use the interpreter once and throw it away.</dd>
1722
1723     <dt>request:</dt> <dd>use the interpreter to handle anything based on
1724              the same file within this request, which is also
1725              request scoped.</dd>
1726
1727     <dt>conn:</dt> <dd>Same as request but attached to the connection_rec</dd>
1728
1729     <dt>thread:</dt> <dd>Use the interpreter for the lifetime of the thread 
1730             handling the request (only available with threaded MPMs).</dd>
1731
1732     <dt>server:</dt>  <dd>This one is different than others because the
1733             server scope is quite long lived, and multiple threads
1734             will have the same server_rec. To accommodate this,
1735             server scoped Lua states are stored in an apr
1736             resource list. The <code>min</code> and <code>max</code> arguments 
1737             specify the minimum and maximum number of Lua states to keep in the 
1738             pool.</dd>
1739    </dl>
1740     <p>
1741     Generally speaking, the <code>thread</code> and <code>server</code> scopes 
1742     execute roughly 2-3 times faster than the rest, because they don't have to 
1743     spawn new Lua states on every request (especially with the event MPM, as 
1744     even keepalive requests will use a new thread for each request). If you are 
1745     satisfied that your scripts will not have problems reusing a state, then 
1746     the <code>thread</code> or <code>server</code> scopes should be used for 
1747     maximum performance. While the <code>thread</code> scope will provide the 
1748     fastest responses, the <code>server</code> scope will use less memory, as 
1749     states are pooled, allowing f.x. 1000 threads to share only 100 Lua states, 
1750     thus using only 10% of the memory required by the <code>thread</code> scope.
1751     </p>
1752
1753 </div>
1754 </div>
1755 <div class="bottomlang">
1756 <p><span>Available Languages: </span><a href="../en/mod/mod_lua.html" title="English">&nbsp;en&nbsp;</a> |
1757 <a href="../fr/mod/mod_lua.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
1758 </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>
1759 <script type="text/javascript"><!--//--><![CDATA[//><!--
1760 var comments_shortname = 'httpd';
1761 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/mod_lua.html';
1762 (function(w, d) {
1763     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
1764         d.write('<div id="comments_thread"><\/div>');
1765         var s = d.createElement('script');
1766         s.type = 'text/javascript';
1767         s.async = true;
1768         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
1769         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
1770     }
1771     else {
1772         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
1773     }
1774 })(window, document);
1775 //--><!]]></script></div><div id="footer">
1776 <p class="apache">Copyright 2013 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>
1777 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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[//><!--
1778 if (typeof(prettyPrint) !== 'undefined') {
1779     prettyPrint();
1780 }
1781 //--><!]]></script>
1782 </body></html>