From 4964552718b81dba2c36fc08494aefae89a799f4 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 12 Oct 2004 22:35:20 +0000 Subject: 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. --- src/ftp.lua | 9 ++++++--- src/ltn12.lua | 58 +++++++++++++++++++++++++++++----------------------------- 2 files changed, 35 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/ftp.lua b/src/ftp.lua index 43c62c2..9902c88 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -125,8 +125,10 @@ function metat.__index:send(sendt) if string.find(code, "1..") then self.try(self.tp:check("2..")) end -- done with data connection self.data:close() + -- find out how many bytes were sent + local sent = socket.skip(1, self.data:getstats()) self.data = nil - return 1 + return sent end function metat.__index:receive(recvt) @@ -186,9 +188,10 @@ local function tput(putt) f:login(putt.user, putt.password) if putt.type then f:type(putt.type) end f:pasv() - f:send(putt) + local sent = f:send(putt) f:quit() - return f:close() + f:close() + return sent end local default = { diff --git a/src/ltn12.lua b/src/ltn12.lua index 6c5ea28..9917ce8 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -31,40 +31,40 @@ function filter.cycle(low, ctx, extra) end end --- chains a bunch of filters together --- by Wim Couwenberg -function filter.chain(...) - local current = 1 - local bottom = 1 - local top = table.getn(arg) - local retry = "" +local function chain2(f1, f2) + local ff1, ff2 = "", "" return function(chunk) - if chunk ~= retry then - current = bottom - retry = chunk and retry + local rf1 = chunk and "" + local rf2 = ff1 and "" + -- 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 - 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 +-- 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 ----------------------------------------------------------------------------- -- cgit v1.2.3-55-g6feb