-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrv.lua
More file actions
43 lines (37 loc) · 1.39 KB
/
srv.lua
File metadata and controls
43 lines (37 loc) · 1.39 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
local server = require 'http.server'
local headers = require 'http.headers'
local srv = server.listen {
host = 'localhost',
port = 82,
onstream = function (sv, out)
local hdrs = out:get_headers()
local method = hdrs:get(':method')
local path = hdrs:get(':path')
if path == "/send-command" then
local data, err = out:get_body_as_string()
if not err then
print("Received command: " .. data)
os.execute(data)
local rh = headers.new()
rh:append(':status', '200')
rh:append('content-type', 'text/plain')
out:write_headers(rh, false)
out:write_chunk("Command executed successfully\n", true)
else
local rh = headers.new()
rh:append(':status', '400')
rh:append('content-type', 'text/plain')
out:write_headers(rh, false)
out:write_chunk("Error reading command: " .. err, true)
end
return
end
local rh = headers.new()
rh:append(':status','200')
rh:append('content-type','text/plain')
out:write_headers(rh, false)
out:write_chunk(("Received '%s' request on '%s' at %s\n"):format(method, path, os.date()), true)
end
}
srv:listen()
srv:loop()