Browse Source

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.
Fractal147 6 years ago
parent
commit
9f4d7a9988
4 changed files with 8 additions and 6 deletions
  1. 2 2
      httpserver-b64decode.lua
  2. 1 1
      httpserver-basicauth.lua
  3. 1 1
      httpserver-connection.lua
  4. 4 2
      httpserver-request.lua

+ 2 - 2
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

+ 1 - 1
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.

+ 1 - 1
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)

+ 4 - 2
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: ")