Software /
code /
prosody
File
util/format.lua @ 9793:9993fd021d19
mod_http: Solve CORS problems once and for all
This blindly allows any cross-site requests.
Future work should add an API to allow each HTTP app some influence over
this for each HTTP path
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 04 Oct 2018 12:22:12 +0200 |
parent | 9693:6ed0d6224d64 |
child | 10034:4fca92d60040 |
line wrap: on
line source
-- -- A string.format wrapper that gracefully handles invalid arguments -- local tostring = tostring; local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack local pack = require "util.table".pack; -- TODO table.pack in 5.2+ local type = type; local dump = require "util.serialization".new("debug"); local function format(formatstring, ...) local args = pack(...); local args_length = args.n; -- format specifier spec: -- 1. Start: '%%' -- 2. Flags: '[%-%+ #0]' -- 3. Width: '%d?%d?' -- 4. Precision: '%.?%d?%d?' -- 5. Option: '[cdiouxXaAeEfgGqs%%]' -- -- The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument, whereas q and s expect a string. -- This function does not accept string values containing embedded zeros, except as arguments to the q option. -- a and A are only in Lua 5.2+ -- process each format specifier local i = 0; formatstring = formatstring:gsub("%%[^cdiouxXaAeEfgGqs%%]*[cdiouxXaAeEfgGqs%%]", function(spec) if spec ~= "%%" then i = i + 1; local arg = args[i]; local option = spec:sub(-1); if arg == nil then args[i] = "nil"; spec = "<%s>"; elseif option == "q" then args[i] = dump(arg); spec = "%s"; elseif option == "s" then args[i] = tostring(arg); elseif type(arg) ~= "number" then -- arg isn't number as expected? args[i] = tostring(arg); spec = "[%s]"; end end return spec; end); -- process extra args while i < args_length do i = i + 1; local arg = args[i]; if arg == nil then args[i] = "<nil>"; else args[i] = tostring(arg); end formatstring = formatstring .. " [%s]" end return formatstring:format(unpack(args)); end return { format = format; };