mirror of
https://github.com/minetest-mods/irc.git
synced 2025-06-28 14:16:09 +02:00
initial commit
This commit is contained in:
50
etc/luasocket-samples/README
Normal file
50
etc/luasocket-samples/README
Normal file
@ -0,0 +1,50 @@
|
||||
This directory contains some sample programs using
|
||||
LuaSocket. This code is not supported.
|
||||
|
||||
listener.lua -- socket to stdout
|
||||
talker.lua -- stdin to socket
|
||||
|
||||
listener.lua and talker.lua are about the simplest
|
||||
applications you can write using LuaSocket. Run
|
||||
|
||||
'lua listener.lua' and 'lua talker.lua'
|
||||
|
||||
on different terminals. Whatever you type on talk.lua will
|
||||
be printed by listen.lua.
|
||||
|
||||
lpr.lua -- lpr client
|
||||
|
||||
This is a cool program written by David Burgess to print
|
||||
files using the Line Printer Daemon protocol, widely used in
|
||||
Unix machines. It uses the lp.lua implementation, in the
|
||||
etc directory. Just run 'lua lpr.lua <filename>
|
||||
queue=<printername>' and the file will print!
|
||||
|
||||
cddb.lua -- CDDB client
|
||||
|
||||
This is the first try on a simple CDDB client. Not really
|
||||
useful, but one day it might become a module.
|
||||
|
||||
daytimeclnt.lua -- day time client
|
||||
|
||||
Just run the program to retrieve the hour and date in
|
||||
readable form from any server running an UDP daytime daemon.
|
||||
|
||||
echoclnt.lua -- UDP echo client
|
||||
echosrvr.lua -- UDP echo server
|
||||
|
||||
These are a UDP echo client/server pair. They work with
|
||||
other client and servers as well.
|
||||
|
||||
tinyirc.lua -- irc like broadcast server
|
||||
|
||||
This is a simple server that waits simultaneously on two
|
||||
server sockets for telnet connections. Everything it
|
||||
receives from the telnet clients is broadcasted to every
|
||||
other connected client. It tests the select function and
|
||||
shows how to create a simple server whith LuaSocket. Just
|
||||
run tinyirc.lua and then open as many telnet connections
|
||||
as you want to ports 8080 and 8081.
|
||||
|
||||
Good luck,
|
||||
Diego.
|
46
etc/luasocket-samples/cddb.lua
Normal file
46
etc/luasocket-samples/cddb.lua
Normal file
@ -0,0 +1,46 @@
|
||||
local socket = require("socket")
|
||||
local http = require("socket.http")
|
||||
|
||||
if not arg or not arg[1] or not arg[2] then
|
||||
print("luasocket cddb.lua <category> <disc-id> [<server>]")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local server = arg[3] or "http://freedb.freedb.org/~cddb/cddb.cgi"
|
||||
|
||||
function parse(body)
|
||||
local lines = string.gfind(body, "(.-)\r\n")
|
||||
local status = lines()
|
||||
local code, message = socket.skip(2, string.find(status, "(%d%d%d) (.*)"))
|
||||
if tonumber(code) ~= 210 then
|
||||
return nil, code, message
|
||||
end
|
||||
local data = {}
|
||||
for l in lines do
|
||||
local c = string.sub(l, 1, 1)
|
||||
if c ~= '#' and c ~= '.' then
|
||||
local key, value = socket.skip(2, string.find(l, "(.-)=(.*)"))
|
||||
value = string.gsub(value, "\\n", "\n")
|
||||
value = string.gsub(value, "\\\\", "\\")
|
||||
value = string.gsub(value, "\\t", "\t")
|
||||
data[key] = value
|
||||
end
|
||||
end
|
||||
return data, code, message
|
||||
end
|
||||
|
||||
local host = socket.dns.gethostname()
|
||||
local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
|
||||
local url = string.format(query, server, arg[1], arg[2], host)
|
||||
local body, headers, code = http.get(url)
|
||||
|
||||
if code == 200 then
|
||||
local data, code, error = parse(body)
|
||||
if not data then
|
||||
print(error or code)
|
||||
else
|
||||
for i,v in pairs(data) do
|
||||
io.write(i, ': ', v, '\n')
|
||||
end
|
||||
end
|
||||
else print(error) end
|
23
etc/luasocket-samples/daytimeclnt.lua
Normal file
23
etc/luasocket-samples/daytimeclnt.lua
Normal file
@ -0,0 +1,23 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: daytime protocol client
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: daytimeclnt.lua,v 1.11 2004/06/21 06:07:57 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require"socket"
|
||||
host = host or "127.0.0.1"
|
||||
port = port or 13
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
host = socket.dns.toip(host)
|
||||
udp = socket.udp()
|
||||
print("Using host '" ..host.. "' and port " ..port.. "...")
|
||||
udp:setpeername(host, port)
|
||||
udp:settimeout(3)
|
||||
sent, err = udp:send("anything")
|
||||
if err then print(err) os.exit() end
|
||||
dgram, err = udp:receive()
|
||||
if not dgram then print(err) os.exit() end
|
||||
io.write(dgram)
|
24
etc/luasocket-samples/echoclnt.lua
Normal file
24
etc/luasocket-samples/echoclnt.lua
Normal file
@ -0,0 +1,24 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: echo protocol client
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: echoclnt.lua,v 1.10 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "localhost"
|
||||
port = port or 7
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
host = socket.dns.toip(host)
|
||||
udp = assert(socket.udp())
|
||||
assert(udp:setpeername(host, port))
|
||||
print("Using remote host '" ..host.. "' and port " .. port .. "...")
|
||||
while 1 do
|
||||
line = io.read()
|
||||
if not line or line == "" then os.exit() end
|
||||
assert(udp:send(line))
|
||||
dgram = assert(udp:receive())
|
||||
print(dgram)
|
||||
end
|
29
etc/luasocket-samples/echosrvr.lua
Normal file
29
etc/luasocket-samples/echosrvr.lua
Normal file
@ -0,0 +1,29 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- UDP sample: echo protocol server
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: echosrvr.lua,v 1.12 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "127.0.0.1"
|
||||
port = port or 7
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Binding to host '" ..host.. "' and port " ..port.. "...")
|
||||
udp = assert(socket.udp())
|
||||
assert(udp:setsockname(host, port))
|
||||
assert(udp:settimeout(5))
|
||||
ip, port = udp:getsockname()
|
||||
assert(ip, port)
|
||||
print("Waiting packets on " .. ip .. ":" .. port .. "...")
|
||||
while 1 do
|
||||
dgram, ip, port = udp:receivefrom()
|
||||
if dgram then
|
||||
print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
|
||||
udp:sendto(dgram, ip, port)
|
||||
else
|
||||
print(ip)
|
||||
end
|
||||
end
|
26
etc/luasocket-samples/listener.lua
Normal file
26
etc/luasocket-samples/listener.lua
Normal file
@ -0,0 +1,26 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- TCP sample: Little program to dump lines received at a given port
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: listener.lua,v 1.11 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "*"
|
||||
port = port or 8080
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Binding to host '" ..host.. "' and port " ..port.. "...")
|
||||
s = assert(socket.bind(host, port))
|
||||
i, p = s:getsockname()
|
||||
assert(i, p)
|
||||
print("Waiting connection from talker on " .. i .. ":" .. p .. "...")
|
||||
c = assert(s:accept())
|
||||
print("Connected. Here is the stuff:")
|
||||
l, e = c:receive()
|
||||
while not e do
|
||||
print(l)
|
||||
l, e = c:receive()
|
||||
end
|
||||
print(e)
|
51
etc/luasocket-samples/lpr.lua
Normal file
51
etc/luasocket-samples/lpr.lua
Normal file
@ -0,0 +1,51 @@
|
||||
local lp = require("socket.lp")
|
||||
|
||||
local function usage()
|
||||
print('\nUsage: lua lpr.lua [filename] [keyword=val...]\n')
|
||||
print('Valid keywords are :')
|
||||
print(
|
||||
' host=remote host or IP address (default "localhost")\n' ..
|
||||
' queue=remote queue or printer name (default "printer")\n' ..
|
||||
' port=remote port number (default 515)\n' ..
|
||||
' user=sending user name\n' ..
|
||||
' format=["binary" | "text" | "ps" | "pr" | "fortran"] (default "binary")\n' ..
|
||||
' banner=true|false\n' ..
|
||||
' indent=number of columns to indent\n' ..
|
||||
' mail=email of address to notify when print is complete\n' ..
|
||||
' title=title to use for "pr" format\n' ..
|
||||
' width=width for "text" or "pr" formats\n' ..
|
||||
' class=\n' ..
|
||||
' job=\n' ..
|
||||
' name=\n' ..
|
||||
' localbind=true|false\n'
|
||||
)
|
||||
return nil
|
||||
end
|
||||
|
||||
if not arg or not arg[1] then
|
||||
return usage()
|
||||
end
|
||||
|
||||
do
|
||||
local opt = {}
|
||||
local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]\?\.?)"
|
||||
for i = 2, table.getn(arg), 1 do
|
||||
string.gsub(arg[i], pat, function(name, value) opt[name] = value end)
|
||||
end
|
||||
if not arg[2] then
|
||||
return usage()
|
||||
end
|
||||
if arg[1] ~= "query" then
|
||||
opt.file = arg[1]
|
||||
r,e=lp.send(opt)
|
||||
io.stdout:write(tostring(r or e),'\n')
|
||||
else
|
||||
r,e=lp.query(opt)
|
||||
io.stdout:write(tostring(r or e), '\n')
|
||||
end
|
||||
end
|
||||
|
||||
-- trivial tests
|
||||
--lua lp.lua lp.lua queue=default host=localhost
|
||||
--lua lp.lua lp.lua queue=default host=localhost format=binary localbind=1
|
||||
--lua lp.lua query queue=default host=localhost
|
21
etc/luasocket-samples/talker.lua
Normal file
21
etc/luasocket-samples/talker.lua
Normal file
@ -0,0 +1,21 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- TCP sample: Little program to send text lines to a given host/port
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: talker.lua,v 1.9 2005/01/02 22:44:00 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "localhost"
|
||||
port = port or 8080
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port = arg[2] or port
|
||||
end
|
||||
print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
|
||||
c = assert(socket.connect(host, port))
|
||||
print("Connected! Please type stuff (empty line to stop):")
|
||||
l = io.read()
|
||||
while l and l ~= "" and not e do
|
||||
assert(c:send(l .. "\n"))
|
||||
l = io.read()
|
||||
end
|
90
etc/luasocket-samples/tinyirc.lua
Normal file
90
etc/luasocket-samples/tinyirc.lua
Normal file
@ -0,0 +1,90 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Select sample: simple text line server
|
||||
-- LuaSocket sample files.
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: tinyirc.lua,v 1.14 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
host = host or "*"
|
||||
port1 = port1 or 8080
|
||||
port2 = port2 or 8181
|
||||
if arg then
|
||||
host = arg[1] or host
|
||||
port1 = arg[2] or port1
|
||||
port2 = arg[3] or port2
|
||||
end
|
||||
|
||||
server1 = assert(socket.bind(host, port1))
|
||||
server2 = assert(socket.bind(host, port2))
|
||||
server1:settimeout(1) -- make sure we don't block in accept
|
||||
server2:settimeout(1)
|
||||
|
||||
io.write("Servers bound\n")
|
||||
|
||||
-- simple set implementation
|
||||
-- the select function doesn't care about what is passed to it as long as
|
||||
-- it behaves like a table
|
||||
-- creates a new set data structure
|
||||
function newset()
|
||||
local reverse = {}
|
||||
local set = {}
|
||||
return setmetatable(set, {__index = {
|
||||
insert = function(set, value)
|
||||
if not reverse[value] then
|
||||
table.insert(set, value)
|
||||
reverse[value] = table.getn(set)
|
||||
end
|
||||
end,
|
||||
remove = function(set, value)
|
||||
local index = reverse[value]
|
||||
if index then
|
||||
reverse[value] = nil
|
||||
local top = table.remove(set)
|
||||
if top ~= value then
|
||||
reverse[top] = index
|
||||
set[index] = top
|
||||
end
|
||||
end
|
||||
end
|
||||
}})
|
||||
end
|
||||
|
||||
set = newset()
|
||||
|
||||
io.write("Inserting servers in set\n")
|
||||
set:insert(server1)
|
||||
set:insert(server2)
|
||||
|
||||
while 1 do
|
||||
local readable, _, error = socket.select(set, nil)
|
||||
for _, input in ipairs(readable) do
|
||||
-- is it a server socket?
|
||||
if input == server1 or input == server2 then
|
||||
io.write("Waiting for clients\n")
|
||||
local new = input:accept()
|
||||
if new then
|
||||
new:settimeout(1)
|
||||
io.write("Inserting client in set\n")
|
||||
set:insert(new)
|
||||
end
|
||||
-- it is a client socket
|
||||
else
|
||||
local line, error = input:receive()
|
||||
if error then
|
||||
input:close()
|
||||
io.write("Removing client from set\n")
|
||||
set:remove(input)
|
||||
else
|
||||
io.write("Broadcasting line '", line, "'\n")
|
||||
writable, error = socket.skip(1, socket.select(nil, set, 1))
|
||||
if not error then
|
||||
for __, output in ipairs(writable) do
|
||||
if output ~= input then
|
||||
output:send(line .. "\n")
|
||||
end
|
||||
end
|
||||
else io.write("No client ready to receive!!!\n") end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
89
etc/luasocket/README
Normal file
89
etc/luasocket/README
Normal file
@ -0,0 +1,89 @@
|
||||
This directory contains code that is more useful than the
|
||||
samples. This code *is* supported.
|
||||
|
||||
tftp.lua -- Trivial FTP client
|
||||
|
||||
This module implements file retrieval by the TFTP protocol.
|
||||
Its main use was to test the UDP code, but since someone
|
||||
found it usefull, I turned it into a module that is almost
|
||||
official (no uploads, yet).
|
||||
|
||||
dict.lua -- Dict client
|
||||
|
||||
The dict.lua module started with a cool simple client
|
||||
for the DICT protocol, written by Luiz Henrique Figueiredo.
|
||||
This new version has been converted into a library, similar
|
||||
to the HTTP and FTP libraries, that can be used from within
|
||||
any luasocket application. Take a look on the source code
|
||||
and you will be able to figure out how to use it.
|
||||
|
||||
lp.lua -- LPD client library
|
||||
|
||||
The lp.lua module implements the client part of the Line
|
||||
Printer Daemon protocol, used to print files on Unix
|
||||
machines. It is courtesy of David Burgess! See the source
|
||||
code and the lpr.lua in the examples directory.
|
||||
|
||||
b64.lua
|
||||
qp.lua
|
||||
eol.lua
|
||||
|
||||
These are tiny programs that perform Base64,
|
||||
Quoted-Printable and end-of-line marker conversions.
|
||||
|
||||
get.lua -- file retriever
|
||||
|
||||
This little program is a client that uses the FTP and
|
||||
HTTP code to implement a command line file graber. Just
|
||||
run
|
||||
|
||||
lua get.lua <remote-file> [<local-file>]
|
||||
|
||||
to download a remote file (either ftp:// or http://) to
|
||||
the specified local file. The program also prints the
|
||||
download throughput, elapsed time, bytes already downloaded
|
||||
etc during download.
|
||||
|
||||
check-memory.lua -- checks memory consumption
|
||||
|
||||
This is just to see how much memory each module uses.
|
||||
|
||||
dispatch.lua -- coroutine based dispatcher
|
||||
|
||||
This is a first try at a coroutine based non-blocking
|
||||
dispatcher for LuaSocket. Take a look at 'check-links.lua'
|
||||
and at 'forward.lua' to see how to use it.
|
||||
|
||||
check-links.lua -- HTML link checker program
|
||||
|
||||
This little program scans a HTML file and checks for broken
|
||||
links. It is similar to check-links.pl by Jamie Zawinski,
|
||||
but uses all facilities of the LuaSocket library and the Lua
|
||||
language. It has not been thoroughly tested, but it should
|
||||
work. Just run
|
||||
|
||||
lua check-links.lua [-n] {<url>} > output
|
||||
|
||||
and open the result to see a list of broken links. You can
|
||||
also use the '-n' switch to run the same program in
|
||||
non-blocking mode to see how much faster things can get.
|
||||
|
||||
forward.lua -- coroutine based forward server
|
||||
|
||||
This is a forward server that can accept several connections
|
||||
and transfers simultaneously using non-blocking I/O and the
|
||||
coroutine-based dispatcher. You can run, for example
|
||||
|
||||
lua forward.lua 8080:proxy.com:3128
|
||||
|
||||
to redirect all local conections to port 8080 to the host
|
||||
'proxy.com' at port 3128.
|
||||
|
||||
unix.c and unix.h
|
||||
|
||||
This is an implementation of Unix local domain sockets and
|
||||
demonstrates how to extend LuaSocket with a new type of
|
||||
transport. It has been tested on Linux and on Mac OS X.
|
||||
|
||||
Good luck,
|
||||
Diego.
|
20
etc/luasocket/b64.lua
Normal file
20
etc/luasocket/b64.lua
Normal file
@ -0,0 +1,20 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program to convert to and from Base64
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: b64.lua,v 1.8 2004/06/16 04:28:21 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
local source = ltn12.source.file(io.stdin)
|
||||
local sink = ltn12.sink.file(io.stdout)
|
||||
local convert
|
||||
if arg and arg[1] == '-d' then
|
||||
convert = mime.decode("base64")
|
||||
else
|
||||
local base64 = mime.encode("base64")
|
||||
local wrap = mime.wrap()
|
||||
convert = ltn12.filter.chain(base64, wrap)
|
||||
end
|
||||
sink = ltn12.sink.chain(convert, sink)
|
||||
ltn12.pump.all(source, sink)
|
112
etc/luasocket/check-links.lua
Normal file
112
etc/luasocket/check-links.lua
Normal file
@ -0,0 +1,112 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program that checks links in HTML files, using coroutines and
|
||||
-- non-blocking I/O via the dispatcher module.
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $$
|
||||
-----------------------------------------------------------------------------
|
||||
local url = require("socket.url")
|
||||
local dispatch = require("dispatch")
|
||||
local http = require("socket.http")
|
||||
dispatch.TIMEOUT = 10
|
||||
|
||||
-- make sure the user knows how to invoke us
|
||||
arg = arg or {}
|
||||
if table.getn(arg) < 1 then
|
||||
print("Usage:\n luasocket check-links.lua [-n] {<url>}")
|
||||
exit()
|
||||
end
|
||||
|
||||
-- '-n' means we are running in non-blocking mode
|
||||
if arg[1] == "-n" then
|
||||
-- if non-blocking I/O was requested, use real dispatcher interface
|
||||
table.remove(arg, 1)
|
||||
handler = dispatch.newhandler("coroutine")
|
||||
else
|
||||
-- if using blocking I/O, use fake dispatcher interface
|
||||
handler = dispatch.newhandler("sequential")
|
||||
end
|
||||
|
||||
local nthreads = 0
|
||||
|
||||
-- get the status of a URL using the dispatcher
|
||||
function getstatus(link)
|
||||
local parsed = url.parse(link, {scheme = "file"})
|
||||
if parsed.scheme == "http" then
|
||||
nthreads = nthreads + 1
|
||||
handler:start(function()
|
||||
local r, c, h, s = http.request{
|
||||
method = "HEAD",
|
||||
url = link,
|
||||
create = handler.tcp
|
||||
}
|
||||
if r and c == 200 then io.write('\t', link, '\n')
|
||||
else io.write('\t', link, ': ', tostring(c), '\n') end
|
||||
nthreads = nthreads - 1
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function readfile(path)
|
||||
path = url.unescape(path)
|
||||
local file, error = io.open(path, "r")
|
||||
if file then
|
||||
local body = file:read("*a")
|
||||
file:close()
|
||||
return body
|
||||
else return nil, error end
|
||||
end
|
||||
|
||||
function load(u)
|
||||
local parsed = url.parse(u, { scheme = "file" })
|
||||
local body, headers, code, error
|
||||
local base = u
|
||||
if parsed.scheme == "http" then
|
||||
body, code, headers = http.request(u)
|
||||
if code == 200 then
|
||||
-- if there was a redirect, update base to reflect it
|
||||
base = headers.location or base
|
||||
end
|
||||
if not body then
|
||||
error = code
|
||||
end
|
||||
elseif parsed.scheme == "file" then
|
||||
body, error = readfile(parsed.path)
|
||||
else error = string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
return base, body, error
|
||||
end
|
||||
|
||||
function getlinks(body, base)
|
||||
-- get rid of comments
|
||||
body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
|
||||
local links = {}
|
||||
-- extract links
|
||||
body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
|
||||
table.insert(links, url.absolute(base, href))
|
||||
end)
|
||||
return links
|
||||
end
|
||||
|
||||
function checklinks(address)
|
||||
local base, body, error = load(address)
|
||||
if not body then print(error) return end
|
||||
print("Checking ", base)
|
||||
local links = getlinks(body, base)
|
||||
for _, link in ipairs(links) do
|
||||
getstatus(link)
|
||||
end
|
||||
end
|
||||
|
||||
for _, address in ipairs(arg) do
|
||||
checklinks(url.absolute("file:", address))
|
||||
end
|
||||
|
||||
while nthreads > 0 do
|
||||
handler:step()
|
||||
end
|
17
etc/luasocket/check-memory.lua
Normal file
17
etc/luasocket/check-memory.lua
Normal file
@ -0,0 +1,17 @@
|
||||
function load(s)
|
||||
collectgarbage()
|
||||
local a = gcinfo()
|
||||
_G[s] = require(s)
|
||||
collectgarbage()
|
||||
local b = gcinfo()
|
||||
print(s .. ":\t " .. (b-a) .. "k")
|
||||
end
|
||||
|
||||
load("socket.url")
|
||||
load("ltn12")
|
||||
load("socket")
|
||||
load("mime")
|
||||
load("socket.tp")
|
||||
load("socket.smtp")
|
||||
load("socket.http")
|
||||
load("socket.ftp")
|
152
etc/luasocket/dict.lua
Normal file
152
etc/luasocket/dict.lua
Normal file
@ -0,0 +1,152 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program to download DICT word definitions
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: dict.lua,v 1.22 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load required modules
|
||||
-----------------------------------------------------------------------------
|
||||
local base = _G
|
||||
local string = require("string")
|
||||
local table = require("table")
|
||||
local socket = require("socket")
|
||||
local url = require("socket.url")
|
||||
local tp = require("socket.tp")
|
||||
module("socket.dict")
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Globals
|
||||
-----------------------------------------------------------------------------
|
||||
HOST = "dict.org"
|
||||
PORT = 2628
|
||||
TIMEOUT = 10
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Low-level dict API
|
||||
-----------------------------------------------------------------------------
|
||||
local metat = { __index = {} }
|
||||
|
||||
function open(host, port)
|
||||
local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT))
|
||||
return base.setmetatable({tp = tp}, metat)
|
||||
end
|
||||
|
||||
function metat.__index:greet()
|
||||
return socket.try(self.tp:check(220))
|
||||
end
|
||||
|
||||
function metat.__index:check(ok)
|
||||
local code, status = socket.try(self.tp:check(ok))
|
||||
return code,
|
||||
base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
|
||||
end
|
||||
|
||||
function metat.__index:getdef()
|
||||
local line = socket.try(self.tp:receive())
|
||||
local def = {}
|
||||
while line ~= "." do
|
||||
table.insert(def, line)
|
||||
line = socket.try(self.tp:receive())
|
||||
end
|
||||
return table.concat(def, "\n")
|
||||
end
|
||||
|
||||
function metat.__index:define(database, word)
|
||||
database = database or "!"
|
||||
socket.try(self.tp:command("DEFINE", database .. " " .. word))
|
||||
local code, count = self:check(150)
|
||||
local defs = {}
|
||||
for i = 1, count do
|
||||
self:check(151)
|
||||
table.insert(defs, self:getdef())
|
||||
end
|
||||
self:check(250)
|
||||
return defs
|
||||
end
|
||||
|
||||
function metat.__index:match(database, strat, word)
|
||||
database = database or "!"
|
||||
strat = strat or "."
|
||||
socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word))
|
||||
self:check(152)
|
||||
local mat = {}
|
||||
local line = socket.try(self.tp:receive())
|
||||
while line ~= '.' do
|
||||
database, word = socket.skip(2, string.find(line, "(%S+) (.*)"))
|
||||
if not mat[database] then mat[database] = {} end
|
||||
table.insert(mat[database], word)
|
||||
line = socket.try(self.tp:receive())
|
||||
end
|
||||
self:check(250)
|
||||
return mat
|
||||
end
|
||||
|
||||
function metat.__index:quit()
|
||||
self.tp:command("QUIT")
|
||||
return self:check(221)
|
||||
end
|
||||
|
||||
function metat.__index:close()
|
||||
return self.tp:close()
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- High-level dict API
|
||||
-----------------------------------------------------------------------------
|
||||
local default = {
|
||||
scheme = "dict",
|
||||
host = "dict.org"
|
||||
}
|
||||
|
||||
local function there(f)
|
||||
if f == "" then return nil
|
||||
else return f end
|
||||
end
|
||||
|
||||
local function parse(u)
|
||||
local t = socket.try(url.parse(u, default))
|
||||
socket.try(t.scheme == "dict", "invalid scheme '" .. t.scheme .. "'")
|
||||
socket.try(t.path, "invalid path in url")
|
||||
local cmd, arg = socket.skip(2, string.find(t.path, "^/(.)(.*)$"))
|
||||
socket.try(cmd == "d" or cmd == "m", "<command> should be 'm' or 'd'")
|
||||
socket.try(arg and arg ~= "", "need at least <word> in URL")
|
||||
t.command, t.argument = cmd, arg
|
||||
arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end)
|
||||
socket.try(t.word, "need at least <word> in URL")
|
||||
arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end)
|
||||
if cmd == "m" then
|
||||
arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
|
||||
end
|
||||
string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
|
||||
return t
|
||||
end
|
||||
|
||||
local function tget(gett)
|
||||
local con = open(gett.host, gett.port)
|
||||
con:greet()
|
||||
if gett.command == "d" then
|
||||
local def = con:define(gett.database, gett.word)
|
||||
con:quit()
|
||||
con:close()
|
||||
if gett.n then return def[gett.n]
|
||||
else return def end
|
||||
elseif gett.command == "m" then
|
||||
local mat = con:match(gett.database, gett.strat, gett.word)
|
||||
con:quit()
|
||||
con:close()
|
||||
return mat
|
||||
else return nil, "invalid command" end
|
||||
end
|
||||
|
||||
local function sget(u)
|
||||
local gett = parse(u)
|
||||
return tget(gett)
|
||||
end
|
||||
|
||||
get = socket.protect(function(gett)
|
||||
if base.type(gett) == "string" then return sget(gett)
|
||||
else return tget(gett) end
|
||||
end)
|
||||
|
301
etc/luasocket/dispatch.lua
Normal file
301
etc/luasocket/dispatch.lua
Normal file
@ -0,0 +1,301 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- A hacked dispatcher module
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $$
|
||||
-----------------------------------------------------------------------------
|
||||
local base = _G
|
||||
local socket = require("socket")
|
||||
local coroutine = require("coroutine")
|
||||
module("dispatch")
|
||||
|
||||
-- if too much time goes by without any activity in one of our sockets, we
|
||||
-- just kill it
|
||||
TIMEOUT = 60
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- We implement 3 types of dispatchers:
|
||||
-- sequential
|
||||
-- coroutine
|
||||
-- threaded
|
||||
-- The user can choose whatever one is needed
|
||||
-----------------------------------------------------------------------------
|
||||
local handlert = {}
|
||||
|
||||
-- default handler is coroutine
|
||||
function newhandler(mode)
|
||||
mode = mode or "coroutine"
|
||||
return handlert[mode]()
|
||||
end
|
||||
|
||||
local function seqstart(self, func)
|
||||
return func()
|
||||
end
|
||||
|
||||
-- sequential handler simply calls the functions and doesn't wrap I/O
|
||||
function handlert.sequential()
|
||||
return {
|
||||
tcp = socket.tcp,
|
||||
start = seqstart
|
||||
}
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Mega hack. Don't try to do this at home.
|
||||
-----------------------------------------------------------------------------
|
||||
-- we can't yield across calls to protect, so we rewrite it with coxpcall
|
||||
-- make sure you don't require any module that uses socket.protect before
|
||||
-- loading our hack
|
||||
function socket.protect(f)
|
||||
return function(...)
|
||||
local co = coroutine.create(f)
|
||||
while true do
|
||||
local results = {coroutine.resume(co, unpack(arg))}
|
||||
local status = table.remove(results, 1)
|
||||
if not status then
|
||||
if type(results[1]) == 'table' then
|
||||
return nil, results[1][1]
|
||||
else error(results[1]) end
|
||||
end
|
||||
if coroutine.status(co) == "suspended" then
|
||||
arg = {coroutine.yield(unpack(results))}
|
||||
else
|
||||
return unpack(results)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Simple set data structure. O(1) everything.
|
||||
-----------------------------------------------------------------------------
|
||||
local function newset()
|
||||
local reverse = {}
|
||||
local set = {}
|
||||
return setmetatable(set, {__index = {
|
||||
insert = function(set, value)
|
||||
if not reverse[value] then
|
||||
table.insert(set, value)
|
||||
reverse[value] = table.getn(set)
|
||||
end
|
||||
end,
|
||||
remove = function(set, value)
|
||||
local index = reverse[value]
|
||||
if index then
|
||||
reverse[value] = nil
|
||||
local top = table.remove(set)
|
||||
if top ~= value then
|
||||
reverse[top] = index
|
||||
set[index] = top
|
||||
end
|
||||
end
|
||||
end
|
||||
}})
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- socket.tcp() wrapper for the coroutine dispatcher
|
||||
-----------------------------------------------------------------------------
|
||||
local function cowrap(dispatcher, tcp, error)
|
||||
if not tcp then return nil, error end
|
||||
-- put it in non-blocking mode right away
|
||||
tcp:settimeout(0)
|
||||
-- metatable for wrap produces new methods on demand for those that we
|
||||
-- don't override explicitly.
|
||||
local metat = { __index = function(table, key)
|
||||
table[key] = function(...)
|
||||
arg[1] = tcp
|
||||
return tcp[key](unpack(arg))
|
||||
end
|
||||
return table[key]
|
||||
end}
|
||||
-- does our user want to do his own non-blocking I/O?
|
||||
local zero = false
|
||||
-- create a wrap object that will behave just like a real socket object
|
||||
local wrap = { }
|
||||
-- we ignore settimeout to preserve our 0 timeout, but record whether
|
||||
-- the user wants to do his own non-blocking I/O
|
||||
function wrap:settimeout(value, mode)
|
||||
if value == 0 then zero = true
|
||||
else zero = false end
|
||||
return 1
|
||||
end
|
||||
-- send in non-blocking mode and yield on timeout
|
||||
function wrap:send(data, first, last)
|
||||
first = (first or 1) - 1
|
||||
local result, error
|
||||
while true do
|
||||
-- return control to dispatcher and tell it we want to send
|
||||
-- if upon return the dispatcher tells us we timed out,
|
||||
-- return an error to whoever called us
|
||||
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- try sending
|
||||
result, error, first = tcp:send(data, first+1, last)
|
||||
-- if we are done, or there was an unexpected error,
|
||||
-- break away from loop
|
||||
if error ~= "timeout" then return result, error, first end
|
||||
end
|
||||
end
|
||||
-- receive in non-blocking mode and yield on timeout
|
||||
-- or simply return partial read, if user requested timeout = 0
|
||||
function wrap:receive(pattern, partial)
|
||||
local error = "timeout"
|
||||
local value
|
||||
while true do
|
||||
-- return control to dispatcher and tell it we want to receive
|
||||
-- if upon return the dispatcher tells us we timed out,
|
||||
-- return an error to whoever called us
|
||||
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- try receiving
|
||||
value, error, partial = tcp:receive(pattern, partial)
|
||||
-- if we are done, or there was an unexpected error,
|
||||
-- break away from loop. also, if the user requested
|
||||
-- zero timeout, return all we got
|
||||
if (error ~= "timeout") or zero then
|
||||
return value, error, partial
|
||||
end
|
||||
end
|
||||
end
|
||||
-- connect in non-blocking mode and yield on timeout
|
||||
function wrap:connect(host, port)
|
||||
local result, error = tcp:connect(host, port)
|
||||
if error == "timeout" then
|
||||
-- return control to dispatcher. we will be writable when
|
||||
-- connection succeeds.
|
||||
-- if upon return the dispatcher tells us we have a
|
||||
-- timeout, just abort
|
||||
if coroutine.yield(dispatcher.sending, tcp) == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
-- when we come back, check if connection was successful
|
||||
result, error = tcp:connect(host, port)
|
||||
if result or error == "already connected" then return 1
|
||||
else return nil, "non-blocking connect failed" end
|
||||
else return result, error end
|
||||
end
|
||||
-- accept in non-blocking mode and yield on timeout
|
||||
function wrap:accept()
|
||||
while 1 do
|
||||
-- return control to dispatcher. we will be readable when a
|
||||
-- connection arrives.
|
||||
-- if upon return the dispatcher tells us we have a
|
||||
-- timeout, just abort
|
||||
if coroutine.yield(dispatcher.receiving, tcp) == "timeout" then
|
||||
return nil, "timeout"
|
||||
end
|
||||
local client, error = tcp:accept()
|
||||
if error ~= "timeout" then
|
||||
return cowrap(dispatcher, client, error)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- remove cortn from context
|
||||
function wrap:close()
|
||||
dispatcher.stamp[tcp] = nil
|
||||
dispatcher.sending.set:remove(tcp)
|
||||
dispatcher.sending.cortn[tcp] = nil
|
||||
dispatcher.receiving.set:remove(tcp)
|
||||
dispatcher.receiving.cortn[tcp] = nil
|
||||
return tcp:close()
|
||||
end
|
||||
return setmetatable(wrap, metat)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Our coroutine dispatcher
|
||||
-----------------------------------------------------------------------------
|
||||
local cometat = { __index = {} }
|
||||
|
||||
function schedule(cortn, status, operation, tcp)
|
||||
if status then
|
||||
if cortn and operation then
|
||||
operation.set:insert(tcp)
|
||||
operation.cortn[tcp] = cortn
|
||||
operation.stamp[tcp] = socket.gettime()
|
||||
end
|
||||
else error(operation) end
|
||||
end
|
||||
|
||||
function kick(operation, tcp)
|
||||
operation.cortn[tcp] = nil
|
||||
operation.set:remove(tcp)
|
||||
end
|
||||
|
||||
function wakeup(operation, tcp)
|
||||
local cortn = operation.cortn[tcp]
|
||||
-- if cortn is still valid, wake it up
|
||||
if cortn then
|
||||
kick(operation, tcp)
|
||||
return cortn, coroutine.resume(cortn)
|
||||
-- othrewise, just get scheduler not to do anything
|
||||
else
|
||||
return nil, true
|
||||
end
|
||||
end
|
||||
|
||||
function abort(operation, tcp)
|
||||
local cortn = operation.cortn[tcp]
|
||||
if cortn then
|
||||
kick(operation, tcp)
|
||||
coroutine.resume(cortn, "timeout")
|
||||
end
|
||||
end
|
||||
|
||||
-- step through all active cortns
|
||||
function cometat.__index:step()
|
||||
-- check which sockets are interesting and act on them
|
||||
local readable, writable = socket.select(self.receiving.set,
|
||||
self.sending.set, 1)
|
||||
-- for all readable connections, resume their cortns and reschedule
|
||||
-- when they yield back to us
|
||||
for _, tcp in ipairs(readable) do
|
||||
schedule(wakeup(self.receiving, tcp))
|
||||
end
|
||||
-- for all writable connections, do the same
|
||||
for _, tcp in ipairs(writable) do
|
||||
schedule(wakeup(self.sending, tcp))
|
||||
end
|
||||
-- politely ask replacement I/O functions in idle cortns to
|
||||
-- return reporting a timeout
|
||||
local now = socket.gettime()
|
||||
for tcp, stamp in pairs(self.stamp) do
|
||||
if tcp.class == "tcp{client}" and now - stamp > TIMEOUT then
|
||||
abort(self.sending, tcp)
|
||||
abort(self.receiving, tcp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function cometat.__index:start(func)
|
||||
local cortn = coroutine.create(func)
|
||||
schedule(cortn, coroutine.resume(cortn))
|
||||
end
|
||||
|
||||
function handlert.coroutine()
|
||||
local stamp = {}
|
||||
local dispatcher = {
|
||||
stamp = stamp,
|
||||
sending = {
|
||||
name = "sending",
|
||||
set = newset(),
|
||||
cortn = {},
|
||||
stamp = stamp
|
||||
},
|
||||
receiving = {
|
||||
name = "receiving",
|
||||
set = newset(),
|
||||
cortn = {},
|
||||
stamp = stamp
|
||||
},
|
||||
}
|
||||
function dispatcher.tcp()
|
||||
return cowrap(dispatcher, socket.tcp())
|
||||
end
|
||||
return setmetatable(dispatcher, cometat)
|
||||
end
|
||||
|
14
etc/luasocket/eol.lua
Normal file
14
etc/luasocket/eol.lua
Normal file
@ -0,0 +1,14 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program to adjust end of line markers.
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: eol.lua,v 1.8 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local mime = require("mime")
|
||||
local ltn12 = require("ltn12")
|
||||
local marker = '\n'
|
||||
if arg and arg[1] == '-d' then marker = '\r\n' end
|
||||
local filter = mime.normalize(marker)
|
||||
local source = ltn12.source.chain(ltn12.source.file(io.stdin), filter)
|
||||
local sink = ltn12.sink.file(io.stdout)
|
||||
ltn12.pump.all(source, sink)
|
65
etc/luasocket/forward.lua
Normal file
65
etc/luasocket/forward.lua
Normal file
@ -0,0 +1,65 @@
|
||||
-- load our favourite library
|
||||
local dispatch = require("dispatch")
|
||||
local handler = dispatch.newhandler()
|
||||
|
||||
-- make sure the user knows how to invoke us
|
||||
if table.getn(arg) < 1 then
|
||||
print("Usage")
|
||||
print(" lua forward.lua <iport:ohost:oport> ...")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- function to move data from one socket to the other
|
||||
local function move(foo, bar)
|
||||
local live
|
||||
while 1 do
|
||||
local data, error, partial = foo:receive(2048)
|
||||
live = data or error == "timeout"
|
||||
data = data or partial
|
||||
local result, error = bar:send(data)
|
||||
if not live or not result then
|
||||
foo:close()
|
||||
bar:close()
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- for each tunnel, start a new server
|
||||
for i, v in ipairs(arg) do
|
||||
-- capture forwarding parameters
|
||||
local _, _, iport, ohost, oport = string.find(v, "([^:]+):([^:]+):([^:]+)")
|
||||
assert(iport, "invalid arguments")
|
||||
-- create our server socket
|
||||
local server = assert(handler.tcp())
|
||||
assert(server:setoption("reuseaddr", true))
|
||||
assert(server:bind("*", iport))
|
||||
assert(server:listen(32))
|
||||
-- handler for the server object loops accepting new connections
|
||||
handler:start(function()
|
||||
while 1 do
|
||||
local client = assert(server:accept())
|
||||
assert(client:settimeout(0))
|
||||
-- for each new connection, start a new client handler
|
||||
handler:start(function()
|
||||
-- handler tries to connect to peer
|
||||
local peer = assert(handler.tcp())
|
||||
assert(peer:settimeout(0))
|
||||
assert(peer:connect(ohost, oport))
|
||||
-- if sucessful, starts a new handler to send data from
|
||||
-- client to peer
|
||||
handler:start(function()
|
||||
move(client, peer)
|
||||
end)
|
||||
-- afte starting new handler, enter in loop sending data from
|
||||
-- peer to client
|
||||
move(peer, client)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- simply loop stepping the server
|
||||
while 1 do
|
||||
handler:step()
|
||||
end
|
140
etc/luasocket/get.lua
Normal file
140
etc/luasocket/get.lua
Normal file
@ -0,0 +1,140 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program to download files from URLs
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: get.lua,v 1.24 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local socket = require("socket")
|
||||
local http = require("socket.http")
|
||||
local ftp = require("socket.ftp")
|
||||
local url = require("socket.url")
|
||||
local ltn12 = require("ltn12")
|
||||
|
||||
-- formats a number of seconds into human readable form
|
||||
function nicetime(s)
|
||||
local l = "s"
|
||||
if s > 60 then
|
||||
s = s / 60
|
||||
l = "m"
|
||||
if s > 60 then
|
||||
s = s / 60
|
||||
l = "h"
|
||||
if s > 24 then
|
||||
s = s / 24
|
||||
l = "d" -- hmmm
|
||||
end
|
||||
end
|
||||
end
|
||||
if l == "s" then return string.format("%5.0f%s", s, l)
|
||||
else return string.format("%5.2f%s", s, l) end
|
||||
end
|
||||
|
||||
-- formats a number of bytes into human readable form
|
||||
function nicesize(b)
|
||||
local l = "B"
|
||||
if b > 1024 then
|
||||
b = b / 1024
|
||||
l = "KB"
|
||||
if b > 1024 then
|
||||
b = b / 1024
|
||||
l = "MB"
|
||||
if b > 1024 then
|
||||
b = b / 1024
|
||||
l = "GB" -- hmmm
|
||||
end
|
||||
end
|
||||
end
|
||||
return string.format("%7.2f%2s", b, l)
|
||||
end
|
||||
|
||||
-- returns a string with the current state of the download
|
||||
local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining"
|
||||
local elapsed_s = "%s received, %s/s throughput, %s elapsed "
|
||||
function gauge(got, delta, size)
|
||||
local rate = got / delta
|
||||
if size and size >= 1 then
|
||||
return string.format(remaining_s, nicesize(got), nicesize(rate),
|
||||
100*got/size, nicetime((size-got)/rate))
|
||||
else
|
||||
return string.format(elapsed_s, nicesize(got),
|
||||
nicesize(rate), nicetime(delta))
|
||||
end
|
||||
end
|
||||
|
||||
-- creates a new instance of a receive_cb that saves to disk
|
||||
-- kind of copied from luasocket's manual callback examples
|
||||
function stats(size)
|
||||
local start = socket.gettime()
|
||||
local got = 0
|
||||
return function(chunk)
|
||||
-- elapsed time since start
|
||||
local delta = socket.gettime() - start
|
||||
if chunk then
|
||||
-- total bytes received
|
||||
got = got + string.len(chunk)
|
||||
-- not enough time for estimate
|
||||
if delta > 0.1 then
|
||||
io.stderr:write("\r", gauge(got, delta, size))
|
||||
io.stderr:flush()
|
||||
end
|
||||
else
|
||||
-- close up
|
||||
io.stderr:write("\r", gauge(got, delta), "\n")
|
||||
end
|
||||
return chunk
|
||||
end
|
||||
end
|
||||
|
||||
-- determines the size of a http file
|
||||
function gethttpsize(u)
|
||||
local r, c, h = http.request {method = "HEAD", url = u}
|
||||
if c == 200 then
|
||||
return tonumber(h["content-length"])
|
||||
end
|
||||
end
|
||||
|
||||
-- downloads a file using the http protocol
|
||||
function getbyhttp(u, file)
|
||||
local save = ltn12.sink.file(file or io.stdout)
|
||||
-- only print feedback if output is not stdout
|
||||
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
|
||||
local r, c, h, s = http.request {url = u, sink = save }
|
||||
if c ~= 200 then io.stderr:write(s or c, "\n") end
|
||||
end
|
||||
|
||||
-- downloads a file using the ftp protocol
|
||||
function getbyftp(u, file)
|
||||
local save = ltn12.sink.file(file or io.stdout)
|
||||
-- only print feedback if output is not stdout
|
||||
-- and we don't know how big the file is
|
||||
if file then save = ltn12.sink.chain(stats(), save) end
|
||||
local gett = url.parse(u)
|
||||
gett.sink = save
|
||||
gett.type = "i"
|
||||
local ret, err = ftp.get(gett)
|
||||
if err then print(err) end
|
||||
end
|
||||
|
||||
-- determines the scheme
|
||||
function getscheme(u)
|
||||
-- this is an heuristic to solve a common invalid url poblem
|
||||
if not string.find(u, "//") then u = "//" .. u end
|
||||
local parsed = url.parse(u, {scheme = "http"})
|
||||
return parsed.scheme
|
||||
end
|
||||
|
||||
-- gets a file either by http or ftp, saving as <name>
|
||||
function get(u, name)
|
||||
local fout = name and io.open(name, "wb")
|
||||
local scheme = getscheme(u)
|
||||
if scheme == "ftp" then getbyftp(u, fout)
|
||||
elseif scheme == "http" then getbyhttp(u, fout)
|
||||
else print("unknown scheme" .. scheme) end
|
||||
end
|
||||
|
||||
-- main program
|
||||
arg = arg or {}
|
||||
if table.getn(arg) < 1 then
|
||||
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
|
||||
os.exit(1)
|
||||
else get(arg[1], arg[2]) end
|
324
etc/luasocket/lp.lua
Normal file
324
etc/luasocket/lp.lua
Normal file
@ -0,0 +1,324 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- LPD support for the Lua language
|
||||
-- LuaSocket toolkit.
|
||||
-- Author: David Burgess
|
||||
-- Modified by Diego Nehab, but David is in charge
|
||||
-- RCS ID: $Id: lp.lua,v 1.14 2005/11/21 07:04:44 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
--[[
|
||||
if you have any questions: RFC 1179
|
||||
]]
|
||||
-- make sure LuaSocket is loaded
|
||||
local io = require("io")
|
||||
local base = _G
|
||||
local os = require("os")
|
||||
local math = require("math")
|
||||
local string = require("string")
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
module("socket.lp")
|
||||
|
||||
-- default port
|
||||
PORT = 515
|
||||
SERVER = os.getenv("SERVER_NAME") or os.getenv("COMPUTERNAME") or "localhost"
|
||||
PRINTER = os.getenv("PRINTER") or "printer"
|
||||
|
||||
local function connect(localhost, option)
|
||||
local host = option.host or SERVER
|
||||
local port = option.port or PORT
|
||||
local skt
|
||||
local try = socket.newtry(function() if skt then skt:close() end end)
|
||||
if option.localbind then
|
||||
-- bind to a local port (if we can)
|
||||
local localport = 721
|
||||
local done, err
|
||||
repeat
|
||||
skt = socket.try(socket.tcp())
|
||||
try(skt:settimeout(30))
|
||||
done, err = skt:bind(localhost, localport)
|
||||
if not done then
|
||||
localport = localport + 1
|
||||
skt:close()
|
||||
skt = nil
|
||||
else break end
|
||||
until localport > 731
|
||||
socket.try(skt, err)
|
||||
else skt = socket.try(socket.tcp()) end
|
||||
try(skt:connect(host, port))
|
||||
return { skt = skt, try = try }
|
||||
end
|
||||
|
||||
--[[
|
||||
RFC 1179
|
||||
5.3 03 - Send queue state (short)
|
||||
|
||||
+----+-------+----+------+----+
|
||||
| 03 | Queue | SP | List | LF |
|
||||
+----+-------+----+------+----+
|
||||
Command code - 3
|
||||
Operand 1 - Printer queue name
|
||||
Other operands - User names or job numbers
|
||||
|
||||
If the user names or job numbers or both are supplied then only those
|
||||
jobs for those users or with those numbers will be sent.
|
||||
|
||||
The response is an ASCII stream which describes the printer queue.
|
||||
The stream continues until the connection closes. Ends of lines are
|
||||
indicated with ASCII LF control characters. The lines may also
|
||||
contain ASCII HT control characters.
|
||||
|
||||
5.4 04 - Send queue state (long)
|
||||
|
||||
+----+-------+----+------+----+
|
||||
| 04 | Queue | SP | List | LF |
|
||||
+----+-------+----+------+----+
|
||||
Command code - 4
|
||||
Operand 1 - Printer queue name
|
||||
Other operands - User names or job numbers
|
||||
|
||||
If the user names or job numbers or both are supplied then only those
|
||||
jobs for those users or with those numbers will be sent.
|
||||
|
||||
The response is an ASCII stream which describes the printer queue.
|
||||
The stream continues until the connection closes. Ends of lines are
|
||||
indicated with ASCII LF control characters. The lines may also
|
||||
contain ASCII HT control characters.
|
||||
]]
|
||||
|
||||
-- gets server acknowledement
|
||||
local function recv_ack(con)
|
||||
local ack = con.skt:receive(1)
|
||||
con.try(string.char(0) == ack, "failed to receive server acknowledgement")
|
||||
end
|
||||
|
||||
-- sends client acknowledement
|
||||
local function send_ack(con)
|
||||
local sent = con.skt:send(string.char(0))
|
||||
con.try(sent == 1, "failed to send acknowledgement")
|
||||
end
|
||||
|
||||
-- sends queue request
|
||||
-- 5.2 02 - Receive a printer job
|
||||
--
|
||||
-- +----+-------+----+
|
||||
-- | 02 | Queue | LF |
|
||||
-- +----+-------+----+
|
||||
-- Command code - 2
|
||||
-- Operand - Printer queue name
|
||||
--
|
||||
-- Receiving a job is controlled by a second level of commands. The
|
||||
-- daemon is given commands by sending them over the same connection.
|
||||
-- The commands are described in the next section (6).
|
||||
--
|
||||
-- After this command is sent, the client must read an acknowledgement
|
||||
-- octet from the daemon. A positive acknowledgement is an octet of
|
||||
-- zero bits. A negative acknowledgement is an octet of any other
|
||||
-- pattern.
|
||||
local function send_queue(con, queue)
|
||||
queue = queue or PRINTER
|
||||
local str = string.format("\2%s\10", queue)
|
||||
local sent = con.skt:send(str)
|
||||
con.try(sent == string.len(str), "failed to send print request")
|
||||
recv_ack(con)
|
||||
end
|
||||
|
||||
-- sends control file
|
||||
-- 6.2 02 - Receive control file
|
||||
--
|
||||
-- +----+-------+----+------+----+
|
||||
-- | 02 | Count | SP | Name | LF |
|
||||
-- +----+-------+----+------+----+
|
||||
-- Command code - 2
|
||||
-- Operand 1 - Number of bytes in control file
|
||||
-- Operand 2 - Name of control file
|
||||
--
|
||||
-- The control file must be an ASCII stream with the ends of lines
|
||||
-- indicated by ASCII LF. The total number of bytes in the stream is
|
||||
-- sent as the first operand. The name of the control file is sent as
|
||||
-- the second. It should start with ASCII "cfA", followed by a three
|
||||
-- digit job number, followed by the host name which has constructed the
|
||||
-- control file. Acknowledgement processing must occur as usual after
|
||||
-- the command is sent.
|
||||
--
|
||||
-- The next "Operand 1" octets over the same TCP connection are the
|
||||
-- intended contents of the control file. Once all of the contents have
|
||||
-- been delivered, an octet of zero bits is sent as an indication that
|
||||
-- the file being sent is complete. A second level of acknowledgement
|
||||
-- processing must occur at this point.
|
||||
|
||||
-- sends data file
|
||||
-- 6.3 03 - Receive data file
|
||||
--
|
||||
-- +----+-------+----+------+----+
|
||||
-- | 03 | Count | SP | Name | LF |
|
||||
-- +----+-------+----+------+----+
|
||||
-- Command code - 3
|
||||
-- Operand 1 - Number of bytes in data file
|
||||
-- Operand 2 - Name of data file
|
||||
--
|
||||
-- The data file may contain any 8 bit values at all. The total number
|
||||
-- of bytes in the stream may be sent as the first operand, otherwise
|
||||
-- the field should be cleared to 0. The name of the data file should
|
||||
-- start with ASCII "dfA". This should be followed by a three digit job
|
||||
-- number. The job number should be followed by the host name which has
|
||||
-- constructed the data file. Interpretation of the contents of the
|
||||
-- data file is determined by the contents of the corresponding control
|
||||
-- file. If a data file length has been specified, the next "Operand 1"
|
||||
-- octets over the same TCP connection are the intended contents of the
|
||||
-- data file. In this case, once all of the contents have been
|
||||
-- delivered, an octet of zero bits is sent as an indication that the
|
||||
-- file being sent is complete. A second level of acknowledgement
|
||||
-- processing must occur at this point.
|
||||
|
||||
|
||||
local function send_hdr(con, control)
|
||||
local sent = con.skt:send(control)
|
||||
con.try(sent and sent >= 1 , "failed to send header file")
|
||||
recv_ack(con)
|
||||
end
|
||||
|
||||
local function send_control(con, control)
|
||||
local sent = con.skt:send(control)
|
||||
con.try(sent and sent >= 1, "failed to send control file")
|
||||
send_ack(con)
|
||||
end
|
||||
|
||||
local function send_data(con,fh,size)
|
||||
local buf
|
||||
while size > 0 do
|
||||
buf,message = fh:read(8192)
|
||||
if buf then
|
||||
st = con.try(con.skt:send(buf))
|
||||
size = size - st
|
||||
else
|
||||
con.try(size == 0, "file size mismatch")
|
||||
end
|
||||
end
|
||||
recv_ack(con) -- note the double acknowledgement
|
||||
send_ack(con)
|
||||
recv_ack(con)
|
||||
return size
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
local control_dflt = {
|
||||
"H"..string.sub(socket.hostname,1,31).."\10", -- host
|
||||
"C"..string.sub(socket.hostname,1,31).."\10", -- class
|
||||
"J"..string.sub(filename,1,99).."\10", -- jobname
|
||||
"L"..string.sub(user,1,31).."\10", -- print banner page
|
||||
"I"..tonumber(indent).."\10", -- indent column count ('f' only)
|
||||
"M"..string.sub(mail,1,128).."\10", -- mail when printed user@host
|
||||
"N"..string.sub(filename,1,131).."\10", -- name of source file
|
||||
"P"..string.sub(user,1,31).."\10", -- user name
|
||||
"T"..string.sub(title,1,79).."\10", -- title for banner ('p' only)
|
||||
"W"..tonumber(width or 132).."\10", -- width of print f,l,p only
|
||||
|
||||
"f"..file.."\10", -- formatted print (remove control chars)
|
||||
"l"..file.."\10", -- print
|
||||
"o"..file.."\10", -- postscript
|
||||
"p"..file.."\10", -- pr format - requires T, L
|
||||
"r"..file.."\10", -- fortran format
|
||||
"U"..file.."\10", -- Unlink (data file only)
|
||||
}
|
||||
]]
|
||||
|
||||
-- generate a varying job number
|
||||
local seq = 0
|
||||
local function newjob(connection)
|
||||
seq = seq + 1
|
||||
return math.floor(socket.gettime() * 1000 + seq)%1000
|
||||
end
|
||||
|
||||
|
||||
local format_codes = {
|
||||
binary = 'l',
|
||||
text = 'f',
|
||||
ps = 'o',
|
||||
pr = 'p',
|
||||
fortran = 'r',
|
||||
l = 'l',
|
||||
r = 'r',
|
||||
o = 'o',
|
||||
p = 'p',
|
||||
f = 'f'
|
||||
}
|
||||
|
||||
-- lp.send{option}
|
||||
-- requires option.file
|
||||
|
||||
send = socket.protect(function(option)
|
||||
socket.try(option and base.type(option) == "table", "invalid options")
|
||||
local file = option.file
|
||||
socket.try(file, "invalid file name")
|
||||
local fh = socket.try(io.open(file,"rb"))
|
||||
local datafile_size = fh:seek("end") -- get total size
|
||||
fh:seek("set") -- go back to start of file
|
||||
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
|
||||
or "localhost"
|
||||
local con = connect(localhost, option)
|
||||
-- format the control file
|
||||
local jobno = newjob()
|
||||
local localip = socket.dns.toip(localhost)
|
||||
localhost = string.sub(localhost,1,31)
|
||||
local user = string.sub(option.user or os.getenv("LPRUSER") or
|
||||
os.getenv("USERNAME") or os.getenv("USER") or "anonymous", 1,31)
|
||||
local lpfile = string.format("dfA%3.3d%-s", jobno, localhost);
|
||||
local fmt = format_codes[option.format] or 'l'
|
||||
local class = string.sub(option.class or localip or localhost,1,31)
|
||||
local _,_,ctlfn = string.find(file,".*[%/%\\](.*)")
|
||||
ctlfn = string.sub(ctlfn or file,1,131)
|
||||
local cfile =
|
||||
string.format("H%-s\nC%-s\nJ%-s\nP%-s\n%.1s%-s\nU%-s\nN%-s\n",
|
||||
localhost,
|
||||
class,
|
||||
option.job or "LuaSocket",
|
||||
user,
|
||||
fmt, lpfile,
|
||||
lpfile,
|
||||
ctlfn); -- mandatory part of ctl file
|
||||
if (option.banner) then cfile = cfile .. 'L'..user..'\10' end
|
||||
if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end
|
||||
if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end
|
||||
if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end
|
||||
if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then
|
||||
cfile = cfile .. 'W'..base.tonumber(option,width)..'\10'
|
||||
end
|
||||
|
||||
con.skt:settimeout(option.timeout or 65)
|
||||
-- send the queue header
|
||||
send_queue(con, option.queue)
|
||||
-- send the control file header
|
||||
local cfilecmd = string.format("\2%d cfA%3.3d%-s\n",string.len(cfile), jobno, localhost);
|
||||
send_hdr(con,cfilecmd)
|
||||
|
||||
-- send the control file
|
||||
send_control(con,cfile)
|
||||
|
||||
-- send the data file header
|
||||
local dfilecmd = string.format("\3%d dfA%3.3d%-s\n",datafile_size, jobno, localhost);
|
||||
send_hdr(con,dfilecmd)
|
||||
|
||||
-- send the data file
|
||||
send_data(con,fh,datafile_size)
|
||||
fh:close()
|
||||
con.skt:close();
|
||||
return jobno, datafile_size
|
||||
end)
|
||||
|
||||
--
|
||||
-- lp.query({host=,queue=printer|'*', format='l'|'s', list=})
|
||||
--
|
||||
query = socket.protect(function(p)
|
||||
p = p or {}
|
||||
local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
|
||||
or "localhost"
|
||||
local con = connect(localhost,p)
|
||||
local fmt
|
||||
if string.sub(p.format or 's',1,1) == 's' then fmt = 3 else fmt = 4 end
|
||||
con.try(con.skt:send(string.format("%c%s %s\n", fmt, p.queue or "*",
|
||||
p.list or "")))
|
||||
local data = con.try(con.skt:receive("*a"))
|
||||
con.skt:close()
|
||||
return data
|
||||
end)
|
24
etc/luasocket/qp.lua
Normal file
24
etc/luasocket/qp.lua
Normal file
@ -0,0 +1,24 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- Little program to convert to and from Quoted-Printable
|
||||
-- LuaSocket sample files
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: qp.lua,v 1.5 2004/06/17 21:46:22 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
local ltn12 = require("ltn12")
|
||||
local mime = require("mime")
|
||||
local convert
|
||||
arg = arg or {}
|
||||
local mode = arg and arg[1] or "-et"
|
||||
if mode == "-et" then
|
||||
local normalize = mime.normalize()
|
||||
local qp = mime.encode("quoted-printable")
|
||||
local wrap = mime.wrap("quoted-printable")
|
||||
convert = ltn12.filter.chain(normalize, qp, wrap)
|
||||
elseif mode == "-eb" then
|
||||
local qp = mime.encode("quoted-printable", "binary")
|
||||
local wrap = mime.wrap("quoted-printable")
|
||||
convert = ltn12.filter.chain(qp, wrap)
|
||||
else convert = mime.decode("quoted-printable") end
|
||||
local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert)
|
||||
local sink = ltn12.sink.file(io.stdout)
|
||||
ltn12.pump.all(source, sink)
|
155
etc/luasocket/tftp.lua
Normal file
155
etc/luasocket/tftp.lua
Normal file
@ -0,0 +1,155 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- TFTP support for the Lua language
|
||||
-- LuaSocket toolkit.
|
||||
-- Author: Diego Nehab
|
||||
-- RCS ID: $Id: tftp.lua,v 1.16 2005/11/22 08:33:29 diego Exp $
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Load required files
|
||||
-----------------------------------------------------------------------------
|
||||
local base = _G
|
||||
local table = require("table")
|
||||
local math = require("math")
|
||||
local string = require("string")
|
||||
local socket = require("socket")
|
||||
local ltn12 = require("ltn12")
|
||||
local url = require("socket.url")
|
||||
module("socket.tftp")
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
-----------------------------------------------------------------------------
|
||||
local char = string.char
|
||||
local byte = string.byte
|
||||
|
||||
PORT = 69
|
||||
local OP_RRQ = 1
|
||||
local OP_WRQ = 2
|
||||
local OP_DATA = 3
|
||||
local OP_ACK = 4
|
||||
local OP_ERROR = 5
|
||||
local OP_INV = {"RRQ", "WRQ", "DATA", "ACK", "ERROR"}
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Packet creation functions
|
||||
-----------------------------------------------------------------------------
|
||||
local function RRQ(source, mode)
|
||||
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
|
||||
end
|
||||
|
||||
local function WRQ(source, mode)
|
||||
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
|
||||
end
|
||||
|
||||
local function ACK(block)
|
||||
local low, high
|
||||
low = math.mod(block, 256)
|
||||
high = (block - low)/256
|
||||
return char(0, OP_ACK, high, low)
|
||||
end
|
||||
|
||||
local function get_OP(dgram)
|
||||
local op = byte(dgram, 1)*256 + byte(dgram, 2)
|
||||
return op
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Packet analysis functions
|
||||
-----------------------------------------------------------------------------
|
||||
local function split_DATA(dgram)
|
||||
local block = byte(dgram, 3)*256 + byte(dgram, 4)
|
||||
local data = string.sub(dgram, 5)
|
||||
return block, data
|
||||
end
|
||||
|
||||
local function get_ERROR(dgram)
|
||||
local code = byte(dgram, 3)*256 + byte(dgram, 4)
|
||||
local msg
|
||||
_,_, msg = string.find(dgram, "(.*)\000", 5)
|
||||
return string.format("error code %d: %s", code, msg)
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- The real work
|
||||
-----------------------------------------------------------------------------
|
||||
local function tget(gett)
|
||||
local retries, dgram, sent, datahost, dataport, code
|
||||
local last = 0
|
||||
socket.try(gett.host, "missing host")
|
||||
local con = socket.try(socket.udp())
|
||||
local try = socket.newtry(function() con:close() end)
|
||||
-- convert from name to ip if needed
|
||||
gett.host = try(socket.dns.toip(gett.host))
|
||||
con:settimeout(1)
|
||||
-- first packet gives data host/port to be used for data transfers
|
||||
local path = string.gsub(gett.path or "", "^/", "")
|
||||
path = url.unescape(path)
|
||||
retries = 0
|
||||
repeat
|
||||
sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port))
|
||||
dgram, datahost, dataport = con:receivefrom()
|
||||
retries = retries + 1
|
||||
until dgram or datahost ~= "timeout" or retries > 5
|
||||
try(dgram, datahost)
|
||||
-- associate socket with data host/port
|
||||
try(con:setpeername(datahost, dataport))
|
||||
-- default sink
|
||||
local sink = gett.sink or ltn12.sink.null()
|
||||
-- process all data packets
|
||||
while 1 do
|
||||
-- decode packet
|
||||
code = get_OP(dgram)
|
||||
try(code ~= OP_ERROR, get_ERROR(dgram))
|
||||
try(code == OP_DATA, "unhandled opcode " .. code)
|
||||
-- get data packet parts
|
||||
local block, data = split_DATA(dgram)
|
||||
-- if not repeated, write
|
||||
if block == last+1 then
|
||||
try(sink(data))
|
||||
last = block
|
||||
end
|
||||
-- last packet brings less than 512 bytes of data
|
||||
if string.len(data) < 512 then
|
||||
try(con:send(ACK(block)))
|
||||
try(con:close())
|
||||
try(sink(nil))
|
||||
return 1
|
||||
end
|
||||
-- get the next packet
|
||||
retries = 0
|
||||
repeat
|
||||
sent = try(con:send(ACK(last)))
|
||||
dgram, err = con:receive()
|
||||
retries = retries + 1
|
||||
until dgram or err ~= "timeout" or retries > 5
|
||||
try(dgram, err)
|
||||
end
|
||||
end
|
||||
|
||||
local default = {
|
||||
port = PORT,
|
||||
path ="/",
|
||||
scheme = "tftp"
|
||||
}
|
||||
|
||||
local function parse(u)
|
||||
local t = socket.try(url.parse(u, default))
|
||||
socket.try(t.scheme == "tftp", "invalid scheme '" .. t.scheme .. "'")
|
||||
socket.try(t.host, "invalid host")
|
||||
return t
|
||||
end
|
||||
|
||||
local function sget(u)
|
||||
local gett = parse(u)
|
||||
local t = {}
|
||||
gett.sink = ltn12.sink.table(t)
|
||||
tget(gett)
|
||||
return table.concat(t)
|
||||
end
|
||||
|
||||
get = socket.protect(function(gett)
|
||||
if base.type(gett) == "string" then return sget(gett)
|
||||
else return tget(gett) end
|
||||
end)
|
||||
|
109
etc/tests/luairc/luabot.lua
Executable file
109
etc/tests/luairc/luabot.lua
Executable file
@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env lua
|
||||
|
||||
local irc = require 'irc'
|
||||
irc.DEBUG = true
|
||||
|
||||
local nick = "doylua"
|
||||
|
||||
local envs = {}
|
||||
|
||||
local function create_env()
|
||||
return {
|
||||
_VERSION = _VERSION,
|
||||
assert = assert,
|
||||
collectgarbage = collectgarbage,
|
||||
error = error,
|
||||
getfenv = getfenv,
|
||||
getmetatable = getmetatable,
|
||||
ipairs = ipairs,
|
||||
loadstring = loadstring,
|
||||
next = next,
|
||||
pairs = pairs,
|
||||
pcall = pcall,
|
||||
rawequal = rawequal,
|
||||
rawget = rawget,
|
||||
rawset = rawset,
|
||||
select = select,
|
||||
setfenv = setfenv,
|
||||
setmetatable = setmetatable,
|
||||
tonumber = tonumber,
|
||||
tostring = tostring,
|
||||
type = type,
|
||||
unpack = unpack,
|
||||
xpcall = xpcall,
|
||||
coroutine = coroutine,
|
||||
math = math,
|
||||
string = string,
|
||||
table = table,
|
||||
}
|
||||
end
|
||||
|
||||
local commands = {
|
||||
eval = function(target, from, code)
|
||||
code = code:gsub("^=", "return ")
|
||||
local fn, err = loadstring(code)
|
||||
if not fn then
|
||||
irc.say(target, from .. ": Error loading code: " .. code .. err:match(".*(:.-)$"))
|
||||
return
|
||||
else
|
||||
setfenv(fn, envs[from])
|
||||
local result = {pcall(fn)}
|
||||
local success = table.remove(result, 1)
|
||||
if not success then
|
||||
irc.say(target, from .. ": Error running code: " .. code .. result[1]:match(".*(:.-)$"))
|
||||
else
|
||||
if result[1] == nil then
|
||||
irc.say(target, from .. ": nil")
|
||||
else
|
||||
irc.say(target, from .. ": " .. table.concat(result, ", "))
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
clear = function(target, from)
|
||||
irc.say(target, from .. ": Clearing your environment")
|
||||
envs[from] = create_env()
|
||||
end,
|
||||
help = function(target, from, arg)
|
||||
if arg == "" or not arg then
|
||||
irc.say(target, from .. ": Commands: !clear, !eval, !help")
|
||||
elseif arg == "eval" then
|
||||
irc.say(target, from .. ": Evaluates a Lua statement in your own persistent environment")
|
||||
elseif arg == "clear" then
|
||||
irc.say(target, from .. ": Clears your personal environment")
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
irc.register_callback("connect", function()
|
||||
irc.join("#doytest")
|
||||
end)
|
||||
|
||||
irc.register_callback("channel_msg", function(channel, from, message)
|
||||
message = message:gsub("^" .. nick .. "[:,>] ", "!eval ")
|
||||
local is_cmd, cmd, arg = message:match("^(!)([%w_]+) ?(.-)$")
|
||||
if is_cmd and commands[cmd] then
|
||||
envs[from] = envs[from] or create_env()
|
||||
commands[cmd](channel.name, from, arg)
|
||||
end
|
||||
end)
|
||||
|
||||
irc.register_callback("private_msg", function(from, message)
|
||||
message = message:gsub("^" .. nick .. "[:,>] ", "!eval ")
|
||||
local is_cmd, cmd, arg = message:match("^(!)([%w_]+) ?(.-)$")
|
||||
envs[from] = envs[from] or create_env()
|
||||
if is_cmd and commands[cmd] then
|
||||
commands[cmd](from, from, arg)
|
||||
else
|
||||
commands["eval"](from, from, message)
|
||||
end
|
||||
end)
|
||||
|
||||
irc.register_callback("nick_change", function(from, old_nick)
|
||||
if envs[old_nick] and not envs[from] then
|
||||
envs[from] = envs[old_nick]
|
||||
envs[old_nick] = nil
|
||||
end
|
||||
end)
|
||||
|
||||
irc.connect{network = "irc.freenode.net", nick = nick, pass = "doylua"}
|
228
etc/tests/luairc/test.lua
Executable file
228
etc/tests/luairc/test.lua
Executable file
@ -0,0 +1,228 @@
|
||||
#!/usr/bin/lua
|
||||
|
||||
local irc = require "irc"
|
||||
local dcc = require "irc.dcc"
|
||||
|
||||
irc.DEBUG = true
|
||||
|
||||
local ip_prog = io.popen("get_ip")
|
||||
local ip = ip_prog:read()
|
||||
ip_prog:close()
|
||||
irc.set_ip(ip)
|
||||
|
||||
local function print_state()
|
||||
for chan in irc.channels() do
|
||||
print(chan..": Channel ops: "..table.concat(chan:ops(), " "))
|
||||
print(chan..": Channel voices: "..table.concat(chan:voices(), " "))
|
||||
print(chan..": Channel normal users: "..table.concat(chan:users(), " "))
|
||||
print(chan..": All channel members: "..table.concat(chan:members(), " "))
|
||||
end
|
||||
end
|
||||
|
||||
local function on_connect()
|
||||
print("Joining channel #doytest...")
|
||||
irc.join("#doytest")
|
||||
print("Joining channel #doytest2...")
|
||||
irc.join("#doytest2")
|
||||
end
|
||||
irc.register_callback("connect", on_connect)
|
||||
|
||||
local function on_me_join(chan)
|
||||
print("Join to " .. chan .. " complete.")
|
||||
print(chan .. ": Channel type: " .. chan.chanmode)
|
||||
if chan.topic.text and chan.topic.text ~= "" then
|
||||
print(chan .. ": Channel topic: " .. chan.topic.text)
|
||||
print(" Set by " .. chan.topic.user ..
|
||||
" at " .. os.date("%c", chan.topic.time))
|
||||
end
|
||||
irc.act(chan.name, "is here")
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("me_join", on_me_join)
|
||||
|
||||
local function on_join(chan, user)
|
||||
print("I saw a join to " .. chan)
|
||||
if tostring(user) ~= "doylua" then
|
||||
irc.say(tostring(chan), "Hi, " .. user)
|
||||
end
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("join", on_join)
|
||||
|
||||
local function on_part(chan, user, part_msg)
|
||||
print("I saw a part from " .. chan .. " saying " .. part_msg)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("part", on_part)
|
||||
|
||||
local function on_nick_change(new_nick, old_nick)
|
||||
print("I saw a nick change: " .. old_nick .. " -> " .. new_nick)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("nick_change", on_nick_change)
|
||||
|
||||
local function on_kick(chan, user)
|
||||
print("I saw a kick in " .. chan)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("kick", on_kick)
|
||||
|
||||
local function on_quit(chan, user)
|
||||
print("I saw a quit from " .. chan)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("quit", on_quit)
|
||||
|
||||
local function whois_cb(cb_data)
|
||||
print("WHOIS data for " .. cb_data.nick)
|
||||
if cb_data.user then print("Username: " .. cb_data.user) end
|
||||
if cb_data.host then print("Host: " .. cb_data.host) end
|
||||
if cb_data.realname then print("Realname: " .. cb_data.realname) end
|
||||
if cb_data.server then print("Server: " .. cb_data.server) end
|
||||
if cb_data.serverinfo then print("Serverinfo: " .. cb_data.serverinfo) end
|
||||
if cb_data.away_msg then print("Awaymsg: " .. cb_data.away_msg) end
|
||||
if cb_data.is_oper then print(nick .. "is an IRCop") end
|
||||
if cb_data.idle_time then print("Idletime: " .. cb_data.idle_time) end
|
||||
if cb_data.channels then
|
||||
print("Channel list for " .. cb_data.nick .. ":")
|
||||
for _, channel in ipairs(cb_data.channels) do print(channel) end
|
||||
end
|
||||
end
|
||||
|
||||
local function serverversion_cb(cb_data)
|
||||
print("VERSION data for " .. cb_data.server)
|
||||
print("Version: " .. cb_data.version)
|
||||
print("Comments: " .. cb_data.comments)
|
||||
end
|
||||
|
||||
local function ping_cb(cb_data)
|
||||
print("CTCP PING for " .. cb_data.nick)
|
||||
print("Roundtrip time: " .. cb_data.time .. "s")
|
||||
end
|
||||
|
||||
local function time_cb(cb_data)
|
||||
print("CTCP TIME for " .. cb_data.nick)
|
||||
print("Localtime: " .. cb_data.time)
|
||||
end
|
||||
|
||||
local function version_cb(cb_data)
|
||||
print("CTCP VERSION for " .. cb_data.nick)
|
||||
print("Version: " .. cb_data.version)
|
||||
end
|
||||
|
||||
local function stime_cb(cb_data)
|
||||
print("TIME for " .. cb_data.server)
|
||||
print("Server time: " .. cb_data.time)
|
||||
end
|
||||
|
||||
local function on_channel_msg(chan, from, msg)
|
||||
if from == "doy" then
|
||||
if msg == "leave" then
|
||||
irc.part(chan.name)
|
||||
return
|
||||
elseif msg:sub(1, 3) == "op " then
|
||||
chan:op(msg:sub(4))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "deop " then
|
||||
chan:deop(msg:sub(6))
|
||||
return
|
||||
elseif msg:sub(1, 6) == "voice " then
|
||||
chan:voice(msg:sub(7))
|
||||
return
|
||||
elseif msg:sub(1, 8) == "devoice " then
|
||||
chan:devoice(msg:sub(9))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "kick " then
|
||||
chan:kick(msg:sub(6))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "send " then
|
||||
dcc.send(from, msg:sub(6))
|
||||
return
|
||||
elseif msg:sub(1, 6) == "whois " then
|
||||
irc.whois(whois_cb, msg:sub(7))
|
||||
return
|
||||
elseif msg:sub(1, 8) == "sversion" then
|
||||
irc.server_version(serverversion_cb)
|
||||
return
|
||||
elseif msg:sub(1, 5) == "ping " then
|
||||
irc.ctcp_ping(ping_cb, msg:sub(6))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "time " then
|
||||
irc.ctcp_time(time_cb, msg:sub(6))
|
||||
return
|
||||
elseif msg:sub(1, 8) == "version " then
|
||||
irc.ctcp_version(version_cb, msg:sub(9))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "stime" then
|
||||
irc.server_time(stime_cb)
|
||||
return
|
||||
elseif msg:sub(1, 6) == "trace " then
|
||||
irc.trace(trace_cb, msg:sub(7))
|
||||
return
|
||||
elseif msg:sub(1, 5) == "trace" then
|
||||
irc.trace(trace_cb)
|
||||
return
|
||||
end
|
||||
end
|
||||
if from ~= "doylua" then
|
||||
irc.say(chan.name, from .. ": " .. msg)
|
||||
end
|
||||
end
|
||||
irc.register_callback("channel_msg", on_channel_msg)
|
||||
|
||||
local function on_private_msg(from, msg)
|
||||
if from == "doy" then
|
||||
if msg == "leave" then
|
||||
irc.quit("gone")
|
||||
return
|
||||
elseif msg:sub(1, 5) == "send " then
|
||||
dcc.send(from, msg:sub(6))
|
||||
return
|
||||
end
|
||||
end
|
||||
if from ~= "doylua" then
|
||||
irc.say(from, msg)
|
||||
end
|
||||
end
|
||||
irc.register_callback("private_msg", on_private_msg)
|
||||
|
||||
local function on_channel_act(chan, from, msg)
|
||||
irc.act(chan.name, "jumps on " .. from)
|
||||
end
|
||||
irc.register_callback("channel_act", on_channel_act)
|
||||
|
||||
local function on_private_act(from, msg)
|
||||
irc.act(from, "jumps on you")
|
||||
end
|
||||
irc.register_callback("private_act", on_private_act)
|
||||
|
||||
local function on_op(chan, from, nick)
|
||||
print(nick .. " was opped in " .. chan .. " by " .. from)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("op", on_op)
|
||||
|
||||
local function on_deop(chan, from, nick)
|
||||
print(nick .. " was deopped in " .. chan .. " by " .. from)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("deop", on_deop)
|
||||
|
||||
local function on_voice(chan, from, nick)
|
||||
print(nick .. " was voiced in " .. chan .. " by " .. from)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("voice", on_voice)
|
||||
|
||||
local function on_devoice(chan, from, nick)
|
||||
print(nick .. " was devoiced in " .. chan .. " by " .. from)
|
||||
print_state()
|
||||
end
|
||||
irc.register_callback("devoice", on_devoice)
|
||||
|
||||
local function on_dcc_send()
|
||||
return true
|
||||
end
|
||||
irc.register_callback("dcc_send", on_dcc_send)
|
||||
|
||||
irc.connect{network = "irc.freenode.net", nick = "doylua"}
|
12
etc/tests/luasocket/README
Normal file
12
etc/tests/luasocket/README
Normal file
@ -0,0 +1,12 @@
|
||||
This provides the automated test scripts used to make sure the library
|
||||
is working properly.
|
||||
|
||||
The files provided are:
|
||||
|
||||
testsrvr.lua -- test server
|
||||
testclnt.lua -- test client
|
||||
|
||||
To run these tests, just run lua on the server and then on the client.
|
||||
|
||||
Good luck,
|
||||
Diego.
|
655
etc/tests/luasocket/testclnt.lua
Normal file
655
etc/tests/luasocket/testclnt.lua
Normal file
@ -0,0 +1,655 @@
|
||||
local socket = require"socket"
|
||||
|
||||
host = host or "localhost"
|
||||
port = port or "8383"
|
||||
|
||||
function pass(...)
|
||||
local s = string.format(unpack(arg))
|
||||
io.stderr:write(s, "\n")
|
||||
end
|
||||
|
||||
function fail(...)
|
||||
local s = string.format(unpack(arg))
|
||||
io.stderr:write("ERROR: ", s, "!\n")
|
||||
socket.sleep(3)
|
||||
os.exit()
|
||||
end
|
||||
|
||||
function warn(...)
|
||||
local s = string.format(unpack(arg))
|
||||
io.stderr:write("WARNING: ", s, "\n")
|
||||
end
|
||||
|
||||
function remote(...)
|
||||
local s = string.format(unpack(arg))
|
||||
s = string.gsub(s, "\n", ";")
|
||||
s = string.gsub(s, "%s+", " ")
|
||||
s = string.gsub(s, "^%s*", "")
|
||||
control:send(s .. "\n")
|
||||
control:receive()
|
||||
end
|
||||
|
||||
function test(test)
|
||||
io.stderr:write("----------------------------------------------\n",
|
||||
"testing: ", test, "\n",
|
||||
"----------------------------------------------\n")
|
||||
end
|
||||
|
||||
function check_timeout(tm, sl, elapsed, err, opp, mode, alldone)
|
||||
if tm < sl then
|
||||
if opp == "send" then
|
||||
if not err then warn("must be buffered")
|
||||
elseif err == "timeout" then pass("proper timeout")
|
||||
else fail("unexpected error '%s'", err) end
|
||||
else
|
||||
if err ~= "timeout" then fail("should have timed out")
|
||||
else pass("proper timeout") end
|
||||
end
|
||||
else
|
||||
if mode == "total" then
|
||||
if elapsed > tm then
|
||||
if err ~= "timeout" then fail("should have timed out")
|
||||
else pass("proper timeout") end
|
||||
elseif elapsed < tm then
|
||||
if err then fail(err)
|
||||
else pass("ok") end
|
||||
else
|
||||
if alldone then
|
||||
if err then fail("unexpected error '%s'", err)
|
||||
else pass("ok") end
|
||||
else
|
||||
if err ~= "timeout" then fail(err)
|
||||
else pass("proper timeoutk") end
|
||||
end
|
||||
end
|
||||
else
|
||||
if err then fail(err)
|
||||
else pass("ok") end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not socket._DEBUG then
|
||||
fail("Please define LUASOCKET_DEBUG and recompile LuaSocket")
|
||||
end
|
||||
|
||||
io.stderr:write("----------------------------------------------\n",
|
||||
"LuaSocket Test Procedures\n",
|
||||
"----------------------------------------------\n")
|
||||
|
||||
start = socket.gettime()
|
||||
|
||||
function reconnect()
|
||||
io.stderr:write("attempting data connection... ")
|
||||
if data then data:close() end
|
||||
remote [[
|
||||
if data then data:close() data = nil end
|
||||
data = server:accept()
|
||||
data:setoption("tcp-nodelay", true)
|
||||
]]
|
||||
data, err = socket.connect(host, port)
|
||||
if not data then fail(err)
|
||||
else pass("connected!") end
|
||||
data:setoption("tcp-nodelay", true)
|
||||
end
|
||||
|
||||
pass("attempting control connection...")
|
||||
control, err = socket.connect(host, port)
|
||||
if err then fail(err)
|
||||
else pass("connected!") end
|
||||
control:setoption("tcp-nodelay", true)
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_methods(sock, methods)
|
||||
for _, v in methods do
|
||||
if type(sock[v]) ~= "function" then
|
||||
fail(sock.class .. " method '" .. v .. "' not registered")
|
||||
end
|
||||
end
|
||||
pass(sock.class .. " methods are ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_mixed(len)
|
||||
reconnect()
|
||||
local inter = math.ceil(len/4)
|
||||
local p1 = "unix " .. string.rep("x", inter) .. "line\n"
|
||||
local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
|
||||
local p3 = "raw " .. string.rep("z", inter) .. "bytes"
|
||||
local p4 = "end" .. string.rep("w", inter) .. "bytes"
|
||||
local bp1, bp2, bp3, bp4
|
||||
remote (string.format("str = data:receive(%d)",
|
||||
string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4)))
|
||||
sent, err = data:send(p1..p2..p3..p4)
|
||||
if err then fail(err) end
|
||||
remote "data:send(str); data:close()"
|
||||
bp1, err = data:receive()
|
||||
if err then fail(err) end
|
||||
bp2, err = data:receive()
|
||||
if err then fail(err) end
|
||||
bp3, err = data:receive(string.len(p3))
|
||||
if err then fail(err) end
|
||||
bp4, err = data:receive("*a")
|
||||
if err then fail(err) end
|
||||
if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then
|
||||
pass("patterns match")
|
||||
else fail("patterns don't match") end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_asciiline(len)
|
||||
reconnect()
|
||||
local str, str10, back, err
|
||||
str = string.rep("x", math.mod(len, 10))
|
||||
str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
|
||||
str = str .. str10
|
||||
remote "str = data:receive()"
|
||||
sent, err = data:send(str.."\n")
|
||||
if err then fail(err) end
|
||||
remote "data:send(str ..'\\n')"
|
||||
back, err = data:receive()
|
||||
if err then fail(err) end
|
||||
if back == str then pass("lines match")
|
||||
else fail("lines don't match") end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_rawline(len)
|
||||
reconnect()
|
||||
local str, str10, back, err
|
||||
str = string.rep(string.char(47), math.mod(len, 10))
|
||||
str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
|
||||
math.floor(len/10))
|
||||
str = str .. str10
|
||||
remote "str = data:receive()"
|
||||
sent, err = data:send(str.."\n")
|
||||
if err then fail(err) end
|
||||
remote "data:send(str..'\\n')"
|
||||
back, err = data:receive()
|
||||
if err then fail(err) end
|
||||
if back == str then pass("lines match")
|
||||
else fail("lines don't match") end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_raw(len)
|
||||
reconnect()
|
||||
local half = math.floor(len/2)
|
||||
local s1, s2, back, err
|
||||
s1 = string.rep("x", half)
|
||||
s2 = string.rep("y", len-half)
|
||||
remote (string.format("str = data:receive(%d)", len))
|
||||
sent, err = data:send(s1)
|
||||
if err then fail(err) end
|
||||
sent, err = data:send(s2)
|
||||
if err then fail(err) end
|
||||
remote "data:send(str)"
|
||||
back, err = data:receive(len)
|
||||
if err then fail(err) end
|
||||
if back == s1..s2 then pass("blocks match")
|
||||
else fail("blocks don't match") end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_totaltimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = string.rep('a', %d)
|
||||
data:send(str)
|
||||
print('server: sleeping for %ds')
|
||||
socket.sleep(%d)
|
||||
print('server: woke up')
|
||||
data:send(str)
|
||||
]], 2*tm, len, sl, sl))
|
||||
data:settimeout(tm, "total")
|
||||
local t = socket.gettime()
|
||||
str, err, partial, elapsed = data:receive(2*len)
|
||||
check_timeout(tm, sl, elapsed, err, "receive", "total",
|
||||
string.len(str or partial) == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_totaltimeoutsend(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = data:receive(%d)
|
||||
print('server: sleeping for %ds')
|
||||
socket.sleep(%d)
|
||||
print('server: woke up')
|
||||
str = data:receive(%d)
|
||||
]], 2*tm, len, sl, sl, len))
|
||||
data:settimeout(tm, "total")
|
||||
str = string.rep("a", 2*len)
|
||||
total, err, partial, elapsed = data:send(str)
|
||||
check_timeout(tm, sl, elapsed, err, "send", "total",
|
||||
total == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_blockingtimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = string.rep('a', %d)
|
||||
data:send(str)
|
||||
print('server: sleeping for %ds')
|
||||
socket.sleep(%d)
|
||||
print('server: woke up')
|
||||
data:send(str)
|
||||
]], 2*tm, len, sl, sl))
|
||||
data:settimeout(tm)
|
||||
str, err, partial, elapsed = data:receive(2*len)
|
||||
check_timeout(tm, sl, elapsed, err, "receive", "blocking",
|
||||
string.len(str or partial) == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_blockingtimeoutsend(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = data:receive(%d)
|
||||
print('server: sleeping for %ds')
|
||||
socket.sleep(%d)
|
||||
print('server: woke up')
|
||||
str = data:receive(%d)
|
||||
]], 2*tm, len, sl, sl, len))
|
||||
data:settimeout(tm)
|
||||
str = string.rep("a", 2*len)
|
||||
total, err, partial, elapsed = data:send(str)
|
||||
check_timeout(tm, sl, elapsed, err, "send", "blocking",
|
||||
total == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function empty_connect()
|
||||
reconnect()
|
||||
if data then data:close() data = nil end
|
||||
remote [[
|
||||
if data then data:close() data = nil end
|
||||
data = server:accept()
|
||||
]]
|
||||
data, err = socket.connect("", port)
|
||||
if not data then
|
||||
pass("ok")
|
||||
data = socket.connect(host, port)
|
||||
else
|
||||
pass("gethostbyname returns localhost on empty string...")
|
||||
end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function isclosed(c)
|
||||
return c:getfd() == -1 or c:getfd() == (2^32-1)
|
||||
end
|
||||
|
||||
function active_close()
|
||||
reconnect()
|
||||
if isclosed(data) then fail("should not be closed") end
|
||||
data:close()
|
||||
if not isclosed(data) then fail("should be closed") end
|
||||
data = nil
|
||||
local udp = socket.udp()
|
||||
if isclosed(udp) then fail("should not be closed") end
|
||||
udp:close()
|
||||
if not isclosed(udp) then fail("should be closed") end
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_closed()
|
||||
local back, partial, err
|
||||
local str = 'little string'
|
||||
reconnect()
|
||||
pass("trying read detection")
|
||||
remote (string.format ([[
|
||||
data:send('%s')
|
||||
data:close()
|
||||
data = nil
|
||||
]], str))
|
||||
-- try to get a line
|
||||
back, err, partial = data:receive()
|
||||
if not err then fail("should have gotten 'closed'.")
|
||||
elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.")
|
||||
elseif str ~= partial then fail("didn't receive partial result.")
|
||||
else pass("graceful 'closed' received") end
|
||||
reconnect()
|
||||
pass("trying write detection")
|
||||
remote [[
|
||||
data:close()
|
||||
data = nil
|
||||
]]
|
||||
total, err, partial = data:send(string.rep("ugauga", 100000))
|
||||
if not err then
|
||||
pass("failed: output buffer is at least %d bytes long!", total)
|
||||
elseif err ~= "closed" then
|
||||
fail("got '"..err.."' instead of 'closed'.")
|
||||
else
|
||||
pass("graceful 'closed' received after %d bytes were sent", partial)
|
||||
end
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_selectbugs()
|
||||
local r, s, e = socket.select(nil, nil, 0.1)
|
||||
assert(type(r) == "table" and type(s) == "table" and
|
||||
(e == "timeout" or e == "error"))
|
||||
pass("both nil: ok")
|
||||
local udp = socket.udp()
|
||||
udp:close()
|
||||
r, s, e = socket.select({ udp }, { udp }, 0.1)
|
||||
assert(type(r) == "table" and type(s) == "table" and
|
||||
(e == "timeout" or e == "error"))
|
||||
pass("closed sockets: ok")
|
||||
e = pcall(socket.select, "wrong", 1, 0.1)
|
||||
assert(e == false)
|
||||
e = pcall(socket.select, {}, 1, 0.1)
|
||||
assert(e == false)
|
||||
pass("invalid input: ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function accept_timeout()
|
||||
io.stderr:write("accept with timeout (if it hangs, it failed): ")
|
||||
local s, e = socket.bind("*", 0, 0)
|
||||
assert(s, e)
|
||||
local t = socket.gettime()
|
||||
s:settimeout(1)
|
||||
local c, e = s:accept()
|
||||
assert(not c, "should not accept")
|
||||
assert(e == "timeout", string.format("wrong error message (%s)", e))
|
||||
t = socket.gettime() - t
|
||||
assert(t < 2, string.format("took to long to give up (%gs)", t))
|
||||
s:close()
|
||||
pass("good")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function connect_timeout()
|
||||
io.stderr:write("connect with timeout (if it hangs, it failed!): ")
|
||||
local t = socket.gettime()
|
||||
local c, e = socket.tcp()
|
||||
assert(c, e)
|
||||
c:settimeout(0.1)
|
||||
local t = socket.gettime()
|
||||
local r, e = c:connect("10.0.0.1", 81)
|
||||
print(r, e)
|
||||
assert(not r, "should not connect")
|
||||
assert(socket.gettime() - t < 2, "took too long to give up.")
|
||||
c:close()
|
||||
print("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function accept_errors()
|
||||
io.stderr:write("not listening: ")
|
||||
local d, e = socket.bind("*", 0)
|
||||
assert(d, e);
|
||||
local c, e = socket.tcp();
|
||||
assert(c, e);
|
||||
d:setfd(c:getfd())
|
||||
d:settimeout(2)
|
||||
local r, e = d:accept()
|
||||
assert(not r and e)
|
||||
print("ok: ", e)
|
||||
io.stderr:write("not supported: ")
|
||||
local c, e = socket.udp()
|
||||
assert(c, e);
|
||||
d:setfd(c:getfd())
|
||||
local r, e = d:accept()
|
||||
assert(not r and e)
|
||||
print("ok: ", e)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function connect_errors()
|
||||
io.stderr:write("connection refused: ")
|
||||
local c, e = socket.connect("localhost", 1);
|
||||
assert(not c and e)
|
||||
print("ok: ", e)
|
||||
io.stderr:write("host not found: ")
|
||||
local c, e = socket.connect("host.is.invalid", 1);
|
||||
assert(not c and e, e)
|
||||
print("ok: ", e)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function rebind_test()
|
||||
local c = socket.bind("localhost", 0)
|
||||
local i, p = c:getsockname()
|
||||
local s, e = socket.tcp()
|
||||
assert(s, e)
|
||||
s:setoption("reuseaddr", false)
|
||||
r, e = s:bind("localhost", p)
|
||||
assert(not r, "managed to rebind!")
|
||||
assert(e)
|
||||
print("ok: ", e)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function getstats_test()
|
||||
reconnect()
|
||||
local t = 0
|
||||
for i = 1, 25 do
|
||||
local c = math.random(1, 100)
|
||||
remote (string.format ([[
|
||||
str = data:receive(%d)
|
||||
data:send(str)
|
||||
]], c))
|
||||
data:send(string.rep("a", c))
|
||||
data:receive(c)
|
||||
t = t + c
|
||||
local r, s, a = data:getstats()
|
||||
assert(r == t, "received count failed" .. tostring(r)
|
||||
.. "/" .. tostring(t))
|
||||
assert(s == t, "sent count failed" .. tostring(s)
|
||||
.. "/" .. tostring(t))
|
||||
end
|
||||
print("ok")
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_nonblocking(size)
|
||||
reconnect()
|
||||
print("Testing " .. 2*size .. " bytes")
|
||||
remote(string.format([[
|
||||
data:send(string.rep("a", %d))
|
||||
socket.sleep(0.5)
|
||||
data:send(string.rep("b", %d) .. "\n")
|
||||
]], size, size))
|
||||
local err = "timeout"
|
||||
local part = ""
|
||||
local str
|
||||
data:settimeout(0)
|
||||
while 1 do
|
||||
str, err, part = data:receive("*l", part)
|
||||
if err ~= "timeout" then break end
|
||||
end
|
||||
assert(str == (string.rep("a", size) .. string.rep("b", size)))
|
||||
reconnect()
|
||||
remote(string.format([[
|
||||
str = data:receive(%d)
|
||||
socket.sleep(0.5)
|
||||
str = data:receive(2*%d, str)
|
||||
data:send(str)
|
||||
]], size, size))
|
||||
data:settimeout(0)
|
||||
local start = 0
|
||||
while 1 do
|
||||
ret, err, start = data:send(str, start+1)
|
||||
if err ~= "timeout" then break end
|
||||
end
|
||||
data:send("\n")
|
||||
data:settimeout(-1)
|
||||
local back = data:receive(2*size)
|
||||
assert(back == str, "'" .. back .. "' vs '" .. str .. "'")
|
||||
print("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
||||
|
||||
test("method registration")
|
||||
test_methods(socket.tcp(), {
|
||||
"accept",
|
||||
"bind",
|
||||
"close",
|
||||
"connect",
|
||||
"dirty",
|
||||
"getfd",
|
||||
"getpeername",
|
||||
"getsockname",
|
||||
"getstats",
|
||||
"setstats",
|
||||
"listen",
|
||||
"receive",
|
||||
"send",
|
||||
"setfd",
|
||||
"setoption",
|
||||
"setpeername",
|
||||
"setsockname",
|
||||
"settimeout",
|
||||
"shutdown",
|
||||
})
|
||||
|
||||
test_methods(socket.udp(), {
|
||||
"close",
|
||||
"getpeername",
|
||||
"dirty",
|
||||
"getfd",
|
||||
"getpeername",
|
||||
"getsockname",
|
||||
"receive",
|
||||
"receivefrom",
|
||||
"send",
|
||||
"sendto",
|
||||
"setfd",
|
||||
"setoption",
|
||||
"setpeername",
|
||||
"setsockname",
|
||||
"settimeout"
|
||||
})
|
||||
|
||||
test("select function")
|
||||
test_selectbugs()
|
||||
|
||||
test("connect function")
|
||||
connect_timeout()
|
||||
empty_connect()
|
||||
connect_errors()
|
||||
|
||||
test("rebinding: ")
|
||||
rebind_test()
|
||||
|
||||
test("active close: ")
|
||||
active_close()
|
||||
|
||||
test("closed connection detection: ")
|
||||
test_closed()
|
||||
|
||||
test("accept function: ")
|
||||
accept_timeout()
|
||||
accept_errors()
|
||||
|
||||
test("getstats test")
|
||||
getstats_test()
|
||||
|
||||
test("character line")
|
||||
test_asciiline(1)
|
||||
test_asciiline(17)
|
||||
test_asciiline(200)
|
||||
test_asciiline(4091)
|
||||
test_asciiline(80199)
|
||||
test_asciiline(8000000)
|
||||
test_asciiline(80199)
|
||||
test_asciiline(4091)
|
||||
test_asciiline(200)
|
||||
test_asciiline(17)
|
||||
test_asciiline(1)
|
||||
|
||||
test("mixed patterns")
|
||||
test_mixed(1)
|
||||
test_mixed(17)
|
||||
test_mixed(200)
|
||||
test_mixed(4091)
|
||||
test_mixed(801990)
|
||||
test_mixed(4091)
|
||||
test_mixed(200)
|
||||
test_mixed(17)
|
||||
test_mixed(1)
|
||||
|
||||
test("binary line")
|
||||
test_rawline(1)
|
||||
test_rawline(17)
|
||||
test_rawline(200)
|
||||
test_rawline(4091)
|
||||
test_rawline(80199)
|
||||
test_rawline(8000000)
|
||||
test_rawline(80199)
|
||||
test_rawline(4091)
|
||||
test_rawline(200)
|
||||
test_rawline(17)
|
||||
test_rawline(1)
|
||||
|
||||
test("raw transfer")
|
||||
test_raw(1)
|
||||
test_raw(17)
|
||||
test_raw(200)
|
||||
test_raw(4091)
|
||||
test_raw(80199)
|
||||
test_raw(8000000)
|
||||
test_raw(80199)
|
||||
test_raw(4091)
|
||||
test_raw(200)
|
||||
test_raw(17)
|
||||
test_raw(1)
|
||||
|
||||
test("non-blocking transfer")
|
||||
test_nonblocking(1)
|
||||
test_nonblocking(17)
|
||||
test_nonblocking(200)
|
||||
test_nonblocking(4091)
|
||||
test_nonblocking(80199)
|
||||
test_nonblocking(800000)
|
||||
test_nonblocking(80199)
|
||||
test_nonblocking(4091)
|
||||
test_nonblocking(200)
|
||||
test_nonblocking(17)
|
||||
test_nonblocking(1)
|
||||
|
||||
test("total timeout on send")
|
||||
test_totaltimeoutsend(800091, 1, 3)
|
||||
test_totaltimeoutsend(800091, 2, 3)
|
||||
test_totaltimeoutsend(800091, 5, 2)
|
||||
test_totaltimeoutsend(800091, 3, 1)
|
||||
|
||||
test("total timeout on receive")
|
||||
test_totaltimeoutreceive(800091, 1, 3)
|
||||
test_totaltimeoutreceive(800091, 2, 3)
|
||||
test_totaltimeoutreceive(800091, 3, 2)
|
||||
test_totaltimeoutreceive(800091, 3, 1)
|
||||
|
||||
test("blocking timeout on send")
|
||||
test_blockingtimeoutsend(800091, 1, 3)
|
||||
test_blockingtimeoutsend(800091, 2, 3)
|
||||
test_blockingtimeoutsend(800091, 3, 2)
|
||||
test_blockingtimeoutsend(800091, 3, 1)
|
||||
|
||||
test("blocking timeout on receive")
|
||||
test_blockingtimeoutreceive(800091, 1, 3)
|
||||
test_blockingtimeoutreceive(800091, 2, 3)
|
||||
test_blockingtimeoutreceive(800091, 3, 2)
|
||||
test_blockingtimeoutreceive(800091, 3, 1)
|
||||
|
||||
test(string.format("done in %.2fs", socket.gettime() - start))
|
15
etc/tests/luasocket/testsrvr.lua
Normal file
15
etc/tests/luasocket/testsrvr.lua
Normal file
@ -0,0 +1,15 @@
|
||||
socket = require("socket");
|
||||
host = host or "localhost";
|
||||
port = port or "8383";
|
||||
server = assert(socket.bind(host, port));
|
||||
ack = "\n";
|
||||
while 1 do
|
||||
print("server: waiting for client connection...");
|
||||
control = assert(server:accept());
|
||||
while 1 do
|
||||
command = assert(control:receive());
|
||||
assert(control:send(ack));
|
||||
print(command);
|
||||
(loadstring(command))();
|
||||
end
|
||||
end
|
37
etc/tests/luasocket/testsupport.lua
Normal file
37
etc/tests/luasocket/testsupport.lua
Normal file
@ -0,0 +1,37 @@
|
||||
function readfile(name)
|
||||
local f = io.open(name, "rb")
|
||||
if not f then return nil end
|
||||
local s = f:read("*a")
|
||||
f:close()
|
||||
return s
|
||||
end
|
||||
|
||||
function similar(s1, s2)
|
||||
return string.lower(string.gsub(s1 or "", "%s", "")) ==
|
||||
string.lower(string.gsub(s2 or "", "%s", ""))
|
||||
end
|
||||
|
||||
function fail(msg)
|
||||
msg = msg or "failed"
|
||||
error(msg, 2)
|
||||
end
|
||||
|
||||
function compare(input, output)
|
||||
local original = readfile(input)
|
||||
local recovered = readfile(output)
|
||||
if original ~= recovered then fail("comparison failed")
|
||||
else print("ok") end
|
||||
end
|
||||
|
||||
local G = _G
|
||||
local set = rawset
|
||||
local warn = print
|
||||
|
||||
local setglobal = function(table, key, value)
|
||||
warn("changed " .. key)
|
||||
set(table, key, value)
|
||||
end
|
||||
|
||||
setmetatable(G, {
|
||||
__newindex = setglobal
|
||||
})
|
Reference in New Issue
Block a user