diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-28 06:16:43 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-28 06:16:43 +0000 |
commit | 694edcc3c1ac3041ff8cdd753a2f7894e8783e6c (patch) | |
tree | f8797b4c7671fbf9042fc011277d69d4c3549045 /src/ftp.lua | |
parent | bf738a03368b8de9c574d9631f131c5a520acf7b (diff) | |
download | luasocket-694edcc3c1ac3041ff8cdd753a2f7894e8783e6c.tar.gz luasocket-694edcc3c1ac3041ff8cdd753a2f7894e8783e6c.tar.bz2 luasocket-694edcc3c1ac3041ff8cdd753a2f7894e8783e6c.zip |
Committing with require.
Diffstat (limited to 'src/ftp.lua')
-rw-r--r-- | src/ftp.lua | 89 |
1 files changed, 76 insertions, 13 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 306b77f..efb872a 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
@@ -10,6 +10,11 @@ if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end | |||
10 | -- get LuaSocket namespace | 10 | -- get LuaSocket namespace |
11 | local socket = _G[LUASOCKET_LIBNAME] | 11 | local socket = _G[LUASOCKET_LIBNAME] |
12 | if not socket then error('module requires LuaSocket') end | 12 | if not socket then error('module requires LuaSocket') end |
13 | |||
14 | -- require other modules | ||
15 | require("ltn12") | ||
16 | require("url") | ||
17 | |||
13 | -- create namespace inside LuaSocket namespace | 18 | -- create namespace inside LuaSocket namespace |
14 | socket.ftp = socket.ftp or {} | 19 | socket.ftp = socket.ftp or {} |
15 | -- make all module globals fall into namespace | 20 | -- make all module globals fall into namespace |
@@ -25,6 +30,7 @@ TIMEOUT = 60 | |||
25 | PORT = 21 | 30 | PORT = 21 |
26 | -- this is the default anonymous password. used when no password is | 31 | -- this is the default anonymous password. used when no password is |
27 | -- provided in url. should be changed to your e-mail. | 32 | -- provided in url. should be changed to your e-mail. |
33 | USER = "ftp" | ||
28 | EMAIL = "anonymous@anonymous.org" | 34 | EMAIL = "anonymous@anonymous.org" |
29 | -- block size used in transfers | 35 | -- block size used in transfers |
30 | BLOCKSIZE = 2048 | 36 | BLOCKSIZE = 2048 |
@@ -48,21 +54,20 @@ local function pasv(pasvt) | |||
48 | end | 54 | end |
49 | 55 | ||
50 | function metat.__index:login(user, password) | 56 | function metat.__index:login(user, password) |
51 | socket.try(self.tp:command("USER", user)) | 57 | socket.try(self.tp:command("user", user or USER)) |
52 | local code, reply = socket.try(self.tp:check{"2..", 331}) | 58 | local code, reply = socket.try(self.tp:check{"2..", 331}) |
53 | if code == 331 then | 59 | if code == 331 then |
54 | socket.try(password, reply) | 60 | socket.try(self.tp:command("pass", password or EMAIL)) |
55 | socket.try(self.tp:command("PASS", password)) | ||
56 | socket.try(self.tp:check("2..")) | 61 | socket.try(self.tp:check("2..")) |
57 | end | 62 | end |
58 | return 1 | 63 | return 1 |
59 | end | 64 | end |
60 | 65 | ||
61 | function metat.__index:pasv() | 66 | function metat.__index:pasv() |
62 | socket.try(self.tp:command("PASV")) | 67 | socket.try(self.tp:command("pasv")) |
63 | local code, reply = socket.try(self.tp:check("2..")) | 68 | local code, reply = socket.try(self.tp:check("2..")) |
64 | local _, _, a, b, c, d, p1, p2 = | 69 | local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" |
65 | string.find(reply, "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)") | 70 | local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) |
66 | socket.try(a and b and c and d and p1 and p2, reply) | 71 | socket.try(a and b and c and d and p1 and p2, reply) |
67 | self.pasvt = { | 72 | self.pasvt = { |
68 | ip = string.format("%d.%d.%d.%d", a, b, c, d), | 73 | ip = string.format("%d.%d.%d.%d", a, b, c, d), |
@@ -97,7 +102,7 @@ function metat.__index:send(sendt) | |||
97 | local data | 102 | local data |
98 | socket.try(self.pasvt or self.portt, "need port or pasv first") | 103 | socket.try(self.pasvt or self.portt, "need port or pasv first") |
99 | if self.pasvt then data = socket.try(pasv(self.pasvt)) end | 104 | if self.pasvt then data = socket.try(pasv(self.pasvt)) end |
100 | socket.try(self.tp:command(sendt.command, sendt.argument)) | 105 | socket.try(self.tp:command(sendt.command or "stor", sendt.argument)) |
101 | local code, reply = socket.try(self.tp:check{"2..", "1.."}) | 106 | local code, reply = socket.try(self.tp:check{"2..", "1.."}) |
102 | if self.portt then data = socket.try(port(self.portt)) end | 107 | if self.portt then data = socket.try(port(self.portt)) end |
103 | local step = sendt.step or ltn12.pump.step | 108 | local step = sendt.step or ltn12.pump.step |
@@ -124,7 +129,7 @@ function metat.__index:receive(recvt) | |||
124 | local data | 129 | local data |
125 | socket.try(self.pasvt or self.portt, "need port or pasv first") | 130 | socket.try(self.pasvt or self.portt, "need port or pasv first") |
126 | if self.pasvt then data = socket.try(pasv(self.pasvt)) end | 131 | if self.pasvt then data = socket.try(pasv(self.pasvt)) end |
127 | socket.try(self.tp:command(recvt.command, recvt.argument)) | 132 | socket.try(self.tp:command(recvt.command or "retr", recvt.argument)) |
128 | local code = socket.try(self.tp:check{"1..", "2.."}) | 133 | local code = socket.try(self.tp:check{"1..", "2.."}) |
129 | if self.portt then data = socket.try(port(self.portt)) end | 134 | if self.portt then data = socket.try(port(self.portt)) end |
130 | local source = socket.source("until-closed", data) | 135 | local source = socket.source("until-closed", data) |
@@ -140,13 +145,13 @@ function metat.__index:receive(recvt) | |||
140 | end | 145 | end |
141 | 146 | ||
142 | function metat.__index:cwd(dir) | 147 | function metat.__index:cwd(dir) |
143 | socket.try(self.tp:command("CWD", dir)) | 148 | socket.try(self.tp:command("cwd", dir)) |
144 | socket.try(self.tp:check(250)) | 149 | socket.try(self.tp:check(250)) |
145 | return 1 | 150 | return 1 |
146 | end | 151 | end |
147 | 152 | ||
148 | function metat.__index:type(type) | 153 | function metat.__index:type(type) |
149 | socket.try(self.tp:command("TYPE", type)) | 154 | socket.try(self.tp:command("type", type)) |
150 | socket.try(self.tp:check(200)) | 155 | socket.try(self.tp:check(200)) |
151 | return 1 | 156 | return 1 |
152 | end | 157 | end |
@@ -158,7 +163,7 @@ function metat.__index:greet() | |||
158 | end | 163 | end |
159 | 164 | ||
160 | function metat.__index:quit() | 165 | function metat.__index:quit() |
161 | socket.try(self.tp:command("QUIT")) | 166 | socket.try(self.tp:command("quit")) |
162 | socket.try(self.tp:check("2..")) | 167 | socket.try(self.tp:check("2..")) |
163 | return 1 | 168 | return 1 |
164 | end | 169 | end |
@@ -171,11 +176,69 @@ end | |||
171 | ----------------------------------------------------------------------------- | 176 | ----------------------------------------------------------------------------- |
172 | -- High level FTP API | 177 | -- High level FTP API |
173 | ----------------------------------------------------------------------------- | 178 | ----------------------------------------------------------------------------- |
179 | local function tput(putt) | ||
180 | local ftp = socket.ftp.open(putt.host, putt.port) | ||
181 | ftp:greet() | ||
182 | ftp:login(putt.user, putt.password) | ||
183 | if putt.type then ftp:type(putt.type) end | ||
184 | ftp:pasv() | ||
185 | ftp:send(putt) | ||
186 | ftp:quit() | ||
187 | return ftp:close() | ||
188 | end | ||
174 | 189 | ||
175 | function put(putt) | 190 | local default = { |
191 | path = "/", | ||
192 | scheme = "ftp" | ||
193 | } | ||
194 | |||
195 | local function parse(url) | ||
196 | local putt = socket.try(socket.url.parse(url, default)) | ||
197 | socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'") | ||
198 | socket.try(putt.host, "invalid host") | ||
199 | local pat = "^type=(.)$" | ||
200 | if putt.params then | ||
201 | putt.type = socket.skip(2, string.find(putt.params, pat)) | ||
202 | socket.try(putt.type == "a" or putt.type == "i") | ||
203 | end | ||
204 | -- skip first backslash in path | ||
205 | putt.argument = string.sub(putt.path, 2) | ||
206 | return putt | ||
176 | end | 207 | end |
177 | 208 | ||
178 | function get(gett) | 209 | local function sput(url, body) |
210 | local putt = parse(url) | ||
211 | putt.source = ltn12.source.string(body) | ||
212 | return tput(putt) | ||
179 | end | 213 | end |
180 | 214 | ||
215 | put = socket.protect(function(putt, body) | ||
216 | if type(putt) == "string" then return sput(putt, body) | ||
217 | else return tput(putt) end | ||
218 | end) | ||
219 | |||
220 | local function tget(gett) | ||
221 | local ftp = socket.ftp.open(gett.host, gett.port) | ||
222 | ftp:greet() | ||
223 | ftp:login(gett.user, gett.password) | ||
224 | if gett.type then ftp:type(gett.type) end | ||
225 | ftp:pasv() | ||
226 | ftp:receive(gett) | ||
227 | ftp:quit() | ||
228 | return ftp:close() | ||
229 | end | ||
230 | |||
231 | local function sget(url, body) | ||
232 | local gett = parse(url) | ||
233 | local t = {} | ||
234 | gett.sink = ltn12.sink.table(t) | ||
235 | tget(gett) | ||
236 | return table.concat(t) | ||
237 | end | ||
238 | |||
239 | get = socket.protect(function(gett) | ||
240 | if type(gett) == "string" then return sget(gett, body) | ||
241 | else return tget(gett) end | ||
242 | end) | ||
243 | |||
181 | return ftp | 244 | return ftp |