From 2d15afd6636d22860d5ab0cd7e3a3c3556e754bf Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 15 Apr 2014 12:07:17 +0100 Subject: [PATCH] Moved local funcs: map, trace, unescape into lovebird table --- lovebird.lua | 53 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/lovebird.lua b/lovebird.lua index cb799e4..838b81e 100644 --- a/lovebird.lua +++ b/lovebird.lua @@ -361,27 +361,8 @@ lovebird.pages["env.json"] = [[ ]] - local loadstring = loadstring or load -local map = function(t, fn) - local res = {} - for k, v in pairs(t) do res[k] = fn(v) end - return res -end - -local trace = function(...) - local str = "[lovebird] " .. table.concat(map({...}, tostring), " ") - print(str) - if not lovebird.wrapprint then lovebird.print(str) end -end - -local unescape = function(str) - local f = function(x) return string.char(tonumber("0x"..x)) end - return (str:gsub("%+", " "):gsub("%%(..)", f)) -end - - function lovebird.init() -- Init server @@ -414,17 +395,37 @@ function lovebird.template(str, params) local output = {} local echo = function(str) table.insert(output, str) end fn(echo, ...) - return table.concat(map(output, tostring)) + return table.concat(lovebird.map(output, tostring)) end end +function lovebird.map(t, fn) + local res = {} + for k, v in pairs(t) do res[k] = fn(v) end + return res +end + + +function lovebird.trace(...) + local str = "[lovebird] " .. table.concat(lovebird.map({...}, tostring), " ") + print(str) + if not lovebird.wrapprint then lovebird.print(str) end +end + + +function lovebird.unescape(str) + local f = function(x) return string.char(tonumber("0x"..x)) end + return (str:gsub("%+", " "):gsub("%%(..)", f)) +end + + function lovebird.parseurl(url) local res = {} res.path, res.search = url:match("/([^%?]*)%??(.*)") res.query = {} for k, v in res.search:gmatch("([^&^?]-)=([^&^#]*)") do - res.query[k] = unescape(v) + res.query[k] = lovebird.unescape(v) end return res end @@ -460,7 +461,7 @@ end function lovebird.print(...) - local str = table.concat(map({...}, tostring), " ") + local str = table.concat(lovebird.map({...}, tostring), " ") local last = lovebird.lines[#lovebird.lines] if last and str == last.str then -- Update last line if this line is a duplicate of it @@ -489,12 +490,12 @@ function lovebird.print(...) end return str end - lovebird.buffer = table.concat(map(lovebird.lines, doline), "
") + lovebird.buffer = table.concat(lovebird.map(lovebird.lines, doline), "
") end function lovebird.onerror(err) - trace("ERROR:", err) + lovebird.trace("ERROR:", err) end @@ -537,7 +538,7 @@ function lovebird.onconnect(client) req.parsedbody = {} if req.body then for k, v in req.body:gmatch("([^&]-)=([^&^#]*)") do - req.parsedbody[k] = unescape(v) + req.parsedbody[k] = lovebird.unescape(v) end end -- Parse request line's url @@ -563,7 +564,7 @@ function lovebird.update() if lovebird.checkwhitelist(addr) then xpcall(function() lovebird.onconnect(client) end, function() end) else - trace("got non-whitelisted connection attempt: ", addr) + lovebird.trace("got non-whitelisted connection attempt: ", addr) client:close() end end