fix http, updated ftp & smtp with similar create method call
This commit is contained in:
parent
01a2ee90a8
commit
9016e91238
3 changed files with 16 additions and 10 deletions
14
src/ftp.lua
14
src/ftp.lua
|
@ -34,8 +34,9 @@ _M.PASSWORD = "anonymous@anonymous.org"
|
|||
-----------------------------------------------------------------------------
|
||||
local metat = { __index = {} }
|
||||
|
||||
function _M.open(server, port, create)
|
||||
local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create))
|
||||
function _M.open(params)
|
||||
local c = function() return params:create() end -- wrap create as a method call
|
||||
local tp = socket.try(tp.connect(params.server, params.port or _M.PORT, _M.TIMEOUT, c))
|
||||
local f = base.setmetatable({ tp = tp }, metat)
|
||||
-- make sure everything gets closed in an exception
|
||||
f.try = socket.newtry(function() f:close() end)
|
||||
|
@ -203,7 +204,7 @@ end
|
|||
local function tput(putt)
|
||||
putt = override(putt)
|
||||
socket.try(putt.host, "missing hostname")
|
||||
local f = _M.open(putt.host, putt.port, putt.create)
|
||||
local f = _M.open(putt)
|
||||
f:greet()
|
||||
f:login(putt.user, putt.password)
|
||||
if putt.type then f:type(putt.type) end
|
||||
|
@ -235,7 +236,7 @@ end
|
|||
local function tget(gett)
|
||||
gett = override(gett)
|
||||
socket.try(gett.host, "missing hostname")
|
||||
local f = _M.open(gett.host, gett.port, gett.create)
|
||||
local f = _M.open(gett)
|
||||
f:greet()
|
||||
f:login(gett.user, gett.password)
|
||||
if gett.type then f:type(gett.type) end
|
||||
|
@ -262,7 +263,8 @@ _M.command = socket.protect(function(cmdt)
|
|||
cmdt = override(cmdt)
|
||||
socket.try(cmdt.host, "missing hostname")
|
||||
socket.try(cmdt.command, "missing command")
|
||||
local f = _M.open(cmdt.host, cmdt.port, cmdt.create)
|
||||
cmdt.create = cmdt.create or socket.tcp
|
||||
local f = _M.open(cmdt)
|
||||
f:greet()
|
||||
f:login(cmdt.user, cmdt.password)
|
||||
f.try(f.tp:command(cmdt.command, cmdt.argument))
|
||||
|
@ -277,6 +279,7 @@ _M.put = socket.protect(function(putt, body)
|
|||
tput(putt)
|
||||
return table.concat(putt.target)
|
||||
else
|
||||
putt.create = putt.create or socket.tcp
|
||||
return tput(putt)
|
||||
end
|
||||
end)
|
||||
|
@ -287,6 +290,7 @@ _M.get = socket.protect(function(gett)
|
|||
tget(gett)
|
||||
return table.concat(gett.target)
|
||||
else
|
||||
gett.create = gett.create or socket.tcp
|
||||
return tget(gett)
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -355,7 +355,7 @@ _M.request = socket.protect(function(reqt, body)
|
|||
local t, code, headers, status = reqt.target, socket.skip(1, trequest(reqt))
|
||||
return table.concat(t), code, headers, status
|
||||
else
|
||||
if not reqt.create then reqt.create = socket.tcp() end -- set default create method
|
||||
reqt.create = reqt.create or socket.tcp
|
||||
return trequest(reqt)
|
||||
end
|
||||
end)
|
||||
|
|
10
src/smtp.lua
10
src/smtp.lua
|
@ -113,9 +113,10 @@ function metat.__index:send(mailt)
|
|||
self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
|
||||
end
|
||||
|
||||
function _M.open(server, port, create)
|
||||
local tp = socket.try(tp.connect(server or _M.SERVER, port or _M.PORT,
|
||||
_M.TIMEOUT, create))
|
||||
function _M.open(mailt)
|
||||
local c = function() return mailt:create() end -- wrap to do a method call
|
||||
local tp = socket.try(tp.connect(mailt.server or _M.SERVER, mailt.port or _M.PORT,
|
||||
_M.TIMEOUT, c))
|
||||
local s = base.setmetatable({tp = tp}, metat)
|
||||
-- make sure tp is closed if we get an exception
|
||||
s.try = socket.newtry(function()
|
||||
|
@ -245,7 +246,8 @@ end
|
|||
-- High level SMTP API
|
||||
-----------------------------------------------------------------------------
|
||||
_M.send = socket.protect(function(mailt)
|
||||
local s = _M.open(mailt.server, mailt.port, mailt.create)
|
||||
mailt.create = mailt.create or socket.tcp
|
||||
local s = _M.open(mailt)
|
||||
local ext = s:greet(mailt.domain)
|
||||
s:auth(mailt.user, mailt.password, ext)
|
||||
s:send(mailt)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue