Updated for LuaSocket 1.4.

Better tested, some name changes.
This commit is contained in:
Diego Nehab 2001-09-12 18:27:55 +00:00
parent 5c622071f9
commit 9546cd10ab

View File

@ -1,60 +1,62 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Simple FTP support for the Lua language using the LuaSocket 1.3b toolkit. -- FTP support for the Lua language
-- LuaSocket 1.4 toolkit.
-- Author: Diego Nehab -- Author: Diego Nehab
-- Date: 26/12/2000 -- Date: 26/12/2000
-- Conforming to: RFC 959 -- Conforming to: RFC 959, LTN7
-- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local Public, Private = {}, {}
FTP = Public
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- timeout in seconds before the program gives up on a connection -- timeout in seconds before the program gives up on a connection
local TIMEOUT = 60 Public.TIMEOUT = 60
-- default port for ftp service -- default port for ftp service
local PORT = 21 Public.PORT = 21
-- this is the default anonymous password. used when no password is -- this is the default anonymous password. used when no password is
-- provided in url. should be changed for your e-mail. -- provided in url. should be changed to your e-mail.
local EMAIL = "anonymous@anonymous.org" Public.EMAIL = "anonymous@anonymous.org"
-- block size used in transfers -- block size used in transfers
local BLOCKSIZE = 4096 Public.BLOCKSIZE = 8192
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Parses a url and returns its scheme, user, password, host, port -- Required libraries
-- and path components, according to RFC 1738, Uniform Resource Locators (URL),
-- of December 1994
-- Input
-- url: unique resource locator desired
-- default: table containing default values to be returned
-- Returns
-- table with the following fields:
-- host: host to connect
-- path: url path
-- port: host port to connect
-- user: user name
-- pass: password
-- scheme: protocol
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local split_url = function(url, default) dofile "concat.lua"
-- initialize default parameters dofile "url.lua"
local parsed = default or {} dofile "code.lua"
-- get scheme
url = gsub(url, "^(.+)://", function (s) %parsed.scheme = s end) -----------------------------------------------------------------------------
-- get user name and password. both can be empty! -- Tries to send DOS mode lines. Closes socket on error.
-- moreover, password can be ommited -- Input
url = gsub(url, "^([^@:/]*)(:?)([^:@/]-)@", function (u, c, p) -- sock: server socket
%parsed.user = u -- line: string to be sent
-- there can be an empty password, but the ':' has to be there -- Returns
-- or else there is no password -- err: message in case of error, nil if successfull
%parsed.pass = nil -- kill default password -----------------------------------------------------------------------------
if c == ":" then %parsed.pass = p end function Private.try_sendline(sock, line)
end) local err = sock:send(line .. "\r\n")
-- get host if err then sock:close() end
url = gsub(url, "^([%w%.%-]+)", function (h) %parsed.host = h end) return err
-- get port if any end
url = gsub(url, "^:(%d+)", function (p) %parsed.port = p end)
-- whatever is left is the path -----------------------------------------------------------------------------
if url ~= "" then parsed.path = url end -- Tries to get a pattern from the server and closes socket on error
return parsed -- sock: socket connected to the server
-- ...: pattern to receive
-- Returns
-- ...: received pattern
-- err: error message if any
-----------------------------------------------------------------------------
function Private.try_receive(...)
local sock = arg[1]
local data, err = call(sock.receive, arg)
if err then sock:close() end
return data, err
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -65,14 +67,12 @@ end
-- ip: string containing ip for data connection -- ip: string containing ip for data connection
-- port: port for data connection -- port: port for data connection
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local get_pasv = function(pasv) function Private.get_pasv(pasv)
local a,b,c,d,p1,p2 local a, b, c, d, p1, p2, _
local ip, port local ip, port
_,_, a, b, c, d, p1, p2 = _,_, a, b, c, d, p1, p2 =
strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)") strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
if not (a and b and c and d and p1 and p2) then if not (a and b and c and d and p1 and p2) then return nil, nil end
return nil, nil
end
ip = format("%d.%d.%d.%d", a, b, c, d) ip = format("%d.%d.%d.%d", a, b, c, d)
port = tonumber(p1)*256 + tonumber(p2) port = tonumber(p1)*256 + tonumber(p2)
return ip, port return ip, port
@ -87,12 +87,11 @@ end
-- Returns -- Returns
-- error message in case of error, nil otherwise -- error message in case of error, nil otherwise
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_command = function(control, cmd, arg) function Private.send_command(control, cmd, arg)
local line, err local line
if arg then line = cmd .. " " .. arg .. "\r\n" if arg then line = cmd .. " " .. arg
else line = cmd .. "\r\n" end else line = cmd end
err = control:send(line) return %Private.try_sendline(control, line)
return err
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -103,16 +102,16 @@ end
-- answer: whole server reply, nil if error -- answer: whole server reply, nil if error
-- code: answer status code or error message -- code: answer status code or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local get_answer = function(control) function Private.get_answer(control)
local code, lastcode, sep local code, lastcode, sep, _
local line, err = control:receive() local line, err = %Private.try_receive(control)
local answer = line local answer = line
if err then return nil, err end if err then return nil, err end
_,_, code, sep = strfind(line, "^(%d%d%d)(.)") _,_, code, sep = strfind(line, "^(%d%d%d)(.)")
if not code or not sep then return nil, answer end if not code or not sep then return nil, answer end
if sep == "-" then -- answer is multiline if sep == "-" then -- answer is multiline
repeat repeat
line, err = control:receive() line, err = %Private.try_receive(control)
if err then return nil, err end if err then return nil, err end
_,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)") _,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)")
answer = answer .. "\n" .. line answer = answer .. "\n" .. line
@ -130,12 +129,9 @@ end
-- code: reply code or nil in case of error -- code: reply code or nil in case of error
-- answer: server complete answer or system error message -- answer: server complete answer or system error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local check_answer = function(control, success) function Private.check_answer(control, success)
local answer, code = %get_answer(control) local answer, code = %Private.get_answer(control)
if not answer then if not answer then return nil, code end
control:close()
return nil, code
end
if type(success) ~= "table" then success = {success} end if type(success) ~= "table" then success = {success} end
for i = 1, getn(success) do for i = 1, getn(success) do
if code == success[i] then if code == success[i] then
@ -158,38 +154,10 @@ end
-- code: reply code or nil in case of error -- code: reply code or nil in case of error
-- answer: server complete answer or system error message -- answer: server complete answer or system error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local try_command = function(control, cmd, arg, success) function Private.command(control, cmd, arg, success)
local err = %send_command(control, cmd, arg) local err = %Private.send_command(control, cmd, arg)
if err then if err then return nil, err end
control:close() return %Private.check_answer(control, success)
return nil, err
end
local code, answer = %check_answer(control, success)
if not code then return nil, answer end
return code, answer
end
-----------------------------------------------------------------------------
-- Creates a table with all directories in path
-- Input
-- file: abolute path to file
-- Returns
-- a table with the following fields
-- name: filename
-- path: directory to file
-- isdir: is it a directory?
-----------------------------------------------------------------------------
local split_path = function(file)
local parsed = {}
file = gsub(file, "(/)$", function(i) %parsed.isdir = i end)
if not parsed.isdir then
file = gsub(file, "([^/]+)$", function(n) %parsed.name = n end)
end
file = gsub(file, "/$", "")
file = gsub(file, "^/", "")
if file == "" then file = nil end
parsed.path = file
if parsed.path or parsed.name or parsed.isdir then return parsed end
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -200,11 +168,10 @@ end
-- code: nil if error -- code: nil if error
-- answer: server answer or error message -- answer: server answer or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local check_greeting = function(control) function Private.greet(control)
local code, answer = %check_answer(control, {120, 220}) local code, answer = %Private.check_answer(control, {120, 220})
if not code then return nil, answer end
if code == 120 then -- please try again, somewhat busy now... if code == 120 then -- please try again, somewhat busy now...
code, answer = %check_answer(control, {220}) return %Private.check_answer(control, {220})
end end
return code, answer return code, answer
end end
@ -214,16 +181,15 @@ end
-- Input -- Input
-- control: control connection with server -- control: control connection with server
-- user: user name -- user: user name
-- pass: user password if any -- password: user password if any
-- Returns -- Returns
-- code: nil if error -- code: nil if error
-- answer: server answer or error message -- answer: server answer or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local login = function(control, user, pass) function Private.login(control, user, password)
local code, answer = %try_command(control, "user", parsed.user, {230, 331}) local code, answer = %Private.command(control, "user", user, {230, 331})
if not code then return nil, answer end if code == 331 and password then -- need pass and we have pass
if code == 331 and parsed.pass then -- need pass and we have pass return %Private.command(control, "pass", password, {230, 202})
code, answer = %try_command(control, "pass", parsed.pass, {230, 202})
end end
return code, answer return code, answer
end end
@ -237,12 +203,9 @@ end
-- code: nil if error -- code: nil if error
-- answer: server answer or error message -- answer: server answer or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local cwd = function(control, path) function Private.cwd(control, path)
local code, answer = 250, "Home directory used" if path then return %Private.command(control, "cwd", path, {250})
if path then else return 250, nil end
code, answer = %try_command(control, "cwd", path, {250})
end
return code, answer
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -253,20 +216,19 @@ end
-- server: server socket bound to local address, nil if error -- server: server socket bound to local address, nil if error
-- answer: error message if any -- answer: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local port = function(control) function Private.port(control)
local code, answer local code, answer
local server, ctl_ip local server, ctl_ip
ctl_ip, answer = control:getsockname() ctl_ip, answer = control:getsockname()
server, answer = bind(ctl_ip, 0) server, answer = bind(ctl_ip, 0)
server:timeout(%TIMEOUT) server:timeout(%Public.TIMEOUT)
local ip, p, ph, pl local ip, p, ph, pl
ip, p = server:getsockname() ip, p = server:getsockname()
pl = mod(p, 256) pl = mod(p, 256)
ph = (p - pl)/256 ph = (p - pl)/256
local arg = gsub(format("%s,%d,%d", ip, ph, pl), "%.", ",") local arg = gsub(format("%s,%d,%d", ip, ph, pl), "%.", ",")
code, answer = %try_command(control, "port", arg, {200}) code, answer = %Private.command(control, "port", arg, {200})
if not code then if not code then
control:close()
server:close() server:close()
return nil, answer return nil, answer
else return server end else return server end
@ -280,10 +242,9 @@ end
-- code: nil if error -- code: nil if error
-- answer: server answer or error message -- answer: server answer or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local logout = function(control) function Private.logout(control)
local code, answer = %try_command(control, "quit", nil, {221}) local code, answer = %Private.command(control, "quit", nil, {221})
if not code then return nil, answer end if code then control:close() end
control:close()
return code, answer return code, answer
end end
@ -295,10 +256,10 @@ end
-- Returns -- Returns
-- nil if successfull, or an error message in case of error -- nil if successfull, or an error message in case of error
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local receive_indirect = function(data, callback) function Private.receive_indirect(data, callback)
local chunk, err, res local chunk, err, res
while not err do while not err do
chunk, err = data:receive(%BLOCKSIZE) chunk, err = %Private.try_receive(data, %Public.BLOCKSIZE)
if err == "closed" then err = "done" end if err == "closed" then err = "done" end
res = callback(chunk, err) res = callback(chunk, err)
if not res then break end if not res then break end
@ -310,33 +271,38 @@ end
-- Input -- Input
-- control: control connection with server -- control: control connection with server
-- server: server socket bound to local address -- server: server socket bound to local address
-- file: file name under current directory -- name: file name
-- isdir: is file a directory name? -- is_directory: is file a directory name?
-- callback: callback to receive file contents -- download_cb: callback to receive file contents
-- Returns -- Returns
-- err: error message in case of error, nil otherwise -- err: error message in case of error, nil otherwise
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local retrieve = function(control, server, file, isdir, callback) function Private.retrieve(control, server, name, is_directory, download_cb)
local code, answer local code, answer
local data local data
-- ask server for file or directory listing accordingly -- ask server for file or directory listing accordingly
if isdir then code, answer = %try_command(control, "nlst", file, {150, 125}) if is_directory then
else code, answer = %try_command(control, "retr", file, {150, 125}) end code, answer = %Private.cwd(control, name)
if not code then return answer end
code, answer = %Private.command(control, "nlst", nil, {150, 125})
else
code, answer = %Private.command(control, "retr", name, {150, 125})
end
if not code then return nil, answer end
data, answer = server:accept() data, answer = server:accept()
server:close() server:close()
if not data then if not data then
control:close() control:close()
return answer return answer
end end
answer = %receive_indirect(data, callback) answer = %Private.receive_indirect(data, download_cb)
if answer then if answer then
control:close() control:close()
return answer return answer
end end
data:close() data:close()
-- make sure file transfered ok -- make sure file transfered ok
code, answer = %check_answer(control, {226, 250}) return %Private.check_answer(control, {226, 250})
if not code then return answer end
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -344,11 +310,11 @@ end
-- Input -- Input
-- data: data connection -- data: data connection
-- send_cb: callback to produce file contents -- send_cb: callback to produce file contents
-- chunk, size: first callback results -- chunk, size: first callback return values
-- Returns -- Returns
-- nil if successfull, or an error message in case of error -- nil if successfull, or an error message in case of error
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local try_sendindirect = function(data, send_cb, chunk, size) function Private.send_indirect(data, send_cb, chunk, size)
local sent, err local sent, err
sent = 0 sent = 0
while 1 do while 1 do
@ -358,9 +324,9 @@ local try_sendindirect = function(data, send_cb, chunk, size)
else return "invalid callback return" end else return "invalid callback return" end
end end
err = data:send(chunk) err = data:send(chunk)
if err then if err then
data:close() data:close()
return err return err
end end
sent = sent + strlen(chunk) sent = sent + strlen(chunk)
if sent >= size then break end if sent >= size then break end
@ -379,9 +345,9 @@ end
-- code: return code, nil if error -- code: return code, nil if error
-- answer: server answer or error message -- answer: server answer or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local store = function(control, server, file, send_cb) function Private.store(control, server, file, send_cb)
local data local data, err
local code, answer = %try_command(control, "stor", file, {150, 125}) local code, answer = %Private.command(control, "stor", file, {150, 125})
if not code then if not code then
control:close() control:close()
return nil, answer return nil, answer
@ -394,7 +360,7 @@ local store = function(control, server, file, send_cb)
return nil, answer return nil, answer
end end
-- send whole file -- send whole file
err = %try_sendindirect(data, send_cb, send_cb()) err = %Private.send_indirect(data, send_cb, send_cb())
if err then if err then
control:close() control:close()
return nil, err return nil, err
@ -402,144 +368,275 @@ local store = function(control, server, file, send_cb)
-- close connection to inform that file transmission is complete -- close connection to inform that file transmission is complete
data:close() data:close()
-- check if file was received correctly -- check if file was received correctly
return %check_answer(control, {226, 250}) return %Private.check_answer(control, {226, 250})
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Change transfer type -- Change transfer type
-- Input -- Input
-- control: control connection with server -- control: control connection with server
-- type: new transfer type -- params: "type=i" for binary or "type=a" for ascii
-- Returns
-- code: nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
local change_type = function(control, type)
if type == "b" then type = "i" else type = "a" end
return %try_command(control, "type", type, {200})
end
-----------------------------------------------------------------------------
-- Retrieve a file from a ftp server
-- Input
-- url: file location
-- receive_cb: callback to receive file contents
-- type: "binary" or "ascii"
-- Returns -- Returns
-- err: error message if any -- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function ftp_getindirect(url, receive_cb, type) function Private.change_type(control, params)
local control, server, data, err local type
local answer, code, server, pfile, file if params == "type=i" then type = "i"
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL}) elseif params == "type=a" then type = "a" end
if type then
local code, err = %Private.command(control, "type", type, {200})
if not code then return err end
end
end
-----------------------------------------------------------------------------
-- Starts a control connection, checks the greeting and log on
-- Input
-- parsed: parsed URL components
-- Returns
-- control: control connection with server, or nil if error
-- err: error message if any
-----------------------------------------------------------------------------
function Private.open(parsed)
-- start control connection -- start control connection
control, err = connect(parsed.host, parsed.port) local control, err = connect(parsed.host, parsed.port)
if not control then return err end if not control then return nil, err end
control:timeout(%TIMEOUT) -- make sure we don't block forever
-- get and check greeting control:timeout(%Public.TIMEOUT)
code, answer = %check_greeting(control) -- check greeting
if not code then return answer end local code, answer = %Private.greet(control)
if not code then return nil, answer end
-- try to log in -- try to log in
code, answer = %login(control, parsed.user, parsed.pass) code, err = %Private.login(control, parsed.user, parsed.password)
if not code then return answer end if not code then return nil, err
-- go to directory else return control end
pfile = %split_path(parsed.path) end
if not pfile then return "invalid path" end
code, answer = %cwd(control, pfile.path) -----------------------------------------------------------------------------
if not code then return answer end -- Closes the connection with the server
-- change to binary type? -- Input
code, answer = %change_type(control, type) -- control: control connection with server
if not code then return answer end -----------------------------------------------------------------------------
function Private.close(control)
-- disconnect
%Private.logout(control)
end
-----------------------------------------------------------------------------
-- Changes to the directory pointed to by URL
-- Input
-- control: control connection with server
-- segment: parsed URL path segments
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.change_dir(control, segment)
local n = getn(segment)
for i = 1, n-1 do
local code, answer = %Private.cwd(control, segment[i])
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:
-- upload_cb: send callback to send file contents
-- segment: parsed URL path segments
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.upload(control, request, segment)
local code, name, upload_cb
-- get remote file name
name = segment[getn(segment)]
if not name then
control:close()
return "Invalid file path"
end
upload_cb = request.upload_cb
-- setup passive connection -- setup passive connection
server, answer = %port(control) local server, answer = %Private.port(control)
if not server then return answer end
-- ask server to receive file
code, answer = %Private.store(control, server, name, upload_cb)
if not code then return answer end
end
-----------------------------------------------------------------------------
-- Download a file from current directory
-- Input
-- control: control connection with server
-- request: a table with the fields:
-- download_cb: receive callback to receive file contents
-- segment: parsed URL path segments
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.download(control, request, segment)
local code, name, is_directory, download_cb
is_directory = segment.is_directory
download_cb = request.download_cb
-- get remote file name
name = segment[getn(segment)]
if not name and not is_directory then
control:close()
return "Invalid file path"
end
-- setup passive connection
local server, answer = %Private.port(control)
if not server then return answer end if not server then return answer end
-- ask server to send file or directory listing -- ask server to send file or directory listing
err = %retrieve(control, server, pfile.name, pfile.isdir, receive_cb) code, answer = %Private.retrieve(control, server, name,
if err then return err end is_directory, download_cb)
-- disconnect if not code then return answer end
%logout(control) 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)
local parsed = URL.parse_url(request.url, {
host = "",
user = "anonymous",
port = 21,
path = "/",
password = %Public.EMAIL
})
-- 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
-----------------------------------------------------------------------------
function Private.parse_path(parsed)
local segment = URL.parse_path(parsed.path)
segment.is_directory = segment.is_directory or (parsed.params == "type=d")
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
-- download_cb: receive callback to receive file contents
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Public.get_cb(request)
local parsed = %Private.parse_url(request)
local control, err = %Private.open(parsed)
if not control then return err end
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)
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Uploads a file to a FTP server -- Uploads a file to a FTP server
-- Input -- Input
-- url: file location -- request: a table with the fields:
-- send_cb: callback to produce the file contents -- url: the target URL
-- type: "binary" or "ascii" -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
-- user: account user name
-- password: account password
-- upload_cb: send callback to send file contents
-- Returns -- Returns
-- err: error message if any -- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function ftp_putindirect(url, send_cb, type) function Public.put_cb(request)
local control, data local parsed = %Private.parse_url(request)
local answer, code, server, file, pfile local control, err = %Private.open(parsed)
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL}) if not control then return err end
-- start control connection local segment = %Private.parse_path(parsed)
control, answer = connect(parsed.host, parsed.port) return %Private.change_dir(control, segment) or
if not control then return answer end %Private.change_type(control, parsed.params) or
control:timeout(%TIMEOUT) %Private.upload(control, request, segment) or
-- get and check greeting %Private.close(control)
code, answer = %check_greeting(control)
if not code then return answer end
-- try to log in
code, answer = %login(control, parsed.user, parsed.pass)
if not code then return answer end
-- go to directory
pfile = %split_path(parsed.path)
if not pfile or pfile.isdir then return "invalid path" end
code, answer = %cwd(control, pfile.path)
if not code then return answer end
-- change to binary type?
code, answer = %change_type(control, type)
if not code then return answer end
-- setup passive connection
server, answer = %port(control)
if not server then return answer end
-- ask server to send file
code, answer = %store(control, server, pfile.name, send_cb)
if not code then return answer end
-- disconnect
%logout(control)
-- no errors
return nil
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Uploads a file to a FTP server -- Uploads a file to a FTP server
-- Input -- Input
-- url: file location -- url_or_request: target url or request table (a table with the fields:
-- bytes: file contents -- url: the target URL
-- type: "binary" or "ascii" -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
-- user: account user name
-- password: account password)
-- content: file contents
-- Returns -- Returns
-- err: error message if any -- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function ftp_put(url, bytes, type) function Public.put(url_or_request, content)
local send_cb = function() local request = %Private.build_request(url_or_request)
return %bytes, strlen(%bytes) request.upload_cb = function()
return %content, strlen(%content)
end end
return ftp_putindirect(url, send_cb, type) return %Public.put_cb(request)
end end
-----------------------------------------------------------------------------
-- We need fast concatenation routines for direct requests
-----------------------------------------------------------------------------
dofile("buffer.lua")
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Retrieve a file from a ftp server -- Retrieve a file from a ftp server
-- Input -- Input
-- url: file location -- url_or_request: target url or request table (a table with the fields:
-- type: "binary" or "ascii" -- 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 -- Returns
-- data: file contents as a string -- data: file contents as a string
-- err: error message in case of error, nil otherwise -- err: error message in case of error, nil otherwise
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function ftp_get(url, type) function Public.get(url_or_request)
local bytes = { buf = buf_create() } local cat = Concat.create()
local receive_cb = function(chunk, err) local request = %Private.build_request(url_or_request)
if not chunk then %bytes.buf = nil end request.download_cb = function(chunk, err)
buf_addstring(%bytes.buf, chunk) if chunk then %cat:addstring(chunk) end
return 1 return 1
end end
err = ftp_getindirect(url, receive_cb, type) local err = %Public.get_cb(request)
return buf_getresult(bytes.buf), err return cat:getresult(), err
end end