forked from loicortola/nodemcu-espress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_response_send.lua
38 lines (38 loc) · 1.02 KB
/
http_response_send.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
------------------------------------------------------------------------------
-- send
------------------------------------------------------------------------------
return function(res, data, status)
local conn = res.conn
local buf
-- write protocol headers
buf = "HTTP/1.1 " .. tostring(status or res.statuscode) .. " " .. dofile('http-' .. tostring(status or res.statuscode)) .. "\r\n"
-- write response headers
res:addheader("Server", "NodeMCU")
if data then
res:addheader("Content-Length", string.len(data))
end
for key, value in pairs(res.headers) do
-- send header
buf = buf .. key .. ": " .. value .. "\r\n"
end
buf = buf .. "\r\n"
-- write body if relevant
if data then
buf = buf .. data
buf = buf .. "\r\n"
end
-- send
conn:send(buf)
buf = nil
-- Remove default sent callback
conn:on("sent", nil)
-- Send last chunk and close connection
conn:on("sent", function(conn)
-- Close connection
conn:on('sent', nil)
-- Call termination callback
res:close()
end
)
conn:send("0\r\n\r\n")
end