fix: fixed the memory leak
This commit is contained in:
parent
7830ae671e
commit
02e4720b3d
|
@ -48,7 +48,8 @@ do
|
||||||
csend("\r\n")
|
csend("\r\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local send_header = function(self, name, value) -- luacheck: ignore
|
|
||||||
|
local send_header = function(_, name, value)
|
||||||
-- NB: quite a naive implementation
|
-- NB: quite a naive implementation
|
||||||
csend(name)
|
csend(name)
|
||||||
csend(": ")
|
csend(": ")
|
||||||
|
@ -80,21 +81,28 @@ do
|
||||||
local http_handler = function(handler)
|
local http_handler = function(handler)
|
||||||
return function(conn)
|
return function(conn)
|
||||||
local csend = (require "fifosock").wrap(conn)
|
local csend = (require "fifosock").wrap(conn)
|
||||||
local cfini = function()
|
|
||||||
conn:on("receive", nil)
|
|
||||||
conn:on("disconnection", nil)
|
|
||||||
csend(function() conn:on("sent", nil) conn:close() end)
|
|
||||||
end
|
|
||||||
local req, res
|
local req, res
|
||||||
local buf = ""
|
local buf = ""
|
||||||
local method, url
|
local method, url
|
||||||
|
|
||||||
|
local cfini = function()
|
||||||
|
conn:on("receive", nil)
|
||||||
|
conn:on("disconnection", nil)
|
||||||
|
csend(function()
|
||||||
|
conn:on("sent", nil)
|
||||||
|
conn:close()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
local ondisconnect = function(connection)
|
local ondisconnect = function(connection)
|
||||||
connection:on("sent", nil)
|
connection:on("sent", nil)
|
||||||
collectgarbage("collect")
|
collectgarbage("collect")
|
||||||
end
|
end
|
||||||
-- header parser
|
-- header parser
|
||||||
local cnt_len = 0
|
local cnt_len = 0
|
||||||
local onheader = function(connection, k, v) -- luacheck: ignore
|
|
||||||
|
local onheader = function(_, k, v)
|
||||||
-- TODO: look for Content-Type: header
|
-- TODO: look for Content-Type: header
|
||||||
-- to help parse body
|
-- to help parse body
|
||||||
-- parse content length to know body length
|
-- parse content length to know body length
|
||||||
|
@ -109,9 +117,10 @@ do
|
||||||
req:onheader(k, v)
|
req:onheader(k, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- body data handler
|
-- body data handler
|
||||||
local body_len = 0
|
local body_len = 0
|
||||||
local ondata = function(connection, chunk) -- luacheck: ignore
|
local ondata = function(_, chunk)
|
||||||
-- feed request data to request handler
|
-- feed request data to request handler
|
||||||
if not req or not req.ondata then return end
|
if not req or not req.ondata then return end
|
||||||
req:ondata(chunk)
|
req:ondata(chunk)
|
||||||
|
@ -123,6 +132,7 @@ do
|
||||||
req:ondata()
|
req:ondata()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local onreceive = function(connection, chunk)
|
local onreceive = function(connection, chunk)
|
||||||
-- merge chunks in buffer
|
-- merge chunks in buffer
|
||||||
if buf then
|
if buf then
|
||||||
|
@ -139,9 +149,11 @@ do
|
||||||
buf = buf:sub(e + 2)
|
buf = buf:sub(e + 2)
|
||||||
-- method, url?
|
-- method, url?
|
||||||
if not method then
|
if not method then
|
||||||
local i, _ -- luacheck: ignore
|
do
|
||||||
|
local _
|
||||||
-- NB: just version 1.1 assumed
|
-- NB: just version 1.1 assumed
|
||||||
_, i, method, url = line:find("^([A-Z]+) (.-) HTTP/1.1$")
|
_, _, method, url = line:find("^([A-Z]+) (.-) HTTP/1.1$")
|
||||||
|
end
|
||||||
if method then
|
if method then
|
||||||
-- make request and response objects
|
-- make request and response objects
|
||||||
req = make_req(connection, method, url)
|
req = make_req(connection, method, url)
|
||||||
|
@ -160,18 +172,19 @@ do
|
||||||
end
|
end
|
||||||
-- headers end
|
-- headers end
|
||||||
else
|
else
|
||||||
|
-- NB: we explicitly reassign receive handler so that
|
||||||
|
-- next received chunks go directly to body handler
|
||||||
|
connection:on("receive", ondata)
|
||||||
-- NB: we feed the rest of the buffer as starting chunk of body
|
-- NB: we feed the rest of the buffer as starting chunk of body
|
||||||
ondata(connection, buf)
|
ondata(connection, buf)
|
||||||
-- buffer no longer needed
|
-- buffer no longer needed
|
||||||
buf = nil
|
buf = nil
|
||||||
-- NB: we explicitly reassign receive handler so that
|
|
||||||
-- next received chunks go directly to body handler
|
|
||||||
connection:on("receive", ondata)
|
|
||||||
-- parser done
|
-- parser done
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
conn:on("receive", onreceive)
|
conn:on("receive", onreceive)
|
||||||
conn:on("disconnection", ondisconnect)
|
conn:on("disconnection", ondisconnect)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue