@@ -12,15 +12,18 @@ local requests = {}
1212
1313local 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\n Connection: close\r\n\r\n Create error\n " )
23- connection :close ()
26+ connection :send (" HTTP/1.1 500 Error\r\n Connection: close\r\n\r\n Create error\n " , close )
2427 else
2528 -- Track Content-Length so we know when we're done
2629 local contentLength = tonumber (request :match (" \r\n Content%-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\n Connection: close\r\n\r\n Saved\n " )
47- connection : close ()
49+ connection :send (" HTTP/1.1 200 OK\r\n Connection: close\r\n\r\n Saved\n " , close )
50+ done ()
4851 end
4952 else
50- connection :send (" HTTP/1.1 500 Error\r\n Connection: close\r\n\r\n Write error\n " )
51- connection : close ()
53+ connection :send (" HTTP/1.1 500 Error\r\n Connection: close\r\n\r\n Write 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\n Connection: close\r\n\r\n Deleted\n " )
69- connection :close ()
71+ connection :send (" HTTP/1.1 200 OK\r\n Connection: close\r\n\r\n Deleted\n " , close )
7072 elseif method == " POST" then
7173 if uri == " " then
7274 -- Reboot the device
73- connection :send (" HTTP/1.1 200 OK\r\n Connection: close\r\n\r\n Rebooting\n " )
74- connection :close ()
75- node .restart ()
75+ connection :send (" HTTP/1.1 200 OK\r\n Connection: close\r\n\r\n Rebooting\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\n Connection: close\r\n\r\n " )
85- connection :close ()
84+ connection :send (" HTTP/1.1 200 OK\r\n Connection: 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\n Content-Encoding: gzip\r\n Connection: close\r\n\r\n "
108108 elseif not file .open (uri , " r" ) then
109- connection :send (" HTTP/1.1 404 Not Found\r\n Connection: close\r\n\r\n File not found\n " )
110- connection :close ()
109+ connection :send (" HTTP/1.1 404 Not Found\r\n Connection: close\r\n\r\n File 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\n Access-Control-Allow-Origin: *\r\n Access-Control-Allow-Methods: POST,OPTIONS\r\n Connection: close\r\n\r\n " )
144- connection :close ()
142+ connection :send (" HTTP/1.1 200 OK\r\n Access-Control-Allow-Origin: *\r\n Access-Control-Allow-Methods: POST,OPTIONS\r\n Connection: close\r\n\r\n " , close )
145143 else
146- connection :send (" HTTP/1.1 501 Not Implemented\r\n Connection: close\r\n\r\n " )
147- connection :close ()
144+ connection :send (" HTTP/1.1 501 Not Implemented\r\n Connection: close\r\n\r\n " , close )
148145 end
149146 end
150147
0 commit comments