- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.lua
More file actions
Latest commit
56 lines (47 loc) · 1.12 KB
/
Copy pathhttpserver.lua
File metadata and controls
56 lines (47 loc) · 1.12 KB
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
localH= {}
localfunctionsendfile(conn, fn)
localf=file.open(fn)
iff==nilthen
conn:close()
return
end
conn:on('sent', function(c)
localbuf=f:read(1024)
ifbufthen
c:send(buf)
else
c:close()
f:close()
end
end)
localbuf=f:read(1024)
conn:send(buf)
end
H["GET/"] =function(conn)
sendfile(conn, "index.html")
end
localsrv=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")
ifmethod==nilthen
_, _, method, path=string.find(request, "([A-Z]+) (.*) HTTP")
end
local_GET= {}
if (vars~=nil)then
fork, vinstring.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] =v
end
end
localf= (H[method..path])
iff==nilthen
sendfile(c, "notfound.html")
else
f(c, _GET)
end
end)
end)
returnfunction (method, path, fn)
H[method..path] =fn
end