LuaXMLRPC logo
LuaXMLRPC Reference Manual
XML-RPC interface to the Lua programming language
home · introduction · data types · basic · client · server · examples · related docs

Introduction

LuaXMLRPC is a Lua library to build XML-RPC clients and servers. It enables a Lua program to:

LuaXMLRPC provides a simple API and an abstraction layer over XML avoiding manipulation of string representation of data structures.

LuaXMLRPC is based on LuaExpat and on Lua 5.0.
The abstraction layer over HTTP depends on LuaSocket 2.0.

Data types

XML-RPC elements are usually represented by the simplest correspondent Lua object. When the correspondance is not obvious, a Lua table is used with a field specifying the element.

From XML-RPC to Lua

When converting from XML-RPC element to a Lua object, a table with a field tag is used. The child elements are stored at numbered indexes and white space is ignored.

XML-RPC data type Lua object
double
int
i4
number
string string
boolean boolean
struct
arrray
table
other elements
{
  tag = "element name",
  [1] = <first child>,
  [2] = <second child>,
  [3] = ...,
}

From Lua to XML-RPC

A conversion from a Lua object to an XML-RPC can be made automatically or explicitly. The automatic conversion rules are:

Lua object XML-RPC data type
number int or double
string string
boolean boolean
{ key = val }
<struct>
  <member>
    <name>key</name>
    <value>val</value>
  </member>
</struct>
val is converted according to the same rules.
In case of a table that has numeric keys, the resulting struct will have the string representation of these numbers as keys (e.g. "1" instead of 1). The library tries to convert integral numbers to integer types, otherwise converting them to floating point numbers.

Basic support

The file xmlrpc.lua implements all basic support for encoding and decoding XML-RPC messages and for transforming data types between the two representations. Following is the list of functions.

Client side

The file xmlrpc.http.lua implements a simple stand-alone client based on LuaSocket 2.0. The following function is provided:

Server side

The distribution also offers a simple XML-RPC server implemented over a CGI launcher. This launcher just have to offer a way to decode POST data and to send data back to the client.

The file xmlrpc.cgi.lua implements a simple XML-RPC server using the package post that can parse incoming POST data from the http server. An appropriate environment for writing Lua functions is implemented; a new assert function generates a XML-RPC fault in case of a false condition; a respond function creates the correct header for the responses.

The main goal of this file is to give a starting point for other real implementations.

Examples

Client example

Below is a small sample code displaying the use of the library in a client application.
require "xmlrpc.http"

local ok, res = xmlrpc.http.call ("http://www.oreillynet.com/meerkat/xml-rpc/server.php", "system.listMethods")
print (ok)
for i, v in pairs(res) do print ('\t', i, v) end

Type conversion example

The next example shows how to force the conversion of types from Lua to XML-RPC.
require "xmlrpc"

double_array_type = xmlrpc.newArray ("double")
double_array = xmlrpc.newTypedValue ( { 1.1, 2, 3, 4 }, double_array_type)

double_array_array_type = xmlrpc.newArray (double_array_type)
double_array_array = xmlrpc.newTypedValue (
{
    { 11, 12, 13, },
    { 21, 22, 23, },
    { 31, 32, 33, },
}, double_array_array_type)
The table double_array_array will be:
<array>
  <data>
    <value>
      <array>
        <data>
          <value><double>11</double></value>
          <value><double>12</double></value>
          <value><double>13</double></value>
        </data>
      </array>
    </value>
    <value>
      <array>
        <data>
          <value><double>21</double></value>
          <value><double>22</double></value>
          <value><double>23</double></value>
        </data>
      </array>
    </value>
    <value>
      <array>
        <data>
          <value><double>31</double></value>
          <value><double>32</double></value>
          <value><double>33</double></value>
        </data>
      </array>
    </value>
  </data>
</array>

Server example

Follow a small example of a server based on a cgi launcher.
require "xmlrpc.cgi"

local kepler_home = "http://www.keplerproject.org"
local kepler_products = { "luasql", "lualdap", "luaexpat", "luaxmlrpc", }
local kepler_sites = {
    luasql = kepler_home.."/luasql",
    lualdap = kepler_home.."/lualdap",
    luaexpat = kepler_home.."/luaexpat",
    luaxmlrpc = kepler_home.."/luaxmlrpc",
}
-- Register methods
xmlrpc.srvMethods {
    kepler = {
        products = function (self) return kepler_products end,
        site = function (self, prod) return kepler_sites[prod] end,
    }
}
-- Parse POST data
local doc = {}
post.parsedata (doc)
-- Decode method call
local method, arg_table = xmlrpc.srvDecode (doc[1])
local func = xmlrpc.dispatch (method)
local ok, result, err = pcall (func, unpack (arg_table or {}))
if ok then
    result = { code = 3, message = result, }
end
respond (xmlrpc.srvEncode (result, not ok)
Note that the package post and the function respond should be provided by the cgi launcher.

Related documentation

Here is a list of related documentation:

Contents



home · introduction · data types · basic · client · server · examples · related docs

$Id: manual.html,v 1.9 2004/12/02 15:04:20 tomas Exp $