webmcp
changeset 91:2f8d8edd1836
URL parsing inside WebMCP to simplify webserver configuration
line diff
1.1 --- a/demo-app/app/main/_filter_view/30_topnav.lua Tue Aug 21 00:59:08 2012 +0200 1.2 +++ b/demo-app/app/main/_filter_view/30_topnav.lua Wed Oct 10 17:41:46 2012 +0200 1.3 @@ -55,8 +55,8 @@ 1.4 mode = "redirect", 1.5 module = request.get_module(), 1.6 view = request.get_view(), 1.7 - id = param.get_id_cgi(), 1.8 - params = param.get_all_cgi() 1.9 + id = param.get_id_raw(), 1.10 + params = param.get_all_raw() 1.11 } 1.12 } 1.13 }
2.1 --- a/doc/lighttpd.sample.conf Tue Aug 21 00:59:08 2012 +0200 2.2 +++ b/doc/lighttpd.sample.conf Wed Oct 10 17:41:46 2012 +0200 2.3 @@ -29,25 +29,9 @@ 2.4 "^/webmcp-demo/static/(.*)$" => 2.5 "/webmcp-demo/static/$1", 2.6 2.7 - # base URL 2.8 - "^/webmcp-demo/(\?(.*))?$" => 2.9 - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=0&_webmcp_module=index&_webmcp_view=index&$2", 2.10 - 2.11 - # module base URLs 2.12 - "^/webmcp-demo/([^/\?]+)/(\?(.*))?$" => 2.13 - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=index&$3", 2.14 - 2.15 - # actions 2.16 - "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)(\?(.*))?$" => 2.17 - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_action=$2&$4", 2.18 - 2.19 - # views without numeric id or string ident 2.20 - "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" => 2.21 - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=$2&_webmcp_suffix=$3&$5", 2.22 - 2.23 - # views with numeric id or string ident 2.24 - "^/webmcp-demo/([^/\?]+)/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" => 2.25 - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=2&_webmcp_module=$1&_webmcp_view=$2&_webmcp_id=$3&_webmcp_suffix=$4&$6", 2.26 + # dynamic URLs 2.27 + "^/webmcp-demo/([^\?])(\?(.*))?$" => 2.28 + "/webmcp-demo/webmcp-wrapper.lua?_webmcp_path=$1&$2", 2.29 2.30 ) 2.31
3.1 --- a/framework/env/param/get_all_cgi.lua Tue Aug 21 00:59:08 2012 +0200 3.2 +++ b/framework/env/param/get_all_cgi.lua Wed Oct 10 17:41:46 2012 +0200 3.3 @@ -1,20 +1,13 @@ 3.4 --[[-- 3.5 -params = -- table with all non-list parameters 3.6 +params = 3.7 param.get_all_cgi() 3.8 3.9 -This function returns a table with all non-list GET/POST parameters (except internal parameters like "_webmcp_id"). 3.10 +Deprecated. Alias for param.get_all_raw(). 3.11 3.12 --]]-- 3.13 3.14 -function param.get_all_cgi() 3.15 - local result = {} 3.16 - for key, value in pairs(cgi.params) do -- TODO: exchanged params too? 3.17 - if 3.18 - (not string.match(key, "^_webmcp_")) and 3.19 - (not string.match(key, "%[%]$")) 3.20 - then 3.21 - result[key] = value 3.22 - end 3.23 - end 3.24 - return result 3.25 +-- TODO: Remove this function. 3.26 + 3.27 +function param.get_all_raw() 3.28 + return param.get_all_raw() 3.29 end
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/framework/env/param/get_all_raw.lua Wed Oct 10 17:41:46 2012 +0200 4.3 @@ -0,0 +1,15 @@ 4.4 +--[[-- 4.5 +params = 4.6 +param.get_all_raw() 4.7 + 4.8 +This function returns a table with all GET/POST parameters (except internal parameters like "_webmcp_path" or "_webmcp_id") unprocessed (that is as a string, unless params.exchange(...) has been used). The returned table must not be modified. 4.9 + 4.10 +--]]-- 4.11 + 4.12 +function param.get_all_raw() 4.13 + if param._exchanged then 4.14 + return param._exchanged.params 4.15 + else 4.16 + return request.get_param_strings() 4.17 + end 4.18 +end
5.1 --- a/framework/env/param/get_id.lua Tue Aug 21 00:59:08 2012 +0200 5.2 +++ b/framework/env/param/get_id.lua Wed Oct 10 17:41:46 2012 +0200 5.3 @@ -17,6 +17,11 @@ 5.4 end 5.5 return value 5.6 else 5.7 - return param.get("_webmcp_id", param_type) 5.8 + local str = request.get_id() 5.9 + if str then 5.10 + return param._get_parser(nil, param_type)(str) 5.11 + else 5.12 + return nil 5.13 + end 5.14 end 5.15 end
6.1 --- a/framework/env/param/get_id_cgi.lua Tue Aug 21 00:59:08 2012 +0200 6.2 +++ b/framework/env/param/get_id_cgi.lua Wed Oct 10 17:41:46 2012 +0200 6.3 @@ -1,11 +1,13 @@ 6.4 --[[-- 6.5 -value = -- id string or nil 6.6 +value = -- id as string (or other type after if params.exchange(...) has been used), or nil 6.7 param.get_id_cgi() 6.8 6.9 -This function returns the string value of the _webmcp_id GET/POST parameter. 6.10 +Deprecated. Alias for param.get_id_raw(). 6.11 6.12 --]]-- 6.13 6.14 +-- TODO: Remove this function. 6.15 + 6.16 function param.get_id_cgi() 6.17 - return cgi.params._webmcp_id 6.18 + return param.get_id_raw() 6.19 end
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/framework/env/param/get_id_raw.lua Wed Oct 10 17:41:46 2012 +0200 7.3 @@ -0,0 +1,15 @@ 7.4 +--[[-- 7.5 +value = -- id as string (or other type after if params.exchange(...) has been used), or nil 7.6 +param.get_id_raw() 7.7 + 7.8 +Returns the requested id for a view unprocessed (that is as a string, unless params.exchange(...) has been used). 7.9 + 7.10 +--]]-- 7.11 + 7.12 +function param.get_id_raw() 7.13 + if param._exchanged then 7.14 + return param._exchanged.id 7.15 + else 7.16 + return request.get_id_string() 7.17 + end 7.18 +end
8.1 --- a/framework/env/request/__init.lua Tue Aug 21 00:59:08 2012 +0200 8.2 +++ b/framework/env/request/__init.lua Wed Oct 10 17:41:46 2012 +0200 8.3 @@ -9,8 +9,63 @@ 8.4 request._csrf_secret = nil 8.5 request._json_requests_allowed = false 8.6 8.7 +request._params = {} 8.8 local depth 8.9 if cgi then -- if-clause to support interactive mode 8.10 + for key, value in pairs(cgi.params) do 8.11 + if not string.match(key, "^_webmcp_") then 8.12 + request._params[key] = value 8.13 + end 8.14 + end 8.15 + local path = cgi.params._webmcp_path 8.16 + if path then 8.17 + local function parse() 8.18 + local module, action, view, suffix, id 8.19 + if path == "" then 8.20 + request._module = "index" 8.21 + request._view = "index" 8.22 + depth = 0 8.23 + end 8.24 + module = string.match(path, "^([^/]+)/$") 8.25 + if module then 8.26 + request._module = module 8.27 + request._view = "index" 8.28 + depth = 1 8.29 + return 8.30 + end 8.31 + module, action = string.match(path, "^([^/]+)/([^/.]+)$") 8.32 + if module then 8.33 + request._module = module 8.34 + request._action = action 8.35 + depth = 1 8.36 + return 8.37 + end 8.38 + module, view, suffix = string.match(path, "^([^/]+)/([^/.]+)%.([^/]+)$") 8.39 + if module then 8.40 + request._module = module 8.41 + request._view = view 8.42 + request._suffix = suffix 8.43 + depth = 1 8.44 + return 8.45 + end 8.46 + module, view, id, suffix = string.match(path, "^([^/]+)/([^/]+)/([^/.]+)%.([^/]+)$") 8.47 + if module then 8.48 + request._module = module 8.49 + request._view = view 8.50 + request._id = id 8.51 + request._suffix = suffix 8.52 + depth = 2 8.53 + return 8.54 + end 8.55 + end 8.56 + parse() 8.57 + else 8.58 + request._module = cgi.params._webmcp_module 8.59 + request._action = cgi.params._webmcp_action 8.60 + request._view = cgi.params._webmcp_view 8.61 + request._suffix = cgi.params._webmcp_suffix 8.62 + request._id = cgi.params._webmcp_id 8.63 + end 8.64 depth = tonumber(cgi.params._webmcp_urldepth) 8.65 end 8.66 if depth and depth > 0 then
9.1 --- a/framework/env/request/get_action.lua Tue Aug 21 00:59:08 2012 +0200 9.2 +++ b/framework/env/request/get_action.lua Wed Oct 10 17:41:46 2012 +0200 9.3 @@ -10,6 +10,6 @@ 9.4 if request._forward_processed then 9.5 return nil 9.6 else 9.7 - return cgi.params._webmcp_action 9.8 + return request._action 9.9 end 9.10 -end 9.11 \ No newline at end of file 9.12 +end
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/framework/env/request/get_id_string.lua Wed Oct 10 17:41:46 2012 +0200 10.3 @@ -0,0 +1,11 @@ 10.4 +--[[-- 10.5 +id_string = 10.6 +request.get_id_string() 10.7 + 10.8 +Returns the requested id for a view as a string (unprocessed). Use param.get_id(...) to get a processed version. 10.9 + 10.10 +--]]-- 10.11 + 10.12 +function request.get_id_string() 10.13 + return request._id 10.14 +end
11.1 --- a/framework/env/request/get_module.lua Tue Aug 21 00:59:08 2012 +0200 11.2 +++ b/framework/env/request/get_module.lua Wed Oct 10 17:41:46 2012 +0200 11.3 @@ -8,8 +8,8 @@ 11.4 11.5 function request.get_module() 11.6 if request._forward_processed then 11.7 - return request._forward.module or cgi.params._webmcp_module or 'index' 11.8 + return request._forward.module or request._module or 'index' 11.9 else 11.10 - return cgi.params._webmcp_module or 'index' 11.11 + return request._module or 'index' 11.12 end 11.13 end
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/framework/env/request/get_param_strings.lua Wed Oct 10 17:41:46 2012 +0200 12.3 @@ -0,0 +1,11 @@ 12.4 +--[[-- 12.5 +params = 12.6 +param.get_param_strings() 12.7 + 12.8 +This function returns a table with all raw GET/POST parameters as strings or list of strings (except internal parameters like "_webmcp_path" or "_webmcp_id"). The returned table must not be modified. 12.9 + 12.10 +--]]-- 12.11 + 12.12 +function param.get_param_strings() 12.13 + return request._params 12.14 +end
13.1 --- a/framework/env/request/get_view.lua Tue Aug 21 00:59:08 2012 +0200 13.2 +++ b/framework/env/request/get_view.lua Wed Oct 10 17:41:46 2012 +0200 13.3 @@ -10,14 +10,14 @@ 13.4 if request._forward_processed then 13.5 return request._forward.view or 'index' 13.6 else 13.7 - if cgi.params._webmcp_view then 13.8 - local suffix = cgi.params._webmcp_suffix or "html" 13.9 + if request._view then 13.10 + local suffix = request._suffix or "html" 13.11 if suffix == "html" then 13.12 - return cgi.params._webmcp_view 13.13 + return request._view 13.14 else 13.15 - return cgi.params._webmcp_view .. "." .. suffix 13.16 + return request._view .. "." .. suffix 13.17 end 13.18 - elseif not cgi.params._webmcp_action then 13.19 + elseif not request._action then 13.20 return 'index' 13.21 else 13.22 return nil
14.1 --- a/framework/env/ui/filters.lua Tue Aug 21 00:59:08 2012 +0200 14.2 +++ b/framework/env/ui/filters.lua Wed Oct 10 17:41:46 2012 +0200 14.3 @@ -97,8 +97,8 @@ 14.4 if not current_option or #current_option == 0 then 14.5 current_option = filter[1].name 14.6 end 14.7 - local id = param.get_id_cgi() 14.8 - local params = param.get_all_cgi() 14.9 + local id = param.get_id_raw() 14.10 + local params = param.get_all_raw() 14.11 ui.container{ 14.12 attr = { class = "ui_filter_head" }, 14.13 content = function()
15.1 --- a/framework/env/ui/paginate.lua Tue Aug 21 00:59:08 2012 +0200 15.2 +++ b/framework/env/ui/paginate.lua Wed Oct 10 17:41:46 2012 +0200 15.3 @@ -35,8 +35,8 @@ 15.4 end 15.5 selector:limit(per_page) 15.6 selector:offset((current_page - 1) * per_page) 15.7 - local id = param.get_id_cgi() 15.8 - local params = param.get_all_cgi() 15.9 + local id = param.get_id_raw() 15.10 + local params = param.get_all_raw() 15.11 local function pagination_elements() 15.12 if page_count > 1 then 15.13 for page = 1, page_count do