Saving before big changes to support IPv6.
This commit is contained in:
parent
bce60be30f
commit
3a8ba90dfb
30 changed files with 948 additions and 850 deletions
44
etc/dict.lua
44
etc/dict.lua
|
@ -44,48 +44,48 @@ function metat.__index:check(ok)
|
|||
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")
|
||||
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))
|
||||
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)
|
||||
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))
|
||||
socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word))
|
||||
self:check(152)
|
||||
local mat = {}
|
||||
local line = socket.try(self.tp:receive())
|
||||
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)
|
||||
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)
|
||||
self.tp:command("QUIT")
|
||||
return self:check(221)
|
||||
end
|
||||
|
||||
function metat.__index:close()
|
||||
|
|
106
etc/get.lua
106
etc/get.lua
|
@ -12,53 +12,53 @@ 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
|
||||
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)
|
||||
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
|
||||
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
|
||||
|
@ -89,10 +89,10 @@ 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
|
||||
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
|
||||
|
@ -101,7 +101,7 @@ function getbyhttp(u, file)
|
|||
-- 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
|
||||
if c ~= 200 then io.stderr:write(s or c, "\n") end
|
||||
end
|
||||
|
||||
-- downloads a file using the ftp protocol
|
||||
|
@ -114,29 +114,29 @@ function getbyftp(u, file)
|
|||
gett.sink = save
|
||||
gett.type = "i"
|
||||
local ret, err = ftp.get(gett)
|
||||
if err then print(err) end
|
||||
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
|
||||
-- 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
|
||||
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)
|
||||
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
|
||||
os.exit(1)
|
||||
else get(arg[1], arg[2]) end
|
||||
|
|
|
@ -268,11 +268,11 @@ send = socket.protect(function(option)
|
|||
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,
|
||||
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",
|
||||
option.job or "LuaSocket",
|
||||
user,
|
||||
fmt, lpfile,
|
||||
lpfile,
|
||||
|
|
68
etc/tftp.lua
68
etc/tftp.lua
|
@ -35,18 +35,18 @@ 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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
|
@ -58,16 +58,16 @@ 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
|
||||
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)
|
||||
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
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
@ -77,40 +77,40 @@ 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 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)
|
||||
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()
|
||||
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)
|
||||
until dgram or datahost ~= "timeout" or retries > 5
|
||||
try(dgram, datahost)
|
||||
-- associate socket with data host/port
|
||||
try(con:setpeername(datahost, dataport))
|
||||
try(con:setpeername(datahost, dataport))
|
||||
-- default sink
|
||||
local sink = gett.sink or ltn12.sink.null()
|
||||
-- process all data packets
|
||||
while 1 do
|
||||
while 1 do
|
||||
-- decode packet
|
||||
code = get_OP(dgram)
|
||||
try(code ~= OP_ERROR, get_ERROR(dgram))
|
||||
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)
|
||||
local block, data = split_DATA(dgram)
|
||||
-- if not repeated, write
|
||||
if block == last+1 then
|
||||
try(sink(data))
|
||||
try(sink(data))
|
||||
last = block
|
||||
end
|
||||
-- last packet brings less than 512 bytes of data
|
||||
if string.len(data) < 512 then
|
||||
if string.len(data) < 512 then
|
||||
try(con:send(ACK(block)))
|
||||
try(con:close())
|
||||
try(sink(nil))
|
||||
|
@ -118,13 +118,13 @@ local function tget(gett)
|
|||
end
|
||||
-- get the next packet
|
||||
retries = 0
|
||||
repeat
|
||||
sent = try(con:send(ACK(last)))
|
||||
dgram, err = con:receive()
|
||||
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
|
||||
until dgram or err ~= "timeout" or retries > 5
|
||||
try(dgram, err)
|
||||
end
|
||||
end
|
||||
|
||||
local default = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue