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