From 601ad8d59f11d7180015d0ecfb9d0a8d67f6f5c1 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Fri, 18 Mar 2022 12:12:39 +0100 Subject: refactor: Address issues raised by linter --- src/ftp.lua | 14 +++++++------- src/http.lua | 11 ++++------- src/ltn12.lua | 3 +-- src/mbox.lua | 13 +++++++------ src/mime.lua | 12 ++---------- src/url.lua | 6 +++--- 6 files changed, 24 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/ftp.lua b/src/ftp.lua index bd528ca..0ebc508 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -56,7 +56,7 @@ end function metat.__index:login(user, password) self.try(self.tp:command("user", user or _M.USER)) - local code, reply = self.try(self.tp:check{"2..", 331}) + local code, _ = self.try(self.tp:check{"2..", 331}) if code == 331 then self.try(self.tp:command("pass", password or _M.PASSWORD)) self.try(self.tp:check("2..")) @@ -66,7 +66,7 @@ end function metat.__index:pasv() self.try(self.tp:command("pasv")) - local code, reply = self.try(self.tp:check("2..")) + local _, reply = self.try(self.tp:check("2..")) local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) self.try(a and b and c and d and p1 and p2, reply) @@ -83,9 +83,9 @@ end function metat.__index:epsv() self.try(self.tp:command("epsv")) - local code, reply = self.try(self.tp:check("229")) + local _, reply = self.try(self.tp:check("229")) local pattern = "%((.)(.-)%1(.-)%1(.-)%1%)" - local d, prt, address, port = string.match(reply, pattern) + local _, _, _, port = string.match(reply, pattern) self.try(port, "invalid epsv response") self.pasvt = { address = self.tp:getpeername(), @@ -102,7 +102,7 @@ end function metat.__index:port(address, port) self.pasvt = nil if not address then - address, port = self.try(self.tp:getsockname()) + address = self.try(self.tp:getsockname()) self.server = self.try(socket.bind(address, 0)) address, port = self.try(self.server:getsockname()) self.try(self.server:settimeout(_M.TIMEOUT)) @@ -118,7 +118,7 @@ end function metat.__index:eprt(family, address, port) self.pasvt = nil if not address then - address, port = self.try(self.tp:getsockname()) + address = self.try(self.tp:getsockname()) self.server = self.try(socket.bind(address, 0)) address, port = self.try(self.server:getsockname()) self.try(self.server:settimeout(_M.TIMEOUT)) @@ -142,7 +142,7 @@ function metat.__index:send(sendt) local command = sendt.command or "stor" -- send the transfer command and check the reply self.try(self.tp:command(command, argument)) - local code, reply = self.try(self.tp:check{"2..", "1.."}) + local code, _ = self.try(self.tp:check{"2..", "1.."}) -- if there is not a pasvt table, then there is a server -- and we already sent a PORT command if not self.pasvt then self:portconnect() end diff --git a/src/http.lua b/src/http.lua index 6a3416e..e3a1742 100644 --- a/src/http.lua +++ b/src/http.lua @@ -41,9 +41,6 @@ local SCHEMES = { https.tcp, 'LuaSocket: Function tcp() not available from LuaSec') return tcp(t) end }} --- default scheme and port for document retrieval -local SCHEME = 'http' -local PORT = SCHEMES[SCHEME].port ----------------------------------------------------------------------------- -- Reads MIME headers from a connection, unfolding where needed ----------------------------------------------------------------------------- @@ -92,7 +89,7 @@ socket.sourcet["http-chunked"] = function(sock, headers) -- was it the last chunk? if size > 0 then -- if not, get chunk and skip terminating CRLF - local chunk, err, part = sock:receive(size) + local chunk, err, _ = sock:receive(size) if chunk then sock:receive() end return chunk, err else @@ -166,8 +163,8 @@ function metat.__index:receivestatusline() if status ~= "HTTP/" then if ec == "timeout" then return 408 - end - return nil, status + end + return nil, status end -- otherwise proceed reading a status line status = self.try(self.c:receive("*l", status)) @@ -366,7 +363,7 @@ end local headers -- ignore any 100-continue messages while code == 100 do - headers = h:receiveheaders() + h:receiveheaders() code, status = h:receivestatusline() end headers = h:receiveheaders() diff --git a/src/ltn12.lua b/src/ltn12.lua index afa735d..f1e05e1 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -13,7 +13,7 @@ local unpack = unpack or table.unpack local base = _G local _M = {} if module then -- heuristic for exporting a global package table - ltn12 = _M + ltn12 = _M -- luacheck: ignore end local filter,source,sink,pump = {},{},{},{} @@ -23,7 +23,6 @@ _M.sink = sink _M.pump = pump local unpack = unpack or table.unpack -local select = base.select -- 2048 seems to be better in windows... _M.BLOCKSIZE = 2048 diff --git a/src/mbox.lua b/src/mbox.lua index ed9e781..12823b0 100644 --- a/src/mbox.lua +++ b/src/mbox.lua @@ -1,8 +1,8 @@ local _M = {} if module then - mbox = _M -end + mbox = _M -- luacheck: ignore +end function _M.split_message(message_s) local message = {} @@ -29,7 +29,7 @@ end function _M.parse_header(header_s) header_s = string.gsub(header_s, "\n[ ]+", " ") header_s = string.gsub(header_s, "\n+", "") - local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") + local _, _, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") return name, value end @@ -49,9 +49,9 @@ function _M.parse_headers(headers_s) end function _M.parse_from(from) - local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") + local _, _, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") if not address then - _, __, address = string.find(from, "%s*(.+)%s*") + _, _, address = string.find(from, "%s*(.+)%s*") end name = name or "" address = address or "" @@ -63,7 +63,8 @@ end function _M.split_mbox(mbox_s) local mbox = {} mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" - local nj, i, j = 1, 1, 1 + local nj, i + local j = 1 while 1 do i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) if not i then break end diff --git a/src/mime.lua b/src/mime.lua index d3abac5..93539de 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -10,7 +10,6 @@ local base = _G local ltn12 = require("ltn12") local mime = require("mime.core") -local string = require("string") local _M = mime -- encode, decode and wrap algorithm tables @@ -18,7 +17,7 @@ local encodet, decodet, wrapt = {},{},{} _M.encodet = encodet _M.decodet = decodet -_M.wrapt = wrapt +_M.wrapt = wrapt -- creates a function that chooses a filter by name from a given table local function choose(table) @@ -27,7 +26,7 @@ local function choose(table) name, opt1, opt2 = "default", name, opt1 end local f = table[name or "nil"] - if not f then + if not f then base.error("unknown key (" .. base.tostring(name) .. ")", 3) else return f(opt1, opt2) end end @@ -52,13 +51,6 @@ decodet['quoted-printable'] = function() return ltn12.filter.cycle(_M.unqp, "") end -local function format(chunk) - if chunk then - if chunk == "" then return "''" - else return string.len(chunk) end - else return "nil" end -end - -- define the line-wrap filters wrapt['text'] = function(length) length = length or 76 diff --git a/src/url.lua b/src/url.lua index 0a3a80a..8e0dc5c 100644 --- a/src/url.lua +++ b/src/url.lua @@ -179,9 +179,9 @@ function _M.parse(url, default) function(u) parsed.userinfo = u; return "" end) authority = string.gsub(authority, ":([^:%]]*)$", function(p) parsed.port = p; return "" end) - if authority ~= "" then + if authority ~= "" then -- IPv6? - parsed.host = string.match(authority, "^%[(.+)%]$") or authority + parsed.host = string.match(authority, "^%[(.+)%]$") or authority end local userinfo = parsed.userinfo if not userinfo then return parsed end @@ -264,7 +264,7 @@ function _M.absolute(base_url, relative_url) relative_parsed.query = base_parsed.query end end - else + else relative_parsed.path = absolute_path(base_parsed.path or "", relative_parsed.path) end -- cgit v1.2.3-55-g6feb