]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_lua.xml
839ac21774d3412965f1aedf802c2df2bcdad2fc
[apache] / docs / manual / mod / mod_lua.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <modulesynopsis metafile="mod_lua.xml.meta">
24
25 <name>mod_lua</name>
26
27 <description>Provides Lua hooks into various portions of the httpd
28 request processing</description>
29 <status>Experimental</status>
30 <sourcefile>mod_lua.c</sourcefile>
31 <identifier>lua_module</identifier>
32 <compatibility>2.3 and later</compatibility>
33
34 <summary>
35 <p>This module allows the server to be extended with scripts written in the
36 Lua programming language.  The extension points (hooks) available with
37 <module>mod_lua</module> include many of the hooks available to
38 natively compiled Apache HTTP Server modules, such as mapping requests to
39 files, generating dynamic responses, access control, authentication, and
40 authorization</p>
41
42 <p>More information on the Lua programming language can be found at the
43 <a href="http://www.lua.org/">the Lua website</a>.</p>
44
45 <note><code>mod_lua</code> is still in experimental state.
46 Until it is declared stable, usage and behavior may change
47 at any time.</note>
48
49 </summary>
50
51 <section id="basicconf"><title>Basic Configuration</title>
52
53 <p>The basic module loading directive is</p>
54
55 <example>
56     LoadModule lua_module modules/mod_lua.so
57 </example>
58
59 <p>
60 <code>mod_lua</code> provides a handler named <code>lua-script</code>,
61 which can be used with an <code>AddHandler</code> directive:</p>
62
63 <example>
64 AddHandler lua-script .lua
65 </example>
66
67 <p>
68 This will cause <code>mod_lua</code> to handle requests for files
69 ending in <code>.lua</code> by invoking that file's
70 <code>handle</code> function.
71 </p>
72
73 <p>For more flexibility, see <directive>LuaMapHandler</directive>.
74 </p>
75
76 </section>
77
78 <section id="writinghandlers"><title>Writing Handlers</title>
79 <p> In the Apache HTTP Server API, the handler is a specific kind of hook
80 responsible for generating the response.  Examples of modules that include a
81 handler are <module>mod_proxy</module>, <module>mod_cgi</module>,
82 and <module>mod_status</module>.</p>
83
84 <p><code>mod_lua</code> always looks to invoke a Lua function for the handler, rather than
85 just evaluating a script body CGI style. A handler function looks
86 something like this:</p>
87
88 <example><title>example.lua</title><pre>
89 -- example handler
90
91 require "string"
92
93 --[[
94      This is the default method name for Lua handlers, see the optional
95      function-name in the LuaMapHandler directive to choose a different
96      entry point.
97 --]]
98 function handle(r)
99     r.content_type = "text/plain"
100     r:puts("Hello Lua World!\n")
101
102     if r.method == 'GET' then
103         for k, v in pairs( r:parseargs() ) do
104             r:puts( string.format("%s: %s", k, v) )
105         end
106     elseif r.method == 'POST' then
107         for k, v in pairs( r:parsebody() ) do
108             r:puts( string.format("%s: %s", k, v) )
109         end
110     else
111         r:puts("unknown HTTP method " .. r.method)
112     end
113 end
114 </pre></example>
115
116 <p>
117 This handler function just prints out the uri or form encoded
118 arguments to a plaintext page.
119 </p>
120
121 <p>
122 This means (and in fact encourages) that you can have multiple
123 handlers (or hooks, or filters) in the same script.
124 </p>
125
126 </section>
127
128 <section id="writinghooks"><title>Writing Hooks</title>
129
130 <p>Hook functions are how modules (and Lua scripts) participate in the
131 processing of requests. Each type of hook exposed by the server exists for
132 a specific purposes such as mapping requests to the filesystem,
133 performing access control, or setting mimetypes.  General purpose hooks
134 that simply run at handy times in the request lifecycle exist as well.</p>
135
136 <p>Hook functions are passed the request object as their only argument.
137 They can return any value, depending on the hook, but most commonly
138 they'll return OK, DONE, or DECLINED, which you can write in lua as
139 <code>apache2.OK</code>, <code>apache2.DONE</code>, or
140 <code>apache2.DECLINED</code>, or else an HTTP status code.</p>
141
142 <example><title>translate_name.lua</title><pre>
143 -- example hook that rewrites the URI to a filesystem path.
144
145 require 'apache2'
146
147 function translate_name(r)
148     if r.uri == "/translate-name" then
149         r.filename = r.document_root .. "/find_me.txt"
150         return apache2.OK
151     end
152     -- we don't care about this URL, give another module a chance
153     return apache2.DECLINED
154 end
155 </pre></example>
156
157 <example><title>translate_name2.lua</title><pre>
158 --[[ example hook that rewrites one URI to another URI. It returns a
159      apache2.DECLINED to give other URL mappers a chance to work on the
160      substitution, including the core translate_name hook which maps based
161      on the DocumentRoot.
162
163      Note: It is currently undefined as to whether this runs before or after
164      mod_alias.
165 --]]
166
167 require 'apache2'
168
169 function translate_name(r)
170     if r.uri == "/translate-name" then
171         r.uri = "/find_me.txt"
172         return apache2.DECLINED
173     end
174     return apache2.DECLINED
175 end
176 </pre></example>
177 </section>
178
179 <section id="datastructures"><title>Data Structures</title>
180
181 <dl>
182 <dt>request_rec</dt>
183         <dd>
184         <p>The request_rec is mapped in as a userdata. It has a metatable
185         which lets you do useful things with it. For the most part it
186         has the same fields as the request_rec struct (see httpd.h
187         until we get better docs here) many of which are writeable as
188         well as readable.  (The table fields' content can be changed, but the
189         fields themselves cannot be set to different tables.)</p>
190
191         <table border="1">
192
193         <tr>
194           <th><strong>Name</strong></th>
195           <th><strong>Lua type</strong></th>
196           <th><strong>Writable</strong></th>
197         </tr>
198         <tr>
199           <td><code>ap_auth_type</code></td>
200           <td>string</td>
201           <td>no</td>
202         </tr>
203         <tr>
204           <td><code>args</code></td>
205           <td>string</td>
206           <td>yes</td>
207         </tr>
208         <tr>
209           <td><code>assbackwards</code></td>
210           <td>boolean</td>
211           <td>no</td>
212         </tr>
213
214         <tr>
215           <td><code>canonical_filename</code></td>
216           <td>string</td>
217           <td>no</td>
218         </tr>
219         <tr>
220           <td><code>content_encoding</code></td>
221           <td>string</td>
222           <td>no</td>
223         </tr>
224         <tr>
225           <td><code>content_type</code></td>
226           <td>string</td>
227           <td>yes</td>
228         </tr>
229
230         <tr>
231           <td><code>document_root</code></td>
232           <td>string</td>
233           <td>no</td>
234         </tr>
235         <tr>
236           <td><code>err_headers_out</code></td>
237           <td>table</td>
238           <td>no</td>
239         </tr>
240         <tr>
241           <td><code>filename</code></td>
242           <td>string</td>
243           <td>yes</td>
244         </tr>
245         <tr>
246           <td><code>headers_in</code></td>
247           <td>table</td>
248           <td>yes</td>
249         </tr>
250         <tr>
251           <td><code>headers_out</code></td>
252           <td>table</td>
253           <td>yes</td>
254         </tr>
255         <tr>
256           <td><code>hostname</code></td>
257           <td>string</td>
258           <td>no</td>
259         </tr>
260         <tr>
261           <td><code>method</code></td>
262           <td>string</td>
263           <td>no</td>
264         </tr>
265         <tr>
266           <td><code>notes</code></td>
267           <td>table</td>
268           <td>yes</td>
269         </tr>
270         <tr>
271           <td><code>path_info</code></td>
272           <td>string</td>
273           <td>no</td>
274         </tr>
275         <tr>
276           <td><code>protocol</code></td>
277           <td>string</td>
278           <td>no</td>
279         </tr>
280         <tr>
281           <td><code>range</code></td>
282           <td>string</td>
283           <td>no</td>
284         </tr>
285         <tr>
286           <td><code>subprocess_env</code></td>
287           <td>table</td>
288           <td>yes</td>
289         </tr>
290         <tr>
291           <td><code>status</code></td>
292           <td>number</td>
293           <td>yes</td>
294         </tr>
295         <tr>
296           <td><code>the_request</code></td>
297           <td>string</td>
298           <td>no</td>
299         </tr>
300         <tr>
301           <td><code>unparsed_uri</code></td>
302           <td>string</td>
303           <td>no</td>
304         </tr>
305         <tr>
306           <td><code>uri</code></td>
307           <td>string</td>
308           <td>yes</td>
309         </tr>
310         <tr>
311           <td><code>user</code></td>
312           <td>string</td>
313           <td>yes</td>
314         </tr>
315         </table>
316
317         <p>The request_rec has (at least) the following methods:</p>
318
319         <example>
320         r:addoutputfilter(name|function) -- add an output filter
321         </example>
322
323         <example>
324         r:parseargs() -- returns a lua table containing the request's
325                          query string arguments
326         </example>
327
328         <example>
329         r:parsebody() -- parse the request body as a POST and return
330                          a lua table
331         </example>
332
333         <example>
334         r:puts("hello", " world", "!") -- print to response body
335         </example>
336
337         <example>
338         r:write("a single string") -- print to response body
339         </example>
340         </dd>
341     </dl>
342
343 </section>
344
345 <section id="logging"><title>Logging Functions</title>
346
347 <example>
348         -- examples of logging messages<br />
349         r:trace1("This is a trace log message") -- trace1 through trace8 can be used <br />
350         r:debug("This is a debug log message")<br />
351         r:info("This is an info log message")<br />
352         r:notice("This is an notice log message")<br />
353         r:warn("This is an warn log message")<br />
354         r:err("This is an err log message")<br />
355         r:alert("This is an alert log message")<br />
356         r:crit("This is an crit log message")<br />
357         r:emerg("This is an emerg log message")<br />
358 </example>
359
360 </section>
361
362 <section id="apache2"><title>apache2 Package</title>
363 <p>A package named <code>apache2</code> is available with (at least) the following contents.</p>
364 <dl>
365   <dt>apache2.OK</dt>
366   <dd>internal constant OK.  Handlers should return this if they've
367   handled the request.</dd>
368   <dt>apache2.DECLINED</dt>
369   <dd>internal constant DECLINED.  Handlers should return this if
370   they are not going to handle the request.</dd>
371   <dt>apache2.DONE</dt>
372   <dd>internal constant DONE.</dd>
373   <dt>apache2.version</dt>
374   <dd>Apache HTTP server version string</dd>
375   <dt>apache2.HTTP_MOVED_TEMPORARILY</dt>
376   <dd>HTTP status code</dd>
377 </dl>
378 <p>(Other HTTP status codes are not yet implemented.)</p>
379 </section>
380
381 <directivesynopsis>
382 <name>LuaRoot</name>
383 <description>Specify the base path for resolving relative paths for mod_lua directives</description>
384 <syntax>LuaRoot /path/to/a/directory</syntax>
385 <contextlist><context>server config</context><context>virtual host</context>
386 <context>directory</context><context>.htaccess</context>
387 </contextlist>
388 <override>All</override>
389
390 <usage>
391     <p>Specify the base path which will be used to evaluate all
392     relative paths within mod_lua. If not specified they
393     will be resolved relative to the current working directory,
394     which may not always work well for a server.</p>
395 </usage>
396 </directivesynopsis>
397
398 <directivesynopsis>
399 <name>LuaScope</name>
400 <description>One of once, request, conn, server -- default is once</description>
401 <syntax>LuaScope once|request|conn|server [max|min max]</syntax>
402 <default>LuaScope once</default>
403 <contextlist><context>server config</context><context>virtual host</context>
404 <context>directory</context><context>.htaccess</context>
405 </contextlist>
406 <override>All</override>
407
408 <usage>
409     <p>Specify the lifecycle scope of the Lua interpreter which will
410     be used by handlers in this "Directory." The default is "once"</p>
411
412    <dl>
413     <dt>once:</dt> <dd>use the interpreter once and throw it away.</dd>
414
415     <dt>request:</dt> <dd>use the interpreter to handle anything based on
416              the same file within this request, which is also
417              request scoped.</dd>
418
419     <dt>conn:</dt> <dd>Same as request but attached to the connection_rec</dd>
420
421     <dt>server:</dt>  <dd>This one is different than others because the
422             server scope is quite long lived, and multiple threads
423             will have the same server_rec. To accommodate this
424             server scoped interpreter are stored in an apr
425             resource list. The min and max arguments are intended
426             to specify the pool size, but are unused at this time.</dd>
427    </dl>
428 </usage>
429 </directivesynopsis>
430
431 <directivesynopsis>
432 <name>LuaMapHandler</name>
433 <description>Map a path to a lua handler</description>
434 <syntax>LuaMapHandler uri-pattern /path/to/lua/script.lua [function-name]</syntax>
435 <contextlist><context>server config</context><context>virtual host</context>
436 <context>directory</context><context>.htaccess</context>
437 </contextlist>
438 <override>All</override>
439 <usage>
440     <p>This directive matches a uri pattern to invoke a specific
441     handler function in a specific file. It uses PCRE regular
442     expressions to match the uri, and supports interpolating
443     match groups into both the file path and the function name
444     be careful writing your regular expressions to avoid security
445     issues.</p>
446    <example><title>Examples:</title>
447     LuaMapHandler /(\w+)/(/w+) /scripts/$1.lua handle_$2
448    </example>
449         <p>This would match uri's such as /photos/show?id=9
450         to the file /scripts/photos.lua and invoke the
451         handler function handle_show on the lua vm after
452         loading that file.</p>
453
454 <example>
455     LuaMapHandler /bingo /scripts/wombat.lua
456 </example>
457         <p>This would invoke the "handle" function, which
458         is the default if no specific function name is
459         provided.</p>
460 </usage>
461 </directivesynopsis>
462
463 <directivesynopsis>
464 <name>LuaPackagePath</name>
465 <description>Add a directory to lua's package.path</description>
466 <syntax>LuaPackagePath /path/to/include/?.lua</syntax>
467 <contextlist><context>server config</context><context>virtual host</context>
468 <context>directory</context><context>.htaccess</context>
469 </contextlist>
470 <override>All</override>
471     <usage><p>Add a path to lua's module search path. Follows the same
472     conventions as lua. This just munges the package.path in the
473     lua vms.</p>
474
475     <example><title>Examples:</title>
476         LuaPackagePath /scripts/lib/?.lua<br />
477         LuaPackagePath /scripts/lib/?/init.lua
478     </example>
479 </usage>
480 </directivesynopsis>
481
482 <directivesynopsis>
483 <name>LuaPackageCPath</name>
484 <description>Add a directory to lua's package.cpath</description>
485 <syntax>LuaPackageCPath /path/to/include/?.soa</syntax>
486 <contextlist><context>server config</context><context>virtual host</context>
487 <context>directory</context><context>.htaccess</context>
488 </contextlist>
489 <override>All</override>
490
491 <usage>
492     <p>Add a path to lua's shared library search path. Follows the same
493     conventions as lua. This just munges the package.cpath in the
494     lua vms.</p>
495
496 </usage>
497 </directivesynopsis>
498
499 <directivesynopsis>
500 <name>LuaCodeCache</name>
501 <description>Configure the compiled code cache.</description>
502 <syntax>LuaCodeCache stat|forever|never</syntax>
503 <default>LuaCodeCache stat</default>
504 <contextlist><context>server config</context><context>virtual host</context>
505 <context>directory</context><context>.htaccess</context>
506 </contextlist>
507 <override>All</override>
508
509 <usage><p>
510     Specify the behavior of the in-memory code cache. The default
511     is stat, which stats the top level script (not any included
512     ones) each time that file is needed, and reloads it if the
513     modified time indicates it is newer than the one it has
514     already loaded. The other values cause it to keep the file
515     cached forever (don't stat and replace) or to never cache the
516     file.</p>
517
518     <p>In general stat or forever is good for production, and stat or never
519     for development.</p>
520
521     <example><title>Examples:</title>
522         LuaCodeCache stat<br />
523         LuaCodeCache forever<br />
524         LuaCodeCache never<br />
525     </example>
526
527 </usage>
528 </directivesynopsis>
529
530 <directivesynopsis>
531 <name>LuaHookTranslateName</name>
532 <description>Provide a hook for the translate name phase of request processing</description>
533 <syntax>LuaHookTranslateName  /path/to/lua/script.lua  hook_function_name</syntax>
534 <contextlist><context>server config</context><context>virtual host</context>
535 <context>directory</context><context>.htaccess</context>
536 </contextlist>
537 <override>All</override>
538
539 <usage><p>
540     Add a hook (at APR_HOOK_MIDDLE) to the translate name phase of
541     request processing. The hook function receives a single
542     argument, the request_rec, and should return a status code,
543     which is either an HTTP error code, or the constants defined
544     in the apache2 module: apache2.OK, apache2.DECLINED, or
545     apache2.DONE. </p>
546
547     <p>For those new to hooks, basically each hook will be invoked
548     until one of them returns apache2.OK. If your hook doesn't
549     want to do the translation it should just return
550     apache2.DECLINED. If the request should stop processing, then
551     return apache2.DONE.</p>
552
553     <p>Example:</p>
554
555 <example><pre>
556 # httpd.conf
557 LuaHookTranslateName /scripts/conf/hooks.lua silly_mapper
558
559 -- /scripts/conf/hooks.lua --
560 require "apache2"
561 function silly_mapper(r)
562     if r.uri == "/" then
563         r.filename = "/var/www/home.lua"
564         return apache2.OK
565     else
566         return apache2.DECLINED
567     end
568 end
569 </pre></example>
570 </usage>
571 </directivesynopsis>
572
573 <directivesynopsis>
574 <name>LuaHookFixups</name>
575 <description>Provide a hook for the fixups phase of request
576 processing</description>
577 <syntax>LuaHookFixups  /path/to/lua/script.lua hook_function_name</syntax>
578 <contextlist><context>server config</context><context>virtual host</context>
579 <context>directory</context><context>.htaccess</context>
580 </contextlist>
581 <override>All</override>
582 <usage>
583 <p>
584     Just like LuaHookTranslateName, but executed at the fixups phase
585 </p>
586 </usage>
587 </directivesynopsis>
588
589 <directivesynopsis>
590 <name>LuaHookMapToStorage</name>
591 <description>Provide a hook for the map_to_storage phase of request processing</description>
592 <syntax>LuaHookMapToStorage  /path/to/lua/script.lua hook_function_name</syntax>
593 <contextlist><context>server config</context><context>virtual host</context>
594 <context>directory</context><context>.htaccess</context>
595 </contextlist>
596 <override>All</override>
597     <usage><p>...</p></usage>
598 </directivesynopsis>
599
600 <directivesynopsis>
601 <name>LuaHookCheckUserID</name>
602 <description>Provide a hook for the check_user_id phase of request processing</description>
603 <syntax>LuaHookCheckUserID  /path/to/lua/script.lua hook_function_name</syntax>
604 <contextlist><context>server config</context><context>virtual host</context>
605 <context>directory</context><context>.htaccess</context>
606 </contextlist>
607 <override>All</override>
608     <usage><p>...</p></usage>
609 </directivesynopsis>
610
611 <directivesynopsis>
612 <name>LuaHookTypeChecker</name>
613 <description>Provide a hook for the type_checker phase of request processing</description>
614 <syntax>LuaHookTypeChecker  /path/to/lua/script.lua hook_function_name</syntax>
615 <contextlist><context>server config</context><context>virtual host</context>
616 <context>directory</context><context>.htaccess</context>
617 </contextlist>
618 <override>All</override>
619     <usage><p>...</p></usage>
620 </directivesynopsis>
621
622 <directivesynopsis>
623 <name>LuaHookAuthChecker</name>
624 <description>Provide a hook for the auth_checker phase of request processing</description>
625 <syntax>LuaHookAuthChecker  /path/to/lua/script.lua hook_function_name</syntax>
626 <contextlist><context>server config</context><context>virtual host</context>
627 <context>directory</context><context>.htaccess</context>
628 </contextlist>
629 <override>All</override>
630 <usage>
631 <p>Invoke a lua function in the auth_checker phase of processing
632 a request.  This can be used to implement arbitrary authentication
633 and authorization checking.  A very simple example:
634 </p>
635 <example><pre>
636 require 'apache2'
637
638 -- fake authcheck hook
639 -- If request has no auth info, set the response header and
640 -- return a 401 to ask the browser for basic auth info.
641 -- If request has auth info, don't actually look at it, just
642 -- pretend we got userid 'foo' and validated it.
643 -- Then check if the userid is 'foo' and accept the request.
644 function authcheck_hook(r)
645
646    -- look for auth info
647    auth = r.headers_in['Authorization']
648    if auth ~= nil then
649      -- fake the user
650      r.user = 'foo'
651    end
652
653    if r.user == nil then
654       r:debug("authcheck: user is nil, returning 401")
655       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
656       return 401
657    elseif r.user == "foo" then
658       r:debug('user foo: OK')
659    else
660       r:debug("authcheck: user='" .. r.user .. "'")
661       r.err_headers_out['WWW-Authenticate'] = 'Basic realm="WallyWorld"'
662       return 401
663    end
664    return apache2.OK
665 end
666 </pre></example>
667 </usage>
668 </directivesynopsis>
669
670 <directivesynopsis>
671 <name>LuaHookAccessChecker</name>
672 <description>Provide a hook for the access_checker phase of request processing</description>
673 <syntax>LuaHookAccessChecker  /path/to/lua/script.lua  hook_function_name</syntax>
674 <contextlist><context>server config</context><context>virtual host</context>
675 <context>directory</context><context>.htaccess</context>
676 </contextlist>
677 <override>All</override>
678 <usage>
679 <p>Add your hook to the access_checker phase.  An access checker
680 hook function usually returns OK, DECLINED, or HTTP_FORBIDDEN.</p>
681 </usage>
682 </directivesynopsis>
683
684 <directivesynopsis>
685 <name>LuaHookInsertFilter</name>
686 <description>Provide a hook for the insert_filter phase of request processing</description>
687 <syntax>LuaHookInsertFilter  /path/to/lua/script.lua hook_function_name</syntax>
688 <contextlist><context>server config</context><context>virtual host</context>
689 <context>directory</context><context>.htaccess</context>
690 </contextlist>
691 <override>All</override>
692     <usage><p>Not Yet Implemented</p></usage>
693 </directivesynopsis>
694
695 <directivesynopsis>
696 <name>LuaQuickHandler</name>
697 <description>Provide a hook for the quick handler of request processing</description>
698 <syntax></syntax>
699 <contextlist><context>server config</context><context>virtual host</context>
700 <context>directory</context><context>.htaccess</context>
701 </contextlist>
702 <override>All</override>
703 <usage><p>...</p></usage>
704 </directivesynopsis>
705
706 </modulesynopsis>
707