webmcp
annotate framework/env/encode/url.lua @ 506:83b3882dc31b
New functions moonhash.shake128(data, len, alphabet), moonhash.shake256(data, len, alphabet)
Removed moonhash.shake128_128(...)
Removed moonhash.shake128_128(...)
| author | jbe | 
|---|---|
| date | Wed Aug 16 00:31:11 2017 +0200 (2017-08-16) | 
| parents | 1b380a0ab940 | 
| children | 
| 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@452 | 81 if not (external and string.find(external, "%?")) then | 
| jbe@452 | 82 add("?") | 
| jbe@453 | 83 elseif external and not string.find(external, "&$") then | 
| jbe@452 | 84 add("&") | 
| jbe@452 | 85 end | 
| jbe/bsw@0 | 86 if id and id_as_param then | 
| jbe/bsw@0 | 87 add("_webmcp_id=", encode.url_part(id), "&") | 
| jbe/bsw@0 | 88 end | 
| jbe/bsw@0 | 89 for key, value in pairs(params) do | 
| jbe/bsw@11 | 90 -- TODO: better way to detect arrays? | 
| jbe/bsw@11 | 91 if string.match(key, "%[%]$") then | 
| jbe/bsw@11 | 92 for idx, entry in ipairs(value) do | 
| jbe/bsw@11 | 93 add(encode.url_part(key), "=", encode.url_part(entry), "&") | 
| jbe/bsw@11 | 94 end | 
| jbe/bsw@11 | 95 else | 
| jbe/bsw@11 | 96 add(encode.url_part(key), "=", encode.url_part(value), "&") | 
| jbe/bsw@11 | 97 end | 
| jbe/bsw@0 | 98 end | 
| jbe/bsw@0 | 99 result[#result] = nil -- remove last '&' or '?' | 
| jbe/bsw@0 | 100 end | 
| jbe@79 | 101 local string_result = table.concat(result) | 
| jbe@79 | 102 if anchor ~= nil then | 
| jbe@79 | 103 string_result = string.match(string_result, "^[^#]*") | 
| jbe@79 | 104 if anchor then | 
| bsw@80 | 105 string_result = string_result .. "#" .. encode.url_part(anchor) | 
| jbe@79 | 106 end | 
| jbe@79 | 107 end | 
| jbe@79 | 108 return string_result | 
| jbe/bsw@0 | 109 end |