Skip to content

Commit

Permalink
Merge pull request #2 from LiLMiPMaP/soundswarm
Browse files Browse the repository at this point in the history
Optimize memory; call connection:close() in send() callback
  • Loading branch information
lionello authored Mar 19, 2017
2 parents 30acae1 + 6347862 commit 6997c81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
35 changes: 16 additions & 19 deletions httpserver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ local requests = {}

local function onConnect(connection)

local function close()
connection:close()
end

local function onReceive(connection, request)
collectgarbage()
local method, uri = request:match("^([A-Z]+) /([^?]*).- HTTP/[1-9]+.[0-9]+\r\n")

if method == "PUT" and uri ~= "" then
file.close()
if not file.open("temp/put", "w") then
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nCreate error\n")
connection:close()
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nCreate error\n", close)
else
-- Track Content-Length so we know when we're done
local contentLength = tonumber(request:match("\r\nContent%-Length: (%S+)\r\n"))
Expand All @@ -43,12 +46,12 @@ local function onConnect(connection)
if file.write(payload) then
contentLength = contentLength - #payload
if contentLength <= 0 then
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nSaved\n")
connection:close()
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nSaved\n", close)
done()
end
else
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nWrite error\n")
connection:close()
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nWrite error\n", close)
done()
end
end
connection:on("receive", writeFile)
Expand All @@ -65,14 +68,11 @@ local function onConnect(connection)
elseif method == "DELETE" then
-- Delete the file from flash
file.remove(uri)
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nDeleted\n")
connection:close()
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nDeleted\n", close)
elseif method == "POST" then
if uri == "" then
-- Reboot the device
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRebooting\n")
connection:close()
node.restart()
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRebooting\n", node.restart)
else
-- Poor man's CGI
local func = dofile(uri)
Expand All @@ -81,8 +81,7 @@ local function onConnect(connection)
-- If you want your function to receive any other packets as well, do this:
--connection:on("receive", func)
else
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
connection:close()
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", close)
end
end
elseif method == "GET" then
Expand All @@ -97,6 +96,7 @@ local function onConnect(connection)
end

local function nextFile()
collectgarbage()
if #requests == 0 then return end
local connection, uri = unpack(requests[1])

Expand All @@ -106,8 +106,7 @@ local function onConnect(connection)
if #uri < 30 and file.open(uri .. ".gz", "r") then
headers = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\n"
elseif not file.open(uri, "r") then
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n")
connection:close()
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n", close)
table.remove(requests, 1)
nextFile()
return
Expand Down Expand Up @@ -140,11 +139,9 @@ local function onConnect(connection)
nextFile()
end
elseif method == "OPTIONS" then
connection:send("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST,OPTIONS\r\nConnection: close\r\n\r\n")
connection:close()
connection:send("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST,OPTIONS\r\nConnection: close\r\n\r\n", close)
else
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n")
connection:close()
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n", close)
end
end

Expand Down
1 change: 1 addition & 0 deletions up
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python nodemcu-uploader/nodemcu-uploader.py upload init.lua httpserver.lua
python nodemcu-uploader/nodemcu-uploader.py node restart

0 comments on commit 6997c81

Please sign in to comment.