CGILua
Building Web Scripts with Lua

Introduction

CGILua uses Lua as a server-side scripting language for creating dynamic Web pages. Both pure Lua Scripts and Lua Pages (LP) are supported by CGILua. A Lua Script is essentially a Lua program that creates the whole contents of a web page and returns it to the client. A Lua Page is a conventional markup text (HTML, XML etc) file that embeds Lua code using some special tags. Those tags are processed by CGILua and resulting page is returned to the client.

Lua Scripts and Lua Pages are equally easy to use, and choosing one of them basically depends on the characteristics of the resulting page. While Lua Pages are more convenient for the separation of logic and format, Lua Scripts are more adequate for creating pages that are simpler in terms of its structure, but require a more significative amount of internal processing.

Allowing these two methods to be intermixed, CGILua provides Web applications developers with great flexibility when both requirements are present. For a detailed description of both scripting methods and some examples of their use see Lua Scripts and Lua Pages.

Architecture

CGILua architecture is divided in two layers. The lower level is represented by the Server API (SAPI) and the higher level is represented by the CGILua API itself. SAPI is the interface between the web server and the CGILua API, so it needs to be implemented for each Web server and launching method used.

A launcher is responsible for the interaction of CGILua and the Web server, implementing SAPI for example using ISAPI on IIS or mod_lua on Apache. The reference implementation of CGILua launchers is Kepler.

The CGILua API is implemented using only SAPI and is totally portable over different launchers and their supporting Web servers. This way any Lua Script or Lua Page can be used by any launcher.

Request life cycle

CGILua processes requests using a CGI metaphor (even if the launcher is not based on CGI) and requests have a life cycle that can be customized by the programmer. The CGILua request life cycle consists in the following sequence of steps for each request:

  1. Add default handlers such as LuaScripts and Lua Pages.
  2. Execute the config.lua file, allowing the customization of the next steps.
  3. Remove dangerous globals (such as debug) from the user script environment.
  4. Build the cgi table (processing POST and GET data).
  5. Change to user script directory.
  6. Execute the registered open functions.
  7. Execute the requested script with the correct environment.
  8. Execute the registered close functions.
  9. Change back to the original directory

Editing the config.lua file one can customize the CGILua behaviour. One typical use would be registering the open and close functions in order to change the request processing behavior. With this customization it is possible to implement new features like session management and private library directories as shown in section Configuration, or even to implement new abstractions over the whole CGILua way of live, like MVC-frameworks such as Orbit.

Installation

CGILua follows the package model for Lua 5.1, therefore it should be "installed". Refer to Compat-5.1 configuration section about how to install the modules properly in a Lua 5.0 environment.

Configuration

CGILua 5.0 offers a single configuration file, called config.lua and a set of functions to alter the default CGILua behaviour. This file can be placed anywhere in the Lua Path, but to make easier to upgrade CGILua without overwriting your config.lua you can use a separate directory for configuration files.

If you using Kepler it creates a $CONF directory for the configuration files. For more details on this usage of Lua Path please check the Kepler documentation.

Some of the uses of config.lua customization are:

Script Handlers
A handler is responsible for the response of a request. You can add new CGILua handlers using cgilua.addscripthandler (see also cgilua.buildplainhandler and cgilua.buildprocesshandler for functions that build simple handlers).
POST Data Sizes
You can change the POST data size limits using cgilua.setmaxinput and cgilua.setmaxfilesize.
Opening and Closing Functions
You can add your functions to the life cycle of CGILua using cgilua.addopenfunction and cgilua.addclosefunction. These functions are executed just before and just after the script execution, even when an error occurs in the script processing.

In particular, the opening and closing functions are useful for different things. Some examples of the use of such functions in config.lua are shown next.

Previous versions of CGILua loaded a env.lua file from the script directory before processing it. To emulate this with CGILua 5.0 you can use something like:

cgilua.addopenfunction (function ()
	cgilua.doif ("env.lua")
end)

If every script needs to load a module (such as the sessions library), you can do:

require"cgilua.session"
cgilua.session.setsessiondir"/tmp/"
cgilua.addopenfunction (cgilua.session.open)
cgilua.addclosefunction (cgilua.session.close)

Note that the function cgilua.addopenfunction must be used to call cgilua.session.open because this function needs to change the cgi table (see section Receiving parameters for more information on this special table) which is not yet available during the execution of the config.lua file (see the Request life cycle).

When some scripts may use the library but others may not, you could define an "enabling" function (which should be called at the very beginning of each script that needs to use sessions):

require"cgilua.session"
cgilua.session.setsessiondir"/tmp/"
cgilua.enablesession = function ()
	cgilua.session.open ()
	cgilua.addclosefunction (cgilua.session.close)
end

Sometimes you need to configure a private libraries directory for each application hosted in the server. This configuration allows the function require to find packages installed in the private directory and in the system directory but not in other application's private directory. To implement this you could do:

local app_lib_dir = {
	["/virtual/path/"] = "/absolute/path/lib/",
}
local package = package
cgilua.addopenfunction (function ()
	local app = app_lib_dir[cgilua.script_vdir]
	if app then
		package.path = app..'/?.lua'..';'..package.path
	end
end)

Lua Scripts

Lua Scripts are text files containing valid Lua code. This style of usage adopts a more "raw" form of web programming, where a program is responsible for the entire generation of the resulting page. Lua Scripts have a default .lua extension.

To generate a valid web document (HTML, XML, WML, CSS etc) the Lua Script must follow the expected HTTP order to produce its output, first sending the correct headers and then sending the actual document contents.

CGILua offers some functions to ease these tasks, such as cgilua.htmlheader to produce the header for a HTML document and cgilua.put to send the document contents (or part of it).

For example, a HTML document which displays the sentence "Hello World!" can be generated with the following Lua Script:

cgilua.htmlheader()
cgilua.put([[
<html>
<head>
  <title>Hello World</title>
</head>
<body>
  <strong>Hello World!</strong>
</body>
</html>]])

It should be noted that the above example generates a "fixed" page: even though the page is generated at execution time there is no "variable" information. That means that the very same document could be generated directly with a simple static HTML file. However, Lua Scripts become especially useful when the document contains information which is not known beforehand or changes according to passed parameters, and it is necessary to generate a "dynamic" page.

Another easy example can be shown, this time using a Lua control structure, variables, and the concatenation operator:

cgilua.htmlheader()  

if cgi.language == 'english' then
  greeting = 'Hello World!'
elseif cgi.language == 'portuguese' then
  greeting = 'Olá Mundo!'
else
  greeting = '[unknown language]'
end

cgilua.put('<html>')  
cgilua.put('<head>')
cgilua.put('  <title>'..greeting..'</title>')
cgilua.put('</head>')
cgilua.put('<body>')
cgilua.put('  <strong>'..greeting..'</strong>')
cgilua.put('</body>')
cgilua.put('</html>')

In the above example the use of cgi.language indicates that language was passed to the Lua Script as a CGILua parameter, coming from a HTML form field (via POST) or from the URL used to activate it (via GET). CGILua automatically decodes such parameters so you can use them at will on your Lua Scripts and Lua Pages.

Lua Pages

A Lua Page is a text template file which will be processed by CGILua before the HTTP server sends it to the client. CGILua does not process the text itself but look for some special markups that include Lua code into the file. After all those markups are processed and merged with the template file, the results are sent to the client.

Lua Pages have a default .lp extension. They are a simpler way to make a dynamic page because there is no need to send the HTTP headers. Usually Lua Pages are HTML pages so CGILua sends the HTML header automatically.

Since there are some restrictions on the uses of HTTP headers sometimes a Lua Script will have to be used instead of a Lua Page.

The fundamental Lua Page markups are:

<?lua chunk ?>
Processes and merges the Lua chunk execution results where the markup is located in the template. The alternative form <% chunk %> can also be used.
<?lua= expression ?>
Processes and merges the Lua expression evaluation where the markup is located in the template. The alternative form <%= expression %> can also be used.

Note that the ending mark could not appear inside a Lua chunk or Lua expression even inside quotes. The Lua Pages pre-processor just makes global substitutions on the template, searching for a matching pair of markups and generating the corresponding Lua code to achieve the same result as the equivalent Lua Script.

The second example on the previous section could be written using a Lua Page like:

<html>
<?lua
if cgi.language == 'english' then
  greeting = 'Hello World!'
elseif cgi.language == 'portuguese' then
  greeting = 'Olá Mundo!'
else
  greeting = '[unknown language]'
end
?>
<head>
  <title><%= greeting %></title>
</head>
<body>
  <>strong<%= greeting %></strong>
</body>
</html>

HTML tags and Lua Page tags can be freely intermixed. However, as on other template languages, it's considered a best practice to not use explicit Lua logic on templates. The recommended aproach is to use only function calls that returns content chunks, so in this example, assuming that function getGreeting was definied in file functions.lua as follows:

function getGreeting()
  local greeting
  if cgi.language == 'english' then
    greeting = 'Hello World!'
  elseif cgi.language == 'portuguese' then
    greeting = 'Olá Mundo!'
  else
    greeting = '[unknown language]'
  end
  return greeting
end

the Lua Page could be rewriten as:

<?lua
assert (loadfile"functions.lua")()
?>
<html>
<head>
  <title><%= getGreeting() %></title>
</head>
<body>
  <strong><%= getGreeting() %></strong>
</body>
</html>

Another interesting feature of Lua Pages is the intermixing of Lua and HTML. It is very usual to have a list of values in a table, iterate over the list and show the items on the page.

A Lua Script could do that using a loop like:

cgilua.put("<ul>")
for i, item in ipairs(list) do
    cgilua.put("<li>"..item.."</li>")
end
cgilua.put("</ul>")

The equivalent loop in Lua Page would be:

<ul>
    <% for i, item in ipairs(list) do %>
    <li><%= item %></li>
    <% end %>
</ul>

Receiving parameters: the cgi table

CGILua offers an unified way of accessing data passed to the scripts for both HTTP method used (GET or POST). No matter which method was used on the client, all parameters will be provided inside the cgi table.

Usually all types of parameters will be available as strings. If the value of a parameter is a number, it will be converted to its string representation.

There are only two exceptions where the value will be a Lua table. The first case occurs on file uploads, where the corresponding table will have the following fields:

filename
the file name as given by the client.
filesize
the file size in bytes.
file
the temporary file handle. The file must be copied because CGILua will remove it after the script ends.

The other case that uses Lua tables occurs when there is more than one value associated with the same parameter name. This happens in the case of a selection list with multiple values; but it also occurs when the form (of the referrer) had two or more elements with the same name attribute (maybe because one was on a form and another was in the query string). All values will be inserted in an indexed table in the order in which they are handled.

Error Handling

There are three functions for error handling in CGILua:

The function cgilua.seterrorhandler defines the error handler, a function called by Lua when an error has just occurred. The error handler has access to the execution stack before the error is thrown so it can build an error message using stack information. Lua also provides a function to do that: debug.traceback.

The function cgilua.seterroroutput defines the function that decides what to do with the error message. It could be sent to the client's browser, written to a log file or sent to an e-mail address (with the help of LuaSocket or LuaLogging for example).

The function cgilua.errorlog is provided to write directly to the http server error log file.

An useful example of its use could be handling unexpected errors. Customizing unexpected error messages to the end user but giving all the information to the application's developers is the goal of the following piece of code:

local ip = cgilua.servervariable"REMOTE_ADDR"
local developers_machines = {
	["192.168.0.20"] = true,
	["192.168.0.27"] = true,
	["192.168.0.30"] = true,
}
local function mail (s)
	require"cgilua.serialize"
	require"socket.smtp"
	-- Build the message
	local msg = {}
	table.insert (msg, tostring(s))
	-- Tries to obtain the REFERER URL
	table.insert (msg, tostring (cgilua.servervariable"HTTP_REFERER"))
	table.insert (msg, cgilua.servervariable"SERVER_NAME"..
		cgilua.servervariable"SCRIPT_NAME")
	-- CGI parameters
	table.insert (msg, "CGI")
	cgilua.serialize(cgi, function (s) table.insert (msg, s) end)
	table.insert (msg, tostring (os.date()))
	table.insert (msg, tostring (ip))
	table.insert (msg, "Cookies:")
	table.insert (msg, tostring (cgilua.servervariable"HTTP_COOKIE" or "no cookies"))
	-- Formats message according to LuaSocket-2.0b3
	local source = socket.smtp.message {
		headers = { subject = "Script Error", },
		body = table.concat (msg, '\n'),
	}
	-- Sends the message
	local r, e = socket.smtp.send {
		from = "sender@my.domain.net",
		rcpt = "developers@my.domain.net",
		source = source,
	}
end
if developers_machines[ip] then
	-- Developer's error treatment: write to the display
	cgilua.seterroroutput (function (msg)
		cgilua.errorlog (msg)
		cgilua.errorlog (cgilua.servervariable"REMOTE_ADDR")
		cgilua.errorlog (os.date())
		cgilua.htmlheader ()
		msg = string.gsub (string.gsub (msg, "\n", "<br>\n"), "\t", "  ")
		cgilua.put (msg)
	end)
else
	-- User's error treatment: shows a standard page and sends an e-mail to
	-- the developer
	cgilua.seterroroutput (function (s)
		cgilua.htmlheader ()
		cgilua.put"<h1>An error occurred</h1>\n"
		cgilua.put"The responsible is being informed."
		mail (s)
	end)
end

The message is written to the browser if the request comes from one of the developer's machines. If it is not the case, a simple polite message is given to the user and a message is sent to the developer's e-mail account containing all possible information to help reproduce the situation.

Valid XHTML 1.0!

$Id: manual.html,v 1.17 2006/08/22 02:01:48 carregal Exp $