mirror of
https://github.com/lxsang/antd-lua-plugin
synced 2025-07-24 01:40:09 +02:00
mimgrating from another repo
This commit is contained in:
40
APIs/OOP.lua
Normal file
40
APIs/OOP.lua
Normal file
@ -0,0 +1,40 @@
|
||||
Object = {}
|
||||
|
||||
function Object:prototype(o)
|
||||
o = o or {} -- create table if user does not provide one
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
function Object:new(o)
|
||||
local obj = self:prototype(o)
|
||||
obj:initialize()
|
||||
return obj
|
||||
end
|
||||
|
||||
function Object:print()
|
||||
print('an Object')
|
||||
end
|
||||
|
||||
function Object:initialize()
|
||||
end
|
||||
|
||||
function Object:asJSON()
|
||||
return '{}'
|
||||
end
|
||||
|
||||
function Object:inherit(o)
|
||||
return self:prototype(o)
|
||||
end
|
||||
|
||||
|
||||
function Object:extends(o)
|
||||
return self:inherit(o)
|
||||
end
|
||||
|
||||
Test = Object:inherit{dummy = 0}
|
||||
|
||||
function Test:toWeb()
|
||||
wio.t(self.dummy)
|
||||
end
|
145
APIs/api.lua
Normal file
145
APIs/api.lua
Normal file
@ -0,0 +1,145 @@
|
||||
-- root dir
|
||||
__ROOT__ = __api__.root
|
||||
-- set require path
|
||||
package.path = __ROOT__ .. '/?.lua;'..__api__.apiroot..'/?.lua'
|
||||
package.cpath = __api__.apiroot..'/?.llib'
|
||||
-- set session
|
||||
SESSION = {}
|
||||
if REQUEST.query ~= nil and REQUEST.query.cookie ~= nil then
|
||||
SESSION = REQUEST.query.cookie
|
||||
end
|
||||
HEADER = REQUEST.query.__xheader__
|
||||
HEADER.mobile = false
|
||||
|
||||
if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then
|
||||
HEADER.mobile = true
|
||||
end
|
||||
|
||||
require("std")
|
||||
require("utils")
|
||||
require("extra_mime")
|
||||
|
||||
function has_module(m)
|
||||
if utils.file_exists(__ROOT__..'/'..m) then
|
||||
if m:find("%.ls$") then
|
||||
return true, true, __ROOT__..'/'..m
|
||||
else
|
||||
return true, false, m:gsub(".lua$","")
|
||||
end
|
||||
elseif utils.file_exists(__ROOT__..'/'..string.gsub(m,'%.','/')..'.lua') then
|
||||
return true, false, m
|
||||
elseif utils.file_exists(__ROOT__..'/'..string.gsub(m,'%.','/')..'.ls') then
|
||||
return true, true, __ROOT__..'/'..string.gsub(m,'%.','/')..'.ls'
|
||||
end
|
||||
return false, false, nil
|
||||
end
|
||||
|
||||
function echo(m)
|
||||
if m then std.t(m) else std.t("Undefined value") end
|
||||
end
|
||||
|
||||
function loadscript(file, args)
|
||||
local f = io.open(file, "rb")
|
||||
local content = ""
|
||||
if f then
|
||||
local html = ""
|
||||
local pro = "local fn = function(...)"
|
||||
local s,e, mt
|
||||
local mtbegin = true -- find begin of scrit, 0 end of scrit
|
||||
local i = 1
|
||||
if args then
|
||||
pro = "local fn = function("..table.concat( args, ",")..")"
|
||||
end
|
||||
for line in io.lines(file) do
|
||||
line = std.trim(line, " ")
|
||||
if(line ~= "") then
|
||||
if(mtbegin) then
|
||||
mt = "^%s*<%?lua"
|
||||
else
|
||||
mt = "%?>%s*$"
|
||||
end
|
||||
s,e = line:find(mt)
|
||||
if(s) then
|
||||
if mtbegin then
|
||||
if html ~= "" then
|
||||
pro= pro.."echo(\""..utils.escape(html).."\")\n"
|
||||
html = ""
|
||||
end
|
||||
local b,f = line:find("%?>%s*$")
|
||||
if b then
|
||||
pro = pro..line:sub(e+1,b-1).."\n"
|
||||
else
|
||||
pro = pro..line:sub(e+1).."\n"
|
||||
mtbegin = not mtbegin
|
||||
end
|
||||
else
|
||||
pro = pro..line:sub(0,s-1).."\n"
|
||||
mtbegin = not mtbegin
|
||||
end
|
||||
else -- no match
|
||||
if mtbegin then
|
||||
-- detect if we have inline lua with format <?=..?>
|
||||
local b,f = line:find("<%?=")
|
||||
if b then
|
||||
local tmp = line
|
||||
pro= pro.."echo("
|
||||
while(b) do
|
||||
-- find the close
|
||||
local x,y = tmp:find("%?>")
|
||||
if x then
|
||||
pro = pro.."\""..utils.escape(html..tmp:sub(0,b-1):gsub("%%","%%%%")).."\".."
|
||||
pro = pro..tmp:sub(f+1,x-1)..".."
|
||||
html = ""
|
||||
tmp = tmp:sub(y+1)
|
||||
b,f = tmp:find("<%?=")
|
||||
else
|
||||
error("Syntax error near line "..i)
|
||||
end
|
||||
end
|
||||
pro = pro.."\""..utils.escape(tmp:gsub("%%","%%%%")).."\")\n"
|
||||
--print(pro)
|
||||
else
|
||||
html = html..std.trim(line," "):gsub("%%","%%%%").."\n"
|
||||
end
|
||||
else
|
||||
if line ~= "" then pro = pro..line.."\n" end
|
||||
end
|
||||
end
|
||||
end
|
||||
i = i+ 1
|
||||
end
|
||||
f:close()
|
||||
if(html ~= "") then
|
||||
pro = pro.."echo(\""..utils.escape(html).."\")\n"
|
||||
end
|
||||
pro = pro.."\nend \n return fn"
|
||||
--print(pro)
|
||||
local r,e = load(pro)
|
||||
if r then return r(), e else return nil,e end
|
||||
end
|
||||
end
|
||||
|
||||
-- OOP support
|
||||
--require("OOP")
|
||||
-- load sqlite helper
|
||||
--require("sqlite")
|
||||
-- enable extra mime
|
||||
|
||||
-- run the file
|
||||
|
||||
|
||||
local m, s, p = has_module(REQUEST.path)
|
||||
if m then
|
||||
-- run the correct module
|
||||
if s then
|
||||
local r,e = loadscript(p)
|
||||
if r then r() else unknow(e) end
|
||||
else
|
||||
require(p)
|
||||
end
|
||||
else
|
||||
unknow("Resource not found for request "..REQUEST.path)
|
||||
end
|
||||
|
||||
|
||||
--require('router')
|
90
APIs/bugbot.lua
Normal file
90
APIs/bugbot.lua
Normal file
@ -0,0 +1,90 @@
|
||||
local pibot = require("pibot")
|
||||
|
||||
local bugbot = {}
|
||||
local cmd = bytes.new(8)
|
||||
--1 IRL R
|
||||
--2 IRR R
|
||||
--3 SNL R
|
||||
--4 SNH R
|
||||
--5 ML RW
|
||||
--6 MR RW
|
||||
--7 MLS RW
|
||||
--8 MRL RW
|
||||
bugbot.init = function()
|
||||
return pibot.init()
|
||||
end
|
||||
|
||||
bugbot.scan = function()
|
||||
local raw = pibot.read(64)
|
||||
if raw then
|
||||
local data = {}
|
||||
data.leftIR = raw[0]
|
||||
data.rightIR = raw[1]
|
||||
data.sonar = raw[2] + bit32.lshift(raw[3], 8)
|
||||
data.motors = {}
|
||||
data.motors.left = {}
|
||||
data.motors.right = {}
|
||||
data.motors.left.status = raw[4]
|
||||
data.motors.left.speed = raw[5]
|
||||
data.motors.right.status = raw[6]
|
||||
data.motors.right.speed = raw[7]
|
||||
return data
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
bugbot.forward = function(sp)
|
||||
cmd[5] = 1 -- fw
|
||||
cmd[6] = sp -- fw
|
||||
cmd[7] = 1
|
||||
cmd[8] = sp
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.action = function(st1,sp1,st2,sp2)
|
||||
|
||||
cmd[5] = st1 -- bw
|
||||
cmd[6] = sp1
|
||||
cmd[7] = st2 -- bw
|
||||
cmd[8] = sp2
|
||||
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.backward = function(sp)
|
||||
cmd[5] = 2 -- bw
|
||||
cmd[6] = sp -- bw
|
||||
cmd[7] = 2
|
||||
cmd[8] = sp
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.stop = function()
|
||||
cmd[5] = 0 -- s
|
||||
cmd[6] = 0 -- s
|
||||
cmd[7] = 0
|
||||
cmd[8] = 0
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.rotateLeft = function(sp)
|
||||
cmd[5] = 2 -- bw
|
||||
cmd[6] = sp -- fw
|
||||
cmd[7] = 1
|
||||
cmd[8] = sp
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.rotateRight = function(sp)
|
||||
cmd[5] = 1 -- fw
|
||||
cmd[6] = sp -- bw
|
||||
cmd[7] = 2
|
||||
cmd[8] = sp
|
||||
pibot.write(cmd)
|
||||
end
|
||||
|
||||
bugbot.release = function()
|
||||
return pibot.release()
|
||||
end
|
||||
return bugbot
|
43
APIs/extra_mime.lua
Normal file
43
APIs/extra_mime.lua
Normal file
@ -0,0 +1,43 @@
|
||||
function std.extra_mime(name)
|
||||
local ext = std.ext(name);
|
||||
local mpath = __ROOT__.."/".."mimes.json"
|
||||
local xmimes = {}
|
||||
if utils.file_exists(mpath) then
|
||||
local f = io.open(mpath, "r")
|
||||
if f then
|
||||
xmimes = JSON.decodeString(f:read("*all"))
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
if(name:find("Makefile$")) then return "text/makefile",false
|
||||
elseif ext == "php" then return "text/php",false
|
||||
elseif ext == "c" or ext == "h" then return "text/c",false
|
||||
elseif ext == "cpp" or ext == "hpp" then return "text/cpp",false
|
||||
elseif ext == "md" then return "text/markdown",false
|
||||
elseif ext == "lua" then return "text/lua",false
|
||||
elseif ext == "yaml" then return "application/x-yaml", false
|
||||
elseif xmimes[ext] then return xmimes[ext].mime, xmimes[ext].binary
|
||||
--elseif ext == "pgm" then return "image/x-portable-graymap", true
|
||||
else
|
||||
return "application/octet-stream",true
|
||||
end
|
||||
end
|
||||
|
||||
function std.mimeOf(name)
|
||||
local mime = std.mime(name)
|
||||
if mime ~= "application/octet-stream" then
|
||||
return mime
|
||||
else
|
||||
return std.extra_mime(name)
|
||||
end
|
||||
end
|
||||
|
||||
function std.isBinary(name)
|
||||
local mime = std.mime(name)
|
||||
if mime ~= "application/octet-stream" then
|
||||
return std.is_bin(name)
|
||||
else
|
||||
local xmime,bin = std.extra_mime(name)
|
||||
return bin
|
||||
end
|
||||
end
|
157
APIs/sqlite.lua
Normal file
157
APIs/sqlite.lua
Normal file
@ -0,0 +1,157 @@
|
||||
sqlite = modules.sqlite()
|
||||
|
||||
if sqlite == nil then return 0 end
|
||||
require("OOP")
|
||||
-- create class
|
||||
DBModel = Object:inherit{db=nil, name=''}
|
||||
|
||||
function DBModel:createTable(m)
|
||||
if self:available() then return true end
|
||||
local sql = "CREATE TABLE "..self.name.."(id INTEGER PRIMARY KEY"
|
||||
for k, v in pairs(m) do
|
||||
if k ~= "id" then
|
||||
sql = sql..","..k.." "..v
|
||||
end
|
||||
end
|
||||
sql = sql..");"
|
||||
return sqlite.query(self.db,sql) == 1
|
||||
end
|
||||
|
||||
function DBModel:insert(m)
|
||||
local keys = {}
|
||||
local values = {}
|
||||
for k,v in pairs(m) do
|
||||
if k ~= "id" then
|
||||
table.insert(keys,k)
|
||||
if type(v) == "number" then
|
||||
table.insert(values, v)
|
||||
elseif type(v) == "boolean" then
|
||||
table.insert( values, v and 1 or 0 )
|
||||
else
|
||||
local t = "\""..v:gsub('"', '""').."\""
|
||||
table.insert(values,t)
|
||||
end
|
||||
end
|
||||
end
|
||||
local sql = "INSERT INTO "..self.name.." ("..table.concat(keys,',')..') VALUES ('
|
||||
sql = sql..table.concat(values,',')..');'
|
||||
return sqlite.query(self.db, sql) == 1
|
||||
end
|
||||
|
||||
function DBModel:get(id)
|
||||
return sqlite.select(self.db, self.name, "*","id="..id)[1]
|
||||
end
|
||||
|
||||
function DBModel:getAll()
|
||||
--local sql = "SELECT * FROM "..self.name
|
||||
--return sqlite.select(self.db, self.name, "1=1")
|
||||
local data = sqlite.select(self.db, self.name, "*", "1=1")
|
||||
if data == nil then return nil end
|
||||
local a = {}
|
||||
for n in pairs(data) do table.insert(a, n) end
|
||||
table.sort(a)
|
||||
return data, a
|
||||
end
|
||||
|
||||
function DBModel:find(cond)
|
||||
local cnd = "1=1"
|
||||
local sel = "*"
|
||||
if cond.exp then
|
||||
cnd = self:gencond(cond.exp)
|
||||
end
|
||||
if cond.order then
|
||||
cnd = cnd.." ORDER BY "
|
||||
local l = {}
|
||||
local i = 1
|
||||
for k,v in pairs(cond.order) do
|
||||
l[i] = k.." "..v
|
||||
i = i+1
|
||||
end
|
||||
cnd = cnd..table.concat(l, ",")
|
||||
end
|
||||
if cond.limit then
|
||||
cnd = cnd.." LIMIT "..cond.limit
|
||||
end
|
||||
if cond.fields then
|
||||
sel = table.concat(cond.fields, ",")
|
||||
--print(sel)
|
||||
end
|
||||
--print(cnd)
|
||||
local data = sqlite.select(self.db, self.name, sel, cnd)
|
||||
if data == nil then return nil end
|
||||
local a = {}
|
||||
for n in pairs(data) do table.insert(a, n) end
|
||||
table.sort(a)
|
||||
return data, a
|
||||
end
|
||||
|
||||
function DBModel:query(sql)
|
||||
return sqlite.query(self.db, sql) == 1
|
||||
end
|
||||
|
||||
function DBModel:update(m)
|
||||
local id = m['id']
|
||||
if id ~= nil then
|
||||
local lst = {}
|
||||
for k,v in pairs(m) do
|
||||
if(type(v)== "number") then
|
||||
table.insert(lst,k.."="..v)
|
||||
elseif type(v) == "boolean" then
|
||||
table.insert( lst, k.."="..(v and 1 or 0) )
|
||||
else
|
||||
table.insert(lst,k.."=\""..v:gsub('"', '""').."\"")
|
||||
end
|
||||
end
|
||||
local sql = "UPDATE "..self.name.." SET "..table.concat(lst,",").." WHERE id="..id..";"
|
||||
return sqlite.query(self.db, sql) == 1
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function DBModel:available()
|
||||
return sqlite.hasTable(self.db, self.name) == 1
|
||||
end
|
||||
function DBModel:deleteByID(id)
|
||||
local sql = "DELETE FROM "..self.name.." WHERE id="..id..";"
|
||||
return sqlite.query(self.db, sql) == 1
|
||||
end
|
||||
function DBModel:gencond(o)
|
||||
for k,v in pairs(o) do
|
||||
if k == "and" or k == "or" then
|
||||
local cnd = {}
|
||||
local i = 1
|
||||
for k1,v1 in pairs(v) do
|
||||
cnd[i] = self:gencond(v1)
|
||||
i = i + 1
|
||||
end
|
||||
return " ("..table.concat(cnd, " "..k.." ")..") "
|
||||
else
|
||||
for k1,v1 in pairs(v) do
|
||||
local t = type(v1)
|
||||
if(t == "string") then
|
||||
return " ("..k1.." "..k..' "'..v1:gsub('"','""')..'") '
|
||||
end
|
||||
return " ("..k1.." "..k.." "..v1..") "
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function DBModel:delete(cond)
|
||||
local sql = "DELETE FROM "..self.name.." WHERE "..self:gencond(cond)..";"
|
||||
return sqlite.query(self.db, sql) == 1
|
||||
end
|
||||
|
||||
function DBModel:lastInsertID()
|
||||
return sqlite.lastInsertID(self.db)
|
||||
end
|
||||
|
||||
function DBModel:close()
|
||||
if self.db then
|
||||
sqlite.dbclose(self.db)
|
||||
end
|
||||
end
|
||||
function DBModel:open()
|
||||
if self.db ~= nil then
|
||||
self.db = sqlite.getdb(self.db)
|
||||
end
|
||||
end
|
122
APIs/std.lua
Normal file
122
APIs/std.lua
Normal file
@ -0,0 +1,122 @@
|
||||
std = modules.std()
|
||||
bytes = modules.bytes()
|
||||
array = modules.array()
|
||||
function std.html()
|
||||
std._html(REQUEST.id)
|
||||
end
|
||||
function std.text()
|
||||
std._text(REQUEST.id)
|
||||
end
|
||||
function std.status(code, msg)
|
||||
std._status(REQUEST.id, code, msg)
|
||||
end
|
||||
function std.custom_header(k,v)
|
||||
--print(k..":"..v)
|
||||
std.t(k..": "..v)
|
||||
end
|
||||
function std.header_flush()
|
||||
std.t("")
|
||||
end
|
||||
--_redirect
|
||||
function std.redirect(s)
|
||||
std._redirect(REQUEST.id,s)
|
||||
end
|
||||
function std.json()
|
||||
std._json(REQUEST.id)
|
||||
end
|
||||
function std.jpeg()
|
||||
std._jpeg(REQUEST.id)
|
||||
end
|
||||
function std.header(s)
|
||||
std._header(REQUEST.id,s)
|
||||
end
|
||||
function std.octstream(s)
|
||||
std._octstream(REQUEST.id,s)
|
||||
end
|
||||
function std.textstream()
|
||||
std._textstream(REQUEST.id)
|
||||
end
|
||||
function std.ti(v)
|
||||
std._ti(REQUEST.id,v)
|
||||
end
|
||||
function std.t(s)
|
||||
std._t(REQUEST.id,s)
|
||||
end
|
||||
function std.f(v)
|
||||
std._f(REQUEST.id,v)
|
||||
end
|
||||
function std.fb(v)
|
||||
std._fb(REQUEST.id,v)
|
||||
end
|
||||
function std.setCookie(t,v,p)
|
||||
p = p or ""
|
||||
std._setCookie(REQUEST.id,t,v,p)
|
||||
end
|
||||
function std.cjson(v, p)
|
||||
|
||||
std.setCookie("application/json; charset=utf-8",v)
|
||||
end
|
||||
function std.chtml(v)
|
||||
std.setCookie("text/html; charset=utf-8",v)
|
||||
end
|
||||
function std.ctext(v)
|
||||
std.setCookie("text/plain; charset=utf-8",v)
|
||||
end
|
||||
--_upload
|
||||
--_route
|
||||
function std.unknow(s)
|
||||
std._unknow(REQUEST.id,s)
|
||||
end
|
||||
|
||||
function std.readOnly(t) -- bugging
|
||||
local proxy = {}
|
||||
local mt = { -- create metatable
|
||||
__index = t,
|
||||
__newindex = function (t,k,v)
|
||||
error("attempt to update a read-only table", 2)
|
||||
end
|
||||
}
|
||||
setmetatable(proxy, mt)
|
||||
return proxy
|
||||
end
|
||||
|
||||
|
||||
-- web socket
|
||||
std.ws = {}
|
||||
function std.ws.header()
|
||||
local h = std.ws_header(REQUEST.id)
|
||||
if(h) then
|
||||
return h --std.readOnly(h)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
function std.ws.read(h)
|
||||
return std.ws_read(REQUEST.id,h)
|
||||
end
|
||||
function std.ws.swrite(s)
|
||||
std.ws_t(REQUEST.id,s)
|
||||
end
|
||||
function std.ws.fwrite(s)
|
||||
std.ws_f(REQUEST.id,s)
|
||||
end
|
||||
function std.ws.write_bytes(arr)
|
||||
std.ws_b(REQUEST.id,arr)
|
||||
end
|
||||
function std.ws.enable()
|
||||
return REQUEST.query ~= nil and REQUEST.query["__web_socket__"] == "1"
|
||||
end
|
||||
function std.ws.close(code)
|
||||
std.ws_close(REQUEST.id,code)
|
||||
end
|
||||
function std.basename(str)
|
||||
local name = string.gsub(std.trim(str,"/"), "(.*/)(.*)", "%2")
|
||||
return name
|
||||
end
|
||||
function std.is_file(f)
|
||||
return std.is_dir(f) == false
|
||||
end
|
||||
std.ws.TEXT = 1
|
||||
std.ws.BIN = 2
|
||||
std.ws.CLOSE = 8
|
137
APIs/utils.lua
Normal file
137
APIs/utils.lua
Normal file
@ -0,0 +1,137 @@
|
||||
utils = {}
|
||||
|
||||
function utils.is_array(table)
|
||||
local max = 0
|
||||
local count = 0
|
||||
for k, v in pairs(table) do
|
||||
if type(k) == "number" then
|
||||
if k > max then max = k end
|
||||
count = count + 1
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
if max > count * 2 then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function utils.escape(s)
|
||||
local replacements = {
|
||||
["\\"] = "\\\\" ,
|
||||
['"'] = '\\"',
|
||||
["\n"] = "\\n",
|
||||
["\t"] = "\\t",
|
||||
["\b"] = "\\b",
|
||||
["\f"] = "\\f",
|
||||
["\r"] = "\\r",
|
||||
["%"] = "%%"
|
||||
}
|
||||
return (s:gsub( "[\\'\"\n\t\b\f\r%%]", replacements ))
|
||||
end
|
||||
|
||||
function utils.escape_pattern(s)
|
||||
return s:gsub("[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
|
||||
end
|
||||
|
||||
function utils.unescape_pattern(s)
|
||||
return s:gsub( "[%%]", "%%%%")
|
||||
end
|
||||
|
||||
function utils.hex_to_char(x)
|
||||
return string.char(tonumber(x, 16))
|
||||
end
|
||||
|
||||
function utils.decodeURI(url)
|
||||
return url:gsub("%%(%x%x)", utils.hex_to_char)
|
||||
end
|
||||
|
||||
function utils.unescape(s)
|
||||
local replacements = {
|
||||
["\\\\"] = "\\" ,
|
||||
['\\"'] = '"' ,
|
||||
["\\n"] = "\n" ,
|
||||
["\\t"] = "\t" ,
|
||||
["\\b"] = "\b" ,
|
||||
["\\f"] = "\f" ,
|
||||
["\\r"] = "\r"
|
||||
}
|
||||
local out = s
|
||||
for k,v in pairs(replacements) do
|
||||
out = out:gsub(k,v)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function utils.file_exists(name)
|
||||
local f=io.open(name,"r")
|
||||
if f~=nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
function utils.url_parser(uri)
|
||||
local pattern = "^(https?)://([%.%w]+):?(%d*)(/?[^#]*)#?.*$"
|
||||
local obj = {}
|
||||
obj.protocol = uri:gsub(pattern, "%1")
|
||||
obj.hostname = uri:gsub(pattern, "%2")
|
||||
obj.port = uri:gsub(pattern, "%3")
|
||||
obj.query = uri:gsub(pattern, "%4")
|
||||
|
||||
if obj.port == "" then obj.port = 80 else obj.port = tonumber(obj.port) end
|
||||
if obj.query == "" then obj.query="/" end
|
||||
return obj
|
||||
end
|
||||
|
||||
JSON = modules.JSON()
|
||||
|
||||
function JSON.encode(obj)
|
||||
local t = type(obj)
|
||||
if t == 'table' then
|
||||
-- encode object
|
||||
if utils.is_array(obj) == false then
|
||||
local lst = {}
|
||||
for k,v in pairs(obj) do
|
||||
table.insert(lst,'"'..k..'":'..JSON.encode(v))
|
||||
end
|
||||
return "{"..table.concat(lst,",").."}"
|
||||
else
|
||||
local lst = {}
|
||||
local a = {}
|
||||
for n in pairs(obj) do table.insert(a, n) end
|
||||
table.sort(a)
|
||||
for i,v in pairs(a) do
|
||||
table.insert(lst,JSON.encode(obj[v]))
|
||||
end
|
||||
return "["..table.concat(lst,",").."]"
|
||||
end
|
||||
elseif t == 'string' then
|
||||
--print('"'..utils.escape(obj)..'"')
|
||||
return '"'..utils.escape(obj)..'"'
|
||||
elseif t == 'boolean' or t == 'number' then
|
||||
return tostring(obj)
|
||||
elseif obj == nil then
|
||||
return "null"
|
||||
else
|
||||
return '"'..tostring(obj)..'"'
|
||||
end
|
||||
end
|
||||
|
||||
function explode(str, div) -- credit: http://richard.warburton.it
|
||||
if (div=='') then return false end
|
||||
local pos,arr = 0,{}
|
||||
-- for each divider found
|
||||
for st,sp in function() return string.find(str,div,pos,true) end do
|
||||
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
|
||||
pos = sp + 1 -- Jump past current divider
|
||||
end
|
||||
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
|
||||
return arr
|
||||
end
|
||||
function implode(arr, div)
|
||||
return table.concat(arr,div)
|
||||
end
|
||||
|
||||
function firstToUpper(str)
|
||||
return (str:gsub("^%l", string.upper))
|
||||
end
|
74
APIs/web.lua
Normal file
74
APIs/web.lua
Normal file
@ -0,0 +1,74 @@
|
||||
-- require the utils library to work
|
||||
--require("utils")
|
||||
-- require("std")
|
||||
local wurl = require("wurl")
|
||||
|
||||
local web = {}
|
||||
|
||||
web.undestand = function(proto)
|
||||
if proto == "http" or proto == "https" then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
web.get = function(url)
|
||||
local obj = utils.url_parser(url)
|
||||
if web.undestand(obj.protocol) then
|
||||
return wurl._get(obj.hostname,obj.port, obj.query)
|
||||
else
|
||||
return nil,"Protocol is unsupported: "..obj.protocol
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
web.post = function(url,data)
|
||||
local obj = utils.url_parser(url)
|
||||
if web.undestand(obj.protocol) then
|
||||
if type(data) == "string" then
|
||||
return wurl._post(obj.hostname,
|
||||
obj.port,
|
||||
obj.query,
|
||||
"application/x-www-form-urlencoded",data)
|
||||
else
|
||||
return wurl._post(obj.hostname,
|
||||
obj.port,
|
||||
obj.query,
|
||||
data.contentType,data.value)
|
||||
end
|
||||
else
|
||||
return nil,"Protocol is unsupported: "..obj.protocol
|
||||
end
|
||||
end
|
||||
|
||||
web.download = function(url,to)
|
||||
local obj = utils.url_parser(url)
|
||||
if web.undestand(obj.protocol) then
|
||||
local file
|
||||
if std.is_dir(to) then
|
||||
-- need to find file name here
|
||||
local pattern = "^[^%?]*/([%w.]*)%??.*$"
|
||||
local filename = string.gsub(obj.query,pattern,"%1")
|
||||
if filename == "" then filename = "index" end
|
||||
file = to.."/"..filename
|
||||
else
|
||||
file = to
|
||||
end
|
||||
local obj = utils.url_parser(url)
|
||||
return wurl._download(obj.hostname,obj.port,obj.query,file)
|
||||
else
|
||||
return false,"Protocol is unsupported: "..obj.protocol
|
||||
end
|
||||
end
|
||||
|
||||
web.upload = function(url,name,file)
|
||||
local obj = utils.url_parser(url)
|
||||
if web.undestand(obj.protocol) then
|
||||
return wurl._upload(obj.hostname,obj.port,obj.query,name,file)
|
||||
else
|
||||
return nil,"Protocol is unsupported: "..obj.protocol
|
||||
end
|
||||
end
|
||||
|
||||
return web
|
Reference in New Issue
Block a user