Skip to content

Commit

Permalink
LUA: reduce file IO cpu overhead and properly close the file
Browse files Browse the repository at this point in the history
  • Loading branch information
magicrub committed Feb 6, 2024
1 parent 4d540d7 commit 2540e67
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions ROMFS/scripts/maim_web.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,25 @@ local function update() -- this is the loop which periodically runs
path = request_uri
end

-- send response
local file = io.open("./scripts" .. path, "r")
if not file then
file = io.open("@ROMFS/scripts" .. path, "r")
end
if not file then
file = io.open("ROMFS/scripts" .. path, "r")
end
if string.find(path, ".html") ~= nil then
-- send html file if exists in one of these directories
local file = io.open("@ROMFS/scripts" .. path, "r") -- scenario for STM32 /ROMFS/scripts
if not file then
file = io.open("ROMFS/scripts" .. path, "r") -- scenario for SITL /ROMFS/scripts
end
if not file then
file = io.open("./scripts" .. path, "r")
end

-- send html file if exists
if file then
if path:match(".html") then
if file then
send_str("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
send_str(file:read("*a"))
file:close()
else
-- send 404 if file not found
send_str("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
send_str("<html><body><h1>404 Not Found</h1></body></html>")
end
file:close()

elseif path == "/zeroize" then
mod_payload:set_zeroize()
Expand Down

0 comments on commit 2540e67

Please sign in to comment.