mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-07-04 18:14:30 +02:00
fix http, updated ftp & smtp with similar create method call
This commit is contained in:
parent
01a2ee90a8
commit
9016e91238
14
src/ftp.lua
14
src/ftp.lua
@ -34,8 +34,9 @@ _M.PASSWORD = "anonymous@anonymous.org"
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local metat = { __index = {} }
|
local metat = { __index = {} }
|
||||||
|
|
||||||
function _M.open(server, port, create)
|
function _M.open(params)
|
||||||
local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create))
|
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)
|
local f = base.setmetatable({ tp = tp }, metat)
|
||||||
-- make sure everything gets closed in an exception
|
-- make sure everything gets closed in an exception
|
||||||
f.try = socket.newtry(function() f:close() end)
|
f.try = socket.newtry(function() f:close() end)
|
||||||
@ -203,7 +204,7 @@ end
|
|||||||
local function tput(putt)
|
local function tput(putt)
|
||||||
putt = override(putt)
|
putt = override(putt)
|
||||||
socket.try(putt.host, "missing hostname")
|
socket.try(putt.host, "missing hostname")
|
||||||
local f = _M.open(putt.host, putt.port, putt.create)
|
local f = _M.open(putt)
|
||||||
f:greet()
|
f:greet()
|
||||||
f:login(putt.user, putt.password)
|
f:login(putt.user, putt.password)
|
||||||
if putt.type then f:type(putt.type) end
|
if putt.type then f:type(putt.type) end
|
||||||
@ -235,7 +236,7 @@ end
|
|||||||
local function tget(gett)
|
local function tget(gett)
|
||||||
gett = override(gett)
|
gett = override(gett)
|
||||||
socket.try(gett.host, "missing hostname")
|
socket.try(gett.host, "missing hostname")
|
||||||
local f = _M.open(gett.host, gett.port, gett.create)
|
local f = _M.open(gett)
|
||||||
f:greet()
|
f:greet()
|
||||||
f:login(gett.user, gett.password)
|
f:login(gett.user, gett.password)
|
||||||
if gett.type then f:type(gett.type) end
|
if gett.type then f:type(gett.type) end
|
||||||
@ -262,7 +263,8 @@ _M.command = socket.protect(function(cmdt)
|
|||||||
cmdt = override(cmdt)
|
cmdt = override(cmdt)
|
||||||
socket.try(cmdt.host, "missing hostname")
|
socket.try(cmdt.host, "missing hostname")
|
||||||
socket.try(cmdt.command, "missing command")
|
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:greet()
|
||||||
f:login(cmdt.user, cmdt.password)
|
f:login(cmdt.user, cmdt.password)
|
||||||
f.try(f.tp:command(cmdt.command, cmdt.argument))
|
f.try(f.tp:command(cmdt.command, cmdt.argument))
|
||||||
@ -277,6 +279,7 @@ _M.put = socket.protect(function(putt, body)
|
|||||||
tput(putt)
|
tput(putt)
|
||||||
return table.concat(putt.target)
|
return table.concat(putt.target)
|
||||||
else
|
else
|
||||||
|
putt.create = putt.create or socket.tcp
|
||||||
return tput(putt)
|
return tput(putt)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@ -287,6 +290,7 @@ _M.get = socket.protect(function(gett)
|
|||||||
tget(gett)
|
tget(gett)
|
||||||
return table.concat(gett.target)
|
return table.concat(gett.target)
|
||||||
else
|
else
|
||||||
|
gett.create = gett.create or socket.tcp
|
||||||
return tget(gett)
|
return tget(gett)
|
||||||
end
|
end
|
||||||
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))
|
local t, code, headers, status = reqt.target, socket.skip(1, trequest(reqt))
|
||||||
return table.concat(t), code, headers, status
|
return table.concat(t), code, headers, status
|
||||||
else
|
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)
|
return trequest(reqt)
|
||||||
end
|
end
|
||||||
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)
|
self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.open(server, port, create)
|
function _M.open(mailt)
|
||||||
local tp = socket.try(tp.connect(server or _M.SERVER, port or _M.PORT,
|
local c = function() return mailt:create() end -- wrap to do a method call
|
||||||
_M.TIMEOUT, create))
|
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)
|
local s = base.setmetatable({tp = tp}, metat)
|
||||||
-- make sure tp is closed if we get an exception
|
-- make sure tp is closed if we get an exception
|
||||||
s.try = socket.newtry(function()
|
s.try = socket.newtry(function()
|
||||||
@ -245,7 +246,8 @@ end
|
|||||||
-- High level SMTP API
|
-- High level SMTP API
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
_M.send = socket.protect(function(mailt)
|
_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)
|
local ext = s:greet(mailt.domain)
|
||||||
s:auth(mailt.user, mailt.password, ext)
|
s:auth(mailt.user, mailt.password, ext)
|
||||||
s:send(mailt)
|
s:send(mailt)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user