nodemcu-httpserver/httpserver-request.lua
Fractal147 9f4d7a9988 Fixed unnecessary globals as in issue #113. (#122)
* Fixed global assignment that should be local

Made result variable be local, see Issue #113

* Made global variable local

Made ASCII variable be local, see Issue #113

* Made more variables local

Related to Issue #113. questionMarkPos, and b,c,d,e,f all are global in scope, and are not cleared from memory, so leak.
Frankly, b, c, d, e, and f are not used either, but will now get GC'd later, if they ever were assigned, so not problematic
line 114 also has _ and i to make local too, so were put on their own line.
i on line 24 also was unnecessarily global, and undetected in issue #113

* Made module more local

Made the basicAuth table local in scope. Since it is returned when dofile is called in httpserver.lua, that already has a correctly scoped table, 'auth'. This is related to issue #113, and should reduce memory loss to globals

* Made bufferedConnection local

bufferedConnection was global and didn't have to be. Part of issue #113.
Now no longer remains in _G (globals table) after a connection has closed.
2018-01-14 22:03:37 -06:00

131 lines
4.0 KiB
Lua

-- httpserver-request
-- Part of nodemcu-httpserver, parses incoming client requests.
-- Author: Marcos Kirsch
local function validateMethod(method)
local httpMethods = {GET=true, HEAD=true, POST=true, PUT=true, DELETE=true, TRACE=true, OPTIONS=true, CONNECT=true, PATCH=true}
-- default for non-existent attributes returns nil, which evaluates to false
return httpMethods[method]
end
local function uriToFilename(uri)
return "http/" .. string.sub(uri, 2, -1)
end
local function hex_to_char(x)
return string.char(tonumber(x, 16))
end
local function uri_decode(input)
return input:gsub("%+", " "):gsub("%%(%x%x)", hex_to_char)
end
local function parseArgs(args)
local r = {}
local i = 1
if args == nil or args == "" then return r end
for arg in string.gmatch(args, "([^&]+)") do
local name, value = string.match(arg, "(.*)=(.*)")
if name ~= nil then r[name] = uri_decode(value) end
i = i + 1
end
return r
end
local function parseFormData(body)
local data = {}
--print("Parsing Form Data")
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
local key, value = string.match(kv, "(.*)=(.*)")
--print("Parsed: " .. key .. " => " .. value)
data[key] = uri_decode(value)
end
return data
end
local function getRequestData(payload)
local requestData
return function ()
--print("Getting Request Data")
-- for backward compatibility before v2.1
if (sjson == nil) then
sjson = cjson
end
if requestData then
return requestData
else
--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 = sjson.decode(body)
elseif mimeType == "application/x-www-form-urlencoded" then
requestData = parseFormData(body)
else
requestData = {}
end
return requestData
end
end
end
local function parseUri(uri)
local r = {}
local filename
local ext
local fullExt = {}
if uri == nil then return r end
if uri == "/" then uri = "/index.html" end
local questionMarkPos, b, c, d, e, f = uri:find("?")
if questionMarkPos == nil then
r.file = uri:sub(1, questionMarkPos)
r.args = {}
else
r.file = uri:sub(1, questionMarkPos - 1)
r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
end
filename = r.file
while filename:match("%.") do
filename,ext = filename:match("(.+)%.(.+)")
table.insert(fullExt,1,ext)
end
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
end
-- Parses the client's request. Returns a dictionary containing pretty much everything
-- the server needs to know about the uri.
return function (request)
--print("Request: \n", request)
local e = request:find("\r\n", 1, true)
if not e then return nil end
local line = request:sub(1, e - 1)
local r = {}
local _, i
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
if not (r.method and r.request) then
--print("invalid request: ")
--print(request)
return nil
end
r.methodIsValid = validateMethod(r.method)
r.uri = parseUri(r.request)
r.getRequestData = getRequestData(request)
return r
end