Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient static serving #123

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpserver-connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- flush() and for closing the connection.
-- Author: Philip Gladstone, Marcos Kirsch

BufferedConnection = {}
local BufferedConnection = {}

-- parameter is the nodemcu-firmware connection
function BufferedConnection:new(connection)
Expand Down
8 changes: 5 additions & 3 deletions httpserver-static.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

return function (connection, req, args)
dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
connection:flush()
coroutine.yield()
-- Send file in little chunks
local bytesRemaining = file.list()[args.file]
-- Chunks larger than 1024 don't work.
Expand All @@ -13,9 +15,9 @@ return function (connection, req, args)
while bytesRemaining > 0 do
local bytesToRead = 0
if bytesRemaining > chunkSize then bytesToRead = chunkSize else bytesToRead = bytesRemaining end
local chunk = fileHandle:read(bytesToRead)
connection:send(chunk)
bytesRemaining = bytesRemaining - #chunk
connection.connection:send(fileHandle:read(bytesToRead))
coroutine.yield()
bytesRemaining = bytesRemaining - bytesToRead
--print(args.file .. ": Sent "..#chunk.. " bytes, " .. bytesRemaining .. " to go.")
chunk = nil
collectgarbage()
Expand Down
4 changes: 4 additions & 0 deletions httpserver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ return function (port)
-- nodemcu-firmware cannot handle long filenames.
uri.args = {code = 400, errorString = "Bad Request", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
req=nil
else
local fileExists = false

Expand All @@ -79,15 +80,18 @@ return function (port)
if not fileExists then
uri.args = {code = 404, errorString = "Not Found", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
req=nil
elseif uri.isScript then
fileServeFunction = dofile(uri.file)
else
if allowStatic[method] then
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
fileServeFunction = dofile("httpserver-static.lc")
req=nil
else
uri.args = {code = 405, errorString = "Method not supported", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
req=nil
end
end
end
Expand Down