mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-02-13 07:22:52 +01:00
Updated for Lua 4.1-w3.
This commit is contained in:
parent
60e7bf48b0
commit
5b2a124305
117
src/ftp.lua
117
src/ftp.lua
@ -1,6 +1,6 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- FTP support for the Lua language
|
-- FTP support for the Lua language
|
||||||
-- LuaSocket 1.4 toolkit.
|
-- LuaSocket 1.5 toolkit.
|
||||||
-- Author: Diego Nehab
|
-- Author: Diego Nehab
|
||||||
-- Date: 26/12/2000
|
-- Date: 26/12/2000
|
||||||
-- Conforming to: RFC 959, LTN7
|
-- Conforming to: RFC 959, LTN7
|
||||||
@ -23,13 +23,6 @@ Public.EMAIL = "anonymous@anonymous.org"
|
|||||||
-- block size used in transfers
|
-- block size used in transfers
|
||||||
Public.BLOCKSIZE = 8192
|
Public.BLOCKSIZE = 8192
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
|
||||||
-- Required libraries
|
|
||||||
-----------------------------------------------------------------------------
|
|
||||||
dofile "concat.lua"
|
|
||||||
dofile "url.lua"
|
|
||||||
dofile "code.lua"
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Tries to send DOS mode lines. Closes socket on error.
|
-- Tries to send DOS mode lines. Closes socket on error.
|
||||||
-- Input
|
-- Input
|
||||||
@ -91,7 +84,7 @@ function Private.send_command(control, cmd, arg)
|
|||||||
local line
|
local line
|
||||||
if arg then line = cmd .. " " .. arg
|
if arg then line = cmd .. " " .. arg
|
||||||
else line = cmd end
|
else line = cmd end
|
||||||
return %Private.try_sendline(control, line)
|
return Private.try_sendline(control, line)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -104,14 +97,14 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.get_answer(control)
|
function Private.get_answer(control)
|
||||||
local code, lastcode, sep, _
|
local code, lastcode, sep, _
|
||||||
local line, err = %Private.try_receive(control)
|
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 = %Private.try_receive(control)
|
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,7 +123,7 @@ end
|
|||||||
-- answer: server complete answer or system error message
|
-- answer: server complete answer or system error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.check_answer(control, success)
|
function Private.check_answer(control, success)
|
||||||
local answer, code = %Private.get_answer(control)
|
local answer, code = Private.get_answer(control)
|
||||||
if not answer then return nil, code end
|
if not answer then 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
|
||||||
@ -155,9 +148,9 @@ end
|
|||||||
-- answer: server complete answer or system error message
|
-- answer: server complete answer or system error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.command(control, cmd, arg, success)
|
function Private.command(control, cmd, arg, success)
|
||||||
local err = %Private.send_command(control, cmd, arg)
|
local err = Private.send_command(control, cmd, arg)
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
return %Private.check_answer(control, success)
|
return Private.check_answer(control, success)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -169,9 +162,9 @@ end
|
|||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.greet(control)
|
function Private.greet(control)
|
||||||
local code, answer = %Private.check_answer(control, {120, 220})
|
local code, answer = Private.check_answer(control, {120, 220})
|
||||||
if code == 120 then -- please try again, somewhat busy now...
|
if code == 120 then -- please try again, somewhat busy now...
|
||||||
return %Private.check_answer(control, {220})
|
return Private.check_answer(control, {220})
|
||||||
end
|
end
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
@ -187,9 +180,9 @@ end
|
|||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.login(control, user, password)
|
function Private.login(control, user, password)
|
||||||
local code, answer = %Private.command(control, "user", user, {230, 331})
|
local code, answer = Private.command(control, "user", user, {230, 331})
|
||||||
if code == 331 and password then -- need pass and we have pass
|
if code == 331 and password then -- need pass and we have pass
|
||||||
return %Private.command(control, "pass", password, {230, 202})
|
return Private.command(control, "pass", password, {230, 202})
|
||||||
end
|
end
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
@ -204,7 +197,7 @@ end
|
|||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.cwd(control, path)
|
function Private.cwd(control, path)
|
||||||
if path then return %Private.command(control, "cwd", path, {250})
|
if path then return Private.command(control, "cwd", path, {250})
|
||||||
else return 250, nil end
|
else return 250, nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -221,13 +214,13 @@ function Private.port(control)
|
|||||||
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(%Public.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 = %Private.command(control, "port", arg, {200})
|
code, answer = Private.command(control, "port", arg, {200})
|
||||||
if not code then
|
if not code then
|
||||||
server:close()
|
server:close()
|
||||||
return nil, answer
|
return nil, answer
|
||||||
@ -243,7 +236,7 @@ end
|
|||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.logout(control)
|
function Private.logout(control)
|
||||||
local code, answer = %Private.command(control, "quit", nil, {221})
|
local code, answer = Private.command(control, "quit", nil, {221})
|
||||||
if code then control:close() end
|
if code then control:close() end
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
@ -259,7 +252,7 @@ end
|
|||||||
function Private.receive_indirect(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 = %Private.try_receive(data, %Public.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
|
||||||
@ -282,11 +275,11 @@ function Private.retrieve(control, server, name, is_directory, content_cb)
|
|||||||
local data
|
local data
|
||||||
-- ask server for file or directory listing accordingly
|
-- ask server for file or directory listing accordingly
|
||||||
if is_directory then
|
if is_directory then
|
||||||
code, answer = %Private.cwd(control, name)
|
code, answer = Private.cwd(control, name)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
code, answer = %Private.command(control, "nlst", nil, {150, 125})
|
code, answer = Private.command(control, "nlst", nil, {150, 125})
|
||||||
else
|
else
|
||||||
code, answer = %Private.command(control, "retr", name, {150, 125})
|
code, answer = Private.command(control, "retr", name, {150, 125})
|
||||||
end
|
end
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
data, answer = server:accept()
|
data, answer = server:accept()
|
||||||
@ -295,14 +288,14 @@ function Private.retrieve(control, server, name, is_directory, content_cb)
|
|||||||
control:close()
|
control:close()
|
||||||
return answer
|
return answer
|
||||||
end
|
end
|
||||||
answer = %Private.receive_indirect(data, content_cb)
|
answer = Private.receive_indirect(data, content_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
|
||||||
return %Private.check_answer(control, {226, 250})
|
return Private.check_answer(control, {226, 250})
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -347,7 +340,7 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.store(control, server, file, send_cb)
|
function Private.store(control, server, file, send_cb)
|
||||||
local data, err
|
local data, err
|
||||||
local code, answer = %Private.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
|
||||||
@ -360,7 +353,7 @@ function Private.store(control, server, file, send_cb)
|
|||||||
return nil, answer
|
return nil, answer
|
||||||
end
|
end
|
||||||
-- send whole file
|
-- send whole file
|
||||||
err = %Private.send_indirect(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
|
||||||
@ -368,7 +361,7 @@ function Private.store(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 %Private.check_answer(control, {226, 250})
|
return Private.check_answer(control, {226, 250})
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -383,7 +376,7 @@ function Private.change_type(control, params)
|
|||||||
local type, _
|
local type, _
|
||||||
_, _, type = strfind(params or "", "type=(.)")
|
_, _, type = strfind(params or "", "type=(.)")
|
||||||
if type == "a" or type == "i" then
|
if type == "a" or type == "i" then
|
||||||
local code, err = %Private.command(control, "type", type, {200})
|
local code, err = Private.command(control, "type", type, {200})
|
||||||
if not code then return err end
|
if not code then return err end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -401,12 +394,12 @@ function Private.open(parsed)
|
|||||||
local control, err = connect(parsed.host, parsed.port)
|
local control, err = connect(parsed.host, parsed.port)
|
||||||
if not control then return nil, err end
|
if not control then return nil, err end
|
||||||
-- make sure we don't block forever
|
-- make sure we don't block forever
|
||||||
control:timeout(%Public.TIMEOUT)
|
control:timeout(Public.TIMEOUT)
|
||||||
-- check greeting
|
-- check greeting
|
||||||
local code, answer = %Private.greet(control)
|
local code, answer = Private.greet(control)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- try to log in
|
-- try to log in
|
||||||
code, err = %Private.login(control, parsed.user, parsed.password)
|
code, err = Private.login(control, parsed.user, parsed.password)
|
||||||
if not code then return nil, err
|
if not code then return nil, err
|
||||||
else return control end
|
else return control end
|
||||||
end
|
end
|
||||||
@ -418,7 +411,7 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.close(control)
|
function Private.close(control)
|
||||||
-- disconnect
|
-- disconnect
|
||||||
%Private.logout(control)
|
Private.logout(control)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -432,7 +425,7 @@ end
|
|||||||
function Private.change_dir(control, segment)
|
function Private.change_dir(control, segment)
|
||||||
local n = getn(segment)
|
local n = getn(segment)
|
||||||
for i = 1, n-1 do
|
for i = 1, n-1 do
|
||||||
local code, answer = %Private.cwd(control, segment[i])
|
local code, answer = Private.cwd(control, segment[i])
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -457,10 +450,10 @@ function Private.upload(control, request, segment)
|
|||||||
end
|
end
|
||||||
content_cb = request.content_cb
|
content_cb = request.content_cb
|
||||||
-- setup passive connection
|
-- setup passive connection
|
||||||
local server, answer = %Private.port(control)
|
local server, answer = Private.port(control)
|
||||||
if not server then return answer end
|
if not server then return answer end
|
||||||
-- ask server to receive file
|
-- ask server to receive file
|
||||||
code, answer = %Private.store(control, server, name, content_cb)
|
code, answer = Private.store(control, server, name, content_cb)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -485,10 +478,10 @@ function Private.download(control, request, segment)
|
|||||||
return "Invalid file path"
|
return "Invalid file path"
|
||||||
end
|
end
|
||||||
-- setup passive connection
|
-- setup passive connection
|
||||||
local server, answer = %Private.port(control)
|
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
|
||||||
code, answer = %Private.retrieve(control, server, name,
|
code, answer = Private.retrieve(control, server, name,
|
||||||
is_directory, content_cb)
|
is_directory, content_cb)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
end
|
end
|
||||||
@ -510,7 +503,7 @@ function Private.parse_url(request)
|
|||||||
user = "anonymous",
|
user = "anonymous",
|
||||||
port = 21,
|
port = 21,
|
||||||
path = "/",
|
path = "/",
|
||||||
password = %Public.EMAIL,
|
password = Public.EMAIL,
|
||||||
scheme = "ftp"
|
scheme = "ftp"
|
||||||
})
|
})
|
||||||
-- explicit login information overrides that given by URL
|
-- explicit login information overrides that given by URL
|
||||||
@ -565,17 +558,17 @@ end
|
|||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.get_cb(request)
|
function Public.get_cb(request)
|
||||||
local parsed = %Private.parse_url(request)
|
local parsed = Private.parse_url(request)
|
||||||
if parsed.scheme ~= "ftp" then
|
if parsed.scheme ~= "ftp" then
|
||||||
return format("unknown scheme '%s'", parsed.scheme)
|
return format("unknown scheme '%s'", parsed.scheme)
|
||||||
end
|
end
|
||||||
local control, err = %Private.open(parsed)
|
local control, err = Private.open(parsed)
|
||||||
if not control then return err end
|
if not control then return err end
|
||||||
local segment = %Private.parse_path(parsed)
|
local segment = Private.parse_path(parsed)
|
||||||
return %Private.change_dir(control, segment) or
|
return Private.change_dir(control, segment) or
|
||||||
%Private.change_type(control, parsed.params) or
|
Private.change_type(control, parsed.params) or
|
||||||
%Private.download(control, request, segment) or
|
Private.download(control, request, segment) or
|
||||||
%Private.close(control)
|
Private.close(control)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -591,17 +584,17 @@ end
|
|||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.put_cb(request)
|
function Public.put_cb(request)
|
||||||
local parsed = %Private.parse_url(request)
|
local parsed = Private.parse_url(request)
|
||||||
if parsed.scheme ~= "ftp" then
|
if parsed.scheme ~= "ftp" then
|
||||||
return format("unknown scheme '%s'", parsed.scheme)
|
return format("unknown scheme '%s'", parsed.scheme)
|
||||||
end
|
end
|
||||||
local control, err = %Private.open(parsed)
|
local control, err = Private.open(parsed)
|
||||||
if not control then return err end
|
if not control then return err end
|
||||||
local segment = %Private.parse_path(parsed)
|
local segment = Private.parse_path(parsed)
|
||||||
return %Private.change_dir(control, segment) or
|
return Private.change_dir(control, segment) or
|
||||||
%Private.change_type(control, parsed.params) or
|
Private.change_type(control, parsed.params) or
|
||||||
%Private.upload(control, request, segment) or
|
Private.upload(control, request, segment) or
|
||||||
%Private.close(control)
|
Private.close(control)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -617,11 +610,11 @@ end
|
|||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.put(url_or_request, content)
|
function Public.put(url_or_request, content)
|
||||||
local request = %Private.build_request(url_or_request)
|
local request = Private.build_request(url_or_request)
|
||||||
request.content_cb = function()
|
request.content_cb = function()
|
||||||
return %content, strlen(%content)
|
return content, strlen(content)
|
||||||
end
|
end
|
||||||
return %Public.put_cb(request)
|
return Public.put_cb(request)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -638,11 +631,11 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.get(url_or_request)
|
function Public.get(url_or_request)
|
||||||
local cat = Concat.create()
|
local cat = Concat.create()
|
||||||
local request = %Private.build_request(url_or_request)
|
local request = Private.build_request(url_or_request)
|
||||||
request.content_cb = function(chunk, err)
|
request.content_cb = function(chunk, err)
|
||||||
if chunk then %cat:addstring(chunk) end
|
if chunk then cat:addstring(chunk) end
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
local err = %Public.get_cb(request)
|
local err = Public.get_cb(request)
|
||||||
return cat:getresult(), err
|
return cat:getresult(), err
|
||||||
end
|
end
|
||||||
|
64
src/smtp.lua
64
src/smtp.lua
@ -1,6 +1,6 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- SMTP support for the Lua language.
|
-- SMTP support for the Lua language.
|
||||||
-- LuaSocket 1.4 toolkit
|
-- LuaSocket 1.5 toolkit
|
||||||
-- Author: Diego Nehab
|
-- Author: Diego Nehab
|
||||||
-- Date: 26/12/2000
|
-- Date: 26/12/2000
|
||||||
-- Conforming to: RFC 821, LTN7
|
-- Conforming to: RFC 821, LTN7
|
||||||
@ -65,7 +65,7 @@ function Private.send_command(sock, command, param)
|
|||||||
local line
|
local line
|
||||||
if param then line = command .. " " .. param .. "\r\n"
|
if param then line = command .. " " .. param .. "\r\n"
|
||||||
else line = command .. "\r\n" end
|
else line = command .. "\r\n" end
|
||||||
return %Private.try_send(sock, line)
|
return Private.try_send(sock, line)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -78,14 +78,14 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.get_answer(control)
|
function Private.get_answer(control)
|
||||||
local code, lastcode, sep, _
|
local code, lastcode, sep, _
|
||||||
local line, err = %Private.try_receive(control)
|
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 = %Private.try_receive(control)
|
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
|
||||||
@ -105,7 +105,7 @@ end
|
|||||||
-- answer: complete server answer or system error message
|
-- answer: complete server answer or system error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.check_answer(control, success)
|
function Private.check_answer(control, success)
|
||||||
local answer, code = %Private.get_answer(control)
|
local answer, code = Private.get_answer(control)
|
||||||
if not answer then return nil, code end
|
if not answer then 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
|
||||||
@ -126,9 +126,9 @@ end
|
|||||||
-- answer: complete server reply
|
-- answer: complete server reply
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.send_helo(sock)
|
function Private.send_helo(sock)
|
||||||
local err = %Private.send_command(sock, "HELO", %Public.DOMAIN)
|
local err = Private.send_command(sock, "HELO", Public.DOMAIN)
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
return %Private.check_answer(sock, 250)
|
return Private.check_answer(sock, 250)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -140,9 +140,9 @@ end
|
|||||||
-- answer: complete server reply or error message
|
-- answer: complete server reply or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.send_quit(sock)
|
function Private.send_quit(sock)
|
||||||
local err = %Private.send_command(sock, "QUIT")
|
local err = Private.send_command(sock, "QUIT")
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
local code, answer = %Private.check_answer(sock, 221)
|
local code, answer = Private.check_answer(sock, 221)
|
||||||
sock:close()
|
sock:close()
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
@ -158,9 +158,9 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.send_mail(sock, sender)
|
function Private.send_mail(sock, sender)
|
||||||
local param = format("FROM:<%s>", sender or "")
|
local param = format("FROM:<%s>", sender or "")
|
||||||
local err = %Private.send_command(sock, "MAIL", param)
|
local err = Private.send_command(sock, "MAIL", param)
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
return %Private.check_answer(sock, 250)
|
return Private.check_answer(sock, 250)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -175,11 +175,11 @@ function Private.send_headers(sock, headers)
|
|||||||
local err
|
local err
|
||||||
-- send request headers
|
-- send request headers
|
||||||
for i, v in headers or {} do
|
for i, v in headers or {} do
|
||||||
err = %Private.try_send(sock, i .. ": " .. v .. "\r\n")
|
err = Private.try_send(sock, i .. ": " .. v .. "\r\n")
|
||||||
if err then return err end
|
if err then return err end
|
||||||
end
|
end
|
||||||
-- mark end of request headers
|
-- mark end of request headers
|
||||||
return %Private.try_send(sock, "\r\n")
|
return Private.try_send(sock, "\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -193,18 +193,18 @@ end
|
|||||||
-- answer: complete server reply or error message
|
-- answer: complete server reply or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.send_data(sock, headers, body)
|
function Private.send_data(sock, headers, body)
|
||||||
local err = %Private.send_command(sock, "DATA")
|
local err = Private.send_command(sock, "DATA")
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
local code, answer = %Private.check_answer(sock, 354)
|
local code, answer = Private.check_answer(sock, 354)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- avoid premature end in message body
|
-- avoid premature end in message body
|
||||||
body = gsub(body or "", "\n%.", "\n%.%.")
|
body = gsub(body or "", "\n%.", "\n%.%.")
|
||||||
-- mark end of message body
|
-- mark end of message body
|
||||||
body = body .. "\r\n.\r\n"
|
body = body .. "\r\n.\r\n"
|
||||||
err = %Private.send_headers(sock, headers)
|
err = Private.send_headers(sock, headers)
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
err = %Private.try_send(sock, body)
|
err = Private.try_send(sock, body)
|
||||||
return %Private.check_answer(sock, 250)
|
return Private.check_answer(sock, 250)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -221,9 +221,9 @@ function Private.send_rcpt(sock, rcpt)
|
|||||||
local code, answer = nil, "No recipient specified"
|
local code, answer = nil, "No recipient specified"
|
||||||
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
||||||
for i = 1, getn(rcpt) do
|
for i = 1, getn(rcpt) do
|
||||||
err = %Private.send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
|
err = Private.send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
code, answer = %Private.check_answer(sock, {250, 251})
|
code, answer = Private.check_answer(sock, {250, 251})
|
||||||
if not code then return code, answer end
|
if not code then return code, answer end
|
||||||
end
|
end
|
||||||
return code, answer
|
return code, answer
|
||||||
@ -240,16 +240,16 @@ end
|
|||||||
function Private.open(server)
|
function Private.open(server)
|
||||||
local code, answer
|
local code, answer
|
||||||
-- default server
|
-- default server
|
||||||
server = server or %Public.SERVER
|
server = server or Public.SERVER
|
||||||
-- connect to server and make sure we won't hang
|
-- connect to server and make sure we won't hang
|
||||||
local sock, err = connect(server, %Public.PORT)
|
local sock, err = connect(server, Public.PORT)
|
||||||
if not sock then return nil, err end
|
if not sock then return nil, err end
|
||||||
sock:timeout(%Public.TIMEOUT)
|
sock:timeout(Public.TIMEOUT)
|
||||||
-- initial server greeting
|
-- initial server greeting
|
||||||
code, answer = %Private.check_answer(sock, 220)
|
code, answer = Private.check_answer(sock, 220)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- HELO
|
-- HELO
|
||||||
code, answer = %Private.send_helo(sock)
|
code, answer = Private.send_helo(sock)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
return sock
|
return sock
|
||||||
end
|
end
|
||||||
@ -270,13 +270,13 @@ end
|
|||||||
function Private.send(sock, message)
|
function Private.send(sock, message)
|
||||||
local code, answer
|
local code, answer
|
||||||
-- MAIL
|
-- MAIL
|
||||||
code, answer = %Private.send_mail(sock, message.from)
|
code, answer = Private.send_mail(sock, message.from)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- RCPT
|
-- RCPT
|
||||||
code, answer = %Private.send_rcpt(sock, message.rcpt)
|
code, answer = Private.send_rcpt(sock, message.rcpt)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- DATA
|
-- DATA
|
||||||
return %Private.send_data(sock, message.headers, message.body)
|
return Private.send_data(sock, message.headers, message.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -289,7 +289,7 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Private.close(sock)
|
function Private.close(sock)
|
||||||
-- QUIT
|
-- QUIT
|
||||||
return %Private.send_quit(sock)
|
return Private.send_quit(sock)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -305,11 +305,11 @@ end
|
|||||||
-- nil if successfull, error message in case of error
|
-- nil if successfull, error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.mail(message)
|
function Public.mail(message)
|
||||||
local sock, err = %Private.open(message.server)
|
local sock, err = Private.open(message.server)
|
||||||
if not sock then return err end
|
if not sock then return err end
|
||||||
local code, answer = %Private.send(sock, message)
|
local code, answer = Private.send(sock, message)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
code, answer = %Private.close(sock)
|
code, answer = Private.close(sock)
|
||||||
if code then return nil end
|
if code then return nil end
|
||||||
return answer
|
return answer
|
||||||
end
|
end
|
||||||
|
36
src/url.lua
36
src/url.lua
@ -1,6 +1,6 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- URI parsing, composition and relative URL resolution
|
-- URI parsing, composition and relative URL resolution
|
||||||
-- LuaSocket 1.4 toolkit.
|
-- LuaSocket 1.5 toolkit.
|
||||||
-- Author: Diego Nehab
|
-- Author: Diego Nehab
|
||||||
-- Date: 20/7/2001
|
-- Date: 20/7/2001
|
||||||
-- Conforming to: RFC 2396, LTN7
|
-- Conforming to: RFC 2396, LTN7
|
||||||
@ -36,24 +36,24 @@ function Public.parse_url(url, default)
|
|||||||
-- remove whitespace
|
-- remove whitespace
|
||||||
url = gsub(url, "%s", "")
|
url = gsub(url, "%s", "")
|
||||||
-- get fragment
|
-- get fragment
|
||||||
url = gsub(url, "#(.*)$", function(f) %parsed.fragment = f end)
|
url = gsub(url, "#(.*)$", function(f) parsed.fragment = f end)
|
||||||
-- get scheme
|
-- get scheme
|
||||||
url = gsub(url, "^([%w][%w%+%-%.]*)%:", function(s) %parsed.scheme = s end)
|
url = gsub(url, "^([%w][%w%+%-%.]*)%:", function(s) parsed.scheme = s end)
|
||||||
-- get authority
|
-- get authority
|
||||||
url = gsub(url, "^//([^/]*)", function(n) %parsed.authority = n end)
|
url = gsub(url, "^//([^/]*)", function(n) parsed.authority = n end)
|
||||||
-- get query string
|
-- get query string
|
||||||
url = gsub(url, "%?(.*)", function(q) %parsed.query = q end)
|
url = gsub(url, "%?(.*)", function(q) parsed.query = q end)
|
||||||
-- get params
|
-- get params
|
||||||
url = gsub(url, "%;(.*)", function(p) %parsed.params = p end)
|
url = gsub(url, "%;(.*)", function(p) parsed.params = p end)
|
||||||
if url ~= "" then parsed.path = url end
|
if url ~= "" then parsed.path = url end
|
||||||
local authority = parsed.authority
|
local authority = parsed.authority
|
||||||
if not authority then return parsed end
|
if not authority then return parsed end
|
||||||
authority = gsub(authority,"^([^@]*)@",function(u) %parsed.userinfo = u end)
|
authority = gsub(authority,"^([^@]*)@",function(u) parsed.userinfo = u end)
|
||||||
authority = gsub(authority, ":([^:]*)$", function(p) %parsed.port = p end)
|
authority = gsub(authority, ":([^:]*)$", function(p) parsed.port = p end)
|
||||||
if authority ~= "" then parsed.host = authority end
|
if authority ~= "" then parsed.host = authority end
|
||||||
local userinfo = parsed.userinfo
|
local userinfo = parsed.userinfo
|
||||||
if not userinfo then return parsed end
|
if not userinfo then return parsed end
|
||||||
userinfo = gsub(userinfo, ":([^:]*)$", function(p) %parsed.password = p end)
|
userinfo = gsub(userinfo, ":([^:]*)$", function(p) parsed.password = p end)
|
||||||
parsed.user = userinfo
|
parsed.user = userinfo
|
||||||
return parsed
|
return parsed
|
||||||
end
|
end
|
||||||
@ -99,8 +99,8 @@ end
|
|||||||
-- corresponding absolute url
|
-- corresponding absolute url
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.absolute_url(base_url, relative_url)
|
function Public.absolute_url(base_url, relative_url)
|
||||||
local base = %Public.parse_url(base_url)
|
local base = Public.parse_url(base_url)
|
||||||
local relative = %Public.parse_url(relative_url)
|
local relative = Public.parse_url(relative_url)
|
||||||
if not base then return relative_url
|
if not base then return relative_url
|
||||||
elseif not relative then return base_url
|
elseif not relative then return base_url
|
||||||
elseif relative.scheme then return relative_url
|
elseif relative.scheme then return relative_url
|
||||||
@ -117,10 +117,10 @@ function Public.absolute_url(base_url, relative_url)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
relative.path = %Private.absolute_path(base.path,relative.path)
|
relative.path = Private.absolute_path(base.path,relative.path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return %Public.build_url(relative)
|
return Public.build_url(relative)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ function Public.parse_path(path)
|
|||||||
local parsed = {}
|
local parsed = {}
|
||||||
path = path or ""
|
path = path or ""
|
||||||
path = gsub(path, "%s", "")
|
path = gsub(path, "%s", "")
|
||||||
gsub(path, "([^/]+)", function (s) tinsert(%parsed, s) end)
|
gsub(path, "([^/]+)", function (s) tinsert(parsed, s) end)
|
||||||
for i = 1, getn(parsed) do
|
for i = 1, getn(parsed) do
|
||||||
parsed[i] = Code.unescape(parsed[i])
|
parsed[i] = Code.unescape(parsed[i])
|
||||||
end
|
end
|
||||||
@ -166,11 +166,11 @@ function Public.build_path(parsed, unsafe)
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
for i = 1, n-1 do
|
for i = 1, n-1 do
|
||||||
path = path .. %Private.protect_segment(parsed[i])
|
path = path .. Private.protect_segment(parsed[i])
|
||||||
path = path .. "/"
|
path = path .. "/"
|
||||||
end
|
end
|
||||||
if n > 0 then
|
if n > 0 then
|
||||||
path = path .. %Private.protect_segment(parsed[n])
|
path = path .. Private.protect_segment(parsed[n])
|
||||||
if parsed.is_directory then path = path .. "/" end
|
if parsed.is_directory then path = path .. "/" end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -194,9 +194,9 @@ Private.segment_set = Private.make_set {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Private.protect_segment(s)
|
function Private.protect_segment(s)
|
||||||
local segment_set = %Private.segment_set
|
local segment_set = Private.segment_set
|
||||||
return gsub(s, "(%W)", function (c)
|
return gsub(s, "(%W)", function (c)
|
||||||
if %segment_set[c] then return c
|
if segment_set[c] then return c
|
||||||
else return Code.escape(c) end
|
else return Code.escape(c) end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user