From 9f4d7a9988d843591668f8cbbcd1f435fa98078d Mon Sep 17 00:00:00 2001 From: Fractal147 <35138372+Fractal147@users.noreply.github.com> Date: Mon, 15 Jan 2018 04:03:37 +0000 Subject: [PATCH] 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. --- httpserver-b64decode.lua | 4 ++-- httpserver-basicauth.lua | 2 +- httpserver-connection.lua | 2 +- httpserver-request.lua | 6 ++++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/httpserver-b64decode.lua b/httpserver-b64decode.lua index a79581e..73c4d58 100755 --- a/httpserver-b64decode.lua +++ b/httpserver-b64decode.lua @@ -37,14 +37,14 @@ end -- logic OR for number values local function lor(x,y) - result = 0 + local result = 0 for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and uipow(2, (p-1)) or 0) end return result end -- Character decoding table local function toBase64Byte(char) - ascii = string.byte(char, 1) + local ascii = string.byte(char, 1) if ascii >= string.byte('A', 1) and ascii <= string.byte('Z', 1) then return ascii - string.byte('A', 1) elseif ascii >= string.byte('a', 1) and ascii <= string.byte('z', 1) then return ascii - string.byte('a', 1) + 26 elseif ascii >= string.byte('0', 1) and ascii <= string.byte('9', 1) then return ascii + 4 diff --git a/httpserver-basicauth.lua b/httpserver-basicauth.lua index 42109ec..863b34c 100644 --- a/httpserver-basicauth.lua +++ b/httpserver-basicauth.lua @@ -2,7 +2,7 @@ -- Part of nodemcu-httpserver, authenticates a user using http basic auth. -- Author: Sam Dieck -basicAuth = {} +local basicAuth = {} -- Returns true if the user/password match one of the users/passwords in httpserver-conf.lua. -- Returns false otherwise. diff --git a/httpserver-connection.lua b/httpserver-connection.lua index 7026482..52aee69 100644 --- a/httpserver-connection.lua +++ b/httpserver-connection.lua @@ -5,7 +5,7 @@ -- flush() and for closing the connection. -- Author: Philip Gladstone, Marcos Kirsch -BufferedConnection = {} +local BufferedConnection = {} -- parameter is the nodemcu-firmware connection function BufferedConnection:new(connection) diff --git a/httpserver-request.lua b/httpserver-request.lua index 3a7d56b..a186969 100644 --- a/httpserver-request.lua +++ b/httpserver-request.lua @@ -21,7 +21,8 @@ local function uri_decode(input) end local function parseArgs(args) - local r = {}; i=1 + 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, "(.*)=(.*)") @@ -83,7 +84,7 @@ local function parseUri(uri) if uri == nil then return r end if uri == "/" then uri = "/index.html" end - questionMarkPos, b, c, d, e, f = uri:find("?") + local questionMarkPos, b, c, d, e, f = uri:find("?") if questionMarkPos == nil then r.file = uri:sub(1, questionMarkPos) r.args = {} @@ -115,6 +116,7 @@ return function (request) 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: ")