2018-09-19 15:08:49 +02:00
|
|
|
std = modules.std()
|
|
|
|
bytes = modules.bytes()
|
|
|
|
array = modules.array()
|
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
|
|
|
|
std.header("Content-Type","application/json")
|
|
|
|
std.header_flush()
|
|
|
|
end
|
|
|
|
function std.chtml(ck)
|
|
|
|
for k,v in pairs(ck) do
|
|
|
|
std.setCookie(k.."="..v.."; Path=/")
|
|
|
|
end
|
|
|
|
std.header("Content-Type","text/html")
|
|
|
|
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)
|
2019-11-30 20:46:03 +01:00
|
|
|
std._f(HTTP_REQUEST.id,v)
|
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()
|
|
|
|
std.header("Content-Type","text/html")
|
|
|
|
std.header_flush()
|
|
|
|
end
|
|
|
|
function std.text()
|
|
|
|
std.header("Content-Type","text/plain")
|
|
|
|
std.header_flush()
|
|
|
|
end
|
|
|
|
|
|
|
|
function std.json()
|
|
|
|
std.header("Content-Type","application/json")
|
|
|
|
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
|