Several fixes and code cleanup. Gzipped files now work. Indentation cleaned up, Other small fixes:
This commit is contained in:
parent
11dde7075b
commit
2f2fb26782
@ -2,7 +2,7 @@
|
|||||||
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function (connection, code, extension, gzip)
|
return function (connection, code, extension, isGzipped)
|
||||||
|
|
||||||
local function getHTTPStatusString(code)
|
local function getHTTPStatusString(code)
|
||||||
local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",}
|
local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",}
|
||||||
@ -12,21 +12,21 @@ return function (connection, code, extension, gzip)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function getMimeType(ext)
|
local function getMimeType(ext)
|
||||||
local gzip = false
|
|
||||||
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
|
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
|
||||||
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json = "application/json", png = "image/png", xml = "text/xml"}
|
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json = "application/json", png = "image/png", xml = "text/xml"}
|
||||||
if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end
|
if mt[ext] then return mt[ext] else return "text/plain" end
|
||||||
return {contentType = contentType, gzip = gzip}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local mimeType = getMimeType(extension)
|
local mimeType = getMimeType(extension)
|
||||||
|
|
||||||
local header = "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 .. "\r\nnCache-Control: private, no-store\r\n"
|
||||||
if mimeType["gzip"] then header = header .. "Content-Encoding: gzip\r\n" end
|
if isGzipped then
|
||||||
|
header = header .. "Cache-Control: max-age=2592000\r\n"
|
||||||
|
header = header .. "Content-Encoding: gzip\r\n"
|
||||||
|
end
|
||||||
header = header .. "Connection: close\r\n\r\n"
|
header = header .. "Connection: close\r\n\r\n"
|
||||||
connection:send(header)
|
connection:send(header)
|
||||||
header = nil
|
header = nil
|
||||||
coroutine.yield()
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -32,45 +32,43 @@ local function parseArgs(args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function parseFormData(body)
|
local function parseFormData(body)
|
||||||
local data = {}
|
local data = {}
|
||||||
print("Parsing Form Data")
|
--print("Parsing Form Data")
|
||||||
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
|
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
|
||||||
local key, value = string.match(kv, "(.*)=(.*)")
|
local key, value = string.match(kv, "(.*)=(.*)")
|
||||||
|
--print("Parsed: " .. key .. " => " .. value)
|
||||||
print("Parsed: " .. key .. " => " .. value)
|
data[key] = uri_decode(value)
|
||||||
data[key] = uri_decode(value)
|
end
|
||||||
end
|
return data
|
||||||
|
|
||||||
return data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getRequestData(payload)
|
local function getRequestData(payload)
|
||||||
local requestData
|
local requestData
|
||||||
return function ()
|
return function ()
|
||||||
print("Getting Request Data")
|
--print("Getting Request Data")
|
||||||
if requestData then
|
if requestData then
|
||||||
return requestData
|
return requestData
|
||||||
else
|
|
||||||
local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)")
|
|
||||||
local body_start = payload:find("\r\n\r\n", 1, true)
|
|
||||||
local body = payload:sub(body_start, #payload)
|
|
||||||
payload = nil
|
|
||||||
collectgarbage()
|
|
||||||
|
|
||||||
-- print("mimeType = [" .. mimeType .. "]")
|
|
||||||
|
|
||||||
if mimeType == "application/json" then
|
|
||||||
print("JSON: " .. body)
|
|
||||||
requestData = cjson.decode(body)
|
|
||||||
elseif mimeType == "application/x-www-form-urlencoded" then
|
|
||||||
requestData = parseFormData(body)
|
|
||||||
else
|
else
|
||||||
requestData = {}
|
--print("payload = [" .. payload .. "]")
|
||||||
|
local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)")
|
||||||
|
local bodyStart = payload:find("\r\n\r\n", 1, true)
|
||||||
|
local body = payload:sub(bodyStart, #payload)
|
||||||
|
payload = nil
|
||||||
|
collectgarbage()
|
||||||
|
--print("mimeType = [" .. mimeType .. "]")
|
||||||
|
--print("bodyStart = [" .. bodyStart .. "]")
|
||||||
|
--print("body = [" .. body .. "]")
|
||||||
|
if mimeType == "application/json" then
|
||||||
|
--print("JSON: " .. body)
|
||||||
|
requestData = cjson.decode(body)
|
||||||
|
elseif mimeType == "application/x-www-form-urlencoded" then
|
||||||
|
requestData = parseFormData(body)
|
||||||
|
else
|
||||||
|
requestData = {}
|
||||||
|
end
|
||||||
|
return requestData
|
||||||
end
|
end
|
||||||
|
end
|
||||||
return requestData
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function parseUri(uri)
|
local function parseUri(uri)
|
||||||
|
|||||||
@ -3,8 +3,8 @@
|
|||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
dofile("httpserver-header.lc")(connection, 200, args.ext, args.gzipped)
|
|
||||||
--print("Begin sending:", args.file)
|
--print("Begin sending:", args.file)
|
||||||
|
dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
|
||||||
-- Send file in little chunks
|
-- Send file in little chunks
|
||||||
local continue = true
|
local continue = true
|
||||||
local size = file.list()[args.file]
|
local size = file.list()[args.file]
|
||||||
@ -24,10 +24,7 @@ return function (connection, req, args)
|
|||||||
bytesSent = bytesSent + #chunk
|
bytesSent = bytesSent + #chunk
|
||||||
chunk = nil
|
chunk = nil
|
||||||
--print("Sent: " .. bytesSent .. " of " .. size)
|
--print("Sent: " .. bytesSent .. " of " .. size)
|
||||||
-- nodemcu-firmware disallows queueing send operations,
|
if bytesSent == size then continue = false end
|
||||||
-- 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
|
||||||
|
|||||||
@ -13,47 +13,51 @@ return function (port)
|
|||||||
-- We do it in a separate thread because we need to yield when sending lots
|
-- We do it in a separate thread because we need to yield when sending lots
|
||||||
-- of data in order to avoid overflowing the mcu's buffer.
|
-- of data in order to avoid overflowing the mcu's buffer.
|
||||||
local connectionThread
|
local connectionThread
|
||||||
|
|
||||||
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
||||||
|
|
||||||
local function startServing(fileServeFunction, connection, req, args)
|
local function startServing(fileServeFunction, connection, req, args)
|
||||||
|
|
||||||
local bufferedConnection = {}
|
local bufferedConnection = {}
|
||||||
connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args)
|
connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args)
|
||||||
fileServeFunction(bconnection, req, args)
|
fileServeFunction(bconnection, req, args)
|
||||||
if not bconnection:flush() then
|
if not bconnection:flush() then
|
||||||
connection:close()
|
connection:close()
|
||||||
connectionThread = nil
|
connectionThread = nil
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
function bufferedConnection:flush()
|
|
||||||
if self.size > 0 then
|
function bufferedConnection:flush()
|
||||||
connection:send(table.concat(self.data, ""))
|
if self.size > 0 then
|
||||||
self.data = {}
|
connection:send(table.concat(self.data, ""))
|
||||||
self.size = 0
|
self.data = {}
|
||||||
return true
|
self.size = 0
|
||||||
end
|
return true
|
||||||
return false
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
function bufferedConnection:send(payload)
|
|
||||||
local l = payload:len()
|
function bufferedConnection:send(payload)
|
||||||
if l + self.size > 1000 then
|
local l = payload:len()
|
||||||
if self:flush() then
|
if l + self.size > 1000 then
|
||||||
coroutine.yield()
|
if self:flush() then
|
||||||
end
|
coroutine.yield()
|
||||||
end
|
end
|
||||||
if l > 800 then
|
end
|
||||||
connection:send(payload)
|
if l > 800 then
|
||||||
coroutine.yield()
|
connection:send(payload)
|
||||||
else
|
coroutine.yield()
|
||||||
table.insert(self.data, payload)
|
else
|
||||||
self.size = self.size + l
|
table.insert(self.data, payload)
|
||||||
end
|
self.size = self.size + l
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
bufferedConnection.size = 0
|
bufferedConnection.size = 0
|
||||||
bufferedConnection.data = {}
|
bufferedConnection.data = {}
|
||||||
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
||||||
if not status then
|
if not status then
|
||||||
print(err)
|
print("Error: ", err)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -62,9 +66,9 @@ return function (port)
|
|||||||
local method = req.method
|
local method = req.method
|
||||||
local uri = req.uri
|
local uri = req.uri
|
||||||
local fileServeFunction = nil
|
local fileServeFunction = nil
|
||||||
|
|
||||||
print("Method: " .. method);
|
--print("Method: " .. method);
|
||||||
|
|
||||||
if #(uri.file) > 32 then
|
if #(uri.file) > 32 then
|
||||||
-- nodemcu-firmware cannot handle long filenames.
|
-- nodemcu-firmware cannot handle long filenames.
|
||||||
uri.args = {code = 400, errorString = "Bad Request"}
|
uri.args = {code = 400, errorString = "Bad Request"}
|
||||||
@ -72,17 +76,17 @@ return function (port)
|
|||||||
else
|
else
|
||||||
local fileExists = file.open(uri.file, "r")
|
local fileExists = file.open(uri.file, "r")
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
if not fileExists then
|
|
||||||
-- gzip check
|
|
||||||
fileExists = file.open(uri.file .. ".gz", "r")
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
if fileExists then
|
if not fileExists then
|
||||||
print("gzip variant exists, serving that one")
|
-- gzip check
|
||||||
uri.file = uri.file .. ".gz"
|
fileExists = file.open(uri.file .. ".gz", "r")
|
||||||
uri.isGzipped = true
|
file.close()
|
||||||
end
|
|
||||||
|
if fileExists then
|
||||||
|
print("gzip variant exists, serving that one")
|
||||||
|
uri.file = uri.file .. ".gz"
|
||||||
|
uri.isGzipped = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not fileExists then
|
if not fileExists then
|
||||||
@ -92,11 +96,11 @@ return function (port)
|
|||||||
fileServeFunction = dofile(uri.file)
|
fileServeFunction = dofile(uri.file)
|
||||||
else
|
else
|
||||||
if allowStatic[method] then
|
if allowStatic[method] then
|
||||||
uri.args = {file = uri.file, ext = uri.ext, gzipped = uri.isGzipped}
|
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
|
||||||
fileServeFunction = dofile("httpserver-static.lc")
|
fileServeFunction = dofile("httpserver-static.lc")
|
||||||
else
|
else
|
||||||
uri.args = {code = 405, errorString = "Method not supported"}
|
uri.args = {code = 405, errorString = "Method not supported"}
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -136,7 +140,7 @@ return function (port)
|
|||||||
local function onSent(connection, payload)
|
local function onSent(connection, payload)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
if connectionThread then
|
if connectionThread then
|
||||||
local connectionThreadStatus = coroutine.status(connectionThread)
|
local connectionThreadStatus = coroutine.status(connectionThread)
|
||||||
if connectionThreadStatus == "suspended" then
|
if connectionThreadStatus == "suspended" then
|
||||||
-- Not finished sending file, resume.
|
-- Not finished sending file, resume.
|
||||||
local status, err = coroutine.resume(connectionThread)
|
local status, err = coroutine.resume(connectionThread)
|
||||||
@ -158,7 +162,7 @@ return function (port)
|
|||||||
connectionThread = nil
|
connectionThread = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user