Fix bugs for newer nodemcu-firmware: new firmware does not allow queuing multiple connection:send() operations. These changes ensure we yield after every send except the last one.
This commit is contained in:
parent
37e58389c6
commit
1b14a516aa
@ -4,16 +4,20 @@
|
|||||||
|
|
||||||
return function (connection, args)
|
return function (connection, args)
|
||||||
|
|
||||||
local function sendHeader(connection, code, errorString, extraHeaders, mimeType)
|
local function getHeader(connection, code, errorString, extraHeaders, mimeType)
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
|
local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n"
|
||||||
for i, header in ipairs(extraHeaders) do
|
for i, extraHeader in ipairs(extraHeaders) do
|
||||||
connection:send(header .. "\r\n")
|
header = header .. extraHeader .. "\r\n"
|
||||||
end
|
end
|
||||||
connection:send("connection: close\r\n\r\n")
|
header = header .. "connection: close\r\n\r\n"
|
||||||
|
return header
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Error " .. args.code .. ": " .. args.errorString)
|
print("Error " .. args.code .. ": " .. args.errorString)
|
||||||
args.headers = args.headers or {}
|
args.headers = args.headers or {}
|
||||||
sendHeader(connection, args.code, args.errorString, args.headers, "text/html")
|
local html = getHeader(connection, args.code, args.errorString, args.headers, "text/html")
|
||||||
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
html = html .. "<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n"
|
||||||
|
connection:send(html)
|
||||||
|
html = nil
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -26,10 +26,12 @@ return function (connection, code, extension)
|
|||||||
|
|
||||||
local mimeType = getMimeType(extension)
|
local mimeType = getMimeType(extension)
|
||||||
|
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n")
|
local header = "HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n"
|
||||||
if mimeType["gzip"] then
|
if mimeType["gzip"] then header = header .. "Content-Encoding: gzip\r\n" end
|
||||||
connection:send("Content-Encoding: gzip\r\n")
|
header = header .. "Connection: close\r\n\r\n"
|
||||||
end
|
connection:send(header)
|
||||||
connection:send("Connection: close\r\n\r\n")
|
header = nil
|
||||||
|
coroutine.yield()
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,9 @@ return function (connection, args)
|
|||||||
--print("Begin sending:", args.file)
|
--print("Begin sending:", args.file)
|
||||||
-- Send file in little chunks
|
-- Send file in little chunks
|
||||||
local continue = true
|
local continue = true
|
||||||
|
local size = file.list()[args.file]
|
||||||
local bytesSent = 0
|
local bytesSent = 0
|
||||||
|
local chunkSize = 1024 -- @TODO: can chunkSize be larger?
|
||||||
while continue do
|
while continue do
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
-- NodeMCU file API lets you open 1 file at a time.
|
-- NodeMCU file API lets you open 1 file at a time.
|
||||||
@ -15,17 +17,17 @@ return function (connection, args)
|
|||||||
-- to support multiple simultaneous clients.
|
-- to support multiple simultaneous clients.
|
||||||
file.open(args.file)
|
file.open(args.file)
|
||||||
file.seek("set", bytesSent)
|
file.seek("set", bytesSent)
|
||||||
local chunk = file.read(256)
|
local chunk = file.read(chunkSize)
|
||||||
file.close()
|
file.close()
|
||||||
if chunk == nil then
|
|
||||||
continue = false
|
|
||||||
else
|
|
||||||
coroutine.yield()
|
|
||||||
connection:send(chunk)
|
connection:send(chunk)
|
||||||
bytesSent = bytesSent + #chunk
|
bytesSent = bytesSent + #chunk
|
||||||
chunk = nil
|
chunk = nil
|
||||||
--print("Sent" .. args.file, bytesSent)
|
--print("Sent: " .. bytesSent .. " of " .. size)
|
||||||
end
|
-- nodemcu-firmware disallows queueing send operations,
|
||||||
|
-- so we must call coroutine.yield() after every connection:send()
|
||||||
|
-- The onSent() will resume us. But only yield if we aren't done yet!
|
||||||
|
if bytesSent == size then continue = false else coroutine.yield() end
|
||||||
end
|
end
|
||||||
--print("Finished sending: ", args.file)
|
--print("Finished sending: ", args.file)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user