-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.lua
More file actions
42 lines (32 loc) · 874 Bytes
/
http.lua
File metadata and controls
42 lines (32 loc) · 874 Bytes
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
-- start a web server for configuration
routes = {}
function send(socket)
local line = r.line()
if line then
socket:send(line)
else
socket:close()
file.close()
end
end
function render(socket, filename)
local header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n'
file.open(filename, 'r')
socket:on('sent', send)
socket:send(header)
end
function error_404(socket)
socket:send('error 404')
socket:close()
end
-- leave server global so it can be stopped later
http_server = net.createServer(net.TCP, 30)
http_server:listen(80, function(socket)
socket:on('receive', function(socket, request)
print(request)
local uri = request:match('%S+ %S+')
local body = request:match('\r\n\r\n(.*)')
local params = decode(body)
if routes[uri] then routes[uri](socket, params) else error_404(socket) end
end)
end)