2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
-- FTP support for the Lua language
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Author: Diego Nehab
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Conforming to: RFC 959, LTN7
|
|
|
|
-- RCS ID: $Id$
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2001-09-12 20:27:55 +02:00
|
|
|
local Public, Private = {}, {}
|
2003-05-25 03:54:13 +02:00
|
|
|
local socket = _G[LUASOCKET_LIBNAME] -- get LuaSocket namespace
|
|
|
|
socket.ftp = Public -- create ftp sub namespace
|
2001-09-12 20:27:55 +02:00
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- timeout in seconds before the program gives up on a connection
|
2001-09-12 20:27:55 +02:00
|
|
|
Public.TIMEOUT = 60
|
2000-12-29 23:15:09 +01:00
|
|
|
-- default port for ftp service
|
2001-09-12 20:27:55 +02:00
|
|
|
Public.PORT = 21
|
2000-12-29 23:15:09 +01:00
|
|
|
-- this is the default anonymous password. used when no password is
|
2001-09-12 20:27:55 +02:00
|
|
|
-- provided in url. should be changed to your e-mail.
|
|
|
|
Public.EMAIL = "anonymous@anonymous.org"
|
2001-06-06 22:55:45 +02:00
|
|
|
-- block size used in transfers
|
2001-09-12 20:27:55 +02:00
|
|
|
Public.BLOCKSIZE = 8192
|
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2003-05-25 03:54:13 +02:00
|
|
|
-- Tries to get a pattern from the server and closes socket on error
|
|
|
|
-- sock: socket connected to the server
|
|
|
|
-- pattern: pattern to receive
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2003-05-25 03:54:13 +02:00
|
|
|
-- received pattern on success
|
|
|
|
-- nil followed by error message on error
|
2001-09-12 20:27:55 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2003-05-25 03:54:13 +02:00
|
|
|
function Private.try_receive(sock, pattern)
|
|
|
|
local data, err = sock:receive(pattern)
|
|
|
|
if not data then sock:close() end
|
|
|
|
return data, err
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2003-05-25 03:54:13 +02:00
|
|
|
-- Tries to send data to the server and closes socket on error
|
2001-09-12 20:27:55 +02:00
|
|
|
-- sock: socket connected to the server
|
2003-05-25 03:54:13 +02:00
|
|
|
-- data: data to send
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Returns
|
2003-05-25 03:54:13 +02:00
|
|
|
-- err: error message if any, nil if successfull
|
2001-09-12 20:27:55 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2003-05-25 03:54:13 +02:00
|
|
|
function Private.try_send(sock, data)
|
|
|
|
local sent, err = sock:send(data)
|
|
|
|
if not sent then sock:close() end
|
|
|
|
return err
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Tries to send DOS mode lines. Closes socket on error.
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- line: string to be sent
|
|
|
|
-- Returns
|
|
|
|
-- err: message in case of error, nil if successfull
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.try_sendline(sock, line)
|
|
|
|
return Private.try_send(sock, line .. "\r\n")
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Gets ip and port for data connection from PASV answer
|
|
|
|
-- Input
|
|
|
|
-- pasv: PASV command answer
|
|
|
|
-- Returns
|
|
|
|
-- ip: string containing ip for data connection
|
|
|
|
-- port: port for data connection
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.get_pasv(pasv)
|
|
|
|
local a, b, c, d, p1, p2, _
|
2000-12-29 23:15:09 +01:00
|
|
|
local ip, port
|
|
|
|
_,_, a, b, c, d, p1, p2 =
|
2003-03-20 01:24:44 +01:00
|
|
|
string.find(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
|
2001-09-12 20:27:55 +02:00
|
|
|
if not (a and b and c and d and p1 and p2) then return nil, nil end
|
2003-03-20 01:24:44 +01:00
|
|
|
ip = string.format("%d.%d.%d.%d", a, b, c, d)
|
2000-12-29 23:15:09 +01:00
|
|
|
port = tonumber(p1)*256 + tonumber(p2)
|
|
|
|
return ip, port
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends a FTP command through socket
|
|
|
|
-- Input
|
|
|
|
-- control: control connection socket
|
|
|
|
-- cmd: command
|
|
|
|
-- arg: command argument if any
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- error message in case of error, nil otherwise
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.send_command(control, cmd, arg)
|
|
|
|
local line
|
|
|
|
if arg then line = cmd .. " " .. arg
|
|
|
|
else line = cmd end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.try_sendline(control, line)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Gets FTP command answer, unfolding if neccessary
|
|
|
|
-- Input
|
|
|
|
-- control: control connection socket
|
|
|
|
-- Returns
|
|
|
|
-- answer: whole server reply, nil if error
|
|
|
|
-- code: answer status code or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.get_answer(control)
|
|
|
|
local code, lastcode, sep, _
|
2002-07-08 23:01:45 +02:00
|
|
|
local line, err = Private.try_receive(control)
|
2000-12-29 23:15:09 +01:00
|
|
|
local answer = line
|
|
|
|
if err then return nil, err end
|
2003-03-20 01:24:44 +01:00
|
|
|
_,_, code, sep = string.find(line, "^(%d%d%d)(.)")
|
2000-12-29 23:15:09 +01:00
|
|
|
if not code or not sep then return nil, answer end
|
|
|
|
if sep == "-" then -- answer is multiline
|
|
|
|
repeat
|
2002-07-08 23:01:45 +02:00
|
|
|
line, err = Private.try_receive(control)
|
2000-12-29 23:15:09 +01:00
|
|
|
if err then return nil, err end
|
2003-03-20 01:24:44 +01:00
|
|
|
_,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)")
|
2000-12-29 23:15:09 +01:00
|
|
|
answer = answer .. "\n" .. line
|
|
|
|
until code == lastcode and sep == " " -- answer ends with same code
|
|
|
|
end
|
|
|
|
return answer, tonumber(code)
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Checks if a message return is correct. Closes control connection if not.
|
|
|
|
-- Input
|
|
|
|
-- control: control connection socket
|
|
|
|
-- success: table with successfull reply status code
|
|
|
|
-- Returns
|
|
|
|
-- code: reply code or nil in case of error
|
|
|
|
-- answer: server complete answer or system error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.check_answer(control, success)
|
2002-07-08 23:01:45 +02:00
|
|
|
local answer, code = Private.get_answer(control)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not answer then return nil, code end
|
2000-12-29 23:15:09 +01:00
|
|
|
if type(success) ~= "table" then success = {success} end
|
2003-03-20 01:24:44 +01:00
|
|
|
for _, s in ipairs(success) do
|
|
|
|
if code == s then
|
2000-12-29 23:15:09 +01:00
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
control:close()
|
|
|
|
return nil, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Trys a command on control socked, in case of error, the control connection
|
|
|
|
-- is closed.
|
|
|
|
-- Input
|
|
|
|
-- control: control connection socket
|
|
|
|
-- cmd: command
|
|
|
|
-- arg: command argument or nil if no argument
|
|
|
|
-- success: table with successfull reply status code
|
|
|
|
-- Returns
|
|
|
|
-- code: reply code or nil in case of error
|
|
|
|
-- answer: server complete answer or system error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.command(control, cmd, arg, success)
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Private.send_command(control, cmd, arg)
|
2001-09-12 20:27:55 +02:00
|
|
|
if err then return nil, err end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(control, success)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Check server greeting
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-- Returns
|
|
|
|
-- code: nil if error
|
|
|
|
-- answer: server answer or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.greet(control)
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.check_answer(control, {120, 220})
|
2000-12-29 23:15:09 +01:00
|
|
|
if code == 120 then -- please try again, somewhat busy now...
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(control, {220})
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Log in on server
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-- user: user name
|
2001-09-12 20:27:55 +02:00
|
|
|
-- password: user password if any
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
|
|
|
-- code: nil if error
|
|
|
|
-- answer: server answer or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.login(control, user, password)
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.command(control, "user", user, {230, 331})
|
2001-09-12 20:27:55 +02:00
|
|
|
if code == 331 and password then -- need pass and we have pass
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.command(control, "pass", password, {230, 202})
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Change to target directory
|
|
|
|
-- Input
|
|
|
|
-- control: socket for control connection with server
|
2001-01-25 23:03:16 +01:00
|
|
|
-- path: directory to change to
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
|
|
|
-- code: nil if error
|
|
|
|
-- answer: server answer or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.cwd(control, path)
|
2002-07-08 23:01:45 +02:00
|
|
|
if path then return Private.command(control, "cwd", path, {250})
|
2001-09-12 20:27:55 +02:00
|
|
|
else return 250, nil end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-01-25 23:03:16 +01:00
|
|
|
-- Change to target directory
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-01-25 23:03:16 +01:00
|
|
|
-- control: socket for control connection with server
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-01-25 23:03:16 +01:00
|
|
|
-- server: server socket bound to local address, nil if error
|
|
|
|
-- answer: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.port(control)
|
2001-01-25 23:03:16 +01:00
|
|
|
local code, answer
|
|
|
|
local server, ctl_ip
|
|
|
|
ctl_ip, answer = control:getsockname()
|
2003-03-20 01:24:44 +01:00
|
|
|
server, answer = socket.bind(ctl_ip, 0)
|
2003-08-16 02:06:04 +02:00
|
|
|
server:settimeout(Public.TIMEOUT)
|
2001-01-25 23:03:16 +01:00
|
|
|
local ip, p, ph, pl
|
|
|
|
ip, p = server:getsockname()
|
2003-03-20 01:24:44 +01:00
|
|
|
pl = math.mod(p, 256)
|
2001-01-25 23:03:16 +01:00
|
|
|
ph = (p - pl)/256
|
2003-03-20 01:24:44 +01:00
|
|
|
local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",")
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.command(control, "port", arg, {200})
|
2001-01-25 23:03:16 +01:00
|
|
|
if not code then
|
|
|
|
server:close()
|
|
|
|
return nil, answer
|
|
|
|
else return server end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Closes control connection with server
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-- Returns
|
|
|
|
-- code: nil if error
|
|
|
|
-- answer: server answer or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.logout(control)
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.command(control, "quit", nil, {221})
|
2001-09-12 20:27:55 +02:00
|
|
|
if code then control:close() end
|
2000-12-29 23:15:09 +01:00
|
|
|
return code, answer
|
|
|
|
end
|
|
|
|
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Receives data and send it to a callback
|
|
|
|
-- Input
|
|
|
|
-- data: data connection
|
|
|
|
-- callback: callback to return file contents
|
|
|
|
-- Returns
|
|
|
|
-- nil if successfull, or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.receive_indirect(data, callback)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, err, res
|
|
|
|
while not err do
|
2002-07-08 23:01:45 +02:00
|
|
|
chunk, err = Private.try_receive(data, Public.BLOCKSIZE)
|
2001-06-06 22:55:45 +02:00
|
|
|
if err == "closed" then err = "done" end
|
|
|
|
res = callback(chunk, err)
|
|
|
|
if not res then break end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Retrieves file or directory listing
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
2001-01-25 23:03:16 +01:00
|
|
|
-- server: server socket bound to local address
|
2001-09-12 20:27:55 +02:00
|
|
|
-- name: file name
|
|
|
|
-- is_directory: is file a directory name?
|
2001-09-26 22:39:46 +02:00
|
|
|
-- content_cb: callback to receive file contents
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-06-06 22:55:45 +02:00
|
|
|
-- err: error message in case of error, nil otherwise
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-26 22:39:46 +02:00
|
|
|
function Private.retrieve(control, server, name, is_directory, content_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
local code, answer
|
2001-01-25 23:03:16 +01:00
|
|
|
local data
|
2000-12-29 23:15:09 +01:00
|
|
|
-- ask server for file or directory listing accordingly
|
2001-09-12 20:27:55 +02:00
|
|
|
if is_directory then
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.cwd(control, name)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not code then return answer end
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.command(control, "nlst", nil, {150, 125})
|
2001-09-12 20:27:55 +02:00
|
|
|
else
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.command(control, "retr", name, {150, 125})
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
if not code then return nil, answer end
|
2001-01-25 23:03:16 +01:00
|
|
|
data, answer = server:accept()
|
|
|
|
server:close()
|
2001-06-06 22:55:45 +02:00
|
|
|
if not data then
|
2000-12-29 23:15:09 +01:00
|
|
|
control:close()
|
2001-06-06 22:55:45 +02:00
|
|
|
return answer
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2002-07-08 23:01:45 +02:00
|
|
|
answer = Private.receive_indirect(data, content_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
if answer then
|
|
|
|
control:close()
|
|
|
|
return answer
|
|
|
|
end
|
|
|
|
data:close()
|
2000-12-29 23:15:09 +01:00
|
|
|
-- make sure file transfered ok
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(control, {226, 250})
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends data comming from a callback
|
|
|
|
-- Input
|
|
|
|
-- data: data connection
|
|
|
|
-- send_cb: callback to produce file contents
|
2001-09-12 20:27:55 +02:00
|
|
|
-- chunk, size: first callback return values
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull, or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.send_indirect(data, send_cb, chunk, size)
|
2003-05-25 03:54:13 +02:00
|
|
|
local total, sent, err
|
|
|
|
total = 0
|
2001-06-06 22:55:45 +02:00
|
|
|
while 1 do
|
|
|
|
if type(chunk) ~= "string" or type(size) ~= "number" then
|
|
|
|
data:close()
|
|
|
|
if not chunk and type(size) == "string" then return size
|
|
|
|
else return "invalid callback return" end
|
|
|
|
end
|
2003-05-25 03:54:13 +02:00
|
|
|
sent, err = data:send(chunk)
|
2001-09-12 20:27:55 +02:00
|
|
|
if err then
|
|
|
|
data:close()
|
|
|
|
return err
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2003-05-25 03:54:13 +02:00
|
|
|
total = total + sent
|
2001-06-06 22:55:45 +02:00
|
|
|
if sent >= size then break end
|
|
|
|
chunk, size = send_cb()
|
|
|
|
end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Stores a file
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
2001-01-25 23:03:16 +01:00
|
|
|
-- server: server socket bound to local address
|
2000-12-29 23:15:09 +01:00
|
|
|
-- file: file name under current directory
|
2001-06-06 22:55:45 +02:00
|
|
|
-- send_cb: callback to produce the file contents
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-01-25 23:03:16 +01:00
|
|
|
-- code: return code, nil if error
|
2000-12-29 23:15:09 +01:00
|
|
|
-- answer: server answer or error message
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.store(control, server, file, send_cb)
|
|
|
|
local data, err
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.command(control, "stor", file, {150, 125})
|
2000-12-29 23:15:09 +01:00
|
|
|
if not code then
|
2001-06-06 22:55:45 +02:00
|
|
|
control:close()
|
2000-12-29 23:15:09 +01:00
|
|
|
return nil, answer
|
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- start data connection
|
2001-01-25 23:03:16 +01:00
|
|
|
data, answer = server:accept()
|
|
|
|
server:close()
|
2001-06-06 22:55:45 +02:00
|
|
|
if not data then
|
2000-12-29 23:15:09 +01:00
|
|
|
control:close()
|
2001-06-06 22:55:45 +02:00
|
|
|
return nil, answer
|
|
|
|
end
|
|
|
|
-- send whole file
|
2002-07-08 23:01:45 +02:00
|
|
|
err = Private.send_indirect(data, send_cb, send_cb())
|
2001-06-06 22:55:45 +02:00
|
|
|
if err then
|
|
|
|
control:close()
|
|
|
|
return nil, err
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- close connection to inform that file transmission is complete
|
|
|
|
data:close()
|
|
|
|
-- check if file was received correctly
|
2002-07-08 23:01:45 +02:00
|
|
|
return Private.check_answer(control, {226, 250})
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Change transfer type
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
2001-09-12 20:27:55 +02:00
|
|
|
-- params: "type=i" for binary or "type=a" for ascii
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-09-12 20:27:55 +02:00
|
|
|
-- err: error message if any
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.change_type(control, params)
|
2001-09-26 22:39:46 +02:00
|
|
|
local type, _
|
2003-03-20 01:24:44 +01:00
|
|
|
_, _, type = string.find(params or "", "type=(.)")
|
2001-09-26 22:39:46 +02:00
|
|
|
if type == "a" or type == "i" then
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, err = Private.command(control, "type", type, {200})
|
2001-09-12 20:27:55 +02:00
|
|
|
if not code then return err end
|
|
|
|
end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Starts a control connection, checks the greeting and log on
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-09-12 20:27:55 +02:00
|
|
|
-- parsed: parsed URL components
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-09-12 20:27:55 +02:00
|
|
|
-- control: control connection with server, or nil if error
|
2000-12-29 23:15:09 +01:00
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.open(parsed)
|
2000-12-29 23:15:09 +01:00
|
|
|
-- start control connection
|
2003-03-20 01:24:44 +01:00
|
|
|
local control, err = socket.connect(parsed.host, parsed.port)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not control then return nil, err end
|
|
|
|
-- make sure we don't block forever
|
2003-08-16 02:06:04 +02:00
|
|
|
control:settimeout(Public.TIMEOUT)
|
2001-09-12 20:27:55 +02:00
|
|
|
-- check greeting
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.greet(control)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not code then return nil, answer end
|
2000-12-29 23:15:09 +01:00
|
|
|
-- try to log in
|
2002-07-08 23:01:45 +02:00
|
|
|
code, err = Private.login(control, parsed.user, parsed.password)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not code then return nil, err
|
|
|
|
else return control end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Closes the connection with the server
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.close(control)
|
2000-12-29 23:15:09 +01:00
|
|
|
-- disconnect
|
2002-07-08 23:01:45 +02:00
|
|
|
Private.logout(control)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Changes to the directory pointed to by URL
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-09-12 20:27:55 +02:00
|
|
|
-- control: control connection with server
|
|
|
|
-- segment: parsed URL path segments
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Private.change_dir(control, segment)
|
2003-03-20 01:24:44 +01:00
|
|
|
local n = table.getn(segment)
|
2001-09-12 20:27:55 +02:00
|
|
|
for i = 1, n-1 do
|
2002-07-08 23:01:45 +02:00
|
|
|
local code, answer = Private.cwd(control, segment[i])
|
2001-09-12 20:27:55 +02:00
|
|
|
if not code then return answer end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Stores a file in current directory
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-- request: a table with the fields:
|
2001-09-26 22:39:46 +02:00
|
|
|
-- content_cb: send callback to send file contents
|
2001-09-12 20:27:55 +02:00
|
|
|
-- segment: parsed URL path segments
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.upload(control, request, segment)
|
2001-09-26 22:39:46 +02:00
|
|
|
local code, name, content_cb
|
2001-09-12 20:27:55 +02:00
|
|
|
-- get remote file name
|
2003-03-20 01:24:44 +01:00
|
|
|
name = segment[table.getn(segment)]
|
2001-09-12 20:27:55 +02:00
|
|
|
if not name then
|
|
|
|
control:close()
|
|
|
|
return "Invalid file path"
|
|
|
|
end
|
2001-09-26 22:39:46 +02:00
|
|
|
content_cb = request.content_cb
|
2001-09-12 20:27:55 +02:00
|
|
|
-- setup passive connection
|
2002-07-08 23:01:45 +02:00
|
|
|
local server, answer = Private.port(control)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not server then return answer end
|
|
|
|
-- ask server to receive file
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.store(control, server, name, content_cb)
|
2000-12-29 23:15:09 +01:00
|
|
|
if not code then return answer end
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Download a file from current directory
|
|
|
|
-- Input
|
|
|
|
-- control: control connection with server
|
|
|
|
-- request: a table with the fields:
|
2001-09-26 22:39:46 +02:00
|
|
|
-- content_cb: receive callback to receive file contents
|
2001-09-12 20:27:55 +02:00
|
|
|
-- segment: parsed URL path segments
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.download(control, request, segment)
|
2001-09-26 22:39:46 +02:00
|
|
|
local code, name, is_directory, content_cb
|
2001-09-12 20:27:55 +02:00
|
|
|
is_directory = segment.is_directory
|
2001-09-26 22:39:46 +02:00
|
|
|
content_cb = request.content_cb
|
2001-09-12 20:27:55 +02:00
|
|
|
-- get remote file name
|
2003-03-20 01:24:44 +01:00
|
|
|
name = segment[table.getn(segment)]
|
2001-09-12 20:27:55 +02:00
|
|
|
if not name and not is_directory then
|
|
|
|
control:close()
|
|
|
|
return "Invalid file path"
|
|
|
|
end
|
2001-01-25 23:03:16 +01:00
|
|
|
-- setup passive connection
|
2002-07-08 23:01:45 +02:00
|
|
|
local server, answer = Private.port(control)
|
2001-01-25 23:03:16 +01:00
|
|
|
if not server then return answer end
|
2001-09-12 20:27:55 +02:00
|
|
|
-- ask server to send file or directory listing
|
2002-07-08 23:01:45 +02:00
|
|
|
code, answer = Private.retrieve(control, server, name,
|
2001-09-26 22:39:46 +02:00
|
|
|
is_directory, content_cb)
|
2000-12-29 23:15:09 +01:00
|
|
|
if not code then return answer end
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Parses the FTP URL setting default values
|
|
|
|
-- Input
|
|
|
|
-- request: a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password
|
|
|
|
-- Returns
|
|
|
|
-- parsed: a table with parsed components
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.parse_url(request)
|
2003-03-20 01:24:44 +01:00
|
|
|
local parsed = socket.url.parse(request.url, {
|
2001-09-12 20:27:55 +02:00
|
|
|
host = "",
|
|
|
|
user = "anonymous",
|
|
|
|
port = 21,
|
|
|
|
path = "/",
|
2002-07-08 23:01:45 +02:00
|
|
|
password = Public.EMAIL,
|
2001-09-26 22:39:46 +02:00
|
|
|
scheme = "ftp"
|
2001-09-12 20:27:55 +02:00
|
|
|
})
|
|
|
|
-- explicit login information overrides that given by URL
|
|
|
|
parsed.user = request.user or parsed.user
|
|
|
|
parsed.password = request.password or parsed.password
|
|
|
|
-- explicit representation type overrides that given by URL
|
|
|
|
if request.type then parsed.params = "type=" .. request.type end
|
|
|
|
return parsed
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Parses the FTP URL path setting default values
|
|
|
|
-- Input
|
|
|
|
-- parsed: a table with the parsed URL components
|
|
|
|
-- Returns
|
|
|
|
-- dirs: a table with parsed directory components
|
|
|
|
-----------------------------------------------------------------------------
|
2003-03-20 01:24:44 +01:00
|
|
|
function Private.parse_path(parsed_url)
|
|
|
|
local segment = socket.url.parse_path(parsed_url.path)
|
|
|
|
segment.is_directory = segment.is_directory or
|
|
|
|
(parsed_url.params == "type=d")
|
2001-09-12 20:27:55 +02:00
|
|
|
return segment
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a request table from a URL or request table
|
|
|
|
-- Input
|
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
|
|
|
-- Returns
|
|
|
|
-- request: request table
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.build_request(data)
|
|
|
|
local request = {}
|
|
|
|
if type(data) == "table" then for i, v in data do request[i] = v end
|
|
|
|
else request.url = data end
|
|
|
|
return request
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Downloads a file from a FTP server
|
|
|
|
-- Input
|
|
|
|
-- request: a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password
|
2001-09-26 22:39:46 +02:00
|
|
|
-- content_cb: receive callback to receive file contents
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.get_cb(request)
|
2002-07-08 23:01:45 +02:00
|
|
|
local parsed = Private.parse_url(request)
|
2001-09-26 22:39:46 +02:00
|
|
|
if parsed.scheme ~= "ftp" then
|
2003-03-20 01:24:44 +01:00
|
|
|
return string.format("unknown scheme '%s'", parsed.scheme)
|
2001-09-26 22:39:46 +02:00
|
|
|
end
|
2002-07-08 23:01:45 +02:00
|
|
|
local control, err = Private.open(parsed)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not control then return err end
|
2002-07-08 23:01:45 +02:00
|
|
|
local segment = Private.parse_path(parsed)
|
|
|
|
return Private.change_dir(control, segment) or
|
|
|
|
Private.change_type(control, parsed.params) or
|
|
|
|
Private.download(control, request, segment) or
|
|
|
|
Private.close(control)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Uploads a file to a FTP server
|
|
|
|
-- Input
|
2001-09-12 20:27:55 +02:00
|
|
|
-- request: a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password
|
2001-09-26 22:39:46 +02:00
|
|
|
-- content_cb: send callback to send file contents
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Public.put_cb(request)
|
2002-07-08 23:01:45 +02:00
|
|
|
local parsed = Private.parse_url(request)
|
2001-09-26 22:39:46 +02:00
|
|
|
if parsed.scheme ~= "ftp" then
|
2003-03-20 01:24:44 +01:00
|
|
|
return string.format("unknown scheme '%s'", parsed.scheme)
|
2001-09-26 22:39:46 +02:00
|
|
|
end
|
2002-07-08 23:01:45 +02:00
|
|
|
local control, err = Private.open(parsed)
|
2001-09-12 20:27:55 +02:00
|
|
|
if not control then return err end
|
2002-07-08 23:01:45 +02:00
|
|
|
local segment = Private.parse_path(parsed)
|
2003-08-31 02:58:07 +02:00
|
|
|
err = Private.change_dir(control, segment) or
|
2002-07-08 23:01:45 +02:00
|
|
|
Private.change_type(control, parsed.params) or
|
|
|
|
Private.upload(control, request, segment) or
|
|
|
|
Private.close(control)
|
2003-08-31 02:58:07 +02:00
|
|
|
if err then return nil, err
|
|
|
|
else return 1 end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
-- Uploads a file to a FTP server
|
|
|
|
-- Input
|
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
2003-08-31 02:58:07 +02:00
|
|
|
-- content: file contents
|
2001-09-12 20:27:55 +02:00
|
|
|
-- content: file contents
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Public.put(url_or_request, content)
|
2002-07-08 23:01:45 +02:00
|
|
|
local request = Private.build_request(url_or_request)
|
2003-08-31 02:58:07 +02:00
|
|
|
request.content = request.content or content
|
|
|
|
request.content_cb = socket.callback.send_string(request.content)
|
2002-07-08 23:01:45 +02:00
|
|
|
return Public.put_cb(request)
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Retrieve a file from a ftp server
|
|
|
|
-- Input
|
2001-09-12 20:27:55 +02:00
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- data: file contents as a string
|
|
|
|
-- err: error message in case of error, nil otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
function Public.get(url_or_request)
|
2003-08-31 02:58:07 +02:00
|
|
|
local concat = socket.concat.create()
|
2002-07-08 23:01:45 +02:00
|
|
|
local request = Private.build_request(url_or_request)
|
2003-08-31 02:58:07 +02:00
|
|
|
request.content_cb = socket.callback.receive_concat(concat)
|
2002-07-08 23:01:45 +02:00
|
|
|
local err = Public.get_cb(request)
|
2003-08-31 02:58:07 +02:00
|
|
|
return concat:getresult(), err
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|