Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
5e849af9
Commit
5e849af9
authored
Jun 14, 2019
by
hanfengcan
Committed by
Marcel Stör
Nov 07, 2020
Browse files
fix: fixed the memory leak
parent
ff778dfc
Changes
1
Show whitespace changes
Inline
Side-by-side
lua_modules/http/httpserver.lua
View file @
5e849af9
...
@@ -48,7 +48,8 @@ do
...
@@ -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
...
@@ -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
req
,
res
local
buf
=
""
local
method
,
url
local
cfini
=
function
()
local
cfini
=
function
()
conn
:
on
(
"receive"
,
nil
)
conn
:
on
(
"receive"
,
nil
)
conn
:
on
(
"disconnection"
,
nil
)
conn
:
on
(
"disconnection"
,
nil
)
csend
(
function
()
conn
:
on
(
"sent"
,
nil
)
conn
:
close
()
end
)
csend
(
function
()
conn
:
on
(
"sent"
,
nil
)
conn
:
close
()
end
)
end
end
local
req
,
res
local
buf
=
""
local
method
,
url
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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment