aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-28 02:36:07 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-28 02:36:07 +0000
commit297b32e828b76ee544c9d4f89f996eda78830cc0 (patch)
tree312ca3a296b1e959acddeca9303cb238ac8324dc
parent05e8f243851049cebda6c5b690d3cde0f1e2c874 (diff)
downloadluasocket-297b32e828b76ee544c9d4f89f996eda78830cc0.tar.gz
luasocket-297b32e828b76ee544c9d4f89f996eda78830cc0.tar.bz2
luasocket-297b32e828b76ee544c9d4f89f996eda78830cc0.zip
LTN12 bug removed.
-rw-r--r--src/ltn12.lua26
-rw-r--r--src/mime.c7
-rw-r--r--src/mime.lua9
-rw-r--r--test/mimetest.lua13
4 files changed, 39 insertions, 16 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua
index 5216ff6..1a13a43 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -40,30 +40,27 @@ end
40function filter.chain(...) 40function filter.chain(...)
41 local n = table.getn(arg) 41 local n = table.getn(arg)
42 local top, index = 1, 1 42 local top, index = 1, 1
43 local retry = ""
43 return function(chunk) 44 return function(chunk)
45 retry = chunk and retry
44 while true do 46 while true do
45 if index == top then 47 if index == top then
46 chunk = arg[index](chunk) 48 chunk = arg[index](chunk)
47 if chunk == "" or top == n then 49 if chunk == "" or top == n then return chunk
48 return chunk 50 elseif chunk then index = index + 1
49 elseif chunk then
50 index = index + 1
51 else 51 else
52 top = top+1 52 top = top+1
53 index = top 53 index = top
54 end 54 end
55 else 55 else
56 local original = chunk 56 chunk = arg[index](chunk or "")
57 chunk = arg[index](original or "")
58 if chunk == "" then 57 if chunk == "" then
59 index = index - 1 58 index = index - 1
60 chunk = original and chunk 59 chunk = retry
61 elseif chunk then 60 elseif chunk then
62 if index == n then return chunk 61 if index == n then return chunk
63 else index = index + 1 end 62 else index = index + 1 end
64 else 63 else base.error("filter returned inappropriate nil") end
65 base.error("filter returned inappropriate nil")
66 end
67 end 64 end
68 end 65 end
69 end 66 end
@@ -138,6 +135,8 @@ function source.rewind(src)
138 end 135 end
139end 136end
140 137
138local print = print
139
141-- chains a source with a filter 140-- chains a source with a filter
142function source.chain(src, f) 141function source.chain(src, f)
143 base.assert(src and f) 142 base.assert(src and f)
@@ -151,13 +150,16 @@ function source.chain(src, f)
151 last_out = f(last_in) 150 last_out = f(last_in)
152 if last_out ~= "" then return last_out end 151 if last_out ~= "" then return last_out end
153 if not last_in then 152 if not last_in then
154 error('filter returned inappropriate ""') 153 base.error('filter returned inappropriate ""')
155 end 154 end
156 end 155 end
157 elseif last_out then 156 elseif last_out then
158 last_out = f(last_in and "") 157 last_out = f(last_in and "")
159 if last_in and not last_out then 158 if last_in and not last_out then
160 error('filter returned inappropriate nil') 159 base.error('filter returned inappropriate nil')
160 end
161 if last_out == "" and not last_in then
162 base.error(base.tostring(f) .. ' returned inappropriate ""')
161 end 163 end
162 return last_out 164 return last_out
163 else 165 else
diff --git a/src/mime.c b/src/mime.c
index 8bc20d3..bd48920 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -267,6 +267,7 @@ static int mime_global_b64(lua_State *L)
267 if (!input) { 267 if (!input) {
268 asize = b64pad(atom, asize, &buffer); 268 asize = b64pad(atom, asize, &buffer);
269 luaL_pushresult(&buffer); 269 luaL_pushresult(&buffer);
270 if (!(*lua_tostring(L, -1))) lua_pushnil(L);
270 lua_pushnil(L); 271 lua_pushnil(L);
271 return 2; 272 return 2;
272 } 273 }
@@ -306,6 +307,7 @@ static int mime_global_unb64(lua_State *L)
306 /* if second is nil, we are done */ 307 /* if second is nil, we are done */
307 if (!input) { 308 if (!input) {
308 luaL_pushresult(&buffer); 309 luaL_pushresult(&buffer);
310 if (!(*lua_tostring(L, -1))) lua_pushnil(L);
309 lua_pushnil(L); 311 lua_pushnil(L);
310 return 2; 312 return 2;
311 } 313 }
@@ -418,7 +420,7 @@ static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer)
418 if (qpclass[input[i]] == QP_PLAIN) luaL_putchar(buffer, input[i]); 420 if (qpclass[input[i]] == QP_PLAIN) luaL_putchar(buffer, input[i]);
419 else qpquote(input[i], buffer); 421 else qpquote(input[i], buffer);
420 } 422 }
421 luaL_addstring(buffer, EQCRLF); 423 if (size > 0) luaL_addstring(buffer, EQCRLF);
422 return 0; 424 return 0;
423} 425}
424 426
@@ -454,7 +456,9 @@ static int mime_global_qp(lua_State *L)
454 if (!input) { 456 if (!input) {
455 asize = qppad(atom, asize, &buffer); 457 asize = qppad(atom, asize, &buffer);
456 luaL_pushresult(&buffer); 458 luaL_pushresult(&buffer);
459 if (!(*lua_tostring(L, -1))) lua_pushnil(L);
457 lua_pushnil(L); 460 lua_pushnil(L);
461 return 2;
458 } 462 }
459 /* otherwise process rest of input */ 463 /* otherwise process rest of input */
460 last = input + isize; 464 last = input + isize;
@@ -531,6 +535,7 @@ static int mime_global_unqp(lua_State *L)
531 /* if second part is nil, we are done */ 535 /* if second part is nil, we are done */
532 if (!input) { 536 if (!input) {
533 luaL_pushresult(&buffer); 537 luaL_pushresult(&buffer);
538 if (!(*lua_tostring(L, -1))) lua_pushnil(L);
534 lua_pushnil(L); 539 lua_pushnil(L);
535 return 2; 540 return 2;
536 } 541 }
diff --git a/src/mime.lua b/src/mime.lua
index 712600c..42d0b3c 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -49,6 +49,15 @@ mime.decodet['quoted-printable'] = function()
49 return ltn12.filter.cycle(unqp, "") 49 return ltn12.filter.cycle(unqp, "")
50end 50end
51 51
52local io, string = io, string
53
54local function format(chunk)
55 if chunk then
56 if chunk == "" then return "''"
57 else return string.len(chunk) end
58 else return "nil" end
59end
60
52-- define the line-wrap filters 61-- define the line-wrap filters
53mime.wrapt['text'] = function(length) 62mime.wrapt['text'] = function(length)
54 length = length or 76 63 length = length or 76
diff --git a/test/mimetest.lua b/test/mimetest.lua
index 2f6b7a8..f52a351 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -39,9 +39,13 @@ local mao = [[
39local function random(handle, io_err) 39local function random(handle, io_err)
40 if handle then 40 if handle then
41 return function() 41 return function()
42 if not handle then error("source is empty!", 2) end
42 local len = math.random(0, 1024) 43 local len = math.random(0, 1024)
43 local chunk = handle:read(len) 44 local chunk = handle:read(len)
44 if not chunk then handle:close() end 45 if not chunk then
46 handle:close()
47 handle = nil
48 end
45 return chunk 49 return chunk
46 end 50 end
47 else return ltn12.source.empty(io_err or "unable to open file") end 51 else return ltn12.source.empty(io_err or "unable to open file") end
@@ -73,6 +77,7 @@ local function encode_qptest(mode)
73end 77end
74 78
75local function compare_qptest() 79local function compare_qptest()
80io.write("testing qp encoding and wrap: ")
76 compare(qptest, dqptest) 81 compare(qptest, dqptest)
77end 82end
78 83
@@ -173,7 +178,6 @@ local function encode_b64test()
173 local sp2 = mime.wrap("base64", 30) 178 local sp2 = mime.wrap("base64", 30)
174 local sp1 = mime.wrap(27) 179 local sp1 = mime.wrap(27)
175 local chain = ltn12.filter.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4) 180 local chain = ltn12.filter.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
176 chain = socket.protect(chain)
177 transform(b64test, eb64test, chain) 181 transform(b64test, eb64test, chain)
178end 182end
179 183
@@ -193,10 +197,12 @@ local function cleanup_b64test()
193end 197end
194 198
195local function compare_b64test() 199local function compare_b64test()
200io.write("testing b64 chained encode: ")
196 compare(b64test, db64test) 201 compare(b64test, db64test)
197end 202end
198 203
199local function identity_test() 204local function identity_test()
205io.write("testing identity: ")
200 local chain = named(ltn12.filter.chain( 206 local chain = named(ltn12.filter.chain(
201 named(mime.encode("quoted-printable"), "1 eq"), 207 named(mime.encode("quoted-printable"), "1 eq"),
202 named(mime.encode("base64"), "2 eb"), 208 named(mime.encode("base64"), "2 eb"),
@@ -223,11 +229,12 @@ local function chunkcheck(original, encoded)
223 local b = string.sub(original, i+1) 229 local b = string.sub(original, i+1)
224 local e, r = mime.b64(a, b) 230 local e, r = mime.b64(a, b)
225 local f = (mime.b64(r)) 231 local f = (mime.b64(r))
226 if (e .. f ~= encoded) then fail(e .. f) end 232 if (e .. (f or "") ~= encoded) then fail(e .. (f or "")) end
227 end 233 end
228end 234end
229 235
230local function padding_b64test() 236local function padding_b64test()
237io.write("testing b64 padding: ")
231 padcheck("a", "YQ==") 238 padcheck("a", "YQ==")
232 padcheck("ab", "YWI=") 239 padcheck("ab", "YWI=")
233 padcheck("abc", "YWJj") 240 padcheck("abc", "YWJj")