/
web_ui_static_GET.lua
49 lines (48 loc) · 1.35 KB
/
web_ui_static_GET.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local mimetypes={
["html"] = "text/html; charset=\"utf-8\"",
["xml"] = "text/xml; charset=\"utf-8\"",
["txt"] = "text/plain; charset=\"utf-8\"",
["png"] = "image/png",
["ico"] = "image/x-icon",
["css"] = "text/css; charset=\"utf-8\"",
["js"] = "text/javascript; charset=\"utf-8\""
}
return function(state,send,data,staticroot,url)
local curfile
local curpos=0
local found
if url:sub(#url)=="/" then
url=url.."index.html"
end
if file.open(staticroot..url) then
file.close()
curfile=staticroot..url
found=true
elseif file.open(staticroot..url..".gz") then
file.close()
curfile=staticroot..url..".gz"
found="gzip"
end
if found then
send("HTTP/1.0 200 OK\r\n")
local ext=url:match(".*%.(%w*)")
if found=="gzip" then
send("Content-Encoding: gzip\r\n")
end
send("Content-Type: "..(mimetypes[ext] or "application/octet-stream").."\r\n\r\n")
local function sendfile(s)
file.open(curfile)
file.seek("set",curpos)
local data=file.read()
if data then
curpos=curpos+#data
send(data)
end
file.close()
return data and sendfile
end
return sendfile
else
return 404
end
end