Merge pull request #38 from hazarkarabay/master

File extension parsing, gzip detection and init.lua fix
This commit is contained in:
Marcos 2015-10-03 21:53:55 -05:00
commit cf0d777632
5 changed files with 12 additions and 12 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) return function (connection, code, extension, gzip)
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",}
@ -15,11 +15,6 @@ return function (connection, code, extension)
local gzip = false 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"}
-- add comressed flag if file ends with gz
if ext:find("%.gz$") then
ext = ext:sub(1, -4)
gzip = true
end
if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end
return {contentType = contentType, gzip = gzip} return {contentType = contentType, gzip = gzip}
end end
@ -27,7 +22,7 @@ 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") connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n")
if mimeType["gzip"] then if gzip then
connection:send("Content-Encoding: gzip\r\n") connection:send("Content-Encoding: gzip\r\n")
end end
connection:send("Connection: close\r\n\r\n") connection:send("Connection: close\r\n\r\n")

View File

@ -94,7 +94,12 @@ local function parseUri(uri)
filename,ext = filename:match("(.+)%.(.+)") filename,ext = filename:match("(.+)%.(.+)")
table.insert(fullExt,1,ext) table.insert(fullExt,1,ext)
end end
r.ext = table.concat(fullExt,".") if #fullExt > 1 and fullExt[#fullExt] == 'gz' then
r.ext = fullExt[#fullExt-1]
r.isGzipped = true
elseif #fullExt >= 1 then
r.ext = fullExt[#fullExt]
end
r.isScript = r.ext == "lua" or r.ext == "lc" r.isScript = r.ext == "lua" or r.ext == "lc"
r.file = uriToFilename(r.file) r.file = uriToFilename(r.file)
return r return r

View File

@ -3,7 +3,7 @@
-- Author: Marcos Kirsch -- Author: Marcos Kirsch
return function (connection, req, args) return function (connection, req, args)
dofile("httpserver-header.lc")(connection, 200, args.ext) dofile("httpserver-header.lc")(connection, 200, args.ext, args.gzipped)
--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

View File

@ -40,7 +40,7 @@ return function (port)
if fileExists then if fileExists then
print("gzip variant exists, serving that one") print("gzip variant exists, serving that one")
uri.file = uri.file .. ".gz" uri.file = uri.file .. ".gz"
uri.ext = uri.ext .. ".gz" uri.isGzipped = true
end end
end end
@ -51,7 +51,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} uri.args = {file = uri.file, ext = uri.ext, gzipped = 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"}

View File

@ -89,7 +89,7 @@ if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
end end
-- Uncomment to automatically start the server in port 80 -- Uncomment to automatically start the server in port 80
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()l) then if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
dofile("httpserver.lc")(80) dofile("httpserver.lc")(80)
end end