aboutsummaryrefslogtreecommitdiff
path: root/src/ftp.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/ftp.lua')
-rw-r--r--src/ftp.lua26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/ftp.lua b/src/ftp.lua
index c83c1cb..5e1b685 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -40,7 +40,7 @@ function open(server, port, create)
40 local f = base.setmetatable({ tp = tp }, metat) 40 local f = base.setmetatable({ tp = tp }, metat)
41 -- make sure everything gets closed in an exception 41 -- make sure everything gets closed in an exception
42 f.try = socket.newtry(function() f:close() end) 42 f.try = socket.newtry(function() f:close() end)
43 return f 43 return f
44end 44end
45 45
46function metat.__index:portconnect() 46function metat.__index:portconnect()
@@ -71,20 +71,20 @@ function metat.__index:pasv()
71 local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" 71 local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)"
72 local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) 72 local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern))
73 self.try(a and b and c and d and p1 and p2, reply) 73 self.try(a and b and c and d and p1 and p2, reply)
74 self.pasvt = { 74 self.pasvt = {
75 ip = string.format("%d.%d.%d.%d", a, b, c, d), 75 ip = string.format("%d.%d.%d.%d", a, b, c, d),
76 port = p1*256 + p2 76 port = p1*256 + p2
77 } 77 }
78 if self.server then 78 if self.server then
79 self.server:close() 79 self.server:close()
80 self.server = nil 80 self.server = nil
81 end 81 end
82 return self.pasvt.ip, self.pasvt.port 82 return self.pasvt.ip, self.pasvt.port
83end 83end
84 84
85function metat.__index:port(ip, port) 85function metat.__index:port(ip, port)
86 self.pasvt = nil 86 self.pasvt = nil
87 if not ip then 87 if not ip then
88 ip, port = self.try(self.tp:getcontrol():getsockname()) 88 ip, port = self.try(self.tp:getcontrol():getsockname())
89 self.server = self.try(socket.bind(ip, 0)) 89 self.server = self.try(socket.bind(ip, 0))
90 ip, port = self.try(self.server:getsockname()) 90 ip, port = self.try(self.server:getsockname())
@@ -100,11 +100,11 @@ end
100 100
101function metat.__index:send(sendt) 101function metat.__index:send(sendt)
102 self.try(self.pasvt or self.server, "need port or pasv first") 102 self.try(self.pasvt or self.server, "need port or pasv first")
103 -- if there is a pasvt table, we already sent a PASV command 103 -- if there is a pasvt table, we already sent a PASV command
104 -- we just get the data connection into self.data 104 -- we just get the data connection into self.data
105 if self.pasvt then self:pasvconnect() end 105 if self.pasvt then self:pasvconnect() end
106 -- get the transfer argument and command 106 -- get the transfer argument and command
107 local argument = sendt.argument or 107 local argument = sendt.argument or
108 url.unescape(string.gsub(sendt.path or "", "^[/\\]", "")) 108 url.unescape(string.gsub(sendt.path or "", "^[/\\]", ""))
109 if argument == "" then argument = nil end 109 if argument == "" then argument = nil end
110 local command = sendt.command or "stor" 110 local command = sendt.command or "stor"
@@ -137,7 +137,7 @@ end
137function metat.__index:receive(recvt) 137function metat.__index:receive(recvt)
138 self.try(self.pasvt or self.server, "need port or pasv first") 138 self.try(self.pasvt or self.server, "need port or pasv first")
139 if self.pasvt then self:pasvconnect() end 139 if self.pasvt then self:pasvconnect() end
140 local argument = recvt.argument or 140 local argument = recvt.argument or
141 url.unescape(string.gsub(recvt.path or "", "^[/\\]", "")) 141 url.unescape(string.gsub(recvt.path or "", "^[/\\]", ""))
142 if argument == "" then argument = nil end 142 if argument == "" then argument = nil end
143 local command = recvt.command or "retr" 143 local command = recvt.command or "retr"
@@ -220,7 +220,7 @@ local function parse(u)
220 socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'") 220 socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'")
221 socket.try(t.host, "missing hostname") 221 socket.try(t.host, "missing hostname")
222 local pat = "^type=(.)$" 222 local pat = "^type=(.)$"
223 if t.params then 223 if t.params then
224 t.type = socket.skip(2, string.find(t.params, pat)) 224 t.type = socket.skip(2, string.find(t.params, pat))
225 socket.try(t.type == "a" or t.type == "i", 225 socket.try(t.type == "a" or t.type == "i",
226 "invalid type '" .. t.type .. "'") 226 "invalid type '" .. t.type .. "'")
@@ -229,7 +229,7 @@ local function parse(u)
229end 229end
230 230
231local function sput(u, body) 231local function sput(u, body)
232 local putt = parse(u) 232 local putt = parse(u)
233 putt.source = ltn12.source.string(body) 233 putt.source = ltn12.source.string(body)
234 return tput(putt) 234 return tput(putt)
235end 235end
@@ -253,7 +253,7 @@ local function tget(gett)
253end 253end
254 254
255local function sget(u) 255local function sget(u)
256 local gett = parse(u) 256 local gett = parse(u)
257 local t = {} 257 local t = {}
258 gett.sink = ltn12.sink.table(t) 258 gett.sink = ltn12.sink.table(t)
259 tget(gett) 259 tget(gett)