# HG changeset patch # User jbe # Date 1349883706 -7200 # Node ID 2f8d8edd18365a044b7e703f3028fee224c4c689 # Parent bdebade717d3a176bfd04672d4d21ba7cea18d7d URL parsing inside WebMCP to simplify webserver configuration diff -r bdebade717d3 -r 2f8d8edd1836 demo-app/app/main/_filter_view/30_topnav.lua --- a/demo-app/app/main/_filter_view/30_topnav.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/demo-app/app/main/_filter_view/30_topnav.lua Wed Oct 10 17:41:46 2012 +0200 @@ -55,8 +55,8 @@ mode = "redirect", module = request.get_module(), view = request.get_view(), - id = param.get_id_cgi(), - params = param.get_all_cgi() + id = param.get_id_raw(), + params = param.get_all_raw() } } } diff -r bdebade717d3 -r 2f8d8edd1836 doc/lighttpd.sample.conf --- a/doc/lighttpd.sample.conf Tue Aug 21 00:59:08 2012 +0200 +++ b/doc/lighttpd.sample.conf Wed Oct 10 17:41:46 2012 +0200 @@ -29,25 +29,9 @@ "^/webmcp-demo/static/(.*)$" => "/webmcp-demo/static/$1", - # base URL - "^/webmcp-demo/(\?(.*))?$" => - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=0&_webmcp_module=index&_webmcp_view=index&$2", - - # module base URLs - "^/webmcp-demo/([^/\?]+)/(\?(.*))?$" => - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=index&$3", - - # actions - "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)(\?(.*))?$" => - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_action=$2&$4", - - # views without numeric id or string ident - "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" => - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=$2&_webmcp_suffix=$3&$5", - - # views with numeric id or string ident - "^/webmcp-demo/([^/\?]+)/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" => - "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=2&_webmcp_module=$1&_webmcp_view=$2&_webmcp_id=$3&_webmcp_suffix=$4&$6", + # dynamic URLs + "^/webmcp-demo/([^\?])(\?(.*))?$" => + "/webmcp-demo/webmcp-wrapper.lua?_webmcp_path=$1&$2", ) diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/param/get_all_cgi.lua --- a/framework/env/param/get_all_cgi.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/param/get_all_cgi.lua Wed Oct 10 17:41:46 2012 +0200 @@ -1,20 +1,13 @@ --[[-- -params = -- table with all non-list parameters +params = param.get_all_cgi() -This function returns a table with all non-list GET/POST parameters (except internal parameters like "_webmcp_id"). +Deprecated. Alias for param.get_all_raw(). --]]-- -function param.get_all_cgi() - local result = {} - for key, value in pairs(cgi.params) do -- TODO: exchanged params too? - if - (not string.match(key, "^_webmcp_")) and - (not string.match(key, "%[%]$")) - then - result[key] = value - end - end - return result +-- TODO: Remove this function. + +function param.get_all_raw() + return param.get_all_raw() end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/param/get_all_raw.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/param/get_all_raw.lua Wed Oct 10 17:41:46 2012 +0200 @@ -0,0 +1,15 @@ +--[[-- +params = +param.get_all_raw() + +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. + +--]]-- + +function param.get_all_raw() + if param._exchanged then + return param._exchanged.params + else + return request.get_param_strings() + end +end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/param/get_id.lua --- a/framework/env/param/get_id.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/param/get_id.lua Wed Oct 10 17:41:46 2012 +0200 @@ -17,6 +17,11 @@ end return value else - return param.get("_webmcp_id", param_type) + local str = request.get_id() + if str then + return param._get_parser(nil, param_type)(str) + else + return nil + end end end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/param/get_id_cgi.lua --- a/framework/env/param/get_id_cgi.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/param/get_id_cgi.lua Wed Oct 10 17:41:46 2012 +0200 @@ -1,11 +1,13 @@ --[[-- -value = -- id string or nil +value = -- id as string (or other type after if params.exchange(...) has been used), or nil param.get_id_cgi() -This function returns the string value of the _webmcp_id GET/POST parameter. +Deprecated. Alias for param.get_id_raw(). --]]-- +-- TODO: Remove this function. + function param.get_id_cgi() - return cgi.params._webmcp_id + return param.get_id_raw() end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/param/get_id_raw.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/param/get_id_raw.lua Wed Oct 10 17:41:46 2012 +0200 @@ -0,0 +1,15 @@ +--[[-- +value = -- id as string (or other type after if params.exchange(...) has been used), or nil +param.get_id_raw() + +Returns the requested id for a view unprocessed (that is as a string, unless params.exchange(...) has been used). + +--]]-- + +function param.get_id_raw() + if param._exchanged then + return param._exchanged.id + else + return request.get_id_string() + end +end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/__init.lua --- a/framework/env/request/__init.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/request/__init.lua Wed Oct 10 17:41:46 2012 +0200 @@ -9,8 +9,63 @@ request._csrf_secret = nil request._json_requests_allowed = false +request._params = {} local depth if cgi then -- if-clause to support interactive mode + for key, value in pairs(cgi.params) do + if not string.match(key, "^_webmcp_") then + request._params[key] = value + end + end + local path = cgi.params._webmcp_path + if path then + local function parse() + local module, action, view, suffix, id + if path == "" then + request._module = "index" + request._view = "index" + depth = 0 + end + module = string.match(path, "^([^/]+)/$") + if module then + request._module = module + request._view = "index" + depth = 1 + return + end + module, action = string.match(path, "^([^/]+)/([^/.]+)$") + if module then + request._module = module + request._action = action + depth = 1 + return + end + module, view, suffix = string.match(path, "^([^/]+)/([^/.]+)%.([^/]+)$") + if module then + request._module = module + request._view = view + request._suffix = suffix + depth = 1 + return + end + module, view, id, suffix = string.match(path, "^([^/]+)/([^/]+)/([^/.]+)%.([^/]+)$") + if module then + request._module = module + request._view = view + request._id = id + request._suffix = suffix + depth = 2 + return + end + end + parse() + else + request._module = cgi.params._webmcp_module + request._action = cgi.params._webmcp_action + request._view = cgi.params._webmcp_view + request._suffix = cgi.params._webmcp_suffix + request._id = cgi.params._webmcp_id + end depth = tonumber(cgi.params._webmcp_urldepth) end if depth and depth > 0 then diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/get_action.lua --- a/framework/env/request/get_action.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/request/get_action.lua Wed Oct 10 17:41:46 2012 +0200 @@ -10,6 +10,6 @@ if request._forward_processed then return nil else - return cgi.params._webmcp_action + return request._action end -end \ No newline at end of file +end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/get_id_string.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/request/get_id_string.lua Wed Oct 10 17:41:46 2012 +0200 @@ -0,0 +1,11 @@ +--[[-- +id_string = +request.get_id_string() + +Returns the requested id for a view as a string (unprocessed). Use param.get_id(...) to get a processed version. + +--]]-- + +function request.get_id_string() + return request._id +end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/get_module.lua --- a/framework/env/request/get_module.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/request/get_module.lua Wed Oct 10 17:41:46 2012 +0200 @@ -8,8 +8,8 @@ function request.get_module() if request._forward_processed then - return request._forward.module or cgi.params._webmcp_module or 'index' + return request._forward.module or request._module or 'index' else - return cgi.params._webmcp_module or 'index' + return request._module or 'index' end end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/get_param_strings.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/request/get_param_strings.lua Wed Oct 10 17:41:46 2012 +0200 @@ -0,0 +1,11 @@ +--[[-- +params = +param.get_param_strings() + +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. + +--]]-- + +function param.get_param_strings() + return request._params +end diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/request/get_view.lua --- a/framework/env/request/get_view.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/request/get_view.lua Wed Oct 10 17:41:46 2012 +0200 @@ -10,14 +10,14 @@ if request._forward_processed then return request._forward.view or 'index' else - if cgi.params._webmcp_view then - local suffix = cgi.params._webmcp_suffix or "html" + if request._view then + local suffix = request._suffix or "html" if suffix == "html" then - return cgi.params._webmcp_view + return request._view else - return cgi.params._webmcp_view .. "." .. suffix + return request._view .. "." .. suffix end - elseif not cgi.params._webmcp_action then + elseif not request._action then return 'index' else return nil diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/ui/filters.lua --- a/framework/env/ui/filters.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/ui/filters.lua Wed Oct 10 17:41:46 2012 +0200 @@ -97,8 +97,8 @@ if not current_option or #current_option == 0 then current_option = filter[1].name end - local id = param.get_id_cgi() - local params = param.get_all_cgi() + local id = param.get_id_raw() + local params = param.get_all_raw() ui.container{ attr = { class = "ui_filter_head" }, content = function() diff -r bdebade717d3 -r 2f8d8edd1836 framework/env/ui/paginate.lua --- a/framework/env/ui/paginate.lua Tue Aug 21 00:59:08 2012 +0200 +++ b/framework/env/ui/paginate.lua Wed Oct 10 17:41:46 2012 +0200 @@ -35,8 +35,8 @@ end selector:limit(per_page) selector:offset((current_page - 1) * per_page) - local id = param.get_id_cgi() - local params = param.get_all_cgi() + local id = param.get_id_raw() + local params = param.get_all_raw() local function pagination_elements() if page_count > 1 then for page = 1, page_count do