nodemcu-firmware/lua_modules/http/httpserver.lua

218 lines
6.4 KiB
Lua
Raw Permalink Normal View History

2015-01-19 18:12:06 +01:00
------------------------------------------------------------------------------
-- HTTP server module
--
-- LICENCE: http://opensource.org/licenses/MIT
-- Vladimir Dronnikov <dronnikov@gmail.com>
------------------------------------------------------------------------------
local collectgarbage, tonumber, tostring = collectgarbage, tonumber, tostring
local http
do
------------------------------------------------------------------------------
-- request methods
------------------------------------------------------------------------------
local make_req = function(conn, method, url)
return {
2015-01-19 18:12:06 +01:00
conn = conn,
method = method,
url = url,
}
end
------------------------------------------------------------------------------
-- response methods
------------------------------------------------------------------------------
local make_res = function(csend, cfini)
local send = function(self, data, status)
2015-01-19 18:12:06 +01:00
-- TODO: req.send should take care of response headers!
if self.send_header then
csend("HTTP/1.1 ")
csend(tostring(status or 200))
2015-01-19 18:12:06 +01:00
-- TODO: real HTTP status code/name table
csend(" OK\r\n")
2015-01-19 18:12:06 +01:00
-- we use chunked transfer encoding, to not deal with Content-Length:
-- response header
self:send_header("Transfer-Encoding", "chunked")
-- TODO: send standard response headers, such as Server:, Date:
end
if data then
-- NB: no headers allowed after response body started
if self.send_header then
self.send_header = nil
-- end response headers
csend("\r\n")
2015-01-19 18:12:06 +01:00
end
-- chunked transfer encoding
csend(("%X\r\n"):format(#data))
csend(data)
csend("\r\n")
2015-01-19 18:12:06 +01:00
end
end
2019-06-14 08:26:19 +02:00
local send_header = function(_, name, value)
2015-01-19 18:12:06 +01:00
-- NB: quite a naive implementation
csend(name)
csend(": ")
csend(value)
csend("\r\n")
end
-- finalize request, optionally sending data
local finish = function(self, data, status)
-- NB: res.send takes care of response headers
2015-01-19 18:12:06 +01:00
if data then
self:send(data, status)
end
-- finalize chunked transfer encoding
csend("0\r\n\r\n")
2015-01-19 18:12:06 +01:00
-- close connection
cfini()
end
--
local res = { }
2015-01-19 18:12:06 +01:00
res.send_header = send_header
res.send = send
res.finish = finish
return res
end
------------------------------------------------------------------------------
2015-01-19 18:12:06 +01:00
-- HTTP parser
------------------------------------------------------------------------------
local http_handler = function(handler)
return function(conn)
local csend = (require "fifosock").wrap(conn)
2019-06-14 08:26:19 +02:00
local req, res
local buf = ""
local method, url
local ondisconnect = function(connection)
connection:on("receive", nil)
connection:on("disconnection", nil)
connection:on("sent", nil)
collectgarbage("collect")
end
local cfini = function()
2019-06-14 08:26:19 +02:00
csend(function()
conn:on("sent", nil)
conn:close()
ondisconnect(conn)
2019-06-14 08:26:19 +02:00
end)
end
2019-06-14 08:26:19 +02:00
2015-01-19 18:12:06 +01:00
-- header parser
local cnt_len = 0
2019-06-14 08:26:19 +02:00
local onheader = function(_, k, v)
2015-01-19 18:12:06 +01:00
-- TODO: look for Content-Type: header
-- to help parse body
-- parse content length to know body length
if k == "content-length" then
cnt_len = tonumber(v)
end
if k == "expect" and v == "100-continue" then
csend("HTTP/1.1 100 Continue\r\n")
2015-01-19 18:12:06 +01:00
end
-- delegate to request object
if req and req.onheader then
req:onheader(k, v)
end
end
2019-06-14 08:26:19 +02:00
2015-01-19 18:12:06 +01:00
-- body data handler
local body_len = 0
2019-06-14 08:26:19 +02:00
local ondata = function(_, chunk)
2015-01-19 18:12:06 +01:00
-- feed request data to request handler
if not req or not req.ondata then return end
req:ondata(chunk)
-- NB: once length of seen chunks equals Content-Length:
-- ondata(conn) is called
2015-01-19 18:12:06 +01:00
body_len = body_len + #chunk
-- print("-B", #chunk, body_len, cnt_len, node.heap())
if body_len >= cnt_len then
req:ondata()
end
end
2019-06-14 08:26:19 +02:00
local onreceive = function(connection, chunk)
2015-01-19 18:12:06 +01:00
-- merge chunks in buffer
if buf then
buf = buf .. chunk
else
buf = chunk
end
-- consume buffer line by line
while #buf > 0 do
-- extract line
local e = buf:find("\r\n", 1, true)
if not e then break end
local line = buf:sub(1, e - 1)
buf = buf:sub(e + 2)
-- method, url?
if not method then
2019-06-14 08:26:19 +02:00
do
local _
-- NB: just version 1.1 assumed
_, _, method, url = line:find("^([A-Z]+) (.-) HTTP/1.1$")
end
2015-01-19 18:12:06 +01:00
if method then
-- make request and response objects
req = make_req(connection, method, url)
res = make_res(csend, cfini)
2015-01-19 18:12:06 +01:00
end
-- spawn request handler
handler(req, res)
2015-01-19 18:12:06 +01:00
-- header line?
elseif #line > 0 then
-- parse header
local _, _, k, v = line:find("^([%w-]+):%s*(.+)")
-- header seems ok?
if k then
k = k:lower()
onheader(connection, k, v)
2015-01-19 18:12:06 +01:00
end
-- headers end
else
2019-06-14 08:26:19 +02:00
-- NB: we explicitly reassign receive handler so that
-- next received chunks go directly to body handler
connection:on("receive", ondata)
2015-01-19 18:12:06 +01:00
-- NB: we feed the rest of the buffer as starting chunk of body
ondata(connection, buf)
2015-01-19 18:12:06 +01:00
-- buffer no longer needed
buf = nil
-- parser done
break
end
end
end
2019-06-14 08:26:19 +02:00
2015-01-19 18:12:06 +01:00
conn:on("receive", onreceive)
conn:on("disconnection", ondisconnect)
end
end
------------------------------------------------------------------------------
-- HTTP server
------------------------------------------------------------------------------
local srv
local createServer = function(port, handler)
-- NB: only one server at a time
if srv then srv:close() end
srv = net.createServer(net.TCP, 15)
-- listen
srv:listen(port, http_handler(handler))
return srv
end
------------------------------------------------------------------------------
-- HTTP server methods
------------------------------------------------------------------------------
http = {
createServer = createServer,
}
end
return http