1
-- CGILua dispatcher module
2
-- @release $Id: dispatcher.lua,v 1.8 2007/12/07 18:49:49 carregal Exp $
4
module(..., package.seeall)
6
-- Checks if an URL matches a route pattern
7
local function route_match(url, pattern)
9
local captures = string.gsub(pattern, "(/$[%w_-]+)", "/([^/]*)")
10
local url_parts = {string.match(url, captures)}
12
for name in string.gmatch(pattern, "/$([%w_-]+)") do
13
params[name] = url_parts[i]
16
return next(params) and params
21
-- Maps the correct function for an URL
22
local function route_map(url)
23
for i, v in ipairs(route_URLs) do
24
local pattern, f, name = unpack(v)
25
local params = route_match(url, pattern)
32
-- Returns an URL for a named route
33
-- @param map_name Name associated with the map in the routed URL table.
34
-- @param params Table of named parameters used in the URL map
35
-- @param queryargs Optional table of named parameters used for the QUERY part of the URL
36
function route_url(map_name, params, queryargs)
37
local queryparams = ""
39
queryparams = "?"..cgilua.urlcode.encodetable(queryargs)
41
for i, v in ipairs(route_URLs) do
42
local pattern, f, name = unpack(v)
43
if name == map_name then
44
local url = string.gsub(pattern, "$([%w_-]+)", params)
45
url = cgilua.urlpath.."/"..cgilua.app_name..url..queryparams
51
-- Defines the routing using a table of URLs maps or a single map
52
-- a map defines a URL mask using $name to extract parameters,
53
-- a function to be called with the extracted parameters and
54
-- a name for the map when used with route_url
55
-- @param table of maps or a single map
58
if type(URLs[1]) == "string" then
59
-- accepts a single map as the only entry in a map table
63
f, args = route_map(cgilua.script_vpath)
68
error("Missing page parameters")
b'\\ No newline at end of file'