mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-19 11:48:21 +01:00
Data connection is now passive. Even minimum FTP servers are usable.
This commit is contained in:
parent
273fd0964e
commit
a466bd5d42
133
src/ftp.lua
133
src/ftp.lua
@ -1,5 +1,5 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Simple FTP support for the Lua language using the LuaSocket toolkit.
|
-- Simple FTP support for the Lua language using the LuaSocket 1.2 toolkit.
|
||||||
-- Author: Diego Nehab
|
-- Author: Diego Nehab
|
||||||
-- Date: 26/12/2000
|
-- Date: 26/12/2000
|
||||||
-- Conforming to: RFC 959
|
-- Conforming to: RFC 959
|
||||||
@ -170,20 +170,22 @@ end
|
|||||||
-- Input
|
-- Input
|
||||||
-- file: abolute path to file
|
-- file: abolute path to file
|
||||||
-- Returns
|
-- Returns
|
||||||
-- file: filename
|
-- a table with the following fields
|
||||||
-- path: table with directories to reach filename
|
-- name: filename
|
||||||
-- isdir: is it a directory or a file
|
-- path: directory to file
|
||||||
|
-- isdir: is it a directory?
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local split_path = function(file)
|
local split_path = function(file)
|
||||||
local path = {}
|
local parsed = {}
|
||||||
local isdir
|
file = gsub(file, "(/)$", function(i) %parsed.isdir = i end)
|
||||||
file = file or "/"
|
if not parsed.isdir then
|
||||||
-- directory ends with a '/'
|
file = gsub(file, "([^/]+)$", function(n) %parsed.name = n end)
|
||||||
_,_, isdir = strfind(file, "([/])$")
|
end
|
||||||
gsub(file, "([^/]+)", function (dir) tinsert(%path, dir) end)
|
file = gsub(file, "/$", "")
|
||||||
if not isdir then file = tremove(path)
|
file = gsub(file, "^/", "")
|
||||||
else file = nil end
|
if file == "" then file = nil end
|
||||||
return file, path, isdir
|
parsed.path = file
|
||||||
|
if parsed.path or parsed.name or parsed.isdir then return parsed end
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -226,40 +228,44 @@ end
|
|||||||
-- Change to target directory
|
-- Change to target directory
|
||||||
-- Input
|
-- Input
|
||||||
-- control: socket for control connection with server
|
-- control: socket for control connection with server
|
||||||
-- path: array with directories in order
|
-- path: directory to change to
|
||||||
-- Returns
|
-- Returns
|
||||||
-- 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)
|
local cwd = function(control, path)
|
||||||
local code, answer = 250, "Home directory used"
|
local code, answer = 250, "Home directory used"
|
||||||
for i = 1, getn(path) do
|
if path then
|
||||||
code, answer = %try_command(control, "cwd", path[i], {250})
|
code, answer = %try_command(control, "cwd", path, {250})
|
||||||
if not code then return nil, answer end
|
|
||||||
end
|
end
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Start data connection with server
|
-- Change to target directory
|
||||||
-- Input
|
-- Input
|
||||||
-- control: control connection with server
|
-- control: socket for control connection with server
|
||||||
-- Returns
|
-- Returns
|
||||||
-- data: socket for data connection with server, nil if error
|
-- server: server socket bound to local address, nil if error
|
||||||
-- answer: server answer or error message
|
-- answer: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local start_dataconnection = function(control)
|
local port = function(control)
|
||||||
-- ask for passive data connection
|
local code, answer
|
||||||
local code, answer = %try_command(control, "pasv", nil, {227})
|
local server, ctl_ip
|
||||||
if not code then return nil, answer end
|
ctl_ip, answer = control:getsockname()
|
||||||
-- get data connection parameters from server reply
|
server, answer = bind(ctl_ip, 0)
|
||||||
local host, port = %get_pasv(answer)
|
server:timeout(%TIMEOUT)
|
||||||
if not host or not port then return nil, answer end
|
local ip, p, ph, pl
|
||||||
-- start data connection with given parameters
|
ip, p = server:getsockname()
|
||||||
local data, err = connect(host, port)
|
pl = mod(p, 256)
|
||||||
if not data then return nil, err end
|
ph = (p - pl)/256
|
||||||
data:timeout(%TIMEOUT)
|
local arg = gsub(format("%s,%d,%d", ip, ph, pl), "%.", ",")
|
||||||
return data
|
code, answer = %try_command(control, "port", arg, {200})
|
||||||
|
if not code then
|
||||||
|
control:close()
|
||||||
|
server:close()
|
||||||
|
return nil, answer
|
||||||
|
else return server end
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -281,22 +287,21 @@ end
|
|||||||
-- Retrieves file or directory listing
|
-- Retrieves file or directory listing
|
||||||
-- Input
|
-- Input
|
||||||
-- control: control connection with server
|
-- control: control connection with server
|
||||||
-- data: data connection with server
|
-- server: server socket bound to local address
|
||||||
-- file: file name under current directory
|
-- file: file name under current directory
|
||||||
-- isdir: is file a directory name?
|
-- isdir: is file a directory name?
|
||||||
-- Returns
|
-- Returns
|
||||||
-- file: string with file contents, nil if error
|
-- file: string with file contents, nil if error
|
||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local retrieve_file = function(control, data, file, isdir)
|
local retrieve_file = function(control, server, file, isdir)
|
||||||
|
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 isdir then code, answer = %try_command(control, "nlst", file, {150, 125})
|
||||||
else code, answer = %try_command(control, "retr", file, {150, 125}) end
|
else code, answer = %try_command(control, "retr", file, {150, 125}) end
|
||||||
if not code then
|
data, answer = server:accept()
|
||||||
control:close()
|
server:close()
|
||||||
data:close()
|
if not data then return nil, answer end
|
||||||
return nil, answer
|
|
||||||
end
|
|
||||||
-- download whole file
|
-- download whole file
|
||||||
file, err = data:receive("*a")
|
file, err = data:receive("*a")
|
||||||
data:close()
|
data:close()
|
||||||
@ -314,19 +319,23 @@ end
|
|||||||
-- Stores a file
|
-- Stores a file
|
||||||
-- Input
|
-- Input
|
||||||
-- control: control connection with server
|
-- control: control connection with server
|
||||||
-- data: data connection with server
|
-- server: server socket bound to local address
|
||||||
-- file: file name under current directory
|
-- file: file name under current directory
|
||||||
-- bytes: file contents in string
|
-- bytes: file contents in string
|
||||||
-- Returns
|
-- Returns
|
||||||
-- file: string with file contents, nil if error
|
-- code: return code, nil if error
|
||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local store_file = function (control, data, file, bytes)
|
local store_file = function (control, server, file, bytes)
|
||||||
|
local data
|
||||||
local code, answer = %try_command(control, "stor", file, {150, 125})
|
local code, answer = %try_command(control, "stor", file, {150, 125})
|
||||||
if not code then
|
if not code then
|
||||||
data:close()
|
data:close()
|
||||||
return nil, answer
|
return nil, answer
|
||||||
end
|
end
|
||||||
|
data, answer = server:accept()
|
||||||
|
server:close()
|
||||||
|
if not data then return nil, answer end
|
||||||
-- send whole file and close connection to mark file end
|
-- send whole file and close connection to mark file end
|
||||||
answer = data:send(bytes)
|
answer = data:send(bytes)
|
||||||
data:close()
|
data:close()
|
||||||
@ -362,8 +371,8 @@ end
|
|||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function ftp_get(url, type)
|
function ftp_get(url, type)
|
||||||
local control, data, err
|
local control, server, data, err
|
||||||
local answer, code, server, file, path
|
local answer, code, server, pfile, file
|
||||||
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
||||||
-- start control connection
|
-- start control connection
|
||||||
control, err = connect(parsed.host, parsed.port)
|
control, err = connect(parsed.host, parsed.port)
|
||||||
@ -376,21 +385,22 @@ function ftp_get(url, type)
|
|||||||
code, answer = %login(control, parsed.user, parsed.pass)
|
code, answer = %login(control, parsed.user, parsed.pass)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- go to directory
|
-- go to directory
|
||||||
file, path, isdir = %split_path(parsed.path)
|
pfile = %split_path(parsed.path)
|
||||||
code, answer = %cwd(control, path)
|
if not pfile then return nil, "invalid path" end
|
||||||
|
code, answer = %cwd(control, pfile.path)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- change to binary type?
|
-- change to binary type?
|
||||||
code, answer = %change_type(control, type)
|
code, answer = %change_type(control, type)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- start data connection
|
-- setup passive connection
|
||||||
data, answer = %start_dataconnection(control)
|
server, answer = %port(control)
|
||||||
if not data then return nil, answer end
|
if not server then return nil, answer end
|
||||||
-- ask server to send file or directory listing
|
-- ask server to send file or directory listing
|
||||||
file, answer = %retrieve_file(control, data, file, isdir)
|
file, answer = %retrieve_file(control, server, pfile.name, pfile.isdir)
|
||||||
if not file then return nil, answer end
|
if not file then return nil, answer end
|
||||||
-- disconnect
|
-- disconnect
|
||||||
%logout(control)
|
%logout(control)
|
||||||
-- return whatever file we received plus a possible error
|
-- return whatever file we received plus a possible error message
|
||||||
return file, answer
|
return file, answer
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -405,7 +415,7 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function ftp_put(url, bytes, type)
|
function ftp_put(url, bytes, type)
|
||||||
local control, data
|
local control, data
|
||||||
local answer, code, server, file, path
|
local answer, code, server, file, pfile
|
||||||
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
||||||
-- start control connection
|
-- start control connection
|
||||||
control, answer = connect(parsed.host, parsed.port)
|
control, answer = connect(parsed.host, parsed.port)
|
||||||
@ -418,20 +428,21 @@ function ftp_put(url, bytes, type)
|
|||||||
code, answer = %login(control, parsed.user, parsed.pass)
|
code, answer = %login(control, parsed.user, parsed.pass)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
-- go to directory
|
-- go to directory
|
||||||
file, path, isdir = %split_path(parsed.path)
|
pfile = %split_path(parsed.path)
|
||||||
code, answer = %cwd(control, 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
|
if not code then return answer end
|
||||||
-- change to binary type?
|
-- change to binary type?
|
||||||
code, answer = %change_type(control, type)
|
code, answer = %change_type(control, type)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
-- start data connection
|
-- setup passive connection
|
||||||
data, answer = %start_dataconnection(control)
|
server, answer = %port(control)
|
||||||
if not data then return answer end
|
if not server then return answer end
|
||||||
-- ask server to send file or directory listing
|
-- ask server to send file
|
||||||
code, answer = %store_file(control, data, file, bytes)
|
code, answer = %store_file(control, server, pfile.name, bytes)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
-- disconnect
|
-- disconnect
|
||||||
%logout(control)
|
%logout(control)
|
||||||
-- return whatever file we received plus a possible error
|
-- no errors
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user