My own ltn12.filter.chain is done.

Implemented part of DB's suggestion for ftp.
Mimetest.lua generates the test file for base64 instead of loading from disk.
This commit is contained in:
Diego Nehab 2004-10-12 22:35:20 +00:00
parent 396946b63a
commit 4964552718
4 changed files with 61 additions and 45 deletions

2
TODO
View File

@ -4,7 +4,6 @@ new scheme to choose family/protocol of object to create
change ltn13 to make sure drawbacks are obvious change ltn13 to make sure drawbacks are obvious
- check discussion - check discussion
make sure errors not thrown by try() are not caught by protect() make sure errors not thrown by try() are not caught by protect()
use wim's filter.chain or something better
use mike's "don't set to blocking before closing unless needed" patch? use mike's "don't set to blocking before closing unless needed" patch?
take a look at DB's smtp patch (add "extra argument" table) take a look at DB's smtp patch (add "extra argument" table)
move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)? move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)?
@ -24,6 +23,7 @@ testar os options!
- proteger ou atomizar o conjunto (timedout, receive), (timedout, send) - proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
- inet_ntoa também é uma merda. - inet_ntoa também é uma merda.
*use wim's filter.chain or something better
*fix PROXY in http.lua *fix PROXY in http.lua
*use new distribution scheme *use new distribution scheme
*create the getstats method. *create the getstats method.

View File

@ -125,8 +125,10 @@ function metat.__index:send(sendt)
if string.find(code, "1..") then self.try(self.tp:check("2..")) end if string.find(code, "1..") then self.try(self.tp:check("2..")) end
-- done with data connection -- done with data connection
self.data:close() self.data:close()
-- find out how many bytes were sent
local sent = socket.skip(1, self.data:getstats())
self.data = nil self.data = nil
return 1 return sent
end end
function metat.__index:receive(recvt) function metat.__index:receive(recvt)
@ -186,9 +188,10 @@ local function tput(putt)
f:login(putt.user, putt.password) f:login(putt.user, putt.password)
if putt.type then f:type(putt.type) end if putt.type then f:type(putt.type) end
f:pasv() f:pasv()
f:send(putt) local sent = f:send(putt)
f:quit() f:quit()
return f:close() f:close()
return sent
end end
local default = { local default = {

View File

@ -31,40 +31,40 @@ function filter.cycle(low, ctx, extra)
end end
end end
-- chains a bunch of filters together local function chain2(f1, f2)
-- by Wim Couwenberg local ff1, ff2 = "", ""
function filter.chain(...)
local current = 1
local bottom = 1
local top = table.getn(arg)
local retry = ""
return function(chunk) return function(chunk)
if chunk ~= retry then local rf1 = chunk and ""
current = bottom local rf2 = ff1 and ""
retry = chunk and retry -- if f2 still has pending data, get it and return it
if ff2 ~= rf2 then
ff2 = f2(rf2)
if ff2 ~= "" then return ff2 end
end
-- here we know f2 needs more data
-- we try to get it from f1
ff1 = f1(chunk)
while 1 do
-- if f1 can't produce data, we need more data from the user
if ff1 == "" then return "" end
-- otherwise we pass new data to f2 until it produces something
-- or f1 runs out of data too
ff2 = f2(ff1)
if ff2 ~= "" then return ff2 end
ff1 = f1(rf1)
end end
repeat
if current == bottom then
chunk = arg[current](chunk)
if chunk == "" or bottom == top then return chunk
elseif chunk then current = current + 1
else
bottom = bottom + 1
current = bottom
end
else
chunk = arg[current](chunk or "")
if chunk == "" then
chunk = retry
current = current - 1
elseif current == top then return chunk
else current = current + 1
end
end
until false
end end
end end
-- chains a bunch of filters together
function filter.chain(...)
local f = arg[1]
for i = 2, table.getn(arg) do
f = chain2(f, arg[i])
end
return f
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Source stuff -- Source stuff
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@ -8,13 +8,10 @@ local qptest = "qptest.bin"
local eqptest = "qptest.bin2" local eqptest = "qptest.bin2"
local dqptest = "qptest.bin3" local dqptest = "qptest.bin3"
local b64test = "lsocket.2.0.dylib" local b64test = "b64test.bin"
local eb64test = "b64test.bin" local eb64test = "b64test.bin2"
local db64test = "b64test.bin2" local db64test = "b64test.bin3"
-- make sure test file exists
local f = assert(io.open(b64test, "r"))
f:close()
-- from Machado de Assis, "A Mão e a Rosa" -- from Machado de Assis, "A Mão e a Rosa"
local mao = [[ local mao = [[
@ -86,6 +83,7 @@ local function named(f, name)
end end
end end
]] ]]
local function named(f) local function named(f)
return f return f
end end
@ -188,6 +186,19 @@ local function cleanup_qptest()
os.remove(dqptest) os.remove(dqptest)
end end
-- create test file
function create_b64test()
local f = assert(io.open(b64test, "wb"))
local t = {}
for j = 1, 100 do
for i = 1, 100 do
t[i] = math.random(0, 255)
end
f:write(string.char(unpack(t)))
end
f:close()
end
local function encode_b64test() local function encode_b64test()
local e1 = mime.encode("base64") local e1 = mime.encode("base64")
local e2 = mime.encode("base64") local e2 = mime.encode("base64")
@ -212,6 +223,7 @@ local function decode_b64test()
end end
local function cleanup_b64test() local function cleanup_b64test()
os.remove(b64test)
os.remove(eb64test) os.remove(eb64test)
os.remove(db64test) os.remove(db64test)
end end
@ -221,12 +233,12 @@ local function compare_b64test()
end end
local function identity_test() local function identity_test()
local chain = ltn12.filter.chain( local chain = named(ltn12.filter.chain(
mime.encode("quoted-printable"), named(mime.encode("quoted-printable"), "1 eq"),
mime.encode("base64"), named(mime.encode("base64"), "2 eb"),
mime.decode("base64"), named(mime.decode("base64"), "3 db"),
mime.decode("quoted-printable") named(mime.decode("quoted-printable"), "4 dq")
) ), "chain")
transform(b64test, eb64test, chain) transform(b64test, eb64test, chain)
compare(b64test, eb64test) compare(b64test, eb64test)
os.remove(eb64test) os.remove(eb64test)
@ -271,6 +283,7 @@ end
local t = socket.gettime() local t = socket.gettime()
identity_test() identity_test()
create_b64test()
encode_b64test() encode_b64test()
decode_b64test() decode_b64test()
compare_b64test() compare_b64test()