Skip to content

Commit 6997c81

Browse files
authored
Merge pull request #2 from LiLMiPMaP/soundswarm
Optimize memory; call connection:close() in send() callback
2 parents 30acae1 + 6347862 commit 6997c81

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

httpserver.lua

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ local requests = {}
1212

1313
local function onConnect(connection)
1414

15+
local function close()
16+
connection:close()
17+
end
18+
1519
local function onReceive(connection, request)
1620
collectgarbage()
1721
local method, uri = request:match("^([A-Z]+) /([^?]*).- HTTP/[1-9]+.[0-9]+\r\n")
1822

1923
if method == "PUT" and uri ~= "" then
2024
file.close()
2125
if not file.open("temp/put", "w") then
22-
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nCreate error\n")
23-
connection:close()
26+
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nCreate error\n", close)
2427
else
2528
-- Track Content-Length so we know when we're done
2629
local contentLength = tonumber(request:match("\r\nContent%-Length: (%S+)\r\n"))
@@ -43,12 +46,12 @@ local function onConnect(connection)
4346
if file.write(payload) then
4447
contentLength = contentLength - #payload
4548
if contentLength <= 0 then
46-
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nSaved\n")
47-
connection:close()
49+
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nSaved\n", close)
50+
done()
4851
end
4952
else
50-
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nWrite error\n")
51-
connection:close()
53+
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nWrite error\n", close)
54+
done()
5255
end
5356
end
5457
connection:on("receive", writeFile)
@@ -65,14 +68,11 @@ local function onConnect(connection)
6568
elseif method == "DELETE" then
6669
-- Delete the file from flash
6770
file.remove(uri)
68-
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nDeleted\n")
69-
connection:close()
71+
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nDeleted\n", close)
7072
elseif method == "POST" then
7173
if uri == "" then
7274
-- Reboot the device
73-
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRebooting\n")
74-
connection:close()
75-
node.restart()
75+
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRebooting\n", node.restart)
7676
else
7777
-- Poor man's CGI
7878
local func = dofile(uri)
@@ -81,8 +81,7 @@ local function onConnect(connection)
8181
-- If you want your function to receive any other packets as well, do this:
8282
--connection:on("receive", func)
8383
else
84-
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
85-
connection:close()
84+
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", close)
8685
end
8786
end
8887
elseif method == "GET" then
@@ -97,6 +96,7 @@ local function onConnect(connection)
9796
end
9897

9998
local function nextFile()
99+
collectgarbage()
100100
if #requests == 0 then return end
101101
local connection, uri = unpack(requests[1])
102102

@@ -106,8 +106,7 @@ local function onConnect(connection)
106106
if #uri < 30 and file.open(uri .. ".gz", "r") then
107107
headers = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\n"
108108
elseif not file.open(uri, "r") then
109-
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n")
110-
connection:close()
109+
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n", close)
111110
table.remove(requests, 1)
112111
nextFile()
113112
return
@@ -140,11 +139,9 @@ local function onConnect(connection)
140139
nextFile()
141140
end
142141
elseif method == "OPTIONS" then
143-
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")
144-
connection:close()
142+
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)
145143
else
146-
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n")
147-
connection:close()
144+
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n", close)
148145
end
149146
end
150147

up

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
python nodemcu-uploader/nodemcu-uploader.py upload init.lua httpserver.lua
2+
python nodemcu-uploader/nodemcu-uploader.py node restart
23

0 commit comments

Comments
 (0)