1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2024-12-27 09:58:21 +01:00
antd-lua-plugin/APIs/std.lua

165 lines
3.3 KiB
Lua
Raw Normal View History

2018-09-19 15:08:49 +02:00
bytes = modules.bytes()
array = modules.array()
2020-01-02 17:51:08 +01:00
modules.sqlite = function()
if not sqlite then
sqlite = require("sqlitedb")
sqlite.getdb = function(s)
return sqlite._getdb(__api__.dbpath.."/"..s..".db")
end
end
return sqlite
end
2019-12-15 12:08:35 +01:00
RESPONSE_HEADER = {
status = 200,
header = {},
cookie = {},
sent = false
}
function std.status(code)
RESPONSE_HEADER.status=code
2018-09-19 15:08:49 +02:00
end
function std.custom_header(k,v)
2019-12-15 12:08:35 +01:00
std.header(k,v)
2018-09-19 15:08:49 +02:00
end
function std.header_flush()
2019-12-15 12:08:35 +01:00
std._send_header(HTTP_REQUEST.id,RESPONSE_HEADER.status, RESPONSE_HEADER.header, RESPONSE_HEADER.cookie)
RESPONSE_HEADER.sent = true
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.header(k,v)
RESPONSE_HEADER.header[k] = v
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.cjson(ck)
for k,v in pairs(ck) do
std.setCookie(k.."="..v.."; Path=/")
end
2020-01-11 23:50:01 +01:00
std.header("Content-Type","application/json; charset=utf-8")
2019-12-15 12:08:35 +01:00
std.header_flush()
end
function std.chtml(ck)
for k,v in pairs(ck) do
std.setCookie(k.."="..v.."; Path=/")
end
2020-01-11 23:50:01 +01:00
std.header("Content-Type","text/html; charset=utf-8")
2019-12-15 12:08:35 +01:00
std.header_flush()
2018-09-19 15:08:49 +02:00
end
function std.t(s)
2019-12-15 12:08:35 +01:00
if RESPONSE_HEADER.sent == false then
std.header_flush()
end
2018-10-05 19:02:35 +02:00
std._t(HTTP_REQUEST.id,s)
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.b(s)
if RESPONSE_HEADER.sent == false then
std.header_flush()
end
std._b(HTTP_REQUEST.id,s)
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.f(v)
std._f(HTTP_REQUEST.id,v)
--ulib.send_file(v, HTTP_REQUEST.socket)
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.setCookie(v)
RESPONSE_HEADER.cookie[#RESPONSE_HEADER.cookie] = v
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
function std.error(status, msg)
std._error(HTTP_REQUEST.id, status, msg)
2018-09-19 15:08:49 +02:00
end
--_upload
--_route
function std.unknow(s)
2019-12-15 12:08:35 +01:00
std.error(404, "Unknown request")
2018-09-19 15:08:49 +02:00
end
2019-12-15 12:08:35 +01:00
--_redirect
--[[ function std.redirect(s)
std._redirect(HTTP_REQUEST.id,s)
end ]]
function std.html()
2020-01-11 23:50:01 +01:00
std.header("Content-Type","text/html; charset=utf-8")
2019-12-15 12:08:35 +01:00
std.header_flush()
end
function std.text()
2020-01-11 23:50:01 +01:00
std.header("Content-Type","text/plain; charset=utf-8")
2019-12-15 12:08:35 +01:00
std.header_flush()
end
function std.json()
2020-01-11 23:50:01 +01:00
std.header("Content-Type","application/json; charset=utf-8")
2019-12-15 12:08:35 +01:00
std.header_flush()
end
function std.jpeg()
std.header("Content-Type","image/jpeg")
std.header_flush()
end
function std.octstream(s)
std.header("Content-Type","application/octet-stream")
std.header("Content-Disposition",'attachment; filename="'..s..'"')
std.header_flush()
end
--[[ function std.textstream()
std._textstream(HTTP_REQUEST.id)
end ]]
2018-09-19 15:08:49 +02:00
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()
2018-10-05 19:02:35 +02:00
local h = std.ws_header(HTTP_REQUEST.id)
2018-09-19 15:08:49 +02:00
if(h) then
return h --std.readOnly(h)
else
return nil
end
end
function std.ws.read(h)
2018-10-05 19:02:35 +02:00
return std.ws_read(HTTP_REQUEST.id,h)
2018-09-19 15:08:49 +02:00
end
function std.ws.swrite(s)
2018-10-05 19:02:35 +02:00
std.ws_t(HTTP_REQUEST.id,s)
2018-09-19 15:08:49 +02:00
end
function std.ws.fwrite(s)
2018-10-05 19:02:35 +02:00
std.ws_f(HTTP_REQUEST.id,s)
2018-09-19 15:08:49 +02:00
end
function std.ws.write_bytes(arr)
2018-10-05 19:02:35 +02:00
std.ws_b(HTTP_REQUEST.id,arr)
2018-09-19 15:08:49 +02:00
end
function std.ws.enable()
2018-10-07 16:13:55 +02:00
return HTTP_REQUEST ~= nil and HTTP_REQUEST.request["__web_socket__"] == "1"
2018-09-19 15:08:49 +02:00
end
function std.ws.close(code)
2018-10-05 19:02:35 +02:00
std.ws_close(HTTP_REQUEST.id,code)
2018-09-19 15:08:49 +02:00
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
2019-12-04 19:52:26 +01:00
2018-09-19 15:08:49 +02:00
std.ws.TEXT = 1
std.ws.BIN = 2
std.ws.CLOSE = 8