aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-10-12 22:35:20 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-10-12 22:35:20 +0000
commit4964552718b81dba2c36fc08494aefae89a799f4 (patch)
tree5473a82045a7f66f31c56f4cbd151b16a61e7e90
parent396946b63a95fc9949b75aed82c7ac1c132ccd40 (diff)
downloadluasocket-4964552718b81dba2c36fc08494aefae89a799f4.tar.gz
luasocket-4964552718b81dba2c36fc08494aefae89a799f4.tar.bz2
luasocket-4964552718b81dba2c36fc08494aefae89a799f4.zip
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.
-rw-r--r--TODO2
-rw-r--r--src/ftp.lua9
-rw-r--r--src/ltn12.lua58
-rw-r--r--test/mimetest.lua37
4 files changed, 61 insertions, 45 deletions
diff --git a/TODO b/TODO
index 557c0db..26ce419 100644
--- a/TODO
+++ b/TODO
@@ -4,7 +4,6 @@ new scheme to choose family/protocol of object to create
4change ltn13 to make sure drawbacks are obvious 4change ltn13 to make sure drawbacks are obvious
5 - check discussion 5 - check discussion
6make sure errors not thrown by try() are not caught by protect() 6make sure errors not thrown by try() are not caught by protect()
7use wim's filter.chain or something better
8use mike's "don't set to blocking before closing unless needed" patch? 7use mike's "don't set to blocking before closing unless needed" patch?
9take a look at DB's smtp patch (add "extra argument" table) 8take a look at DB's smtp patch (add "extra argument" table)
10move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)? 9move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)?
@@ -24,6 +23,7 @@ testar os options!
24 - proteger ou atomizar o conjunto (timedout, receive), (timedout, send) 23 - proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
25 - inet_ntoa também é uma merda. 24 - inet_ntoa também é uma merda.
26 25
26*use wim's filter.chain or something better
27*fix PROXY in http.lua 27*fix PROXY in http.lua
28*use new distribution scheme 28*use new distribution scheme
29*create the getstats method. 29*create the getstats method.
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)
125 if string.find(code, "1..") then self.try(self.tp:check("2..")) end 125 if string.find(code, "1..") then self.try(self.tp:check("2..")) end
126 -- done with data connection 126 -- done with data connection
127 self.data:close() 127 self.data:close()
128 -- find out how many bytes were sent
129 local sent = socket.skip(1, self.data:getstats())
128 self.data = nil 130 self.data = nil
129 return 1 131 return sent
130end 132end
131 133
132function metat.__index:receive(recvt) 134function metat.__index:receive(recvt)
@@ -186,9 +188,10 @@ local function tput(putt)
186 f:login(putt.user, putt.password) 188 f:login(putt.user, putt.password)
187 if putt.type then f:type(putt.type) end 189 if putt.type then f:type(putt.type) end
188 f:pasv() 190 f:pasv()
189 f:send(putt) 191 local sent = f:send(putt)
190 f:quit() 192 f:quit()
191 return f:close() 193 f:close()
194 return sent
192end 195end
193 196
194local default = { 197local 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)
31 end 31 end
32end 32end
33 33
34-- chains a bunch of filters together 34local function chain2(f1, f2)
35-- by Wim Couwenberg 35 local ff1, ff2 = "", ""
36function filter.chain(...)
37 local current = 1
38 local bottom = 1
39 local top = table.getn(arg)
40 local retry = ""
41 return function(chunk) 36 return function(chunk)
42 if chunk ~= retry then 37 local rf1 = chunk and ""
43 current = bottom 38 local rf2 = ff1 and ""
44 retry = chunk and retry 39 -- if f2 still has pending data, get it and return it
40 if ff2 ~= rf2 then
41 ff2 = f2(rf2)
42 if ff2 ~= "" then return ff2 end
43 end
44 -- here we know f2 needs more data
45 -- we try to get it from f1
46 ff1 = f1(chunk)
47 while 1 do
48 -- if f1 can't produce data, we need more data from the user
49 if ff1 == "" then return "" end
50 -- otherwise we pass new data to f2 until it produces something
51 -- or f1 runs out of data too
52 ff2 = f2(ff1)
53 if ff2 ~= "" then return ff2 end
54 ff1 = f1(rf1)
45 end 55 end
46 repeat
47 if current == bottom then
48 chunk = arg[current](chunk)
49 if chunk == "" or bottom == top then return chunk
50 elseif chunk then current = current + 1
51 else
52 bottom = bottom + 1
53 current = bottom
54 end
55 else
56 chunk = arg[current](chunk or "")
57 if chunk == "" then
58 chunk = retry
59 current = current - 1
60 elseif current == top then return chunk
61 else current = current + 1
62 end
63 end
64 until false
65 end 56 end
66end 57end
67 58
59-- chains a bunch of filters together
60function filter.chain(...)
61 local f = arg[1]
62 for i = 2, table.getn(arg) do
63 f = chain2(f, arg[i])
64 end
65 return f
66end
67
68----------------------------------------------------------------------------- 68-----------------------------------------------------------------------------
69-- Source stuff 69-- Source stuff
70----------------------------------------------------------------------------- 70-----------------------------------------------------------------------------
diff --git a/test/mimetest.lua b/test/mimetest.lua
index d9bb772..89926d8 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -8,13 +8,10 @@ local qptest = "qptest.bin"
8local eqptest = "qptest.bin2" 8local eqptest = "qptest.bin2"
9local dqptest = "qptest.bin3" 9local dqptest = "qptest.bin3"
10 10
11local b64test = "lsocket.2.0.dylib" 11local b64test = "b64test.bin"
12local eb64test = "b64test.bin" 12local eb64test = "b64test.bin2"
13local db64test = "b64test.bin2" 13local db64test = "b64test.bin3"
14 14
15-- make sure test file exists
16local f = assert(io.open(b64test, "r"))
17f:close()
18 15
19-- from Machado de Assis, "A Mão e a Rosa" 16-- from Machado de Assis, "A Mão e a Rosa"
20local mao = [[ 17local mao = [[
@@ -86,6 +83,7 @@ local function named(f, name)
86 end 83 end
87end 84end
88]] 85]]
86
89local function named(f) 87local function named(f)
90 return f 88 return f
91end 89end
@@ -188,6 +186,19 @@ local function cleanup_qptest()
188 os.remove(dqptest) 186 os.remove(dqptest)
189end 187end
190 188
189-- create test file
190function create_b64test()
191 local f = assert(io.open(b64test, "wb"))
192 local t = {}
193 for j = 1, 100 do
194 for i = 1, 100 do
195 t[i] = math.random(0, 255)
196 end
197 f:write(string.char(unpack(t)))
198 end
199 f:close()
200end
201
191local function encode_b64test() 202local function encode_b64test()
192 local e1 = mime.encode("base64") 203 local e1 = mime.encode("base64")
193 local e2 = mime.encode("base64") 204 local e2 = mime.encode("base64")
@@ -212,6 +223,7 @@ local function decode_b64test()
212end 223end
213 224
214local function cleanup_b64test() 225local function cleanup_b64test()
226 os.remove(b64test)
215 os.remove(eb64test) 227 os.remove(eb64test)
216 os.remove(db64test) 228 os.remove(db64test)
217end 229end
@@ -221,12 +233,12 @@ local function compare_b64test()
221end 233end
222 234
223local function identity_test() 235local function identity_test()
224 local chain = ltn12.filter.chain( 236 local chain = named(ltn12.filter.chain(
225 mime.encode("quoted-printable"), 237 named(mime.encode("quoted-printable"), "1 eq"),
226 mime.encode("base64"), 238 named(mime.encode("base64"), "2 eb"),
227 mime.decode("base64"), 239 named(mime.decode("base64"), "3 db"),
228 mime.decode("quoted-printable") 240 named(mime.decode("quoted-printable"), "4 dq")
229 ) 241 ), "chain")
230 transform(b64test, eb64test, chain) 242 transform(b64test, eb64test, chain)
231 compare(b64test, eb64test) 243 compare(b64test, eb64test)
232 os.remove(eb64test) 244 os.remove(eb64test)
@@ -271,6 +283,7 @@ end
271local t = socket.gettime() 283local t = socket.gettime()
272 284
273identity_test() 285identity_test()
286create_b64test()
274encode_b64test() 287encode_b64test()
275decode_b64test() 288decode_b64test()
276compare_b64test() 289compare_b64test()