-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.lua
56 lines (47 loc) · 1.12 KB
/
httpserver.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
50
51
52
53
54
55
56
-- httpserver
local H = {}
local function sendfile(conn, fn)
local f = file.open(fn)
if f == nil then
conn:close()
return
end
conn:on('sent', function(c)
local buf = f:read(1024)
if buf then
c:send(buf)
else
c:close()
f:close()
end
end)
local buf = f:read(1024)
conn:send(buf)
end
H["GET/"] = function(conn)
sendfile(conn, "index.html")
end
local srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(c, request)
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.*)?(.+) HTTP")
if method == nil then
_, _, method, path = string.find(request, "([A-Z]+) (.*) HTTP")
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
local f = (H[method .. path])
if f == nil then
sendfile(c, "notfound.html")
else
f(c, _GET)
end
end)
end)
return function (method, path, fn)
H[method .. path] = fn
end