webmcp

annotate framework/env/encode/url.lua @ 412:7d43be9afa56

Improved memory cleanup in case of out-of-memory errors (PQnotifies and PQunescapeBytea)
author jbe
date Fri Jan 08 03:10:33 2016 +0100 (2016-01-08)
parents 56d237b81c18
children 122271e0118e
rev   line source
jbe/bsw@0 1 --[[--
jbe@267 2 url_string = -- a string containing an URL
jbe/bsw@0 3 encode.url{
jbe@267 4 external = external, -- external URL (instead of specifying base, module, etc. below)
jbe@267 5 base = base, -- optional string containing a base URL of a WebMCP application
jbe@267 6 static = static, -- an URL relative to the static file directory
jbe@267 7 module = module, -- a module name of the WebMCP application
jbe@267 8 view = view, -- a view name of the WebMCP application
jbe@267 9 action = action, -- an action name of the WebMCP application
jbe@267 10 id = id, -- optional id to be passed to the view or action to select a particular data record
jbe@267 11 params = params, -- optional parameters to be passed to the view or action
jbe@267 12 anchor = anchor -- anchor in URL
jbe/bsw@0 13 }
jbe/bsw@0 14
jbe/bsw@0 15 This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module.
jbe/bsw@0 16
jbe/bsw@0 17 --]]--
jbe/bsw@0 18
jbe/bsw@0 19 function encode.url(args)
jbe/bsw@0 20 local external = args.external
jbe/bsw@0 21 local base = args.base or request.get_relative_baseurl()
jbe/bsw@0 22 local static = args.static
jbe/bsw@0 23 local module = args.module
jbe/bsw@0 24 local view = args.view
jbe/bsw@0 25 local action = args.action
jbe/bsw@0 26 local id = args.id
jbe/bsw@0 27 local params = args.params or {}
bsw@80 28 local anchor = args.anchor
jbe/bsw@0 29 local result = {}
jbe/bsw@0 30 local id_as_param = false
jbe/bsw@0 31 local function add(...)
jbe/bsw@0 32 for i = 1, math.huge do
jbe/bsw@0 33 local v = select(i, ...)
jbe/bsw@0 34 if v == nil then break end
jbe/bsw@0 35 result[#result+1] = v
jbe/bsw@0 36 end
jbe/bsw@0 37 end
jbe/bsw@0 38 if external then
jbe/bsw@0 39 add(external)
jbe/bsw@0 40 else
jbe/bsw@0 41 add(base)
jbe/bsw@0 42 if not string.find(base, "/$") then
jbe/bsw@0 43 add("/")
jbe/bsw@0 44 end
jbe/bsw@0 45 if static then
jbe/bsw@0 46 add("static/")
jbe/bsw@0 47 add(static)
jbe/bsw@0 48 elseif module or view or action or id then
jbe/bsw@0 49 assert(module, "Module not specified.")
jbe/bsw@0 50 add(encode.url_part(module), "/")
jbe/bsw@0 51 if view and not action then
jbe/bsw@0 52 local view_base, view_suffix = string.match(
jbe/bsw@0 53 view,
jbe/bsw@0 54 "^([^.]*)(.*)$"
jbe/bsw@0 55 )
jbe/bsw@0 56 add(encode.url_part(view_base))
jbe/bsw@0 57 if args.id then
jbe/bsw@0 58 add("/", encode.url_part(id))
jbe/bsw@0 59 end
jbe/bsw@0 60 if view_suffix == "" then
jbe/bsw@0 61 add(".html")
jbe/bsw@0 62 else
jbe/bsw@0 63 add(view_suffix) -- view_suffix includes dot as first character
jbe/bsw@0 64 end
jbe/bsw@0 65 elseif action and not view then
jbe/bsw@0 66 add(encode.url_part(action))
jbe/bsw@0 67 id_as_param = true
jbe/bsw@0 68 elseif view and action then
jbe/bsw@0 69 error("Both a view and an action was specified.")
jbe/bsw@0 70 end
jbe/bsw@0 71 end
jbe/bsw@0 72 do
jbe/bsw@0 73 local new_params = request.get_perm_params()
jbe/bsw@0 74 for key, value in pairs(params) do
jbe/bsw@0 75 new_params[key] = value
jbe/bsw@0 76 end
jbe/bsw@0 77 params = new_params
jbe/bsw@0 78 end
jbe/bsw@0 79 end
jbe/bsw@0 80 if next(params) ~= nil or (id and id_as_param) then
jbe/bsw@0 81 add("?")
jbe/bsw@0 82 if id and id_as_param then
jbe/bsw@0 83 add("_webmcp_id=", encode.url_part(id), "&")
jbe/bsw@0 84 end
jbe/bsw@0 85 for key, value in pairs(params) do
jbe/bsw@11 86 -- TODO: better way to detect arrays?
jbe/bsw@11 87 if string.match(key, "%[%]$") then
jbe/bsw@11 88 for idx, entry in ipairs(value) do
jbe/bsw@11 89 add(encode.url_part(key), "=", encode.url_part(entry), "&")
jbe/bsw@11 90 end
jbe/bsw@11 91 else
jbe/bsw@11 92 add(encode.url_part(key), "=", encode.url_part(value), "&")
jbe/bsw@11 93 end
jbe/bsw@0 94 end
jbe/bsw@0 95 result[#result] = nil -- remove last '&' or '?'
jbe/bsw@0 96 end
jbe@79 97 local string_result = table.concat(result)
jbe@79 98 if anchor ~= nil then
jbe@79 99 string_result = string.match(string_result, "^[^#]*")
jbe@79 100 if anchor then
bsw@80 101 string_result = string_result .. "#" .. encode.url_part(anchor)
jbe@79 102 end
jbe@79 103 end
jbe@79 104 return string_result
jbe/bsw@0 105 end

Impressum / About Us