Finish port to Lua 5. Everything is working fine.
Still doesn't work in Windows.
This commit is contained in:
parent
7da19138e3
commit
53857360bb
11 changed files with 289 additions and 277 deletions
51
src/ftp.lua
51
src/ftp.lua
|
@ -8,7 +8,7 @@
|
|||
-----------------------------------------------------------------------------
|
||||
|
||||
local Public, Private = {}, {}
|
||||
FTP = Public
|
||||
socket.ftp = Public
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
|
@ -47,7 +47,7 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.try_receive(...)
|
||||
local sock = arg[1]
|
||||
local data, err = call(sock.receive, arg)
|
||||
local data, err = sock.receive(unpack(arg))
|
||||
if err then sock:close() end
|
||||
return data, err
|
||||
end
|
||||
|
@ -64,9 +64,9 @@ function Private.get_pasv(pasv)
|
|||
local a, b, c, d, p1, p2, _
|
||||
local ip, port
|
||||
_,_, a, b, c, d, p1, p2 =
|
||||
strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
|
||||
string.find(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
|
||||
if not (a and b and c and d and p1 and p2) then return nil, nil end
|
||||
ip = format("%d.%d.%d.%d", a, b, c, d)
|
||||
ip = string.format("%d.%d.%d.%d", a, b, c, d)
|
||||
port = tonumber(p1)*256 + tonumber(p2)
|
||||
return ip, port
|
||||
end
|
||||
|
@ -100,13 +100,13 @@ function Private.get_answer(control)
|
|||
local line, err = Private.try_receive(control)
|
||||
local answer = line
|
||||
if err then return nil, err end
|
||||
_,_, code, sep = strfind(line, "^(%d%d%d)(.)")
|
||||
_,_, 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 = strfind(line, "^(%d%d%d)(.)")
|
||||
_,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)")
|
||||
answer = answer .. "\n" .. line
|
||||
until code == lastcode and sep == " " -- answer ends with same code
|
||||
end
|
||||
|
@ -126,8 +126,8 @@ 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 i = 1, getn(success) do
|
||||
if code == success[i] then
|
||||
for _, s in ipairs(success) do
|
||||
if code == s then
|
||||
return code, answer
|
||||
end
|
||||
end
|
||||
|
@ -213,13 +213,13 @@ function Private.port(control)
|
|||
local code, answer
|
||||
local server, ctl_ip
|
||||
ctl_ip, answer = control:getsockname()
|
||||
server, answer = bind(ctl_ip, 0)
|
||||
server, answer = socket.bind(ctl_ip, 0)
|
||||
server:timeout(Public.TIMEOUT)
|
||||
local ip, p, ph, pl
|
||||
ip, p = server:getsockname()
|
||||
pl = mod(p, 256)
|
||||
pl = math.mod(p, 256)
|
||||
ph = (p - pl)/256
|
||||
local arg = gsub(format("%s,%d,%d", ip, ph, pl), "%.", ",")
|
||||
local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",")
|
||||
code, answer = Private.command(control, "port", arg, {200})
|
||||
if not code then
|
||||
server:close()
|
||||
|
@ -321,7 +321,7 @@ function Private.send_indirect(data, send_cb, chunk, size)
|
|||
data:close()
|
||||
return err
|
||||
end
|
||||
sent = sent + strlen(chunk)
|
||||
sent = sent + string.len(chunk)
|
||||
if sent >= size then break end
|
||||
chunk, size = send_cb()
|
||||
end
|
||||
|
@ -374,7 +374,7 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.change_type(control, params)
|
||||
local type, _
|
||||
_, _, type = strfind(params or "", "type=(.)")
|
||||
_, _, type = string.find(params or "", "type=(.)")
|
||||
if type == "a" or type == "i" then
|
||||
local code, err = Private.command(control, "type", type, {200})
|
||||
if not code then return err end
|
||||
|
@ -391,7 +391,7 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.open(parsed)
|
||||
-- start control connection
|
||||
local control, err = connect(parsed.host, parsed.port)
|
||||
local control, err = socket.connect(parsed.host, parsed.port)
|
||||
if not control then return nil, err end
|
||||
-- make sure we don't block forever
|
||||
control:timeout(Public.TIMEOUT)
|
||||
|
@ -423,7 +423,7 @@ end
|
|||
-- err: error message if any
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.change_dir(control, segment)
|
||||
local n = getn(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
|
||||
|
@ -443,7 +443,7 @@ end
|
|||
function Private.upload(control, request, segment)
|
||||
local code, name, content_cb
|
||||
-- get remote file name
|
||||
name = segment[getn(segment)]
|
||||
name = segment[table.getn(segment)]
|
||||
if not name then
|
||||
control:close()
|
||||
return "Invalid file path"
|
||||
|
@ -472,7 +472,7 @@ function Private.download(control, request, segment)
|
|||
is_directory = segment.is_directory
|
||||
content_cb = request.content_cb
|
||||
-- get remote file name
|
||||
name = segment[getn(segment)]
|
||||
name = segment[table.getn(segment)]
|
||||
if not name and not is_directory then
|
||||
control:close()
|
||||
return "Invalid file path"
|
||||
|
@ -498,7 +498,7 @@ end
|
|||
-- parsed: a table with parsed components
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.parse_url(request)
|
||||
local parsed = URL.parse_url(request.url, {
|
||||
local parsed = socket.url.parse(request.url, {
|
||||
host = "",
|
||||
user = "anonymous",
|
||||
port = 21,
|
||||
|
@ -521,9 +521,10 @@ end
|
|||
-- 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")
|
||||
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")
|
||||
return segment
|
||||
end
|
||||
|
||||
|
@ -560,7 +561,7 @@ end
|
|||
function Public.get_cb(request)
|
||||
local parsed = Private.parse_url(request)
|
||||
if parsed.scheme ~= "ftp" then
|
||||
return format("unknown scheme '%s'", parsed.scheme)
|
||||
return string.format("unknown scheme '%s'", parsed.scheme)
|
||||
end
|
||||
local control, err = Private.open(parsed)
|
||||
if not control then return err end
|
||||
|
@ -586,7 +587,7 @@ end
|
|||
function Public.put_cb(request)
|
||||
local parsed = Private.parse_url(request)
|
||||
if parsed.scheme ~= "ftp" then
|
||||
return format("unknown scheme '%s'", parsed.scheme)
|
||||
return string.format("unknown scheme '%s'", parsed.scheme)
|
||||
end
|
||||
local control, err = Private.open(parsed)
|
||||
if not control then return err end
|
||||
|
@ -612,7 +613,7 @@ end
|
|||
function Public.put(url_or_request, content)
|
||||
local request = Private.build_request(url_or_request)
|
||||
request.content_cb = function()
|
||||
return content, strlen(content)
|
||||
return content, string.len(content)
|
||||
end
|
||||
return Public.put_cb(request)
|
||||
end
|
||||
|
@ -630,7 +631,7 @@ end
|
|||
-- err: error message in case of error, nil otherwise
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.get(url_or_request)
|
||||
local cat = Concat.create()
|
||||
local cat = socket.concat.create()
|
||||
local request = Private.build_request(url_or_request)
|
||||
request.content_cb = function(chunk, err)
|
||||
if chunk then cat:addstring(chunk) end
|
||||
|
|
10
src/http.lua
10
src/http.lua
|
@ -8,7 +8,7 @@
|
|||
-----------------------------------------------------------------------------
|
||||
|
||||
local Public, Private = {}, {}
|
||||
http = Public
|
||||
socket.http = Public
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
|
@ -427,7 +427,7 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.authorize(request, parsed, response)
|
||||
request.headers["authorization"] = "Basic " ..
|
||||
Code.base64(parsed.user .. ":" .. parsed.password)
|
||||
socket.code.base64(parsed.user .. ":" .. parsed.password)
|
||||
local authorize = {
|
||||
redirects = request.redirects,
|
||||
method = request.method,
|
||||
|
@ -471,7 +471,7 @@ function Private.redirect(request, response)
|
|||
method = request.method,
|
||||
-- the RFC says the redirect URL has to be absolute, but some
|
||||
-- servers do not respect that
|
||||
url = URL.absolute_url(request.url, response.headers["location"]),
|
||||
url = socket.url.absolute(request.url, response.headers["location"]),
|
||||
body_cb = request.body_cb,
|
||||
headers = request.headers
|
||||
}
|
||||
|
@ -535,7 +535,7 @@ end
|
|||
-- error: error message, or nil if successfull
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.request_cb(request, response)
|
||||
local parsed = URL.parse_url(request.url, {
|
||||
local parsed = socket.url.parse(request.url, {
|
||||
host = "",
|
||||
port = Public.PORT,
|
||||
path ="/",
|
||||
|
@ -622,7 +622,7 @@ function Public.request(request)
|
|||
return request.body, string.len(request.body)
|
||||
end
|
||||
end
|
||||
local cat = Concat.create()
|
||||
local cat = socket.concat.create()
|
||||
response.body_cb = function(chunk, err)
|
||||
if chunk then cat:addstring(chunk) end
|
||||
return 1
|
||||
|
|
42
src/mbox.lua
42
src/mbox.lua
|
@ -4,11 +4,11 @@ mbox = Public
|
|||
|
||||
function Public.split_message(message_s)
|
||||
local message = {}
|
||||
message_s = gsub(message_s, "\r\n", "\n")
|
||||
gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end)
|
||||
gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end)
|
||||
message_s = string.gsub(message_s, "\r\n", "\n")
|
||||
string.gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end)
|
||||
string.gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end)
|
||||
if not message.body then
|
||||
gsub(message_s, "^\n(.*)", function (b) %message.body = b end)
|
||||
string.gsub(message_s, "^\n(.*)", function (b) %message.body = b end)
|
||||
end
|
||||
if not message.headers and not message.body then
|
||||
message.headers = message_s
|
||||
|
@ -18,26 +18,26 @@ end
|
|||
|
||||
function Public.split_headers(headers_s)
|
||||
local headers = {}
|
||||
headers_s = gsub(headers_s, "\r\n", "\n")
|
||||
headers_s = gsub(headers_s, "\n[ ]+", " ")
|
||||
gsub("\n" .. headers_s, "\n([^\n]+)", function (h) tinsert(%headers, h) end)
|
||||
headers_s = string.gsub(headers_s, "\r\n", "\n")
|
||||
headers_s = string.gsub(headers_s, "\n[ ]+", " ")
|
||||
string.gsub("\n" .. headers_s, "\n([^\n]+)", function (h) table.insert(%headers, h) end)
|
||||
return headers
|
||||
end
|
||||
|
||||
function Public.parse_header(header_s)
|
||||
header_s = gsub(header_s, "\n[ ]+", " ")
|
||||
header_s = gsub(header_s, "\n+", "")
|
||||
local _, __, name, value = strfind(header_s, "([^%s:]-):%s*(.*)")
|
||||
header_s = string.gsub(header_s, "\n[ ]+", " ")
|
||||
header_s = string.gsub(header_s, "\n+", "")
|
||||
local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)")
|
||||
return name, value
|
||||
end
|
||||
|
||||
function Public.parse_headers(headers_s)
|
||||
local headers_t = %Public.split_headers(headers_s)
|
||||
local headers = {}
|
||||
for i = 1, getn(headers_t) do
|
||||
for i = 1, table.getn(headers_t) do
|
||||
local name, value = %Public.parse_header(headers_t[i])
|
||||
if name then
|
||||
name = strlower(name)
|
||||
name = string.lower(name)
|
||||
if headers[name] then
|
||||
headers[name] = headers[name] .. ", " .. value
|
||||
else headers[name] = value end
|
||||
|
@ -47,34 +47,34 @@ function Public.parse_headers(headers_s)
|
|||
end
|
||||
|
||||
function Public.parse_from(from)
|
||||
local _, __, name, address = strfind(from, "^%s*(.-)%s*%<(.-)%>")
|
||||
local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>")
|
||||
if not address then
|
||||
_, __, address = strfind(from, "%s*(.+)%s*")
|
||||
_, __, address = string.find(from, "%s*(.+)%s*")
|
||||
end
|
||||
name = name or ""
|
||||
address = address or ""
|
||||
if name == "" then name = address end
|
||||
name = gsub(name, '"', "")
|
||||
name = string.gsub(name, '"', "")
|
||||
return name, address
|
||||
end
|
||||
|
||||
function Public.split_mbox(mbox_s)
|
||||
mbox = {}
|
||||
mbox_s = gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
|
||||
mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
|
||||
local nj, i, j = 1, 1, 1
|
||||
while 1 do
|
||||
i, nj = strfind(mbox_s, "\n\nFrom .-\n", j)
|
||||
i, nj = string.find(mbox_s, "\n\nFrom .-\n", j)
|
||||
if not i then break end
|
||||
local message = strsub(mbox_s, j, i-1)
|
||||
tinsert(mbox, message)
|
||||
local message = string.sub(mbox_s, j, i-1)
|
||||
table.insert(mbox, message)
|
||||
j = nj+1
|
||||
end
|
||||
return mbox
|
||||
end
|
||||
|
||||
function Public.parse_mbox(mbox_s)
|
||||
function Public.parse(mbox_s)
|
||||
local mbox = %Public.split_mbox(mbox_s)
|
||||
for i = 1, getn(mbox) do
|
||||
for i = 1, table.getn(mbox) do
|
||||
mbox[i] = %Public.parse_message(mbox[i])
|
||||
end
|
||||
return mbox
|
||||
|
|
21
src/smtp.lua
21
src/smtp.lua
|
@ -8,7 +8,7 @@
|
|||
-----------------------------------------------------------------------------
|
||||
|
||||
local Public, Private = {}, {}
|
||||
SMTP = Public
|
||||
socket.smtp = Public
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
|
@ -47,7 +47,7 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.try_receive(...)
|
||||
local sock = arg[1]
|
||||
local data, err = call(sock.receive, arg)
|
||||
local data, err = sock.receive(unpack(arg))
|
||||
if err then sock:close() end
|
||||
return data, err
|
||||
end
|
||||
|
@ -81,13 +81,13 @@ function Private.get_answer(control)
|
|||
local line, err = Private.try_receive(control)
|
||||
local answer = line
|
||||
if err then return nil, err end
|
||||
_,_, code, sep = strfind(line, "^(%d%d%d)(.)")
|
||||
_,_, 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 = strfind(line, "^(%d%d%d)(.)")
|
||||
_,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)")
|
||||
answer = answer .. "\n" .. line
|
||||
until code == lastcode and sep == " " -- answer ends with same code
|
||||
end
|
||||
|
@ -108,7 +108,7 @@ 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 i = 1, getn(success) do
|
||||
for i = 1, table.getn(success) do
|
||||
if code == success[i] then
|
||||
return code, answer
|
||||
end
|
||||
|
@ -157,7 +157,7 @@ end
|
|||
-- answer: complete server reply or error message
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.send_mail(sock, sender)
|
||||
local param = format("FROM:<%s>", sender or "")
|
||||
local param = string.format("FROM:<%s>", sender or "")
|
||||
local err = Private.send_command(sock, "MAIL", param)
|
||||
if err then return nil, err end
|
||||
return Private.check_answer(sock, 250)
|
||||
|
@ -198,7 +198,7 @@ function Private.send_data(sock, headers, body)
|
|||
local code, answer = Private.check_answer(sock, 354)
|
||||
if not code then return nil, answer end
|
||||
-- avoid premature end in message body
|
||||
body = gsub(body or "", "\n%.", "\n%.%.")
|
||||
body = string.gsub(body or "", "\n%.", "\n%.%.")
|
||||
-- mark end of message body
|
||||
body = body .. "\r\n.\r\n"
|
||||
err = Private.send_headers(sock, headers)
|
||||
|
@ -220,8 +220,9 @@ function Private.send_rcpt(sock, rcpt)
|
|||
local err
|
||||
local code, answer = nil, "No recipient specified"
|
||||
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
||||
for i = 1, getn(rcpt) do
|
||||
err = Private.send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
|
||||
for i = 1, table.getn(rcpt) do
|
||||
err = Private.send_command(sock, "RCPT",
|
||||
string.format("TO:<%s>", rcpt[i]))
|
||||
if err then return nil, err end
|
||||
code, answer = Private.check_answer(sock, {250, 251})
|
||||
if not code then return code, answer end
|
||||
|
@ -242,7 +243,7 @@ function Private.open(server)
|
|||
-- default server
|
||||
server = server or Public.SERVER
|
||||
-- connect to server and make sure we won't hang
|
||||
local sock, err = connect(server, Public.PORT)
|
||||
local sock, err = socket.connect(server, Public.PORT)
|
||||
if not sock then return nil, err end
|
||||
sock:timeout(Public.TIMEOUT)
|
||||
-- initial server greeting
|
||||
|
|
18
src/url.lua
18
src/url.lua
|
@ -8,7 +8,7 @@
|
|||
----------------------------------------------------------------------------
|
||||
|
||||
local Public, Private = {}, {}
|
||||
URL = Public
|
||||
socket.url = Public
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Parses a url and returns a table with all its parts according to RFC 2396
|
||||
|
@ -28,7 +28,7 @@ URL = Public
|
|||
-- Obs:
|
||||
-- the leading '/' in {/<path>} is considered part of <path>
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.parse_url(url, default)
|
||||
function Public.parse(url, default)
|
||||
-- initialize default parameters
|
||||
local parsed = default or {}
|
||||
-- empty url is parsed to nil
|
||||
|
@ -70,7 +70,7 @@ end
|
|||
-- Returns
|
||||
-- a stringing with the corresponding URL
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.build_url(parsed)
|
||||
function Public.build(parsed)
|
||||
local url = parsed.path or ""
|
||||
if parsed.params then url = url .. ";" .. parsed.params end
|
||||
if parsed.query then url = url .. "?" .. parsed.query end
|
||||
|
@ -102,9 +102,9 @@ end
|
|||
-- Returns
|
||||
-- corresponding absolute url
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.absolute_url(base_url, relative_url)
|
||||
local base = Public.parse_url(base_url)
|
||||
local relative = Public.parse_url(relative_url)
|
||||
function Public.absolute(base_url, relative_url)
|
||||
local base = Public.parse(base_url)
|
||||
local relative = Public.parse(relative_url)
|
||||
if not base then return relative_url
|
||||
elseif not relative then return base_url
|
||||
elseif relative.scheme then return relative_url
|
||||
|
@ -124,7 +124,7 @@ function Public.absolute_url(base_url, relative_url)
|
|||
relative.path = Private.absolute_path(base.path,relative.path)
|
||||
end
|
||||
end
|
||||
return Public.build_url(relative)
|
||||
return Public.build(relative)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -141,7 +141,7 @@ function Public.parse_path(path)
|
|||
path = string.gsub(path, "%s", "")
|
||||
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
|
||||
for i = 1, table.getn(parsed) do
|
||||
parsed[i] = Code.unescape(parsed[i])
|
||||
parsed[i] = socket.code.unescape(parsed[i])
|
||||
end
|
||||
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
|
||||
if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
|
||||
|
@ -201,7 +201,7 @@ function Private.protect_segment(s)
|
|||
local segment_set = Private.segment_set
|
||||
return string.gsub(s, "(%W)", function (c)
|
||||
if segment_set[c] then return c
|
||||
else return Code.escape(c) end
|
||||
else return socket.code.escape(c) end
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue