diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-10-12 22:35:20 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-10-12 22:35:20 +0000 |
commit | 4964552718b81dba2c36fc08494aefae89a799f4 (patch) | |
tree | 5473a82045a7f66f31c56f4cbd151b16a61e7e90 | |
parent | 396946b63a95fc9949b75aed82c7ac1c132ccd40 (diff) | |
download | luasocket-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-- | TODO | 2 | ||||
-rw-r--r-- | src/ftp.lua | 9 | ||||
-rw-r--r-- | src/ltn12.lua | 58 | ||||
-rw-r--r-- | test/mimetest.lua | 37 |
4 files changed, 61 insertions, 45 deletions
@@ -4,7 +4,6 @@ new scheme to choose family/protocol of object to create | |||
4 | change ltn13 to make sure drawbacks are obvious | 4 | change ltn13 to make sure drawbacks are obvious |
5 | - check discussion | 5 | - check discussion |
6 | make sure errors not thrown by try() are not caught by protect() | 6 | make sure errors not thrown by try() are not caught by protect() |
7 | use wim's filter.chain or something better | ||
8 | use mike's "don't set to blocking before closing unless needed" patch? | 7 | use mike's "don't set to blocking before closing unless needed" patch? |
9 | take a look at DB's smtp patch (add "extra argument" table) | 8 | take a look at DB's smtp patch (add "extra argument" table) |
10 | move wsocket.c:sock_send kludge to buffer.c:sendraw (probably)? | 9 | move 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 |
130 | end | 132 | end |
131 | 133 | ||
132 | function metat.__index:receive(recvt) | 134 | function 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 | ||
192 | end | 195 | end |
193 | 196 | ||
194 | local default = { | 197 | 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) | |||
31 | end | 31 | end |
32 | end | 32 | end |
33 | 33 | ||
34 | -- chains a bunch of filters together | 34 | local function chain2(f1, f2) |
35 | -- by Wim Couwenberg | 35 | local ff1, ff2 = "", "" |
36 | function 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 |
66 | end | 57 | end |
67 | 58 | ||
59 | -- chains a bunch of filters together | ||
60 | function 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 | ||
66 | end | ||
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" | |||
8 | local eqptest = "qptest.bin2" | 8 | local eqptest = "qptest.bin2" |
9 | local dqptest = "qptest.bin3" | 9 | local dqptest = "qptest.bin3" |
10 | 10 | ||
11 | local b64test = "lsocket.2.0.dylib" | 11 | local b64test = "b64test.bin" |
12 | local eb64test = "b64test.bin" | 12 | local eb64test = "b64test.bin2" |
13 | local db64test = "b64test.bin2" | 13 | local db64test = "b64test.bin3" |
14 | 14 | ||
15 | -- make sure test file exists | ||
16 | local f = assert(io.open(b64test, "r")) | ||
17 | f: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" |
20 | local mao = [[ | 17 | local mao = [[ |
@@ -86,6 +83,7 @@ local function named(f, name) | |||
86 | end | 83 | end |
87 | end | 84 | end |
88 | ]] | 85 | ]] |
86 | |||
89 | local function named(f) | 87 | local function named(f) |
90 | return f | 88 | return f |
91 | end | 89 | end |
@@ -188,6 +186,19 @@ local function cleanup_qptest() | |||
188 | os.remove(dqptest) | 186 | os.remove(dqptest) |
189 | end | 187 | end |
190 | 188 | ||
189 | -- create test file | ||
190 | function 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() | ||
200 | end | ||
201 | |||
191 | local function encode_b64test() | 202 | local 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() | |||
212 | end | 223 | end |
213 | 224 | ||
214 | local function cleanup_b64test() | 225 | local function cleanup_b64test() |
226 | os.remove(b64test) | ||
215 | os.remove(eb64test) | 227 | os.remove(eb64test) |
216 | os.remove(db64test) | 228 | os.remove(db64test) |
217 | end | 229 | end |
@@ -221,12 +233,12 @@ local function compare_b64test() | |||
221 | end | 233 | end |
222 | 234 | ||
223 | local function identity_test() | 235 | local 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 | |||
271 | local t = socket.gettime() | 283 | local t = socket.gettime() |
272 | 284 | ||
273 | identity_test() | 285 | identity_test() |
286 | create_b64test() | ||
274 | encode_b64test() | 287 | encode_b64test() |
275 | decode_b64test() | 288 | decode_b64test() |
276 | compare_b64test() | 289 | compare_b64test() |