New filter scheme.

ltn12 and mime updated.
smtp/ftp broken.
This commit is contained in:
Diego Nehab 2004-03-16 06:42:53 +00:00
parent b6edaac284
commit bcc0c2a9f0
17 changed files with 568 additions and 530 deletions

View file

@ -5,62 +5,29 @@
-- Conforming to: RFC 959, LTN7
-- RCS ID: $Id$
-----------------------------------------------------------------------------
local Public, Private = {}, {}
local socket = _G[LUASOCKET_LIBNAME] -- get LuaSocket namespace
socket.ftp = Public -- create ftp sub namespace
-- make sure LuaSocket is loaded
if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
if not socket then error('module requires LuaSocket') end
-- create namespace inside LuaSocket namespace
socket.ftp = socket.ftp or {}
-- make all module globals fall into namespace
setmetatable(socket.ftp, { __index = _G })
setfenv(1, socket.ftp)
-----------------------------------------------------------------------------
-- Program constants
-----------------------------------------------------------------------------
-- timeout in seconds before the program gives up on a connection
Public.TIMEOUT = 60
TIMEOUT = 60
-- default port for ftp service
Public.PORT = 21
PORT = 21
-- this is the default anonymous password. used when no password is
-- provided in url. should be changed to your e-mail.
Public.EMAIL = "anonymous@anonymous.org"
EMAIL = "anonymous@anonymous.org"
-- block size used in transfers
Public.BLOCKSIZE = 8192
-----------------------------------------------------------------------------
-- Tries to get a pattern from the server and closes socket on error
-- sock: socket connected to the server
-- pattern: pattern to receive
-- Returns
-- received pattern on success
-- nil followed by error message on error
-----------------------------------------------------------------------------
function Private.try_receive(sock, pattern)
local data, err = sock:receive(pattern)
if not data then sock:close() end
return data, err
end
-----------------------------------------------------------------------------
-- Tries to send data to the server and closes socket on error
-- sock: socket connected to the server
-- data: data to send
-- Returns
-- err: error message if any, nil if successfull
-----------------------------------------------------------------------------
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")
end
BLOCKSIZE = 2048
-----------------------------------------------------------------------------
-- Gets ip and port for data connection from PASV answer
@ -70,7 +37,7 @@ end
-- ip: string containing ip for data connection
-- port: port for data connection
-----------------------------------------------------------------------------
function Private.get_pasv(pasv)
local function get_pasv(pasv)
local a, b, c, d, p1, p2, _
local ip, port
_,_, a, b, c, d, p1, p2 =
@ -81,88 +48,6 @@ function Private.get_pasv(pasv)
return ip, port
end
-----------------------------------------------------------------------------
-- Sends a FTP command through socket
-- Input
-- control: control connection socket
-- cmd: command
-- arg: command argument if any
-- Returns
-- error message in case of error, nil otherwise
-----------------------------------------------------------------------------
function Private.send_command(control, cmd, arg)
local line
if arg then line = cmd .. " " .. arg
else line = cmd end
return Private.try_sendline(control, line)
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
-----------------------------------------------------------------------------
function Private.get_answer(control)
local code, lastcode, sep, _
local line, err = Private.try_receive(control)
local answer = line
if err then return nil, err end
_,_, code, sep = string.find(line, "^(%d%d%d)(.)")
if not code or not sep then return nil, answer end
if sep == "-" then -- answer is multiline
repeat
line, err = Private.try_receive(control)
if err then return nil, err end
_,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)")
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
-----------------------------------------------------------------------------
function Private.check_answer(control, success)
local answer, code = Private.get_answer(control)
if not answer then return nil, code end
if type(success) ~= "table" then success = {success} end
for _, s in ipairs(success) do
if code == s then
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
-----------------------------------------------------------------------------
function Private.command(control, cmd, arg, success)
local err = Private.send_command(control, cmd, arg)
if err then return nil, err end
return Private.check_answer(control, success)
end
-----------------------------------------------------------------------------
-- Check server greeting
-- Input
@ -171,10 +56,10 @@ end
-- code: nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
function Private.greet(control)
local code, answer = Private.check_answer(control, {120, 220})
local function greet(control)
local code, answer = check_answer(control, {120, 220})
if code == 120 then -- please try again, somewhat busy now...
return Private.check_answer(control, {220})
return check_answer(control, {220})
end
return code, answer
end
@ -189,10 +74,10 @@ end
-- code: nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
function Private.login(control, user, password)
local code, answer = Private.command(control, "user", user, {230, 331})
local function login(control, user, password)
local code, answer = command(control, "user", user, {230, 331})
if code == 331 and password then -- need pass and we have pass
return Private.command(control, "pass", password, {230, 202})
return command(control, "pass", password, {230, 202})
end
return code, answer
end
@ -206,9 +91,7 @@ end
-- code: nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
function Private.cwd(control, path)
if path then return Private.command(control, "cwd", path, {250})
else return 250, nil end
local function cwd(control, path)
end
-----------------------------------------------------------------------------
@ -219,18 +102,18 @@ end
-- server: server socket bound to local address, nil if error
-- answer: error message if any
-----------------------------------------------------------------------------
function Private.port(control)
local function port(control)
local code, answer
local server, ctl_ip
ctl_ip, answer = control:getsockname()
server, answer = socket.bind(ctl_ip, 0)
server:settimeout(Public.TIMEOUT)
server:settimeout(TIMEOUT)
local ip, p, ph, pl
ip, p = server:getsockname()
pl = math.mod(p, 256)
ph = (p - pl)/256
local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",")
code, answer = Private.command(control, "port", arg, {200})
code, answer = command(control, "port", arg, {200})
if not code then
server:close()
return nil, answer
@ -245,8 +128,8 @@ end
-- code: nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
function Private.logout(control)
local code, answer = Private.command(control, "quit", nil, {221})
local function logout(control)
local code, answer = command(control, "quit", nil, {221})
if code then control:close() end
return code, answer
end
@ -259,10 +142,10 @@ end
-- Returns
-- nil if successfull, or an error message in case of error
-----------------------------------------------------------------------------
function Private.receive_indirect(data, callback)
local function receive_indirect(data, callback)
local chunk, err, res
while not err do
chunk, err = Private.try_receive(data, Public.BLOCKSIZE)
chunk, err = try_receive(data, BLOCKSIZE)
if err == "closed" then err = "done" end
res = callback(chunk, err)
if not res then break end
@ -280,16 +163,16 @@ end
-- Returns
-- err: error message in case of error, nil otherwise
-----------------------------------------------------------------------------
function Private.retrieve(control, server, name, is_directory, content_cb)
local function retrieve(control, server, name, is_directory, content_cb)
local code, answer
local data
-- ask server for file or directory listing accordingly
if is_directory then
code, answer = Private.cwd(control, name)
code, answer = cwd(control, name)
if not code then return answer end
code, answer = Private.command(control, "nlst", nil, {150, 125})
code, answer = command(control, "nlst", nil, {150, 125})
else
code, answer = Private.command(control, "retr", name, {150, 125})
code, answer = command(control, "retr", name, {150, 125})
end
if not code then return nil, answer end
data, answer = server:accept()
@ -298,43 +181,14 @@ function Private.retrieve(control, server, name, is_directory, content_cb)
control:close()
return answer
end
answer = Private.receive_indirect(data, content_cb)
answer = receive_indirect(data, content_cb)
if answer then
control:close()
return answer
end
data:close()
-- make sure file transfered ok
return Private.check_answer(control, {226, 250})
end
-----------------------------------------------------------------------------
-- Sends data comming from a callback
-- Input
-- data: data connection
-- send_cb: callback to produce file contents
-- chunk, size: first callback return values
-- Returns
-- nil if successfull, or an error message in case of error
-----------------------------------------------------------------------------
function Private.send_indirect(data, send_cb, chunk, size)
local total, sent, err
total = 0
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
sent, err = data:send(chunk)
if err then
data:close()
return err
end
total = total + sent
if sent >= size then break end
chunk, size = send_cb()
end
return check_answer(control, {226, 250})
end
-----------------------------------------------------------------------------
@ -348,9 +202,9 @@ end
-- code: return code, nil if error
-- answer: server answer or error message
-----------------------------------------------------------------------------
function Private.store(control, server, file, send_cb)
local function store(control, server, file, send_cb)
local data, err
local code, answer = Private.command(control, "stor", file, {150, 125})
local code, answer = command(control, "stor", file, {150, 125})
if not code then
control:close()
return nil, answer
@ -363,7 +217,7 @@ function Private.store(control, server, file, send_cb)
return nil, answer
end
-- send whole file
err = Private.send_indirect(data, send_cb, send_cb())
err = send_indirect(data, send_cb, send_cb())
if err then
control:close()
return nil, err
@ -371,7 +225,7 @@ function Private.store(control, server, file, send_cb)
-- close connection to inform that file transmission is complete
data:close()
-- check if file was received correctly
return Private.check_answer(control, {226, 250})
return check_answer(control, {226, 250})
end
-----------------------------------------------------------------------------
@ -382,11 +236,11 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.change_type(control, params)
local function change_type(control, params)
local type, _
_, _, type = string.find(params or "", "type=(.)")
if type == "a" or type == "i" then
local code, err = Private.command(control, "type", type, {200})
local code, err = command(control, "type", type, {200})
if not code then return err end
end
end
@ -399,45 +253,42 @@ end
-- control: control connection with server, or nil if error
-- err: error message if any
-----------------------------------------------------------------------------
function Private.open(parsed)
-- start control connection
local control, err = socket.connect(parsed.host, parsed.port)
local function open(parsed)
local control, err = socket.tp.connect(parsed.host, parsed.port)
if not control then return nil, err end
-- make sure we don't block forever
control:settimeout(Public.TIMEOUT)
-- check greeting
local code, answer = Private.greet(control)
if not code then return nil, answer end
-- try to log in
code, err = Private.login(control, parsed.user, parsed.password)
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)
-- 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 = table.getn(segment)
for i = 1, n-1 do
local code, answer = Private.cwd(control, segment[i])
if not code then return answer end
local code, reply
-- greet
code, reply = control:check({120, 220})
if code == 120 then -- busy, try again
code, reply = control:check(220)
end
-- authenticate
code, reply = control:command("user", user)
code, reply = control:check({230, 331})
if code == 331 and password then -- need pass and we have pass
control:command("pass", password)
code, reply = control:check({230, 202})
end
-- change directory
local segment = parse_path(parsed)
for i, v in ipairs(segment) do
code, reply = control:command("cwd")
code, reply = control:check(250)
end
-- change type
local type = string.sub(params or "", 7, 7)
if type == "a" or type == "i" then
code, reply = control:command("type", type)
code, reply = control:check(200)
end
end
return change_dir(control, segment) or
change_type(control, parsed.params) or
download(control, request, segment) or
close(control)
end
end
-----------------------------------------------------------------------------
@ -450,7 +301,7 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.upload(control, request, segment)
local function upload(control, request, segment)
local code, name, content_cb
-- get remote file name
name = segment[table.getn(segment)]
@ -460,10 +311,10 @@ function Private.upload(control, request, segment)
end
content_cb = request.content_cb
-- setup passive connection
local server, answer = Private.port(control)
local server, answer = port(control)
if not server then return answer end
-- ask server to receive file
code, answer = Private.store(control, server, name, content_cb)
code, answer = store(control, server, name, content_cb)
if not code then return answer end
end
@ -477,7 +328,7 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.download(control, request, segment)
local function download(control, request, segment)
local code, name, is_directory, content_cb
is_directory = segment.is_directory
content_cb = request.content_cb
@ -488,10 +339,10 @@ function Private.download(control, request, segment)
return "Invalid file path"
end
-- setup passive connection
local server, answer = Private.port(control)
local server, answer = port(control)
if not server then return answer end
-- ask server to send file or directory listing
code, answer = Private.retrieve(control, server, name,
code, answer = retrieve(control, server, name,
is_directory, content_cb)
if not code then return answer end
end
@ -507,13 +358,12 @@ end
-- Returns
-- parsed: a table with parsed components
-----------------------------------------------------------------------------
function Private.parse_url(request)
local function parse_url(request)
local parsed = socket.url.parse(request.url, {
host = "",
user = "anonymous",
port = 21,
path = "/",
password = Public.EMAIL,
password = EMAIL,
scheme = "ftp"
})
-- explicit login information overrides that given by URL
@ -531,7 +381,7 @@ end
-- Returns
-- dirs: a table with parsed directory components
-----------------------------------------------------------------------------
function Private.parse_path(parsed_url)
local function 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")
@ -549,7 +399,7 @@ end
-- Returns
-- request: request table
-----------------------------------------------------------------------------
function Private.build_request(data)
local function 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
@ -568,18 +418,18 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Public.get_cb(request)
local parsed = Private.parse_url(request)
function get_cb(request)
local parsed = parse_url(request)
if parsed.scheme ~= "ftp" then
return string.format("unknown scheme '%s'", parsed.scheme)
end
local control, err = Private.open(parsed)
local control, err = 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)
local segment = parse_path(parsed)
return change_dir(control, segment) or
change_type(control, parsed.params) or
download(control, request, segment) or
close(control)
end
-----------------------------------------------------------------------------
@ -594,18 +444,18 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Public.put_cb(request)
local parsed = Private.parse_url(request)
function put_cb(request)
local parsed = parse_url(request)
if parsed.scheme ~= "ftp" then
return string.format("unknown scheme '%s'", parsed.scheme)
end
local control, err = Private.open(parsed)
local control, err = open(parsed)
if not control then return err end
local segment = Private.parse_path(parsed)
err = Private.change_dir(control, segment) or
Private.change_type(control, parsed.params) or
Private.upload(control, request, segment) or
Private.close(control)
local segment = parse_path(parsed)
err = change_dir(control, segment) or
change_type(control, parsed.params) or
upload(control, request, segment) or
close(control)
if err then return nil, err
else return 1 end
end
@ -623,11 +473,11 @@ end
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Public.put(url_or_request, content)
local request = Private.build_request(url_or_request)
function put(url_or_request, content)
local request = build_request(url_or_request)
request.content = request.content or content
request.content_cb = socket.callback.send_string(request.content)
return Public.put_cb(request)
return put_cb(request)
end
-----------------------------------------------------------------------------
@ -642,12 +492,12 @@ end
-- data: file contents as a string
-- err: error message in case of error, nil otherwise
-----------------------------------------------------------------------------
function Public.get(url_or_request)
function get(url_or_request)
local concat = socket.concat.create()
local request = Private.build_request(url_or_request)
local request = build_request(url_or_request)
request.content_cb = socket.callback.receive_concat(concat)
local err = Public.get_cb(request)
local err = get_cb(request)
return concat:getresult(), err
end
return ftp
return socket.ftp