Several fixes and code cleanup. Gzipped files now work. Indentation cleaned up, Other small fixes:

This commit is contained in:
Marcos Kirsch 2016-02-15 22:52:30 -06:00
parent 11dde7075b
commit 2f2fb26782
4 changed files with 92 additions and 93 deletions

View File

@ -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

View File

@ -33,41 +33,39 @@ 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 else
--print("payload = [" .. payload .. "]")
local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)") local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)")
local body_start = payload:find("\r\n\r\n", 1, true) local bodyStart = payload:find("\r\n\r\n", 1, true)
local body = payload:sub(body_start, #payload) local body = payload:sub(bodyStart, #payload)
payload = nil payload = nil
collectgarbage() collectgarbage()
--print("mimeType = [" .. mimeType .. "]") --print("mimeType = [" .. mimeType .. "]")
--print("bodyStart = [" .. bodyStart .. "]")
--print("body = [" .. body .. "]")
if mimeType == "application/json" then if mimeType == "application/json" then
print("JSON: " .. body) --print("JSON: " .. body)
requestData = cjson.decode(body) requestData = cjson.decode(body)
elseif mimeType == "application/x-www-form-urlencoded" then elseif mimeType == "application/x-www-form-urlencoded" then
requestData = parseFormData(body) requestData = parseFormData(body)
else else
requestData = {} requestData = {}
end end
return requestData return requestData
end end
end end

View File

@ -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

View File

@ -17,6 +17,7 @@ return function (port)
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)
@ -25,6 +26,7 @@ return function (port)
connectionThread = nil connectionThread = nil
end end
end) end)
function bufferedConnection:flush() function bufferedConnection:flush()
if self.size > 0 then if self.size > 0 then
connection:send(table.concat(self.data, "")) connection:send(table.concat(self.data, ""))
@ -34,6 +36,7 @@ return function (port)
end end
return false return false
end end
function bufferedConnection:send(payload) function bufferedConnection:send(payload)
local l = payload:len() local l = payload:len()
if l + self.size > 1000 then if l + self.size > 1000 then
@ -49,11 +52,12 @@ return function (port)
self.size = self.size + l self.size = self.size + l
end 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
@ -63,7 +67,7 @@ return function (port)
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.
@ -92,7 +96,7 @@ 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"}