File extension parsing fix

If a filename contains dots, extension was parsed as everything after
the first dot.
File extension must be what it is after the last dot.

Also includes a rewrited workaround for mimetypes if the requested file
gzip compressed.
This commit is contained in:
Hazar Karabay 2015-09-16 22:10:21 +03:00
parent 792c0814a5
commit b7f78481a2
4 changed files with 11 additions and 11 deletions

View File

@ -2,7 +2,7 @@
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
-- Author: Marcos Kirsch
return function (connection, code, extension)
return function (connection, code, extension, gzip)
local function getHTTPStatusString(code)
local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",}
@ -15,11 +15,6 @@ return function (connection, code, extension)
local gzip = false
-- 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"}
-- 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
return {contentType = contentType, gzip = gzip}
end
@ -27,7 +22,7 @@ return function (connection, code, extension)
local mimeType = getMimeType(extension)
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")
end
connection:send("Connection: close\r\n\r\n")

View File

@ -94,7 +94,12 @@ local function parseUri(uri)
filename,ext = filename:match("(.+)%.(.+)")
table.insert(fullExt,1,ext)
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.file = uriToFilename(r.file)
return r

View File

@ -3,7 +3,7 @@
-- Author: Marcos Kirsch
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)
-- Send file in little chunks
local continue = true

View File

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