Skip to content

Commit 6347862

Browse files
committed
Call close() in send callback
1 parent 6f1f2ca commit 6347862

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

httpserver.lua

Lines changed: 15 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
@@ -107,8 +106,7 @@ local function onConnect(connection)
107106
if #uri < 30 and file.open(uri .. ".gz", "r") then
108107
headers = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\n"
109108
elseif not file.open(uri, "r") then
110-
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n")
111-
connection:close()
109+
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n", close)
112110
table.remove(requests, 1)
113111
nextFile()
114112
return
@@ -141,11 +139,9 @@ local function onConnect(connection)
141139
nextFile()
142140
end
143141
elseif method == "OPTIONS" then
144-
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")
145-
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)
146143
else
147-
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n")
148-
connection:close()
144+
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n", close)
149145
end
150146
end
151147

0 commit comments

Comments
 (0)